diff flys-backend/contrib/shpimporter/hws.py @ 5178:13f3484d4618

Extract Bundesland from path as a fallback if bundesland field is not set.
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 06 Mar 2013 16:44:42 +0100
parents d25eebfc5cee
children c8e79cff622a
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/hws.py	Wed Mar 06 16:28:04 2013 +0100
+++ b/flys-backend/contrib/shpimporter/hws.py	Wed Mar 06 16:44:42 2013 +0100
@@ -16,6 +16,7 @@
 NAME="HWS"
 
 # Keep in sync with hws_kinds table:
+# strings need to be lowercase
 HWS_KIND = {
         "durchlass" : 1,
         "damm" : 2,
@@ -28,6 +29,7 @@
     }
 
 # Keep in sync with fed_states table:
+# strings need to be lowercase
 FED_STATES = {
     "bayern" : 1,
     "hessen" : 2,
@@ -67,6 +69,8 @@
             "H[oeƶ]{0,2}he_{0,1}IST$" : "z",
         }
 
+    printedforpath=[]
+
     def getPath(self, base):
         return "%s/%s" % (base, PATH)
 
@@ -83,6 +87,17 @@
         shp = ogr.Open(path)
         return self.isGeometryValid(shp.GetLayerByName(name).GetGeomType())
 
+    def getFedStateIDfromPath(self, path):
+        """
+        Tries to get extract a bundesland from the path
+        """
+        for state in sorted(FED_STATES.keys(), key = len, reverse = True):
+            if state in path.lower():
+                if not path in self.printedforpath:
+                    logger.info("Extracted federal state from path: %s" % state)
+                    self.printedforpath.append(path)
+                return FED_STATES[state]
+
     def createNewFeature(self, featureDef, feat, **args):
         newFeat  = ogr.Feature(featureDef)
         geometry = feat.GetGeometryRef()
@@ -109,9 +124,14 @@
 
             if not fed_id:
                 logger.warn("Unknown Bundesland: %s" % \
-                        feat.GetField("Bundesland"))
+                        feat.GetField(fname))
             else:
                 newFeat.SetField("fed_state_id", fed_id)
+        else:
+            # Try to get the bundesland from path
+            fed_id = self.getFedStateIDfromPath(args['path'])
+            if fed_id:
+                newFeat.SetField("fed_state_id", fed_id)
 
         fname = self.searchField("(ufer$)|(flussseite$)")
         if self.IsFieldSet(feat, fname):

http://dive4elements.wald.intevation.org