annotate backend/contrib/shpimporter/buildings.py @ 8682:5102733c4567

issue1755 Prevent out of bounds access when there is no data If there is no data it will default to the complete river. In the following overview you can then see that there is no data. Better then a "Error changing state".
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 13 Apr 2015 11:03:00 +0200
parents 5aa05a7a34b7
children
rev   line source
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
1 # -*- coding: utf-8 -*-
5004
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
2 try:
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
3 from osgeo import ogr
5077
c5187ab9f571 Fix Syntax Errror
Andre Heinecke <aheinecke@intevation.de>
parents: 5004
diff changeset
4 except ImportError:
5004
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
5 import ogr
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
6
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
7 from importer import Importer
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
8
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
9 TABLE_NAME="buildings"
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 PATH="Geodaesie/Bauwerke"
3654
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2861
diff changeset
11 NAME="Buildings"
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
13 BUILDING_KINDS= {
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
14 "sonstige" : 0,
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
15 "brücken" : 1,
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
16 "wehre" : 2,
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
17 "pegel" : 3,
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
18 }
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
19
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
20 class Building(Importer):
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
21 fieldmap = {
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
22 "^station$" : "km",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
23 "^km$" : "km",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
24 "^wsv-km$" : "km",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
25 "^z$" : "z",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
26 "^H[oeö]{0,2}he$" : "z",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
27 "^m+NHN$" : "z",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
28 "^KWNAAM$" : "description",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
29 "^Name$" : "description"
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
30 }
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
31
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
32 def getPath(self, base):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
33 return "%s/%s" % (base, PATH)
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
34
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
35
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
36 def getTablename(self):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
37 return TABLE_NAME
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
38
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
39
3654
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2861
diff changeset
40 def getName(self):
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2861
diff changeset
41 return NAME
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2861
diff changeset
42
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2861
diff changeset
43
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
44 def isGeometryValid(self, geomType):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
45 return geomType == 2
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
46
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
48 def isShapeRelevant(self, name, path):
5556
b91cc44312b7 Fix skipping of buhnen.shp
Andre Heinecke <aheinecke@intevation.de>
parents: 5401
diff changeset
49 return "buhnen.shp" not in path.lower()
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
50
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
51 def getKind(self, feat, path):
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
52 # First try to resolve it with the filename
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
53 for fname in ["brücke.shp", "bruecke.shp",
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
54 "brücken.shp", "bruecken.shp"]:
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
55 if path.lower().endswith(fname):
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
56 return BUILDING_KINDS["brücken"]
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
57 for fname in ["wehr.shp", "wehre.shp"]:
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
58 if path.lower().endswith(fname):
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
59 return BUILDING_KINDS["wehre"]
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
60 for fname in ["pegel.shp"]:
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
61 if path.lower().endswith(fname):
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
62 return BUILDING_KINDS["pegel"]
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
63
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
64 # Now it gets ugly when we search all attributes
5401
ff11b178f152 Fix searchValue and mark a field where a string is found as handled
Andre Heinecke <aheinecke@intevation.de>
parents: 5387
diff changeset
65 ret = self.searchValue(feat, "^br[ueü]{0,2}cke[n]{0,1}$")
ff11b178f152 Fix searchValue and mark a field where a string is found as handled
Andre Heinecke <aheinecke@intevation.de>
parents: 5387
diff changeset
66 if ret:
ff11b178f152 Fix searchValue and mark a field where a string is found as handled
Andre Heinecke <aheinecke@intevation.de>
parents: 5387
diff changeset
67 self.handled(ret)
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
68 return BUILDING_KINDS["brücken"]
5401
ff11b178f152 Fix searchValue and mark a field where a string is found as handled
Andre Heinecke <aheinecke@intevation.de>
parents: 5387
diff changeset
69 ret = self.searchValue(feat, "^wehr[e]{0,1}$")
ff11b178f152 Fix searchValue and mark a field where a string is found as handled
Andre Heinecke <aheinecke@intevation.de>
parents: 5387
diff changeset
70 if ret:
ff11b178f152 Fix searchValue and mark a field where a string is found as handled
Andre Heinecke <aheinecke@intevation.de>
parents: 5387
diff changeset
71 self.handled(ret)
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
72 return BUILDING_KINDS["wehre"]
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
73
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
74 return BUILDING_KINDS["sonstige"]
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
75
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
76
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
77 def createNewFeature(self, featureDef, feat, **args):
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
78 newFeat = ogr.Feature(featureDef)
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
79 geometry = feat.GetGeometryRef()
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
80 geometry.SetCoordinateDimension(2)
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
81 newFeat.SetGeometry(geometry)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
82
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
83 self.copyFields(feat, newFeat, self.fieldmap)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
84
5387
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
85 newFeat.SetField("kind_id", self.getKind(feat, args['path']))
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
86 newFeat.SetField("name", args["name"])
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
87
974f0e3dcc28 Import buildings according to specification
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
88 newFeat.SetField("river_id", self.river_id)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
89
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
90 return newFeat
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
91

http://dive4elements.wald.intevation.org