comparison flys-backend/contrib/shpimporter/importer.py @ 3689:c938e568c4a2 2.9

merged flys-backend/2.9
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:44 +0200
parents 2cd2657b3d60
children 82e931f88137
comparison
equal deleted inserted replaced
3651:06a65baae494 3689:c938e568c4a2
1 import ogr, osr
2 import shpimporter
3
4 class Importer:
5
6 def __init__(self, config):
7 self.config = config
8 self.dbconn = 'OCI:%s/%s@%s' % (config.user, config.password, config.host)
9 self.river_id = config.river_id
10 self.dest_srs = osr.SpatialReference()
11 self.dest_srs.ImportFromEPSG(config.target_srs)
12
13
14 def getKind(self, path):
15 raise NotImplementedError("Importer.getKind is abstract!")
16
17
18 def getPath(self, base):
19 raise NotImplementedError("Importer.getPath is abstract!")
20
21
22 def getTablename(self):
23 raise NotImplementedError("Importer.getTablename is abstract!")
24
25
26 def getName(self):
27 raise NotImplementedError("Importer.getTablename is abstract!")
28
29
30 def IsFieldSet(self, feat, name):
31 try:
32 isset = feat.GetField(name)
33 return isset is not None
34 except:
35 return False
36
37
38 def IsDoubleFieldSet(self, feat, name):
39 try:
40 isset = feat.GetFieldAsDouble(name)
41 return isset is not None
42 except:
43 return False
44
45
46 def isShapeRelevant(self, name, path):
47 return True
48
49
50 def walkOverShapes(self, shape):
51 (name, path) = shape
52 if not self.isShapeRelevant(name, path):
53 shpimporter.INFO("Skip shapefile '%s'" % path)
54 return
55
56 shp = ogr.Open(shape[1])
57 if shp is None:
58 shpimporter.ERROR("Shapefile '%s' could not be opened!" % path)
59 return
60
61 shpimporter.INFO("Processing shapefile '%s'" % path)
62 srcLayer = shp.GetLayerByName(name)
63
64 if srcLayer is None:
65 shpimporter.ERROR("Layer '%s' was not found!" % name)
66 return
67
68 return self.shape2Database(srcLayer, name, path)
69
70
71 def transform(self, feat):
72 geometry = feat.GetGeometryRef()
73 src_srs = geometry.GetSpatialReference()
74
75 if src_srs is None:
76 shpimporter.ERROR("No source SRS given! No transformation possible!")
77 return feat
78
79 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
80 geometry.Transform(transformer)
81
82 return feat
83
84
85 def shape2Database(self, srcLayer, name, path):
86 table = ogr.Open(self.dbconn)
87 destLayer = table.GetLayerByName(self.getTablename())
88
89 if srcLayer is None:
90 shpimporter.ERROR("Shapefile is None!")
91 return -1
92
93 if destLayer is None:
94 shpimporter.ERROR("No destination layer given!")
95 return -1
96
97 count = srcLayer.GetFeatureCount()
98 shpimporter.DEBUG("Try to add %i features to database." % count)
99
100 srcLayer.ResetReading()
101
102 geomType = -1
103 success = 0
104 unsupported = 0
105 creationFailed = 0
106 featureDef = destLayer.GetLayerDefn()
107
108 for feat in srcLayer:
109 geom = feat.GetGeometryRef()
110
111 if geom is None:
112 continue
113
114 geomType = geom.GetGeometryType()
115
116 if self.isGeometryValid(geomType):
117 newFeat = self.createNewFeature(featureDef,
118 feat,
119 name=name,
120 path=path)
121
122 if newFeat is not None:
123 newFeat.SetField("path", path)
124 newFeat = self.transform(newFeat)
125 res = destLayer.CreateFeature(newFeat)
126 if res is None or res > 0:
127 shpimporter.Error("Unable to insert feature: %r" % res)
128 else:
129 success = success + 1
130 else:
131 creationFailed = creationFailed + 1
132 else:
133 unsupported = unsupported + 1
134
135 shpimporter.INFO("Inserted %i features" % success)
136 shpimporter.INFO("Failed to create %i features" % creationFailed)
137 shpimporter.INFO("Found %i unsupported features" % unsupported)
138
139 try:
140 if self.config.dry_run > 0:
141 return geomType
142 destLayer.CommitTransaction()
143 except e:
144 shpimporter.ERROR("Exception while committing transaction.")
145
146 return geomType
147

http://dive4elements.wald.intevation.org