diff flys-backend/contrib/shpimporter/importer.py @ 2861:b0132e1b9719

Added further shape importers and added the option to reproject shapes during the import process. flys-backend/trunk@4342 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 03 May 2012 14:19:52 +0000
parents bd9e76e0b55d
children 998f1a7dcfde
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/importer.py	Wed May 02 08:49:57 2012 +0000
+++ b/flys-backend/contrib/shpimporter/importer.py	Thu May 03 14:19:52 2012 +0000
@@ -1,10 +1,12 @@
-import ogr
+import ogr, osr
 
 class Importer:
 
-    def __init__(self, dbconn, river_id):
+    def __init__(self, dbconn, river_id, dest_srs):
         self.dbconn   = dbconn
         self.river_id = river_id
+        self.dest_srs = osr.SpatialReference()
+        self.dest_srs.ImportFromEPSG(dest_srs)
 
 
     def getKind(self, path):
@@ -60,6 +62,20 @@
         return self.shape2Database(srcLayer, name, path)
 
 
+    def transform(self, feat):
+        geometry = feat.GetGeometryRef()
+        src_srs  = geometry.GetSpatialReference()
+
+        if src_srs is None:
+            print "Error: No source SRS given! No transformation possible!"
+            return feat
+
+        transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
+        geometry.Transform(transformer)
+
+        return feat
+
+
     def shape2Database(self, srcLayer, name, path):
         table     = ogr.Open(self.dbconn)
         destLayer = table.GetLayerByName(self.getTablename())
@@ -80,10 +96,15 @@
         geomType    = -1
         success     = 0
         unsupported = 0
+        creationFailed = 0
         featureDef  = destLayer.GetLayerDefn()
 
         for feat in srcLayer:
             geom     = feat.GetGeometryRef()
+
+            if geom is None:
+                continue
+
             geomType = geom.GetGeometryType()
 
             if self.isGeometryValid(geomType):
@@ -93,15 +114,19 @@
                                                 path=path)
 
                 if newFeat is not None:
+                    newFeat = self.transform(newFeat)
                     res = destLayer.CreateFeature(newFeat)
                     if res is None or res > 0:
                         print "Error while inserting feature: %r" % res
                     else:
                         success = success + 1
+                else:
+                    creationFailed = creationFailed + 1
             else:
                 unsupported = unsupported + 1
 
         print "Inserted %i features" % success
+        print "Failed to create %i features" % creationFailed
         print "Found %i unsupported features" % unsupported
 
         try:

http://dive4elements.wald.intevation.org