view flys-backend/contrib/shpimporter/hws.py @ 4935:c0a58558b817 dami

Importer: - Handle regular expressions for attribute names - Convert Strings to UTF-8 - Add regular expressions for hws_points values
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 31 Jan 2013 12:23:41 +0100
parents 11b459a3eb5c
children 7323847fa7df
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,
        "Hochufer" : 2,
        "Graben" : 3,
    }

# Keep in sync with fed_states table:
FED_STATES = {
    "Bayern" : 1,
    "Hessen" : 2,
    "Niedersachsen" : 3,
    "Nordrhein-Westfalen" : 4,
    "Nordrhein Westfalen" : 4,
    "Rheinland-Pfalz" : 5,
    "Rheinland Pfalz" : 5,
    "Saarland" : 6,
    "Schleswig-Holstein" : 7,
    "Schleswig Holstein" : 7,
    "Brandenburg" : 8,
    "Mecklenburg-Vorpommern" : 9,
    "Mecklenburg Vorpommern" : 9,
    "Thüringen" : 10,
    "Baden-Württemberg" : 11,
    "Baden Württemberg" : 11,
    "Sachsen-Anhalt" : 12,
    "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_{0,1}KM$" : "dike_km",
            "Bereich$" : "range",
            "H[oeö]{0,2}he_{0,1}SOLL$" : "z_target",
            "(WSP_){0,1}BfG_{0,1}100$" : "rated_level",
            "H[oeö]{0,2}he_{0,1}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 == ogr.wkbPoint or geomType == ogr.wkbPoint25D

    def isShapeRelevant(self, name, path):
        shp = ogr.Open(path)
        if shp.GetLayerByName(name).GetGeomType() == ogr.wkbPoint or \
             shp.GetLayerByName(name).GetGeomType() == ogr.wkbPoint25D:
            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.SetField("ogr_fid", feat.GetFID())
        artname = self.searchField("art$")
        if self.IsFieldSet(feat, artname):
            self.handled(artname)
            kind_id = HWS_KIND.get(feat.GetField(artname))
            if not kind_id:
                print ("Unknown Art: %s" % \
                        feat.GetField(artname))
            else:
                newFeat.SetField("kind_id", kind_id)

        fname = self.searchField("Bundesland$")
        if self.IsFieldSet(feat, fname):
            self.handled(fname)
            fed_id = FED_STATES.get(feat.GetField(fname))

            if not fed_id:
                print ("Unknown Bundesland: %s" % \
                        feat.GetField("Bundesland"))
            else:
                newFeat.SetField("fed_state_id", fed_id)

        fname = self.searchField("(ufer$)|(flussseite$)")
        if self.IsFieldSet(feat, fname):
            self.handled(fname)
            shoreString = feat.GetField(fname)
            if "links" in shoreString.lower():
                newFeat.SetField("shore_side", True)
            elif "rechts" in shoreString.lower():
                newFeat.SetField("shore_side", False)


        fname = self.searchField("river_{0,1}id$")
        if self.IsFieldSet(feat, fname):
            self.handled(fname)
            if feat.GetField(fname) != self.river_id:
                print ("River_id mismatch between shapefile and"
                     " importer parameter.")
            newFeat.SetField("river_id", feat.GetField(fname))
        else:
            newFeat.SetField("river_id", self.river_id)

        fname = self.searchField("name$")
        if not self.IsFieldSet(feat, fname):
            newFeat.SetField("name", args['name'])

        fname = self.searchField("offiziell$")
        if self.IsFieldSet(feat, fname):
            self.handled(fname)
            offiziell = feat.GetField(fname)
            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"):
            fname = self.searchField("freibord(_m){0,1}$")
            self.handled(fname)
            z = newFeat.GetFieldAsDouble("z")
            rl = newFeat.GetFieldAsDouble("rated_level")
            newFeat.SetField("freeboard", z - rl)

        return newFeat


http://dive4elements.wald.intevation.org