aheinecke@4875: # -*- coding: utf-8 -*-
aheinecke@4875: import os
aheinecke@4875: 
aheinecke@4875: try:
aheinecke@4875:     from osgeo import ogr
aheinecke@5077: except ImportError:
aheinecke@4875:     import ogr
ingo@2862: 
ingo@2862: from importer import Importer
aheinecke@4875: import utils
ingo@2862: 
aheinecke@5006: import logging
aheinecke@5215: logger = logging.getLogger("HWS")
aheinecke@5006: 
ingo@2862: PATH="Hydrologie/HW-Schutzanlagen"
ingo@3654: NAME="HWS"
ingo@2862: 
aheinecke@4875: # Keep in sync with hws_kinds table:
aheinecke@5178: # strings need to be lowercase
aheinecke@4875: HWS_KIND = {
aheinecke@5002:         "durchlass" : 1,
aheinecke@5002:         "damm" : 2,
aheinecke@5002:         "deich" : 2,
aheinecke@5002:         "hochufer" : 2,
aheinecke@5002:         "graben" : 3,
aheinecke@5002:         "rohr1" : 1,
tom@5218:         "rohr 1" : 1,
tom@5218:         "rohr 2" : 1,
tom@5143:         "hauptdeich" : 2,
tom@5143:         "sommerdeich" : 2
aheinecke@4875:     }
ingo@2862: 
aheinecke@4875: # Keep in sync with fed_states table:
aheinecke@5178: # strings need to be lowercase
aheinecke@4875: FED_STATES = {
aheinecke@5002:     "bayern" : 1,
aheinecke@5002:     "hessen" : 2,
aheinecke@5002:     "niedersachsen" : 3,
aheinecke@5002:     "nordrhein-westfalen" : 4,
aheinecke@5002:     "nordrhein westfalen" : 4,
aheinecke@5002:     "rheinland-pfalz" : 5,
aheinecke@5002:     "rheinland pfalz" : 5,
aheinecke@5002:     "saarland" : 6,
aheinecke@5002:     "schleswig-holstein" : 7,
aheinecke@5002:     "schleswig holstein" : 7,
aheinecke@5002:     "brandenburg" : 8,
aheinecke@5002:     "mecklenburg-vorpommern" : 9,
aheinecke@5002:     "mecklenburg vorpommern" : 9,
aheinecke@5002:     "thüringen" : 10,
aheinecke@5002:     "baden-württemberg" : 11,
aheinecke@5002:     "baden württemberg" : 11,
aheinecke@5002:     "sachsen-anhalt" : 12,
aheinecke@5002:     "sachsen anhalt" : 12,
aheinecke@5002:     "sachsen" : 13,
aheinecke@5002:     "berlin" : 14,
aheinecke@5002:     "bremen" : 15,
aheinecke@5002:     "hamburg" : 16,
aheinecke@4875: }
aheinecke@4875: 
aheinecke@4875: class HWSPoints(Importer):
aheinecke@4875:     fieldmap = {
aheinecke@4935:             "name$" : "name",
aheinecke@4935:             "quelle$" : "source",
aheinecke@4935:             "anmerkung$" : "description",
aheinecke@4935:             "stand$" : "status_date",
aheinecke@4935:             "verband$" : "agency",
aheinecke@4935:             "Deich_{0,1}KM$" : "dike_km",
aheinecke@4935:             "Bereich$" : "range",
aheinecke@4935:             "H[oeö]{0,2}he_{0,1}SOLL$" : "z_target",
aheinecke@4935:             "(WSP_){0,1}BfG_{0,1}100$" : "rated_level",
aheinecke@4935:             "H[oeö]{0,2}he_{0,1}IST$" : "z",
aheinecke@4875:         }
aheinecke@4875: 
aheinecke@5178:     printedforpath=[]
aheinecke@5178: 
aheinecke@4875:     def getPath(self, base):
aheinecke@4875:         return "%s/%s" % (base, PATH)
aheinecke@4875: 
aheinecke@4875:     def getTablename(self):
aheinecke@4875:         return "hws_points"
aheinecke@4875: 
aheinecke@4875:     def getName(self):
aheinecke@4875:         return "HWS_POINTS"
aheinecke@4875: 
aheinecke@4875:     def isGeometryValid(self, geomType):
aheinecke@4934:         return geomType == ogr.wkbPoint or geomType == ogr.wkbPoint25D
aheinecke@4875: 
aheinecke@4875:     def isShapeRelevant(self, name, path):
aheinecke@4934:         shp = ogr.Open(path)
aheinecke@4955:         return self.isGeometryValid(shp.GetLayerByName(name).GetGeomType())
aheinecke@4875: 
aheinecke@5178:     def getFedStateIDfromPath(self, path):
aheinecke@5178:         """
aheinecke@5178:         Tries to get extract a bundesland from the path
aheinecke@5178:         """
aheinecke@5178:         for state in sorted(FED_STATES.keys(), key = len, reverse = True):
aheinecke@5178:             if state in path.lower():
aheinecke@5178:                 if not path in self.printedforpath:
aheinecke@5178:                     logger.info("Extracted federal state from path: %s" % state)
aheinecke@5178:                     self.printedforpath.append(path)
aheinecke@5178:                 return FED_STATES[state]
aheinecke@5178: 
aheinecke@4875:     def createNewFeature(self, featureDef, feat, **args):
aheinecke@4875:         newFeat  = ogr.Feature(featureDef)
aheinecke@4875:         geometry = feat.GetGeometryRef()
aheinecke@4881:         geometry.SetCoordinateDimension(2)
aheinecke@4875: 
aheinecke@4884:         self.copyFields(feat, newFeat, self.fieldmap)
aheinecke@4875: 
aheinecke@4875:         newFeat.SetGeometry(geometry)
aheinecke@4875: 
aheinecke@4935:         artname = self.searchField("art$")
aheinecke@4935:         if self.IsFieldSet(feat, artname):
aheinecke@4935:             self.handled(artname)
aheinecke@5002:             kind_id = HWS_KIND.get(feat.GetField(artname).lower())
aheinecke@4875:             if not kind_id:
aheinecke@5006:                 logger.warn("Unknown Art: %s" % \
aheinecke@4935:                         feat.GetField(artname))
aheinecke@5382:                 newFeat.SetField("kind_id", 2)
aheinecke@4875:             else:
aheinecke@4875:                 newFeat.SetField("kind_id", kind_id)
aheinecke@5382:         else:
aheinecke@5382:             newFeat.SetField("kind_id", 2)
aheinecke@4875: 
aheinecke@4935:         fname = self.searchField("Bundesland$")
aheinecke@4935:         if self.IsFieldSet(feat, fname):
aheinecke@4935:             self.handled(fname)
aheinecke@5002:             fed_id = FED_STATES.get(feat.GetField(fname).lower())
aheinecke@4875: 
aheinecke@4875:             if not fed_id:
aheinecke@5006:                 logger.warn("Unknown Bundesland: %s" % \
aheinecke@5178:                         feat.GetField(fname))
aheinecke@4875:             else:
aheinecke@4875:                 newFeat.SetField("fed_state_id", fed_id)
aheinecke@5178:         else:
aheinecke@5178:             # Try to get the bundesland from path
aheinecke@5178:             fed_id = self.getFedStateIDfromPath(args['path'])
aheinecke@5178:             if fed_id:
aheinecke@5178:                 newFeat.SetField("fed_state_id", fed_id)
aheinecke@4875: 
aheinecke@4935:         fname = self.searchField("(ufer$)|(flussseite$)")
aheinecke@4935:         if self.IsFieldSet(feat, fname):
aheinecke@4935:             self.handled(fname)
aheinecke@4935:             shoreString = feat.GetField(fname)
aheinecke@4875:             if "links" in shoreString.lower():
aheinecke@4875:                 newFeat.SetField("shore_side", True)
aheinecke@4875:             elif "rechts" in shoreString.lower():
aheinecke@4875:                 newFeat.SetField("shore_side", False)
aheinecke@4875: 
aheinecke@4935: 
aheinecke@4935:         fname = self.searchField("river_{0,1}id$")
aheinecke@4935:         if self.IsFieldSet(feat, fname):
aheinecke@4935:             self.handled(fname)
aheinecke@4935:             if feat.GetField(fname) != self.river_id:
aheinecke@5006:                 logger.warn("River_id mismatch between shapefile and"
aheinecke@4935:                      " importer parameter.")
aheinecke@4935:             newFeat.SetField("river_id", feat.GetField(fname))
aheinecke@4935:         else:
aheinecke@4935:             newFeat.SetField("river_id", self.river_id)
aheinecke@4935: 
aheinecke@4935:         fname = self.searchField("name$")
aheinecke@4935:         if not self.IsFieldSet(feat, fname):
aheinecke@4875:             newFeat.SetField("name", args['name'])
aheinecke@4875: 
aheinecke@4935:         fname = self.searchField("offiziell$")
aheinecke@4935:         if self.IsFieldSet(feat, fname):
aheinecke@4935:             self.handled(fname)
aheinecke@4935:             offiziell = feat.GetField(fname)
aheinecke@4881:             if offiziell == "1" or offiziell == 1:
aheinecke@5003:                 newFeat.SetField("official", True)
aheinecke@5003:         # Set the official value based on the file name as a fallback
aheinecke@5003:         elif args.get("name", "").lower() == "rohre_und_sperren" or \
aheinecke@5003:                 args.get("name", "").lower() == "rohre-und-sperren":
aheinecke@5003:              newFeat.SetField("official", True)
aheinecke@5392:         else:
aheinecke@5392:             newFeat.SetField("official", False)
aheinecke@4881: 
aheinecke@4881:         if self.IsFieldSet(newFeat, "z") and \
aheinecke@4881:             self.IsFieldSet(newFeat, "rated_level"):
aheinecke@4935:             fname = self.searchField("freibord(_m){0,1}$")
aheinecke@4935:             self.handled(fname)
aheinecke@4881:             z = newFeat.GetFieldAsDouble("z")
aheinecke@4881:             rl = newFeat.GetFieldAsDouble("rated_level")
aheinecke@4881:             newFeat.SetField("freeboard", z - rl)
aheinecke@4875: 
aheinecke@4875:         return newFeat
aheinecke@4875: 
aheinecke@4955: class HWSLines(HWSPoints):
aheinecke@4875: 
aheinecke@4955:     # TODO: GEOM_target, GEOM_rated_level, dike_km_from, dike_km_to
aheinecke@4955:     fieldmap = {
aheinecke@4955:             "name$" : "name",
aheinecke@4955:             "quelle$" : "source",
aheinecke@4955:             "anmerkung$" : "description",
aheinecke@4955:             "stand$" : "status_date",
aheinecke@4955:             "verband$" : "agency",
aheinecke@4955:             "Bereich$" : "range",
aheinecke@4955:         }
aheinecke@4955: 
aheinecke@4955:     def getPath(self, base):
aheinecke@4955:         return "%s/%s" % (base, PATH)
aheinecke@4955: 
aheinecke@4955:     def getTablename(self):
aheinecke@4955:         return "hws_lines"
aheinecke@4955: 
aheinecke@4955:     def getName(self):
aheinecke@4955:         return "HWS_LINES"
aheinecke@4955: 
aheinecke@4955:     def isGeometryValid(self, geomType):
aheinecke@5137:         return geomType in [ogr.wkbLineString,
aheinecke@5137:                             ogr.wkbLineString25D,
aheinecke@5137:                             ogr.wkbMultiLineString25D,
aheinecke@5137:                             ogr.wkbMultiLineString]
aheinecke@4955: 
aheinecke@4955:     def isShapeRelevant(self, name, path):
aheinecke@4955:         shp = ogr.Open(path)
aheinecke@4955:         return self.isGeometryValid(shp.GetLayerByName(name).GetGeomType())
aheinecke@4955: 
aheinecke@4955:     def createNewFeature(self, featureDef, feat, **args):
aheinecke@4955:         newFeat = HWSPoints.createNewFeature(self, featureDef, feat, **args)
aheinecke@4955:         geometry = feat.GetGeometryRef()
aheinecke@5137:         if geometry.GetCoordinateDimension() == 2:
aheinecke@5137:             geometry.SetCoordinateDimension(3)
aheinecke@5139:             for i in range(0, geometry.GetPointCount()):
aheinecke@5139:                 x,y,z = geometry.GetPoint(i)
aheinecke@5139:                 z = 9999
aheinecke@5139:                 geometry.SetPoint(i, x, y, z)
aheinecke@4955:         newFeat.SetGeometry(geometry)
aheinecke@4955: 
aheinecke@5137:         return utils.convertToMultiLine(newFeat)
aheinecke@4955: 
aheinecke@4955: