Mercurial > dive4elements > river
diff 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 |
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/importer.py Wed Jan 23 11:51:14 2013 +0100 +++ b/flys-backend/contrib/shpimporter/importer.py Wed Jan 23 15:30:09 2013 +0100 @@ -1,19 +1,19 @@ -import ogr, osr +try: + from osgeo import ogr +except ImportErrror: + import ogr +import osr import shpimporter class Importer: - def __init__(self, config): + def __init__(self, config, dbconn): self.config = config - if config.ogr_connection: - self.dbconn = '%s' % config.ogr_connection - else: - self.dbconn = 'OCI:%s/%s@%s' % (config.user, config.password, config.host) + self.dbconn = dbconn self.river_id = config.river_id self.dest_srs = osr.SpatialReference() self.dest_srs.ImportFromEPSG(config.target_srs) - def getKind(self, path): raise NotImplementedError("Importer.getKind is abstract!") @@ -86,12 +86,7 @@ def shape2Database(self, srcLayer, name, path): - table = ogr.Open(self.dbconn) - if not table: - shpimporter.ERROR("Could not connect to database %s" % self.dbconn) - return -1 - - destLayer = table.GetLayerByName(self.getTablename()) + destLayer = self.dbconn.GetLayerByName(self.getTablename()) if srcLayer is None: shpimporter.ERROR("Shapefile is None!") @@ -127,11 +122,11 @@ path=path) if newFeat is not None: - newFeat.SetField("path", path) + newFeat.SetField("path", path) newFeat = self.transform(newFeat) res = destLayer.CreateFeature(newFeat) if res is None or res > 0: - shpimporter.ERROR("Unable to insert feature: %r" % res) + shpimporter.ERROR("Unable to insert feature. Error: %r" % res) else: success = success + 1 else: