comparison backend/contrib/shpimporter/hws.py @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-backend/contrib/shpimporter/hws.py@95050a944d81
children aef987124822
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 # -*- coding: utf-8 -*-
2 import os
3
4 try:
5 from osgeo import ogr
6 except ImportError:
7 import ogr
8
9 from importer import Importer
10 import utils
11
12 import logging
13 logger = logging.getLogger("HWS")
14
15 PATH="Hydrologie/HW-Schutzanlagen"
16 NAME="HWS"
17
18 # Keep in sync with hws_kinds table:
19 # strings need to be lowercase
20 HWS_KIND = {
21 "durchlass" : 1,
22 "damm" : 2,
23 "deich" : 2,
24 "hochufer" : 2,
25 "graben" : 3,
26 "rohr1" : 1,
27 "rohr 1" : 1,
28 "rohr 2" : 1,
29 "hauptdeich" : 2,
30 "sommerdeich" : 2
31 }
32
33 # Keep in sync with fed_states table:
34 # strings need to be lowercase
35 FED_STATES = {
36 "bayern" : 1,
37 "hessen" : 2,
38 "niedersachsen" : 3,
39 "nordrhein-westfalen" : 4,
40 "nordrhein westfalen" : 4,
41 "rheinland-pfalz" : 5,
42 "rheinland pfalz" : 5,
43 "saarland" : 6,
44 "schleswig-holstein" : 7,
45 "schleswig holstein" : 7,
46 "brandenburg" : 8,
47 "mecklenburg-vorpommern" : 9,
48 "mecklenburg vorpommern" : 9,
49 "thüringen" : 10,
50 "baden-württemberg" : 11,
51 "baden württemberg" : 11,
52 "sachsen-anhalt" : 12,
53 "sachsen anhalt" : 12,
54 "sachsen" : 13,
55 "berlin" : 14,
56 "bremen" : 15,
57 "hamburg" : 16,
58 }
59
60 class HWSPoints(Importer):
61 fieldmap = {
62 "name$" : "name",
63 "quelle$" : "source",
64 "anmerkung$" : "description",
65 "stand$" : "status_date",
66 "verband$" : "agency",
67 "Deich_{0,1}KM$" : "dike_km",
68 "Bereich$" : "range",
69 "H[oeö]{0,2}he_{0,1}SOLL$" : "z_target",
70 "(WSP_){0,1}BfG_{0,1}100$" : "rated_level",
71 "H[oeö]{0,2}he_{0,1}IST$" : "z",
72 }
73
74 printedforpath=[]
75
76 def getPath(self, base):
77 return "%s/%s" % (base, PATH)
78
79 def getTablename(self):
80 return "hws_points"
81
82 def getName(self):
83 return "HWS_POINTS"
84
85 def isGeometryValid(self, geomType):
86 return geomType == ogr.wkbPoint or geomType == ogr.wkbPoint25D
87
88 def isShapeRelevant(self, name, path):
89 shp = ogr.Open(path)
90 return self.isGeometryValid(shp.GetLayerByName(name).GetGeomType())
91
92 def getFedStateIDfromPath(self, path):
93 """
94 Tries to get extract a bundesland from the path
95 """
96 for state in sorted(FED_STATES.keys(), key = len, reverse = True):
97 if state in path.lower():
98 if not path in self.printedforpath:
99 logger.info("Extracted federal state from path: %s" % state)
100 self.printedforpath.append(path)
101 return FED_STATES[state]
102
103 def createNewFeature(self, featureDef, feat, **args):
104 newFeat = ogr.Feature(featureDef)
105 geometry = feat.GetGeometryRef()
106 geometry.SetCoordinateDimension(2)
107
108 self.copyFields(feat, newFeat, self.fieldmap)
109
110 newFeat.SetGeometry(geometry)
111
112 artname = self.searchField("art$")
113 if self.IsFieldSet(feat, artname):
114 self.handled(artname)
115 kind_id = HWS_KIND.get(feat.GetField(artname).lower())
116 if not kind_id:
117 logger.warn("Unknown Art: %s" % \
118 feat.GetField(artname))
119 newFeat.SetField("kind_id", 2)
120 else:
121 newFeat.SetField("kind_id", kind_id)
122 else:
123 newFeat.SetField("kind_id", 2)
124
125 fname = self.searchField("Bundesland$")
126 if self.IsFieldSet(feat, fname):
127 self.handled(fname)
128 fed_id = FED_STATES.get(feat.GetField(fname).lower())
129
130 if not fed_id:
131 logger.warn("Unknown Bundesland: %s" % \
132 feat.GetField(fname))
133 else:
134 newFeat.SetField("fed_state_id", fed_id)
135 else:
136 # Try to get the bundesland from path
137 fed_id = self.getFedStateIDfromPath(args['path'])
138 if fed_id:
139 newFeat.SetField("fed_state_id", fed_id)
140
141 fname = self.searchField("(ufer$)|(flussseite$)")
142 if self.IsFieldSet(feat, fname):
143 self.handled(fname)
144 shoreString = feat.GetField(fname)
145 if "links" in shoreString.lower():
146 newFeat.SetField("shore_side", True)
147 elif "rechts" in shoreString.lower():
148 newFeat.SetField("shore_side", False)
149
150
151 fname = self.searchField("river_{0,1}id$")
152 if self.IsFieldSet(feat, fname):
153 self.handled(fname)
154 if feat.GetField(fname) != self.river_id:
155 logger.warn("River_id mismatch between shapefile and"
156 " importer parameter.")
157 newFeat.SetField("river_id", feat.GetField(fname))
158 else:
159 newFeat.SetField("river_id", self.river_id)
160
161 fname = self.searchField("name$")
162 if not self.IsFieldSet(feat, fname):
163 newFeat.SetField("name", args['name'])
164
165 fname = self.searchField("offiziell$")
166 if self.IsFieldSet(feat, fname):
167 self.handled(fname)
168 offiziell = feat.GetField(fname)
169 if offiziell == "1" or offiziell == 1:
170 newFeat.SetField("official", True)
171 # Set the official value based on the file name as a fallback
172 elif args.get("name", "").lower() == "rohre_und_sperren" or \
173 args.get("name", "").lower() == "rohre-und-sperren":
174 newFeat.SetField("official", True)
175 else:
176 newFeat.SetField("official", False)
177
178 if self.IsFieldSet(newFeat, "z") and \
179 self.IsFieldSet(newFeat, "rated_level"):
180 fname = self.searchField("freibord(_m){0,1}$")
181 self.handled(fname)
182 z = newFeat.GetFieldAsDouble("z")
183 rl = newFeat.GetFieldAsDouble("rated_level")
184 newFeat.SetField("freeboard", z - rl)
185
186 return newFeat
187
188 class HWSLines(HWSPoints):
189
190 # TODO: GEOM_target, GEOM_rated_level, dike_km_from, dike_km_to
191 fieldmap = {
192 "name$" : "name",
193 "quelle$" : "source",
194 "anmerkung$" : "description",
195 "stand$" : "status_date",
196 "verband$" : "agency",
197 "Bereich$" : "range",
198 }
199
200 def getPath(self, base):
201 return "%s/%s" % (base, PATH)
202
203 def getTablename(self):
204 return "hws_lines"
205
206 def getName(self):
207 return "HWS_LINES"
208
209 def isGeometryValid(self, geomType):
210 return geomType in [ogr.wkbLineString,
211 ogr.wkbLineString25D,
212 ogr.wkbMultiLineString25D,
213 ogr.wkbMultiLineString]
214
215 def isShapeRelevant(self, name, path):
216 shp = ogr.Open(path)
217 return self.isGeometryValid(shp.GetLayerByName(name).GetGeomType())
218
219 def createNewFeature(self, featureDef, feat, **args):
220 newFeat = HWSPoints.createNewFeature(self, featureDef, feat, **args)
221 geometry = feat.GetGeometryRef()
222 if geometry.GetCoordinateDimension() == 2:
223 geometry.SetCoordinateDimension(3)
224 for i in range(0, geometry.GetPointCount()):
225 x,y,z = geometry.GetPoint(i)
226 z = 9999
227 geometry.SetPoint(i, x, y, z)
228 newFeat.SetGeometry(geometry)
229
230 return utils.convertToMultiLine(newFeat)
231
232

http://dive4elements.wald.intevation.org