diff flys-backend/contrib/shpimporter/importer.py @ 2853:bd9e76e0b55d

Improved the python shapefile importer. flys-backend/trunk@4313 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 27 Apr 2012 06:39:12 +0000
parents
children b0132e1b9719
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/contrib/shpimporter/importer.py	Fri Apr 27 06:39:12 2012 +0000
@@ -0,0 +1,113 @@
+import ogr
+
+class Importer:
+
+    def __init__(self, dbconn, river_id):
+        self.dbconn   = dbconn
+        self.river_id = river_id
+
+
+    def getKind(self, path):
+        raise NotImplementedError("Importer.getKind is abstract!")
+
+
+    def getPath(self, base):
+        raise NotImplementedError("Importer.getPath is abstract!")
+
+
+    def getTablename(self):
+        raise NotImplementedError("Importer.getTablename is abstract!")
+
+
+    def IsFieldSet(self, feat, name):
+        try:
+            isset = feat.GetField(name)
+            return isset is not None
+        except:
+            return False
+
+
+    def IsDoubleFieldSet(self, feat, name):
+        try:
+            isset = feat.GetFieldAsDouble(name)
+            return isset is not None
+        except:
+            return False
+
+
+    def isShapeRelevant(self, name, path):
+        return True
+
+
+    def walkOverShapes(self, shape):
+        (name, path) = shape
+        if not self.isShapeRelevant(name, path):
+            print "Skip shapefile '%s'" % name
+            return
+
+        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, path)
+
+
+    def shape2Database(self, srcLayer, name, path):
+        table     = ogr.Open(self.dbconn)
+        destLayer = table.GetLayerByName(self.getTablename())
+
+        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
+        success     = 0
+        unsupported = 0
+        featureDef  = destLayer.GetLayerDefn()
+
+        for feat in srcLayer:
+            geom     = feat.GetGeometryRef()
+            geomType = geom.GetGeometryType()
+
+            if self.isGeometryValid(geomType):
+                newFeat = self.createNewFeature(featureDef,
+                                                feat,
+                                                name=name,
+                                                path=path)
+
+                if newFeat is not None:
+                    res = destLayer.CreateFeature(newFeat)
+                    if res is None or res > 0:
+                        print "Error while inserting feature: %r" % res
+                    else:
+                        success = success + 1
+            else:
+                unsupported = unsupported + 1
+
+        print "Inserted %i features" % success
+        print "Found %i unsupported features" % unsupported
+
+        try:
+            destLayer.CommitTransaction()
+        except e:
+            print "Exception while committing transaction."
+
+        return geomType
+

http://dive4elements.wald.intevation.org