annotate flys-backend/contrib/shpimporter/importer.py @ 4976:a6ee62a070b0 dami

I'm learning how to use cursors \o/
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 13 Feb 2013 17:49:30 +0100
parents a92241d40228
children 998b29c8d2fd
rev   line source
4872
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
1 try:
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
2 from osgeo import ogr
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
3 except ImportErrror:
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
4 import ogr
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
5 import osr
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
6 import shpimporter
4877
852d4c508f33 (importer) Try to convert paths to UTF-8 for DB insertion
Andre Heinecke <aheinecke@intevation.de>
parents: 4876
diff changeset
7 import utils
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
8 import re
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
9
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10 class Importer:
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
11
4970
174fbaa3d344 Add handling of River Names and remove target_src parameter
Andre Heinecke <aheinecke@intevation.de>
parents: 4951
diff changeset
12 def __init__(self, river_id, dbconn, dry_run):
174fbaa3d344 Add handling of River Names and remove target_src parameter
Andre Heinecke <aheinecke@intevation.de>
parents: 4951
diff changeset
13 self.river_id = river_id
4872
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
14 self.dbconn = dbconn
4970
174fbaa3d344 Add handling of River Names and remove target_src parameter
Andre Heinecke <aheinecke@intevation.de>
parents: 4951
diff changeset
15 self.dry_run = dry_run
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
16 self.dest_srs = osr.SpatialReference()
4970
174fbaa3d344 Add handling of River Names and remove target_src parameter
Andre Heinecke <aheinecke@intevation.de>
parents: 4951
diff changeset
17 self.dest_srs.ImportFromEPSG(31467)
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
18 self.handled_fields = []
4885
89a8764cabcc Importer: Only show imported fields if copy_fields is used
Andre Heinecke <aheinecke@intevation.de>
parents: 4884
diff changeset
19 self.tracking_import = False
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
20 self.srcLayer = None
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
21
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
22 def getKind(self, path):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
23 raise NotImplementedError("Importer.getKind is abstract!")
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
24
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
25 def getPath(self, base):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
26 raise NotImplementedError("Importer.getPath is abstract!")
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
27
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
28 def getTablename(self):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
29 raise NotImplementedError("Importer.getTablename is abstract!")
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
30
3654
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2862
diff changeset
31 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: 2862
diff changeset
32 raise NotImplementedError("Importer.getTablename is abstract!")
59ca5dab2782 Shape importer: use python's OptionParse to read user specific configuration from command line.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2862
diff changeset
33
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
34 def IsFieldSet(self, feat, name):
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
35 if not name:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
36 return False
4878
82d00b0c7302 (importer) Avoid uneccessary error and improve debug output
Andre Heinecke <aheinecke@intevation.de>
parents: 4877
diff changeset
37 if feat.GetFieldIndex(name) == -1:
82d00b0c7302 (importer) Avoid uneccessary error and improve debug output
Andre Heinecke <aheinecke@intevation.de>
parents: 4877
diff changeset
38 return False # Avoids an Error in IsFieldSet
82d00b0c7302 (importer) Avoid uneccessary error and improve debug output
Andre Heinecke <aheinecke@intevation.de>
parents: 4877
diff changeset
39 return feat.IsFieldSet(feat.GetFieldIndex(name))
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
40
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
41 def searchField(self, regex):
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
42 """
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
43 Searches for a field in the current src layer that matches
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
44 the expression regex.
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
45 Throws an exception if more then one field matches
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
46 @param feat: The feature to search for attributes
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
47 @param regex: The regex to look for
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
48
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
49 @returns: The field name as a string
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
50 """
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
51
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
52 if not hasattr(self.srcLayer, "fieldnames"):
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
53 self.srcLayer.fieldnames = []
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
54 for i in range(0, self.srcLayer.GetLayerDefn().GetFieldCount()):
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
55 self.srcLayer.fieldnames.append(
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
56 self.srcLayer.GetLayerDefn().GetFieldDefn(i).GetNameRef())
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
57
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
58 result = None
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
59 for name in self.srcLayer.fieldnames:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
60 match = re.match(regex, name, re.IGNORECASE)
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
61 if match:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
62 if result:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
63 raise Exception("More then one field matches: %s" % regex)
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
64 else:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
65 result = match.group(0)
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
66 return result
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
67
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
68 def IsDoubleFieldSet(self, feat, name):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
69 try:
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
70 isset = feat.GetFieldAsDouble(name)
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
71 return isset is not None
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
72 except:
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
73 return False
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
74
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
75 def isShapeRelevant(self, name, path):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
76 return True
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
77
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
78 def walkOverShapes(self, shape):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
79 (name, path) = shape
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
80 if not self.isShapeRelevant(name, path):
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
81 shpimporter.INFO("Skip shapefile '%s'" % path)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
82 return
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
83
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
84 shp = ogr.Open(shape[1])
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
85 if shp is None:
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
86 shpimporter.ERROR("Shapefile '%s' could not be opened!" % path)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
87 return
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
88
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
89 shpimporter.INFO("Processing shapefile '%s'" % path)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
90 srcLayer = shp.GetLayerByName(name)
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
91
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
92 if srcLayer is None:
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
93 shpimporter.ERROR("Layer '%s' was not found!" % name)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
94 return
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
95
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
96 return self.shape2Database(srcLayer, name, path)
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
97
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
98 def transform(self, feat):
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
99 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: 2853
diff changeset
100 src_srs = geometry.GetSpatialReference()
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
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: 2853
diff changeset
102 if src_srs is None:
3679
2cd2657b3d60 Fixed broken method call in geo importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3677
diff changeset
103 shpimporter.ERROR("No source SRS given! No transformation possible!")
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
104 return feat
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
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: 2853
diff changeset
106 transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
4974
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
107 if geometry.Transform(transformer):
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
108 return None
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
109
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
110 return feat
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
111
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
112 def handled(self, field):
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
113 """
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
114 Register a field or a map of as handled during the import.
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
115
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
116 There is a warning printed after the import for each unhandled field!
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
117 """
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
118 if not field in self.handled_fields:
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
119 self.handled_fields.append(field)
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
120
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
121 def copyFields(self, src, target, mapping):
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
122 """
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
123 Checks the mapping dictonary for key value pairs to
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
124 copy from the source to the destination feature.
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
125 The keys can be reguar expressions that are matched
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
126 agains the source fieldnames
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
127
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
128 The Key is the attribute of the source feature to be copied
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
129 into the target attribute named by the dict's value.
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
130 """
4885
89a8764cabcc Importer: Only show imported fields if copy_fields is used
Andre Heinecke <aheinecke@intevation.de>
parents: 4884
diff changeset
131 self.tracking_import = True
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
132 for key, value in mapping.items():
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
133 realname = self.searchField(key)
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
134 if realname == None:
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
135 continue
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
136 if not realname in self.handled_fields:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
137 self.handled_fields.append(realname)
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
138 # 0 OFTInteger, Simple 32bit integer
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
139 # 1 OFTIntegerList, List of 32bit integers
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
140 # 2 OFTReal, Double Precision floating point
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
141 # 3 OFTRealList, List of doubles
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
142 # 4 OFTString, String of ASCII chars
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
143 # 5 OFTStringList, Array of strings
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
144 # 6 OFTWideString, deprecated
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
145 # 7 OFTWideStringList, deprecated
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
146 # 8 OFTBinary, Raw Binary data
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
147 # 9 OFTDate, Date
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
148 # 10 OFTTime, Time
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
149 # 11 OFTDateTime, Date and Time
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
150 if src.IsFieldSet(src.GetFieldIndex(realname)):
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
151 if src.GetFieldType(realname) == 2:
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
152 target.SetField(value, src.GetFieldAsDouble(realname))
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
153 else:
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
154 target.SetField(value, utils.getUTF8(src.GetField(realname)))
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
155
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
156 def shape2Database(self, srcLayer, name, path):
4872
a563e9f58f93 Improve error handling and unify dbconn for all importers
Andre Heinecke <aheinecke@intevation.de>
parents: 4871
diff changeset
157 destLayer = self.dbconn.GetLayerByName(self.getTablename())
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
158
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
159 if srcLayer is None:
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
160 shpimporter.ERROR("Shapefile is None!")
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
161 return -1
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
162
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
163 if destLayer is None:
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
164 shpimporter.ERROR("No destination layer given!")
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
165 return -1
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
166
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
167 count = srcLayer.GetFeatureCount()
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
168 shpimporter.DEBUG("Try to add %i features to database." % count)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
169
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
170 srcLayer.ResetReading()
4935
c0a58558b817 Importer: - Handle regular expressions for attribute names
Andre Heinecke <aheinecke@intevation.de>
parents: 4886
diff changeset
171 self.srcLayer = srcLayer
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
172
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
173 geomType = -1
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
174 success = 0
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
175 unsupported = 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: 2853
diff changeset
176 creationFailed = 0
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
177 featureDef = destLayer.GetLayerDefn()
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
178
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
179 for feat in srcLayer:
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
180 geom = feat.GetGeometryRef()
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
181
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
182 if geom is None:
4878
82d00b0c7302 (importer) Avoid uneccessary error and improve debug output
Andre Heinecke <aheinecke@intevation.de>
parents: 4877
diff changeset
183 shpimporter.DEBUG("Unkown Geometry reference for feature")
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
184 continue
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
185
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
186 geomType = geom.GetGeometryType()
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
187
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
188 if self.isGeometryValid(geomType):
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
189 newFeat = self.createNewFeature(featureDef,
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
190 feat,
4951
23a090b41543 Importer: Check lowercase pathname and handle utf-8 conversion
Andre Heinecke <aheinecke@intevation.de>
parents: 4935
diff changeset
191 name=utils.getUTF8(name),
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
192 path=path)
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
193
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
194 if newFeat is not None:
4877
852d4c508f33 (importer) Try to convert paths to UTF-8 for DB insertion
Andre Heinecke <aheinecke@intevation.de>
parents: 4876
diff changeset
195 newFeat.SetField("path", utils.getUTF8Path(path))
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
196 newFeat = self.transform(newFeat)
4974
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
197 if newFeat:
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
198 res = destLayer.CreateFeature(newFeat)
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
199 if res is None or res > 0:
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
200 shpimporter.ERROR("Unable to insert feature. Error: %r" % res)
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
201 else:
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
202 success = success + 1
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
203 else:
4974
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
204 shpimporter.ERROR("Could not transform feature: %s " % feat.GetFID())
a92241d40228 Handle transformation errors
Andre Heinecke <aheinecke@intevation.de>
parents: 4970
diff changeset
205 creationFailed += 1
2861
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
206 else:
b0132e1b9719 Added further shape importers and added the option to reproject shapes during the import process.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 2853
diff changeset
207 creationFailed = creationFailed + 1
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
208 else:
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
209 unsupported = unsupported + 1
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
210
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
211 shpimporter.INFO("Inserted %i features" % success)
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
212 shpimporter.INFO("Failed to create %i features" % creationFailed)
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
213 shpimporter.INFO("Found %i unsupported features" % unsupported)
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
214
4886
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
215 if self.tracking_import:
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
216 unhandled = []
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
217 for i in range(0, srcLayer.GetLayerDefn().GetFieldCount()):
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
218 act_field = srcLayer.GetLayerDefn().GetFieldDefn(i).GetNameRef()
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
219 if not act_field in self.handled_fields:
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
220 unhandled.append(act_field)
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
221
4886
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
222 if len(unhandled):
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
223 shpimporter.INFO("Did not import values from fields: %s " % \
8e99d2d9364d Importer: When not tracking dont print info about unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4885
diff changeset
224 " ".join(unhandled))
4884
b457532dae63 Importer: Move copy fields into importer class and track imported/unimported fields
Andre Heinecke <aheinecke@intevation.de>
parents: 4878
diff changeset
225
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
226 try:
4970
174fbaa3d344 Add handling of River Names and remove target_src parameter
Andre Heinecke <aheinecke@intevation.de>
parents: 4951
diff changeset
227 if self.dry_run:
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
228 return geomType
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
229 destLayer.CommitTransaction()
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
230 except e:
3655
8654e4480fc6 Shape importer: added command line option dry_run to supress database transactions.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 3654
diff changeset
231 shpimporter.ERROR("Exception while committing transaction.")
2853
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
232
bd9e76e0b55d Improved the python shapefile importer.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
233 return geomType

http://dive4elements.wald.intevation.org