view flys-backend/contrib/shpimporter/hws.py @ 5123:64766b89beb6

WQAdaptedInputPanel: Refactored to remove code dupes.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 27 Feb 2013 12:19:41 +0100
parents b457532dae63
children 60b026b4340e
line wrap: on
line source
# -*- coding: utf-8 -*-
import os

try:
    from osgeo import ogr
except ImportErrror:
    import ogr

from importer import Importer
import utils

PATH="Hydrologie/HW-Schutzanlagen"
NAME="HWS"

# Keep in sync with hws_kinds table:
HWS_KIND = {
        "Durchlass" : 1,
        "Damm" : 2,
        "Deich" : 2,
        "Graben" : 3,
    }

# Keep in sync with fed_states table:
FED_STATES = {
    "Bayern" : 1,
    "Hessen" : 2,
    "Niedersachsen" : 3,
    "Nordrhein-Westfalen" : 4,
    "Rheinland-Pfalz" : 5,
    "Saarland" : 6,
    "Schleswig-Holstein" : 7,
    "Brandenburg" : 8,
    "Mecklenburg-Vorpommern" : 9,
    "Thüringen" : 10,
    "Baden-Württemberg" : 11,
    "Sachsen-Anhalt" : 12,
    "Sachsen" : 13,
    "Berlin" : 14,
    "Bremen" : 15,
    "Hamburg" : 16,
}

class HWSLines(Importer):

    def getPath(self, base):
        return "%s/%s" % (base, PATH)

    def getTablename(self):
        return "hws_lines"

    def getName(self):
        return "HWS_LINES"

    def isGeometryValid(self, geomType):
        return geomType == 2

    def isShapeRelevant(self, name, path):
        return True

    def createNewFeature(self, featureDef, feat, **args):
        newFeat  = ogr.Feature(featureDef)
        geometry = feat.GetGeometryRef()
        geometry.SetCoordinateDimension(2)

        newFeat.SetGeometry(geometry)

        if self.IsFieldSet(feat, "river_id"):
            newFeat.SetField("river_id", feat.GetField("river_id"))
        else:
            newFeat.SetField("river_id", self.river_id)

        if self.IsFieldSet(feat, "TYP"):
            newFeat.SetField("type", feat.GetField("TYP"))

        if self.IsFieldSet(feat, "Bauart"):
            newFeat.SetField("hws_facility", feat.GetField("Bauart"))

        if self.IsFieldSet(feat, "Name"):
            newFeat.SetField("name", feat.GetField("name"))
        else:
            newFeat.SetField("name", args['name'])

        return newFeat

class HWSPoints(Importer):
    fieldmap = {
            "Name" : "name",
            "Quelle" : "source",
            "Anmerkung" : "description",
            "Stand" : "status_date",
            "Verband" : "agency",
            "Deich_KM" : "dike_km",
            "Bereich" : "range",
            "Höhe_SOLL" : "z_target",
            "WSP_BfG100" : "rated_level",
            "Hoehe_IST" : "z",
        }

    def getPath(self, base):
        return "%s/%s" % (base, PATH)

    def getTablename(self):
        return "hws_points"

    def getName(self):
        return "HWS_POINTS"

    def isGeometryValid(self, geomType):
        return geomType == 1

    def isShapeRelevant(self, name, path):
        if "punkte" in os.path.basename(path).lower():
            return True
        else:
            return False

    def createNewFeature(self, featureDef, feat, **args):
        newFeat  = ogr.Feature(featureDef)
        geometry = feat.GetGeometryRef()
        geometry.SetCoordinateDimension(2)

        self.copyFields(feat, newFeat, self.fieldmap)

        newFeat.SetGeometry(geometry)

        newFeat.SetFID(feat.GetFID())

        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" % \
                        feat.GetField("Art"))
            else:
                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:
                print ("Unbekanntes Bundesland: %s" % \
                        feat.GetField("Bundesland"))
            else:
                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.")
            newFeat.SetField("river_id", feat.GetField("river_id"))
        else:
            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)
            elif "rechts" in shoreString.lower():
                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)
            else:
                newFeat.SetField("offiziell", False)

        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)

        return newFeat


http://dive4elements.wald.intevation.org