annotate flys-backend/contrib/shpimporter/boundaries.py @ 5025:9ef49f3b0304 dami

Sync Posgres and Oracle spatial schema
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 19 Feb 2013 14:58:33 +0100
parents f1c01fecf194
children c5187ab9f571
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
f1c01fecf194 Fix imports of ogr, this removes a warning in gdal 1.9 but keeps
Andre Heinecke <aheinecke@intevation.de>
parents: 4952
diff changeset
3 except ImportErrror:
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
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
7
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 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
9 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
10 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
11 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
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
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 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
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 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
17 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
18
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 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
20 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
21
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
22 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
23 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
24
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
25 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
26 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
27
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
28 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
29 shp = ogr.Open(path)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
30 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
31 self.getKind(path) > 0:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
32 return True
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
33 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
34 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
35
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
36 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
37 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
38 return 1
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
39 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
40 return 2
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
41 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
42 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
43 else:
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
44 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
45
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
46 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
47 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
48
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
49 newFeat = ogr.Feature(featureDef)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
50 geometry = feat.GetGeometryRef()
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
51 geometry.SetCoordinateDimension(2)
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.SetGeometry(geometry)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
54 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
55 newFeat.SetField("kind", kind)
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, "SECTIE"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
57 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
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, "SOBEK"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
60 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
61
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
62 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
63 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
64 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
65 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
66
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
67 return newFeat
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
68
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
69 class HydrBoundaryPoly(HydrBoundary):
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 getTablename(self):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
72 return TABLE_NAME_POLY
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 getName(self):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
75 return "%s (Polygons)" % NAME
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 isGeometryValid(self, geomType):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
78 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
79
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
80 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
81 shp = ogr.Open(path)
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
82 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
83 self.getKind(path) > 0:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
84 return True
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
85 else:
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
86 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
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 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
89 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
90
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 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
92 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
93 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
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.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
96 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
97 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
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, "SECTIE"):
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
100 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
101
4952
5a0225ddae2b Importer: Add sectie and strover to boundaries and select more
Andre Heinecke <aheinecke@intevation.de>
parents: 3654
diff changeset
102 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
103 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
104
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
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 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
107 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
108 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
109 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
110
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 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
112

http://dive4elements.wald.intevation.org