annotate flys-backend/contrib/shpimporter/boundaries.py @ 4995:998b29c8d2fd dami

Improve debug output for unsupported features and skipped shapefiles
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 15 Feb 2013 11:51:58 +0100
parents 5a0225ddae2b
children f1c01fecf194
rev   line source
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
1 import ogr
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
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
3 from importer import 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
4
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 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
6 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
7 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
8 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
9
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
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
11 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
12
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 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
14 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
15
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 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
17 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
18
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
19 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
20 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
21
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
22 def isGeometryValid(self, geomType):
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
23 return geomType == ogr.wkbLineString
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
24
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
25 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
26 shp = ogr.Open(path)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
27 if self.isGeometryValid(shp.GetLayerByName(name).GetGeomType()) and \
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
28 self.getKind(path) > 0:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
29 return True
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
30 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
31 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
32
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
33 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
34 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
35 return 1
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
36 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
37 return 2
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
38 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
39 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
40 else:
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
41 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
42
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
43 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
44 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
45
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
46 newFeat = ogr.Feature(featureDef)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
47 geometry = feat.GetGeometryRef()
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
48 geometry.SetCoordinateDimension(2)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
49
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
50 newFeat.SetGeometry(geometry)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
51 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
52 newFeat.SetField("kind", kind)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
53 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
54 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
55
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
56 if self.IsFieldSet(feat, "SOBEK"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
57 newFeat.SetField("sobek", feat.GetField("SOBEK"))
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
58
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
59 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
60 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
61 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
62 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
63
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
64 return newFeat
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 class HydrBoundaryPoly(HydrBoundary):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
67
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
68 def getTablename(self):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
69 return TABLE_NAME_POLY
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
70
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
71 def getName(self):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
72 return "%s (Polygons)" % NAME
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
73
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
74 def isGeometryValid(self, geomType):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
75 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
76
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
77 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
78 shp = ogr.Open(path)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
79 if self.isGeometryValid(shp.GetLayerByName(name).GetGeomType()) and \
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
80 self.getKind(path) > 0:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
81 return True
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
82 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
83 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
84
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
85 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
86 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
87
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
88 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
89 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
90 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
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 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
93 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
94 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
95
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
96 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
97 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
98
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
99 if self.IsFieldSet(feat, "SOBEK"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
100 newFeat.SetField("sobek", feat.GetField("SOBEK"))
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
101
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
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
103 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
104 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
105 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
106 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
107
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 return newFeat
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

http://dive4elements.wald.intevation.org