annotate backend/contrib/shpimporter/boundaries.py @ 8764:d5917ff74d8a

(issue1838) Get min and max directly from database. The former implementation led to a connection leak.
author Tom Gottfried <tom@intevation.de>
date Mon, 17 Aug 2015 10:18:05 +0200
parents baae6cbc1086
children
rev   line source
5004
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 4952
diff changeset
1 try:
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 4952
diff changeset
2 from osgeo import ogr
5077
c5187ab9f571 Fix Syntax Errror
Andre Heinecke <aheinecke@intevation.de>
parents: 5004
diff changeset
3 except ImportError:
5004
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 4952
diff changeset
4 import ogr
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
5
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
6 from importer import Importer
5182
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
7 import utils
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
8
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
9 TABLE_NAME="hydr_boundaries"
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 TABLE_NAME_POLY="hydr_boundaries_poly"
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
11 PATH="Hydrologie/Hydr.Grenzen"
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
12 NAME="Hydr. Boundaries"
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
13
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
14
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
15 class HydrBoundary(Importer):
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
17 def getPath(self, base):
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
18 return "%s/%s" % (base, PATH)
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
19
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
20 def getTablename(self):
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
21 return TABLE_NAME
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
22
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
23 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
24 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
25
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
26 def isGeometryValid(self, geomType):
5182
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
27 return geomType in [ogr.wkbLineString,
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
28 ogr.wkbLineString25D,
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
29 ogr.wkbMultiLineString25D,
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
30 ogr.wkbMultiLineString]
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
31
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
32 def isShapeRelevant(self, name, path):
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
33 shp = ogr.Open(path)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
34 if self.isGeometryValid(shp.GetLayerByName(name).GetGeomType()) and \
5511
4e166c514940 Do not import floodplains as hydr boundaries again
Andre Heinecke <aheinecke@intevation.de>
parents: 5302
diff changeset
35 self.getKind(path) > 0 and not "talaue" in path.lower():
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
36 return True
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
37 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
38 return False
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
39
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
40 def getKind(self, path):
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
41 if "linien/bfg" in path.lower():
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
42 return 1
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
43 elif "linien/land" in path.lower():
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
44 return 2
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
45 elif "/sonstige/" in path.lower():
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
46 return 3
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47 else:
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
48 return 0
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
49
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
50 def createNewFeature(self, featureDef, feat, **args):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
51 kind = self.getKind(args['path'])
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
52
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
53 newFeat = ogr.Feature(featureDef)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
54 geometry = feat.GetGeometryRef()
5182
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
55 geometry.SetCoordinateDimension(3)
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
56
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
57 newFeat.SetGeometry(geometry)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
58 newFeat.SetField("name", args['name'])
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
59 newFeat.SetField("kind", kind)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
60 if self.IsFieldSet(feat, "SECTIE"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
61 newFeat.SetField("sectie", feat.GetField("SECTIE"))
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
62
5750
148365ff484d Geoimpoter: Use attribute STROVOER, not SOBEK in hydr. boundaries
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5749
diff changeset
63 if self.IsFieldSet(feat, "STROVOER"):
148365ff484d Geoimpoter: Use attribute STROVOER, not SOBEK in hydr. boundaries
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5749
diff changeset
64 newFeat.SetField("sobek", feat.GetField("STROVOER"))
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
65
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
66 if self.IsFieldSet(feat, "river_id"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
67 newFeat.SetField("river_id", feat.GetField("river_id"))
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
68 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
69 newFeat.SetField("river_id", self.river_id)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
70
5182
8aac391871f9 SCHEME CHANGE: Hydr.Boundaries are now three dimensional multilinestrings
Andre Heinecke <aheinecke@intevation.de>
parents: 5077
diff changeset
71 return utils.convertToMultiLine(newFeat)
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
72
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
73 class HydrBoundaryPoly(HydrBoundary):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
74
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
75 def getTablename(self):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
76 return TABLE_NAME_POLY
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
77
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
78 def getName(self):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
79 return "%s (Polygons)" % NAME
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
80
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
81 def isGeometryValid(self, geomType):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
82 return geomType == ogr.wkbPolygon or geomType == ogr.wkbMultiPolygon
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
83
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
84 def isShapeRelevant(self, name, path):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
85 shp = ogr.Open(path)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
86 if self.isGeometryValid(shp.GetLayerByName(name).GetGeomType()) and \
6738
baae6cbc1086 Geo-Importer: Don not import misc. floodplains as hydraulic boundary.
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5851
diff changeset
87 self.getKind(path) > 0 and not "talaue" in path.lower():
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
88 return True
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
89 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
90 return False
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
91
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
92 def createNewFeature(self, featureDef, feat, **args):
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
93 kind = self.getKind(args['path'])
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
94
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
95 newFeat = ogr.Feature(featureDef)
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
96 geometry = feat.GetGeometryRef()
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
97 geometry.SetCoordinateDimension(2)
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
98
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
99 newFeat.SetGeometry(geometry)
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
100 newFeat.SetField("name", args['name'])
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
101 newFeat.SetField("kind", kind)
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
102
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
103 if self.IsFieldSet(feat, "SECTIE"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
104 newFeat.SetField("sectie", feat.GetField("SECTIE"))
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
105
5851
d5e95f926d13 use correct attribute name STROVOER also for polygon boundaries
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5838
diff changeset
106 if self.IsFieldSet(feat, "STROVOER"):
d5e95f926d13 use correct attribute name STROVOER also for polygon boundaries
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5838
diff changeset
107 newFeat.SetField("sobek", feat.GetField("STROVOER"))
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
108
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
109 if self.IsFieldSet(feat, "river_id"):
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
110 newFeat.SetField("river_id", feat.GetField("river_id"))
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
111 else:
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
112 newFeat.SetField("river_id", self.river_id)
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
113
5302
71a24278322c fixed import of fixpoints and hydraulic boundaries (made by A. Heinecke)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5182
diff changeset
114 return utils.convertToMultiPolygon(newFeat)
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
115

http://dive4elements.wald.intevation.org