ingo@2861: import ogr, osr ingo@3655: import shpimporter ingo@2853: ingo@2853: class Importer: ingo@2853: ingo@3654: def __init__(self, config): ingo@3654: self.config = config ingo@3654: self.dbconn = 'OCI:%s/%s@%s' % (config.user, config.password, config.host) ingo@3654: self.river_id = config.river_id ingo@2861: self.dest_srs = osr.SpatialReference() ingo@3654: self.dest_srs.ImportFromEPSG(config.target_srs) ingo@2853: ingo@2853: ingo@2853: def getKind(self, path): ingo@2853: raise NotImplementedError("Importer.getKind is abstract!") ingo@2853: ingo@2853: ingo@2853: def getPath(self, base): ingo@2853: raise NotImplementedError("Importer.getPath is abstract!") ingo@2853: ingo@2853: ingo@2853: def getTablename(self): ingo@2853: raise NotImplementedError("Importer.getTablename is abstract!") ingo@2853: ingo@2853: ingo@3654: def getName(self): ingo@3654: raise NotImplementedError("Importer.getTablename is abstract!") ingo@3654: ingo@3654: ingo@2853: def IsFieldSet(self, feat, name): ingo@2853: try: ingo@2853: isset = feat.GetField(name) ingo@2853: return isset is not None ingo@2853: except: ingo@2853: return False ingo@2853: ingo@2853: ingo@2853: def IsDoubleFieldSet(self, feat, name): ingo@2853: try: ingo@2853: isset = feat.GetFieldAsDouble(name) ingo@2853: return isset is not None ingo@2853: except: ingo@2853: return False ingo@2853: ingo@2853: ingo@2853: def isShapeRelevant(self, name, path): ingo@2853: return True ingo@2853: ingo@2853: ingo@2853: def walkOverShapes(self, shape): ingo@2853: (name, path) = shape ingo@2853: if not self.isShapeRelevant(name, path): ingo@3655: shpimporter.INFO("Skip shapefile '%s'" % path) ingo@2853: return ingo@2853: ingo@2853: shp = ogr.Open(shape[1]) ingo@2853: if shp is None: ingo@3655: shpimporter.ERROR("Shapefile '%s' could not be opened!" % path) ingo@2853: return ingo@2853: ingo@3655: shpimporter.INFO("Processing shapefile '%s'" % path) ingo@2853: srcLayer = shp.GetLayerByName(name) ingo@2853: ingo@2853: if srcLayer is None: ingo@3655: shpimporter.ERROR("Layer '%s' was not found!" % name) ingo@2853: return ingo@2853: ingo@2853: return self.shape2Database(srcLayer, name, path) ingo@2853: ingo@2853: ingo@2861: def transform(self, feat): ingo@2861: geometry = feat.GetGeometryRef() ingo@2861: src_srs = geometry.GetSpatialReference() ingo@2861: ingo@2861: if src_srs is None: ingo@3679: shpimporter.ERROR("No source SRS given! No transformation possible!") ingo@2861: return feat ingo@2861: ingo@2861: transformer = osr.CoordinateTransformation(src_srs, self.dest_srs) ingo@2861: geometry.Transform(transformer) ingo@2861: ingo@2861: return feat ingo@2861: ingo@2861: ingo@2853: def shape2Database(self, srcLayer, name, path): ingo@2853: table = ogr.Open(self.dbconn) ingo@2853: destLayer = table.GetLayerByName(self.getTablename()) ingo@2853: ingo@2853: if srcLayer is None: ingo@3655: shpimporter.ERROR("Shapefile is None!") ingo@2853: return -1 ingo@2853: ingo@2853: if destLayer is None: ingo@3655: shpimporter.ERROR("No destination layer given!") ingo@2853: return -1 ingo@2853: ingo@2853: count = srcLayer.GetFeatureCount() ingo@3655: shpimporter.DEBUG("Try to add %i features to database." % count) ingo@2853: ingo@2853: srcLayer.ResetReading() ingo@2853: ingo@2853: geomType = -1 ingo@2853: success = 0 ingo@2853: unsupported = 0 ingo@2861: creationFailed = 0 ingo@2853: featureDef = destLayer.GetLayerDefn() ingo@2853: ingo@2853: for feat in srcLayer: ingo@2853: geom = feat.GetGeometryRef() ingo@2861: ingo@2861: if geom is None: ingo@2861: continue ingo@2861: ingo@2853: geomType = geom.GetGeometryType() ingo@2853: ingo@2853: if self.isGeometryValid(geomType): ingo@2853: newFeat = self.createNewFeature(featureDef, ingo@2853: feat, ingo@2853: name=name, ingo@2853: path=path) ingo@2853: ingo@2853: if newFeat is not None: ingo@3677: newFeat.SetField("path", path) ingo@2861: newFeat = self.transform(newFeat) ingo@2853: res = destLayer.CreateFeature(newFeat) ingo@2853: if res is None or res > 0: ingo@3950: shpimporter.ERROR("Unable to insert feature: %r" % res) ingo@2853: else: ingo@2853: success = success + 1 ingo@2861: else: ingo@2861: creationFailed = creationFailed + 1 ingo@2853: else: ingo@2853: unsupported = unsupported + 1 ingo@2853: ingo@3655: shpimporter.INFO("Inserted %i features" % success) ingo@3655: shpimporter.INFO("Failed to create %i features" % creationFailed) ingo@3655: shpimporter.INFO("Found %i unsupported features" % unsupported) ingo@2853: ingo@2853: try: ingo@3655: if self.config.dry_run > 0: ingo@3655: return geomType ingo@2853: destLayer.CommitTransaction() ingo@2853: except e: ingo@3655: shpimporter.ERROR("Exception while committing transaction.") ingo@2853: ingo@2853: return geomType ingo@2853: