Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
2860:2f874d14ac68 | 2861:b0132e1b9719 |
---|---|
1 import ogr | 1 import ogr, osr |
2 | 2 |
3 class Importer: | 3 class Importer: |
4 | 4 |
5 def __init__(self, dbconn, river_id): | 5 def __init__(self, dbconn, river_id, dest_srs): |
6 self.dbconn = dbconn | 6 self.dbconn = dbconn |
7 self.river_id = river_id | 7 self.river_id = river_id |
8 self.dest_srs = osr.SpatialReference() | |
9 self.dest_srs.ImportFromEPSG(dest_srs) | |
8 | 10 |
9 | 11 |
10 def getKind(self, path): | 12 def getKind(self, path): |
11 raise NotImplementedError("Importer.getKind is abstract!") | 13 raise NotImplementedError("Importer.getKind is abstract!") |
12 | 14 |
58 return | 60 return |
59 | 61 |
60 return self.shape2Database(srcLayer, name, path) | 62 return self.shape2Database(srcLayer, name, path) |
61 | 63 |
62 | 64 |
65 def transform(self, feat): | |
66 geometry = feat.GetGeometryRef() | |
67 src_srs = geometry.GetSpatialReference() | |
68 | |
69 if src_srs is None: | |
70 print "Error: No source SRS given! No transformation possible!" | |
71 return feat | |
72 | |
73 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs) | |
74 geometry.Transform(transformer) | |
75 | |
76 return feat | |
77 | |
78 | |
63 def shape2Database(self, srcLayer, name, path): | 79 def shape2Database(self, srcLayer, name, path): |
64 table = ogr.Open(self.dbconn) | 80 table = ogr.Open(self.dbconn) |
65 destLayer = table.GetLayerByName(self.getTablename()) | 81 destLayer = table.GetLayerByName(self.getTablename()) |
66 | 82 |
67 if srcLayer is None: | 83 if srcLayer is None: |
78 srcLayer.ResetReading() | 94 srcLayer.ResetReading() |
79 | 95 |
80 geomType = -1 | 96 geomType = -1 |
81 success = 0 | 97 success = 0 |
82 unsupported = 0 | 98 unsupported = 0 |
99 creationFailed = 0 | |
83 featureDef = destLayer.GetLayerDefn() | 100 featureDef = destLayer.GetLayerDefn() |
84 | 101 |
85 for feat in srcLayer: | 102 for feat in srcLayer: |
86 geom = feat.GetGeometryRef() | 103 geom = feat.GetGeometryRef() |
104 | |
105 if geom is None: | |
106 continue | |
107 | |
87 geomType = geom.GetGeometryType() | 108 geomType = geom.GetGeometryType() |
88 | 109 |
89 if self.isGeometryValid(geomType): | 110 if self.isGeometryValid(geomType): |
90 newFeat = self.createNewFeature(featureDef, | 111 newFeat = self.createNewFeature(featureDef, |
91 feat, | 112 feat, |
92 name=name, | 113 name=name, |
93 path=path) | 114 path=path) |
94 | 115 |
95 if newFeat is not None: | 116 if newFeat is not None: |
117 newFeat = self.transform(newFeat) | |
96 res = destLayer.CreateFeature(newFeat) | 118 res = destLayer.CreateFeature(newFeat) |
97 if res is None or res > 0: | 119 if res is None or res > 0: |
98 print "Error while inserting feature: %r" % res | 120 print "Error while inserting feature: %r" % res |
99 else: | 121 else: |
100 success = success + 1 | 122 success = success + 1 |
123 else: | |
124 creationFailed = creationFailed + 1 | |
101 else: | 125 else: |
102 unsupported = unsupported + 1 | 126 unsupported = unsupported + 1 |
103 | 127 |
104 print "Inserted %i features" % success | 128 print "Inserted %i features" % success |
129 print "Failed to create %i features" % creationFailed | |
105 print "Found %i unsupported features" % unsupported | 130 print "Found %i unsupported features" % unsupported |
106 | 131 |
107 try: | 132 try: |
108 destLayer.CommitTransaction() | 133 destLayer.CommitTransaction() |
109 except e: | 134 except e: |