aheinecke@4875: # -*- coding: utf-8 -*- aheinecke@4875: import os aheinecke@4875: aheinecke@4875: try: aheinecke@4875: from osgeo import ogr aheinecke@4875: except ImportErrror: aheinecke@4875: import ogr ingo@2862: ingo@2862: from importer import Importer aheinecke@4875: import utils ingo@2862: aheinecke@5006: import logging aheinecke@5006: logger = logging.getLogger("Hochwasserschutzanlagen") aheinecke@5006: ingo@2862: PATH="Hydrologie/HW-Schutzanlagen" ingo@3654: NAME="HWS" ingo@2862: aheinecke@4875: # Keep in sync with hws_kinds table: 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, aheinecke@4875: } ingo@2862: aheinecke@4875: # Keep in sync with fed_states table: 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@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@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@4875: newFeat.SetField("ogr_fid", feat.GetFID()) 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@4875: else: aheinecke@4875: newFeat.SetField("kind_id", kind_id) 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@4875: feat.GetField("Bundesland")) aheinecke@4875: else: aheinecke@4875: 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@4881: else: aheinecke@5003: newFeat.SetField("official", False) 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@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@4955: return geomType == ogr.wkbLineString or geomType == ogr.wkbLineString25D 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@4955: geometry.SetCoordinateDimension(3) aheinecke@4955: newFeat.SetGeometry(geometry) aheinecke@4955: aheinecke@4955: return newFeat aheinecke@4955: aheinecke@4955: