Mercurial > dive4elements > river
diff flys-backend/contrib/shpimporter/uesg.py @ 2798:5a654f2e35bc
Added a python tool to import shapefiles into database.
flys-backend/trunk@4127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 07 Mar 2012 20:37:03 +0000 |
parents | |
children | bd9e76e0b55d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/contrib/shpimporter/uesg.py Wed Mar 07 20:37:03 2012 +0000 @@ -0,0 +1,137 @@ +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 +