[Dive4elements-commits] [PATCH 1 of 3] Extract Bundesland from path as a fallback if bundesland field is not set

Wald Commits scm-commit at wald.intevation.org
Thu Mar 7 10:45:45 CET 2013


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1362584682 -3600
# Node ID 13f3484d461821eb5bd856fa312347ab56c8a773
# Parent  08c5e3a646dc789cd78b6dc8fc133af088866ec9
Extract Bundesland from path as a fallback if bundesland field is not set.

diff -r 08c5e3a646dc -r 13f3484d4618 flys-backend/contrib/shpimporter/hws.py
--- 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):


More information about the Dive4elements-commits mailing list