comparison flys-backend/contrib/shpimporter/uesg.py @ 2798:5a654f2e35bc

Added a python tool to import shapefiles into database. flys-backend/trunk@4127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 07 Mar 2012 20:37:03 +0000
parents
children bd9e76e0b55d
comparison
equal deleted inserted replaced
2797:e41d03bf9807 2798:5a654f2e35bc
1 import ogr
2
3 TABLE_NAME="floodmaps"
4
5 class UESG:
6
7 def __init__(self, dbconn):
8 self.dbconn = dbconn
9
10
11 def isGeometryValid(self, geomType):
12 if geomType == 3 or geomType == 6:
13 return True
14 else:
15 return False
16
17
18 def getKind(self, path):
19 kind = 0
20 if path.find("Berechnung") > 0:
21 kind = kind + 100
22
23 if path.find("Aktuell") > 0:
24 kind = kind + 10
25 else:
26 kind = kind + 20
27
28 if path.find("Land") > 0:
29 kind = kind + 2
30 else:
31 kind = kind + 1
32 else:
33 kind = kind + 200
34
35 return kind
36
37
38 def walkOverShapes(self, shape):
39 (name, path) = shape
40 shp = ogr.Open(shape[1])
41 if shp is None:
42 print "Shapefile '%s' could not be opened!" % path
43 return
44
45 print "Opened shapefile '%s'" % path
46 srcLayer = shp.GetLayerByName(name)
47
48 if srcLayer is None:
49 print "Layer '%s' was not found!" % name
50 return
51
52 return self.shape2Database(srcLayer, name, self.getKind(path))
53
54
55 def shape2Database(self, srcLayer, name, kind):
56 table = ogr.Open(self.dbconn)
57 destLayer = table.GetLayerByName(TABLE_NAME)
58
59 if srcLayer is None:
60 print "Shapefile is None!"
61 return -1
62
63 if destLayer is None:
64 print "No destination layer given!"
65 return -1
66
67 count = srcLayer.GetFeatureCount()
68 print "Try to add %i features to database." % count
69
70 srcLayer.ResetReading()
71
72 geomType = -1
73
74 featureDef = destLayer.GetLayerDefn()
75
76 for feat in srcLayer:
77 geom = feat.GetGeometryRef()
78 geomType = geom.GetGeometryType()
79
80 newFeat = self.createNewFeature(featureDef, feat)
81 newFeat.SetField("kind", kind)
82 newFeat.SetField("name", name)
83
84 if self.isGeometryValid(geomType):
85 res = destLayer.CreateFeature(newFeat)
86 if res is None or res > 0:
87 print "Error while inserting feature: %r" % res
88 else:
89 print "Geometry type not supported: %i" % geomType
90
91 try:
92 destLayer.CommitTransaction()
93 except e:
94 print "Exception while committing transaction."
95
96 return geomType
97
98
99 def createNewFeature(self, featureDef, feat):
100 newFeat = ogr.Feature(featureDef)
101 newFeat.SetGeometry(feat.GetGeometryRef())
102
103 if feat.IsFieldSet("river_id"):
104 riverId = feat.GetField("river_id")
105 else:
106 riverId = 1
107
108 if feat.IsFieldSet("diff"):
109 diff = feat.GetFieldAsDouble("diff")
110 else:
111 diff = 0
112
113 if feat.IsFieldSet("count"):
114 count = feat.GetFieldAsInteger("count")
115 else:
116 count = 0
117
118 if feat.IsFieldSet("area"):
119 area = feat.GetFieldAsDouble("area")
120 else:
121 area = 0
122
123 if feat.IsFieldSet("perimeter"):
124 perimeter = feat.GetFieldAsDouble("perimeter")
125 else:
126 perimeter = 0
127
128 groupId = 2
129
130 newFeat.SetField("river_id", riverId)
131 newFeat.SetField("diff", diff)
132 newFeat.SetField("count", count)
133 newFeat.SetField("area", area)
134 newFeat.SetField("perimeter", perimeter)
135
136 return newFeat
137

http://dive4elements.wald.intevation.org