changeset 3655:8654e4480fc6

Shape importer: added command line option dry_run to supress database transactions. flys-backend/trunk@5235 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 22 Aug 2012 13:04:05 +0000
parents 59ca5dab2782
children 6603aeeda6e3
files flys-backend/ChangeLog flys-backend/contrib/shpimporter/axis.py flys-backend/contrib/shpimporter/importer.py flys-backend/contrib/shpimporter/shpimporter.py
diffstat 4 files changed, 36 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Wed Aug 22 11:55:55 2012 +0000
+++ b/flys-backend/ChangeLog	Wed Aug 22 13:04:05 2012 +0000
@@ -1,3 +1,14 @@
+2012-08-22  Ingo Weinzierl <ingo@intevation.de>
+
+	* contrib/shpimporter/axis.py: Use log methods of shpimporter and
+	  removed print() calls.
+
+	* contrib/shpimporter/importer.py: Evaluate command line option 'dry_run'.
+	  Do not commit database transaction if it is activated.
+
+	* contrib/shpimporter/shpimporter.py: Added new command line option
+	  'dry_run' to supress database transactions.
+
 2012-08-22  Ingo Weinzierl <ingo@intevation.de>
 
 	* contrib/shpimporter/shpimporter.py: Use OptionParse to read command line
--- a/flys-backend/contrib/shpimporter/axis.py	Wed Aug 22 11:55:55 2012 +0000
+++ b/flys-backend/contrib/shpimporter/axis.py	Wed Aug 22 13:04:05 2012 +0000
@@ -1,6 +1,7 @@
 import ogr
 
 from importer import Importer
+import shpimporter
 
 NAME="Axis"
 TABLE_NAME="river_axes"
--- a/flys-backend/contrib/shpimporter/importer.py	Wed Aug 22 11:55:55 2012 +0000
+++ b/flys-backend/contrib/shpimporter/importer.py	Wed Aug 22 13:04:05 2012 +0000
@@ -1,4 +1,5 @@
 import ogr, osr
+import shpimporter
 
 class Importer:
 
@@ -47,22 +48,21 @@
 
 
     def walkOverShapes(self, shape):
-        print "---"
         (name, path) = shape
         if not self.isShapeRelevant(name, path):
-            print "Skip shapefile '%s'" % name
+            shpimporter.INFO("Skip shapefile '%s'" % path)
             return
 
         shp = ogr.Open(shape[1])
         if shp is None:
-            print "Shapefile '%s' could not be opened!" % path
+            shpimporter.ERROR("Shapefile '%s' could not be opened!" % path)
             return
 
-        print "Opened shapefile '%s'" % path
+        shpimporter.INFO("Processing shapefile '%s'" % path)
         srcLayer = shp.GetLayerByName(name)
 
         if srcLayer is None:
-            print "Layer '%s' was not found!" % name
+            shpimporter.ERROR("Layer '%s' was not found!" % name)
             return
 
         return self.shape2Database(srcLayer, name, path)
@@ -73,7 +73,7 @@
         src_srs  = geometry.GetSpatialReference()
 
         if src_srs is None:
-            print "Error: No source SRS given! No transformation possible!"
+            shpimporter.Error("No source SRS given! No transformation possible!")
             return feat
 
         transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
@@ -87,15 +87,15 @@
         destLayer = table.GetLayerByName(self.getTablename())
 
         if srcLayer is None:
-            print "Shapefile is None!"
+            shpimporter.ERROR("Shapefile is None!")
             return -1
 
         if destLayer is None:
-            print "No destination layer given!"
+            shpimporter.ERROR("No destination layer given!")
             return -1
 
         count = srcLayer.GetFeatureCount()
-        print "Try to add %i features to database." % count
+        shpimporter.DEBUG("Try to add %i features to database." % count)
 
         srcLayer.ResetReading()
 
@@ -123,7 +123,7 @@
                     newFeat = self.transform(newFeat)
                     res = destLayer.CreateFeature(newFeat)
                     if res is None or res > 0:
-                        print "Error while inserting feature: %r" % res
+                        shpimporter.Error("Unable to insert feature: %r" % res)
                     else:
                         success = success + 1
                 else:
@@ -131,14 +131,16 @@
             else:
                 unsupported = unsupported + 1
 
-        print "Inserted %i features" % success
-        print "Failed to create %i features" % creationFailed
-        print "Found %i unsupported features" % unsupported
+        shpimporter.INFO("Inserted %i features" % success)
+        shpimporter.INFO("Failed to create %i features" % creationFailed)
+        shpimporter.INFO("Found %i unsupported features" % unsupported)
 
         try:
+            if self.config.dry_run > 0:
+                return geomType
             destLayer.CommitTransaction()
         except e:
-            print "Exception while committing transaction."
+            shpimporter.ERROR("Exception while committing transaction.")
 
         return geomType
 
--- a/flys-backend/contrib/shpimporter/shpimporter.py	Wed Aug 22 11:55:55 2012 +0000
+++ b/flys-backend/contrib/shpimporter/shpimporter.py	Wed Aug 22 13:04:05 2012 +0000
@@ -62,6 +62,7 @@
     parser.add_option("--password", type="string")
     parser.add_option("--river_id", type="int")
     parser.add_option("--verbose", type="int", default=1)
+    parser.add_option("--dry_run", type="int", default=0)
     parser.add_option("--skip_axis", type="int")
     parser.add_option("--skip_hydr_boundaries", type="int")
     parser.add_option("--skip_buildings", type="int")
@@ -134,10 +135,12 @@
         return
 
     if config == None:
-        print "No Config"
         ERROR("Unable to read config from command line!")
         return
 
+    if config.dry_run > 0:
+        INFO("You enable 'dry_run'. No database transaction will take place!")
+
     importers = getImporters(config)
     types = {}
 
@@ -149,13 +152,14 @@
         INFO("Start import of '%s'" % importer.getName())
 
         shapes = utils.findShapefiles(importer.getPath(config.directory))
-        INFO("Found %i Shapefiles" % len(shapes))
+        DEBUG("Found %i Shapefiles" % len(shapes))
 
         for shpTuple in shapes:
             geomType = importer.walkOverShapes(shpTuple)
             try:
-                num = types[geomType]
-                types[geomType] = num+1
+                if geomType is not None:
+                    num = types[geomType]
+                    types[geomType] = num+1
             except:
                 types[geomType] = 1
 

http://dive4elements.wald.intevation.org