comparison flys-backend/contrib/shpimporter/importer.py @ 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 c37effda3655
comparison
equal deleted inserted replaced
3654:59ca5dab2782 3655:8654e4480fc6
1 import ogr, osr 1 import ogr, osr
2 import shpimporter
2 3
3 class Importer: 4 class Importer:
4 5
5 def __init__(self, config): 6 def __init__(self, config):
6 self.config = config 7 self.config = config
45 def isShapeRelevant(self, name, path): 46 def isShapeRelevant(self, name, path):
46 return True 47 return True
47 48
48 49
49 def walkOverShapes(self, shape): 50 def walkOverShapes(self, shape):
50 print "---"
51 (name, path) = shape 51 (name, path) = shape
52 if not self.isShapeRelevant(name, path): 52 if not self.isShapeRelevant(name, path):
53 print "Skip shapefile '%s'" % name 53 shpimporter.INFO("Skip shapefile '%s'" % path)
54 return 54 return
55 55
56 shp = ogr.Open(shape[1]) 56 shp = ogr.Open(shape[1])
57 if shp is None: 57 if shp is None:
58 print "Shapefile '%s' could not be opened!" % path 58 shpimporter.ERROR("Shapefile '%s' could not be opened!" % path)
59 return 59 return
60 60
61 print "Opened shapefile '%s'" % path 61 shpimporter.INFO("Processing shapefile '%s'" % path)
62 srcLayer = shp.GetLayerByName(name) 62 srcLayer = shp.GetLayerByName(name)
63 63
64 if srcLayer is None: 64 if srcLayer is None:
65 print "Layer '%s' was not found!" % name 65 shpimporter.ERROR("Layer '%s' was not found!" % name)
66 return 66 return
67 67
68 return self.shape2Database(srcLayer, name, path) 68 return self.shape2Database(srcLayer, name, path)
69 69
70 70
71 def transform(self, feat): 71 def transform(self, feat):
72 geometry = feat.GetGeometryRef() 72 geometry = feat.GetGeometryRef()
73 src_srs = geometry.GetSpatialReference() 73 src_srs = geometry.GetSpatialReference()
74 74
75 if src_srs is None: 75 if src_srs is None:
76 print "Error: No source SRS given! No transformation possible!" 76 shpimporter.Error("No source SRS given! No transformation possible!")
77 return feat 77 return feat
78 78
79 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs) 79 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
80 geometry.Transform(transformer) 80 geometry.Transform(transformer)
81 81
85 def shape2Database(self, srcLayer, name, path): 85 def shape2Database(self, srcLayer, name, path):
86 table = ogr.Open(self.dbconn) 86 table = ogr.Open(self.dbconn)
87 destLayer = table.GetLayerByName(self.getTablename()) 87 destLayer = table.GetLayerByName(self.getTablename())
88 88
89 if srcLayer is None: 89 if srcLayer is None:
90 print "Shapefile is None!" 90 shpimporter.ERROR("Shapefile is None!")
91 return -1 91 return -1
92 92
93 if destLayer is None: 93 if destLayer is None:
94 print "No destination layer given!" 94 shpimporter.ERROR("No destination layer given!")
95 return -1 95 return -1
96 96
97 count = srcLayer.GetFeatureCount() 97 count = srcLayer.GetFeatureCount()
98 print "Try to add %i features to database." % count 98 shpimporter.DEBUG("Try to add %i features to database." % count)
99 99
100 srcLayer.ResetReading() 100 srcLayer.ResetReading()
101 101
102 geomType = -1 102 geomType = -1
103 success = 0 103 success = 0
121 121
122 if newFeat is not None: 122 if newFeat is not None:
123 newFeat = self.transform(newFeat) 123 newFeat = self.transform(newFeat)
124 res = destLayer.CreateFeature(newFeat) 124 res = destLayer.CreateFeature(newFeat)
125 if res is None or res > 0: 125 if res is None or res > 0:
126 print "Error while inserting feature: %r" % res 126 shpimporter.Error("Unable to insert feature: %r" % res)
127 else: 127 else:
128 success = success + 1 128 success = success + 1
129 else: 129 else:
130 creationFailed = creationFailed + 1 130 creationFailed = creationFailed + 1
131 else: 131 else:
132 unsupported = unsupported + 1 132 unsupported = unsupported + 1
133 133
134 print "Inserted %i features" % success 134 shpimporter.INFO("Inserted %i features" % success)
135 print "Failed to create %i features" % creationFailed 135 shpimporter.INFO("Failed to create %i features" % creationFailed)
136 print "Found %i unsupported features" % unsupported 136 shpimporter.INFO("Found %i unsupported features" % unsupported)
137 137
138 try: 138 try:
139 if self.config.dry_run > 0:
140 return geomType
139 destLayer.CommitTransaction() 141 destLayer.CommitTransaction()
140 except e: 142 except e:
141 print "Exception while committing transaction." 143 shpimporter.ERROR("Exception while committing transaction.")
142 144
143 return geomType 145 return geomType
144 146

http://dive4elements.wald.intevation.org