Mercurial > dive4elements > river
changeset 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 | 2b371e42a9af |
files | flys-backend/contrib/shpimporter/importer.py flys-backend/contrib/shpimporter/shpimporter.py |
diffstat | 2 files changed, 41 insertions(+), 34 deletions(-) [+] |
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:
--- a/flys-backend/contrib/shpimporter/shpimporter.py Wed Jan 23 11:51:14 2013 +0100 +++ b/flys-backend/contrib/shpimporter/shpimporter.py Wed Jan 23 15:30:09 2013 +0100 @@ -1,4 +1,7 @@ -import ogr +try: + from osgeo import ogr +except ImportErrror: + import ogr import utils, optparse @@ -35,21 +38,21 @@ print "ERROR: %s" % msg -def getImporters(config): +def getImporters(config, dbconn): return [ - Axis(config), - KM(config), - CrosssectionTrack(config), - Line(config), - Fixpoint(config), - Building(config), - Floodplain(config), - HydrBoundary(config), - HydrBoundaryPoly(config), - HWS(config), - GaugeLocation(config), - Catchment(config), - UESG(config) + Axis(config, dbconn), + KM(config, dbconn), + CrosssectionTrack(config, dbconn), + Line(config, dbconn), + Fixpoint(config, dbconn), + Building(config, dbconn), + Floodplain(config, dbconn), + HydrBoundary(config, dbconn), + HydrBoundaryPoly(config, dbconn), + HWS(config, dbconn), + GaugeLocation(config, dbconn), + Catchment(config, dbconn), + UESG(config, dbconn) ] @@ -129,12 +132,12 @@ return False -def parse(): +def main(): config=None try: config = getConfig() except: - return + return -1 if config == None: ERROR("Unable to read config from command line!") @@ -143,7 +146,16 @@ if config.dry_run > 0: INFO("You enable 'dry_run'. No database transaction will take place!") - importers = getImporters(config) + if config.ogr_connection: + dbconn = ogr.Open(config.ogr_connection) + else: + dbconn = ogr.Open('OCI:%s/%s@%s' % (config.user, config.password, config.host)) + + if not dbconn: + shpimporter.ERROR("Could not connect to database %s" % self.dbconn) + return -1 + + importers = getImporters(config, dbconn) types = {} for importer in importers: @@ -170,4 +182,4 @@ if __name__ == '__main__': - parse() + main()