changeset 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 186dd186d5bd
children 89a8764cabcc
files flys-backend/contrib/shpimporter/hws.py flys-backend/contrib/shpimporter/importer.py flys-backend/contrib/shpimporter/utils.py
diffstat 3 files changed, 58 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/hws.py	Fri Jan 25 15:43:05 2013 +0100
+++ b/flys-backend/contrib/shpimporter/hws.py	Mon Jan 28 12:25:24 2013 +0100
@@ -119,7 +119,7 @@
         geometry = feat.GetGeometryRef()
         geometry.SetCoordinateDimension(2)
 
-        utils.copyFields(feat, newFeat, self.fieldmap)
+        self.copyFields(feat, newFeat, self.fieldmap)
 
         newFeat.SetGeometry(geometry)
 
@@ -128,6 +128,7 @@
         newFeat.SetField("ogr_fid", feat.GetFID())
 
         if self.IsFieldSet(feat, "Art"):
+            self.handled("Art")
             kind_id = HWS_KIND.get(feat.GetField("Art"))
             if not kind_id:
                 print ("Unbekannte Art: %s" % \
@@ -136,6 +137,7 @@
                 newFeat.SetField("kind_id", kind_id)
 
         if self.IsFieldSet(feat, "Bundesland"):
+            self.handled("Bundesland")
             fed_id = FED_STATES.get(feat.GetField("Bundesland"))
 
             if not fed_id:
@@ -145,6 +147,7 @@
                 newFeat.SetField("fed_state_id", fed_id)
 
         if self.IsFieldSet(feat, "river_id"):
+            self.handled("river_id")
             if feat.GetField("river_id") != self.river_id:
                 print ("River_id mismatch between shapefile and"
                      " importer parameter.")
@@ -153,6 +156,7 @@
             newFeat.SetField("river_id", self.river_id)
 
         if self.IsFieldSet(feat, "Ufer"):
+            self.handled("Ufer")
             shoreString = feat.GetField("Ufer")
             if "links" in shoreString.lower():
                 newFeat.SetField("shore_side", True)
@@ -160,9 +164,11 @@
                 newFeat.SetField("shore_side", False)
 
         if not self.IsFieldSet(feat, "Name"):
+            self.handled("Name")
             newFeat.SetField("name", args['name'])
 
         if self.IsFieldSet(feat, "offiziell"):
+            self.handled("offiziell")
             offiziell = feat.GetField("offiziell")
             if offiziell == "1" or offiziell == 1:
                 newFeat.SetField("offiziell", True)
@@ -171,6 +177,7 @@
 
         if self.IsFieldSet(newFeat, "z") and \
             self.IsFieldSet(newFeat, "rated_level"):
+            self.handled("Freibord_m")
             z = newFeat.GetFieldAsDouble("z")
             rl = newFeat.GetFieldAsDouble("rated_level")
             newFeat.SetField("freeboard", z - rl)
--- 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
--- a/flys-backend/contrib/shpimporter/utils.py	Fri Jan 25 15:43:05 2013 +0100
+++ b/flys-backend/contrib/shpimporter/utils.py	Mon Jan 28 12:25:24 2013 +0100
@@ -31,35 +31,3 @@
     except UnicodeDecodeError:
         # Probably Windows iso-8859-x names so lets try again
         return unicode.encode(unicode(path, "iso-8859-15"), "UTF-8")
-
-
-# 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
-
-
-def copyFields(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.
-    """
-    for key, value in mapping.items():
-        if src.GetFieldIndex(key) == -1:
-            continue
-        if src.IsFieldSet(src.GetFieldIndex(key)):
-            if src.GetFieldType(key) == 2:
-                target.SetField(value, src.GetFieldAsDouble(key))
-            else:
-                target.SetField(value, src.GetField(key))

http://dive4elements.wald.intevation.org