view flys-backend/contrib/shpimporter/uesg.py @ 2846:c1c6c9190636

Improved the db schema to store MINFO specific waterlevel differences. flys-backend/trunk@4289 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 24 Apr 2012 13:04:30 +0000
parents 5a654f2e35bc
children bd9e76e0b55d
line wrap: on
line source
import ogr

TABLE_NAME="floodmaps"

class UESG:

    def __init__(self, dbconn):
        self.dbconn = dbconn


    def isGeometryValid(self, geomType):
        if geomType == 3 or geomType == 6:
            return True
        else:
            return False


    def getKind(self, path):
        kind = 0
        if path.find("Berechnung") > 0:
            kind = kind + 100

            if path.find("Aktuell") > 0:
                kind = kind + 10
            else:
                kind = kind + 20

            if path.find("Land") > 0:
                kind = kind + 2
            else:
                kind = kind + 1
        else:
            kind = kind + 200

        return kind


    def walkOverShapes(self, shape):
        (name, path) = shape
        shp = ogr.Open(shape[1])
        if shp is None:
            print "Shapefile '%s' could not be opened!" % path
            return

        print "Opened shapefile '%s'" % path
        srcLayer = shp.GetLayerByName(name)

        if srcLayer is None:
            print "Layer '%s' was not found!" % name
            return

        return self.shape2Database(srcLayer, name, self.getKind(path))


    def shape2Database(self, srcLayer, name, kind):
        table = ogr.Open(self.dbconn)
        destLayer = table.GetLayerByName(TABLE_NAME)

        if srcLayer is None:
            print "Shapefile is None!"
            return -1

        if destLayer is None:
            print "No destination layer given!"
            return -1

        count = srcLayer.GetFeatureCount()
        print "Try to add %i features to database." % count

        srcLayer.ResetReading()

        geomType = -1

        featureDef = destLayer.GetLayerDefn()

        for feat in srcLayer:
            geom = feat.GetGeometryRef()
            geomType = geom.GetGeometryType()

            newFeat = self.createNewFeature(featureDef, feat)
            newFeat.SetField("kind", kind)
            newFeat.SetField("name", name)

            if self.isGeometryValid(geomType):
                res = destLayer.CreateFeature(newFeat)
                if res is None or res > 0:
                    print "Error while inserting feature: %r" % res
            else:
                print "Geometry type not supported: %i" % geomType

        try:
            destLayer.CommitTransaction()
        except e:
            print "Exception while committing transaction."

        return geomType


    def createNewFeature(self, featureDef, feat):
        newFeat = ogr.Feature(featureDef)
        newFeat.SetGeometry(feat.GetGeometryRef())

        if feat.IsFieldSet("river_id"):
            riverId = feat.GetField("river_id")
        else:
            riverId = 1

        if feat.IsFieldSet("diff"):
            diff = feat.GetFieldAsDouble("diff")
        else:
            diff = 0

        if feat.IsFieldSet("count"):
            count = feat.GetFieldAsInteger("count")
        else:
            count = 0

        if feat.IsFieldSet("area"):
            area = feat.GetFieldAsDouble("area")
        else:
            area = 0

        if feat.IsFieldSet("perimeter"):
            perimeter = feat.GetFieldAsDouble("perimeter")
        else:
            perimeter = 0

        groupId = 2

        newFeat.SetField("river_id", riverId)
        newFeat.SetField("diff", diff)
        newFeat.SetField("count", count)
        newFeat.SetField("area", area)
        newFeat.SetField("perimeter", perimeter)

        return newFeat

http://dive4elements.wald.intevation.org