Mercurial > dive4elements > river
comparison flys-backend/contrib/shpimporter/importer.py @ 4935:c0a58558b817 dami
Importer: - Handle regular expressions for attribute names
- Convert Strings to UTF-8
- Add regular expressions for hws_points values
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Thu, 31 Jan 2013 12:23:41 +0100 |
parents | 8e99d2d9364d |
children | 23a090b41543 |
comparison
equal
deleted
inserted
replaced
4934:11b459a3eb5c | 4935:c0a58558b817 |
---|---|
3 except ImportErrror: | 3 except ImportErrror: |
4 import ogr | 4 import ogr |
5 import osr | 5 import osr |
6 import shpimporter | 6 import shpimporter |
7 import utils | 7 import utils |
8 import re | |
8 | 9 |
9 class Importer: | 10 class Importer: |
10 | 11 |
11 def __init__(self, config, dbconn): | 12 def __init__(self, config, dbconn): |
12 self.config = config | 13 self.config = config |
14 self.river_id = config.river_id | 15 self.river_id = config.river_id |
15 self.dest_srs = osr.SpatialReference() | 16 self.dest_srs = osr.SpatialReference() |
16 self.dest_srs.ImportFromEPSG(config.target_srs) | 17 self.dest_srs.ImportFromEPSG(config.target_srs) |
17 self.handled_fields = [] | 18 self.handled_fields = [] |
18 self.tracking_import = False | 19 self.tracking_import = False |
20 self.srcLayer = None | |
19 | 21 |
20 def getKind(self, path): | 22 def getKind(self, path): |
21 raise NotImplementedError("Importer.getKind is abstract!") | 23 raise NotImplementedError("Importer.getKind is abstract!") |
22 | 24 |
23 def getPath(self, base): | 25 def getPath(self, base): |
28 | 30 |
29 def getName(self): | 31 def getName(self): |
30 raise NotImplementedError("Importer.getTablename is abstract!") | 32 raise NotImplementedError("Importer.getTablename is abstract!") |
31 | 33 |
32 def IsFieldSet(self, feat, name): | 34 def IsFieldSet(self, feat, name): |
35 if not name: | |
36 return False | |
33 if feat.GetFieldIndex(name) == -1: | 37 if feat.GetFieldIndex(name) == -1: |
34 return False # Avoids an Error in IsFieldSet | 38 return False # Avoids an Error in IsFieldSet |
35 return feat.IsFieldSet(feat.GetFieldIndex(name)) | 39 return feat.IsFieldSet(feat.GetFieldIndex(name)) |
40 | |
41 def searchField(self, regex): | |
42 """ | |
43 Searches for a field in the current src layer that matches | |
44 the expression regex. | |
45 Throws an exception if more then one field matches | |
46 @param feat: The feature to search for attributes | |
47 @param regex: The regex to look for | |
48 | |
49 @returns: The field name as a string | |
50 """ | |
51 | |
52 if not hasattr(self.srcLayer, "fieldnames"): | |
53 self.srcLayer.fieldnames = [] | |
54 for i in range(0, self.srcLayer.GetLayerDefn().GetFieldCount()): | |
55 self.srcLayer.fieldnames.append( | |
56 self.srcLayer.GetLayerDefn().GetFieldDefn(i).GetNameRef()) | |
57 | |
58 result = None | |
59 for name in self.srcLayer.fieldnames: | |
60 match = re.match(regex, name, re.IGNORECASE) | |
61 if match: | |
62 if result: | |
63 raise Exception("More then one field matches: %s" % regex) | |
64 else: | |
65 result = match.group(0) | |
66 return result | |
36 | 67 |
37 def IsDoubleFieldSet(self, feat, name): | 68 def IsDoubleFieldSet(self, feat, name): |
38 try: | 69 try: |
39 isset = feat.GetFieldAsDouble(name) | 70 isset = feat.GetFieldAsDouble(name) |
40 return isset is not None | 71 return isset is not None |
88 | 119 |
89 def copyFields(self, src, target, mapping): | 120 def copyFields(self, src, target, mapping): |
90 """ | 121 """ |
91 Checks the mapping dictonary for key value pairs to | 122 Checks the mapping dictonary for key value pairs to |
92 copy from the source to the destination feature. | 123 copy from the source to the destination feature. |
124 The keys can be reguar expressions that are matched | |
125 agains the source fieldnames | |
93 | 126 |
94 The Key is the attribute of the source feature to be copied | 127 The Key is the attribute of the source feature to be copied |
95 into the target attribute named by the dict's value. | 128 into the target attribute named by the dict's value. |
96 """ | 129 """ |
97 self.tracking_import = True | 130 self.tracking_import = True |
98 self.handled_fields.extend(mapping.keys()) | |
99 for key, value in mapping.items(): | 131 for key, value in mapping.items(): |
100 if src.GetFieldIndex(key) == -1: | 132 realname = self.searchField(key) |
133 if realname == None: | |
101 continue | 134 continue |
135 if not realname in self.handled_fields: | |
136 self.handled_fields.append(realname) | |
102 # 0 OFTInteger, Simple 32bit integer | 137 # 0 OFTInteger, Simple 32bit integer |
103 # 1 OFTIntegerList, List of 32bit integers | 138 # 1 OFTIntegerList, List of 32bit integers |
104 # 2 OFTReal, Double Precision floating point | 139 # 2 OFTReal, Double Precision floating point |
105 # 3 OFTRealList, List of doubles | 140 # 3 OFTRealList, List of doubles |
106 # 4 OFTString, String of ASCII chars | 141 # 4 OFTString, String of ASCII chars |
109 # 7 OFTWideStringList, deprecated | 144 # 7 OFTWideStringList, deprecated |
110 # 8 OFTBinary, Raw Binary data | 145 # 8 OFTBinary, Raw Binary data |
111 # 9 OFTDate, Date | 146 # 9 OFTDate, Date |
112 # 10 OFTTime, Time | 147 # 10 OFTTime, Time |
113 # 11 OFTDateTime, Date and Time | 148 # 11 OFTDateTime, Date and Time |
114 if src.IsFieldSet(src.GetFieldIndex(key)): | 149 if src.IsFieldSet(src.GetFieldIndex(realname)): |
115 if src.GetFieldType(key) == 2: | 150 if src.GetFieldType(realname) == 2: |
116 target.SetField(value, src.GetFieldAsDouble(key)) | 151 target.SetField(value, src.GetFieldAsDouble(realname)) |
117 else: | 152 else: |
118 target.SetField(value, src.GetField(key)) | 153 target.SetField(value, utils.getUTF8(src.GetField(realname))) |
119 | 154 |
120 def shape2Database(self, srcLayer, name, path): | 155 def shape2Database(self, srcLayer, name, path): |
121 destLayer = self.dbconn.GetLayerByName(self.getTablename()) | 156 destLayer = self.dbconn.GetLayerByName(self.getTablename()) |
122 | 157 |
123 if srcLayer is None: | 158 if srcLayer is None: |
130 | 165 |
131 count = srcLayer.GetFeatureCount() | 166 count = srcLayer.GetFeatureCount() |
132 shpimporter.DEBUG("Try to add %i features to database." % count) | 167 shpimporter.DEBUG("Try to add %i features to database." % count) |
133 | 168 |
134 srcLayer.ResetReading() | 169 srcLayer.ResetReading() |
170 self.srcLayer = srcLayer | |
135 | 171 |
136 geomType = -1 | 172 geomType = -1 |
137 success = 0 | 173 success = 0 |
138 unsupported = 0 | 174 unsupported = 0 |
139 creationFailed = 0 | 175 creationFailed = 0 |