comparison flys-backend/contrib/shpimporter/importer.py @ 4872:a563e9f58f93

Improve error handling and unify dbconn for all importers
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 23 Jan 2013 15:30:09 +0100
parents 890eaa0a5162
children 884afa149192
comparison
equal deleted inserted replaced
4871:890eaa0a5162 4872:a563e9f58f93
1 import ogr, osr 1 try:
2 from osgeo import ogr
3 except ImportErrror:
4 import ogr
5 import osr
2 import shpimporter 6 import shpimporter
3 7
4 class Importer: 8 class Importer:
5 9
6 def __init__(self, config): 10 def __init__(self, config, dbconn):
7 self.config = config 11 self.config = config
8 if config.ogr_connection: 12 self.dbconn = dbconn
9 self.dbconn = '%s' % config.ogr_connection
10 else:
11 self.dbconn = 'OCI:%s/%s@%s' % (config.user, config.password, config.host)
12 self.river_id = config.river_id 13 self.river_id = config.river_id
13 self.dest_srs = osr.SpatialReference() 14 self.dest_srs = osr.SpatialReference()
14 self.dest_srs.ImportFromEPSG(config.target_srs) 15 self.dest_srs.ImportFromEPSG(config.target_srs)
15
16 16
17 def getKind(self, path): 17 def getKind(self, path):
18 raise NotImplementedError("Importer.getKind is abstract!") 18 raise NotImplementedError("Importer.getKind is abstract!")
19 19
20 20
84 84
85 return feat 85 return feat
86 86
87 87
88 def shape2Database(self, srcLayer, name, path): 88 def shape2Database(self, srcLayer, name, path):
89 table = ogr.Open(self.dbconn) 89 destLayer = self.dbconn.GetLayerByName(self.getTablename())
90 if not table:
91 shpimporter.ERROR("Could not connect to database %s" % self.dbconn)
92 return -1
93
94 destLayer = table.GetLayerByName(self.getTablename())
95 90
96 if srcLayer is None: 91 if srcLayer is None:
97 shpimporter.ERROR("Shapefile is None!") 92 shpimporter.ERROR("Shapefile is None!")
98 return -1 93 return -1
99 94
125 feat, 120 feat,
126 name=name, 121 name=name,
127 path=path) 122 path=path)
128 123
129 if newFeat is not None: 124 if newFeat is not None:
130 newFeat.SetField("path", path) 125 newFeat.SetField("path", path)
131 newFeat = self.transform(newFeat) 126 newFeat = self.transform(newFeat)
132 res = destLayer.CreateFeature(newFeat) 127 res = destLayer.CreateFeature(newFeat)
133 if res is None or res > 0: 128 if res is None or res > 0:
134 shpimporter.ERROR("Unable to insert feature: %r" % res) 129 shpimporter.ERROR("Unable to insert feature. Error: %r" % res)
135 else: 130 else:
136 success = success + 1 131 success = success + 1
137 else: 132 else:
138 creationFailed = creationFailed + 1 133 creationFailed = creationFailed + 1
139 else: 134 else:

http://dive4elements.wald.intevation.org