comparison flys-backend/contrib/shpimporter/importer.py @ 4884:b457532dae63

Importer: Move copy fields into importer class and track imported/unimported fields
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 28 Jan 2013 12:25:24 +0100
parents 82d00b0c7302
children 89a8764cabcc
comparison
equal deleted inserted replaced
4883:186dd186d5bd 4884:b457532dae63
12 self.config = config 12 self.config = config
13 self.dbconn = dbconn 13 self.dbconn = dbconn
14 self.river_id = config.river_id 14 self.river_id = config.river_id
15 self.dest_srs = osr.SpatialReference() 15 self.dest_srs = osr.SpatialReference()
16 self.dest_srs.ImportFromEPSG(config.target_srs) 16 self.dest_srs.ImportFromEPSG(config.target_srs)
17 self.handled_fields = []
17 18
18 def getKind(self, path): 19 def getKind(self, path):
19 raise NotImplementedError("Importer.getKind is abstract!") 20 raise NotImplementedError("Importer.getKind is abstract!")
20 21
21 def getPath(self, base): 22 def getPath(self, base):
73 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs) 74 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
74 geometry.Transform(transformer) 75 geometry.Transform(transformer)
75 76
76 return feat 77 return feat
77 78
79 def handled(self, field):
80 """
81 Register a field or a map of as handled during the import.
82
83 There is a warning printed after the import for each unhandled field!
84 """
85 if not field in self.handled_fields:
86 self.handled_fields.append(field)
87
88 def copyFields(self, src, target, mapping):
89 """
90 Checks the mapping dictonary for key value pairs to
91 copy from the source to the destination feature.
92
93 The Key is the attribute of the source feature to be copied
94 into the target attribute named by the dict's value.
95 """
96 self.handled_fields.extend(mapping.keys())
97 for key, value in mapping.items():
98 if src.GetFieldIndex(key) == -1:
99 continue
100 # 0 OFTInteger, Simple 32bit integer
101 # 1 OFTIntegerList, List of 32bit integers
102 # 2 OFTReal, Double Precision floating point
103 # 3 OFTRealList, List of doubles
104 # 4 OFTString, String of ASCII chars
105 # 5 OFTStringList, Array of strings
106 # 6 OFTWideString, deprecated
107 # 7 OFTWideStringList, deprecated
108 # 8 OFTBinary, Raw Binary data
109 # 9 OFTDate, Date
110 # 10 OFTTime, Time
111 # 11 OFTDateTime, Date and Time
112 if src.IsFieldSet(src.GetFieldIndex(key)):
113 if src.GetFieldType(key) == 2:
114 target.SetField(value, src.GetFieldAsDouble(key))
115 else:
116 target.SetField(value, src.GetField(key))
117
78 def shape2Database(self, srcLayer, name, path): 118 def shape2Database(self, srcLayer, name, path):
79 destLayer = self.dbconn.GetLayerByName(self.getTablename()) 119 destLayer = self.dbconn.GetLayerByName(self.getTablename())
80 120
81 if srcLayer is None: 121 if srcLayer is None:
82 shpimporter.ERROR("Shapefile is None!") 122 shpimporter.ERROR("Shapefile is None!")
127 167
128 shpimporter.INFO("Inserted %i features" % success) 168 shpimporter.INFO("Inserted %i features" % success)
129 shpimporter.INFO("Failed to create %i features" % creationFailed) 169 shpimporter.INFO("Failed to create %i features" % creationFailed)
130 shpimporter.INFO("Found %i unsupported features" % unsupported) 170 shpimporter.INFO("Found %i unsupported features" % unsupported)
131 171
172 unhandled = []
173 for i in range(0, srcLayer.GetLayerDefn().GetFieldCount()):
174 act_field = srcLayer.GetLayerDefn().GetFieldDefn(i).GetNameRef()
175 if not act_field in self.handled_fields:
176 unhandled.append(act_field)
177
178 if len(unhandled):
179 shpimporter.INFO("Did not import values from fields: %s " % \
180 " ".join(unhandled))
181
132 try: 182 try:
133 if self.config.dry_run > 0: 183 if self.config.dry_run > 0:
134 return geomType 184 return geomType
135 destLayer.CommitTransaction() 185 destLayer.CommitTransaction()
136 except e: 186 except e:

http://dive4elements.wald.intevation.org