diff 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
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/importer.py	Fri Jan 25 15:43:05 2013 +0100
+++ b/flys-backend/contrib/shpimporter/importer.py	Mon Jan 28 12:25:24 2013 +0100
@@ -14,6 +14,7 @@
         self.river_id = config.river_id
         self.dest_srs = osr.SpatialReference()
         self.dest_srs.ImportFromEPSG(config.target_srs)
+        self.handled_fields = []
 
     def getKind(self, path):
         raise NotImplementedError("Importer.getKind is abstract!")
@@ -75,6 +76,45 @@
 
         return feat
 
+    def handled(self, field):
+        """
+        Register a field or a map of as handled during the import.
+
+        There is a warning printed after the import for each unhandled field!
+        """
+        if not field in self.handled_fields:
+            self.handled_fields.append(field)
+
+    def copyFields(self, src, target, mapping):
+        """
+        Checks the mapping dictonary for key value pairs to
+        copy from the source to the destination feature.
+
+        The Key is the attribute of the source feature to be copied
+        into the target attribute named by the dict's value.
+        """
+        self.handled_fields.extend(mapping.keys())
+        for key, value in mapping.items():
+            if src.GetFieldIndex(key) == -1:
+                continue
+            # 0 OFTInteger, Simple 32bit integer
+            # 1 OFTIntegerList, List of 32bit integers
+            # 2 OFTReal, Double Precision floating point
+            # 3 OFTRealList, List of doubles
+            # 4 OFTString, String of ASCII chars
+            # 5 OFTStringList, Array of strings
+            # 6 OFTWideString, deprecated
+            # 7 OFTWideStringList, deprecated
+            # 8 OFTBinary, Raw Binary data
+            # 9 OFTDate, Date
+            # 10 OFTTime, Time
+            # 11 OFTDateTime, Date and Time
+            if src.IsFieldSet(src.GetFieldIndex(key)):
+                if src.GetFieldType(key) == 2:
+                    target.SetField(value, src.GetFieldAsDouble(key))
+                else:
+                    target.SetField(value, src.GetField(key))
+
     def shape2Database(self, srcLayer, name, path):
         destLayer = self.dbconn.GetLayerByName(self.getTablename())
 
@@ -129,6 +169,16 @@
         shpimporter.INFO("Failed to create %i features" % creationFailed)
         shpimporter.INFO("Found %i unsupported features" % unsupported)
 
+        unhandled = []
+        for i in range(0, srcLayer.GetLayerDefn().GetFieldCount()):
+            act_field = srcLayer.GetLayerDefn().GetFieldDefn(i).GetNameRef()
+            if not act_field in self.handled_fields:
+                unhandled.append(act_field)
+
+        if len(unhandled):
+            shpimporter.INFO("Did not import values from fields: %s " % \
+                    " ".join(unhandled))
+
         try:
             if self.config.dry_run > 0:
                 return geomType

http://dive4elements.wald.intevation.org