ingo@2853: import ogr ingo@2853: ingo@2853: class Importer: ingo@2853: ingo@2853: def __init__(self, dbconn, river_id): ingo@2853: self.dbconn = dbconn ingo@2853: self.river_id = river_id 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@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@2853: print "Skip shapefile '%s'" % name ingo@2853: return ingo@2853: ingo@2853: shp = ogr.Open(shape[1]) ingo@2853: if shp is None: ingo@2853: print "Shapefile '%s' could not be opened!" % path ingo@2853: return ingo@2853: ingo@2853: print "Opened shapefile '%s'" % path ingo@2853: srcLayer = shp.GetLayerByName(name) ingo@2853: ingo@2853: if srcLayer is None: ingo@2853: print "Layer '%s' was not found!" % name ingo@2853: return ingo@2853: ingo@2853: return self.shape2Database(srcLayer, name, path) ingo@2853: ingo@2853: 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@2853: print "Shapefile is None!" ingo@2853: return -1 ingo@2853: ingo@2853: if destLayer is None: ingo@2853: print "No destination layer given!" ingo@2853: return -1 ingo@2853: ingo@2853: count = srcLayer.GetFeatureCount() ingo@2853: print "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@2853: featureDef = destLayer.GetLayerDefn() ingo@2853: ingo@2853: for feat in srcLayer: ingo@2853: geom = feat.GetGeometryRef() 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@2853: res = destLayer.CreateFeature(newFeat) ingo@2853: if res is None or res > 0: ingo@2853: print "Error while inserting feature: %r" % res ingo@2853: else: ingo@2853: success = success + 1 ingo@2853: else: ingo@2853: unsupported = unsupported + 1 ingo@2853: ingo@2853: print "Inserted %i features" % success ingo@2853: print "Found %i unsupported features" % unsupported ingo@2853: ingo@2853: try: ingo@2853: destLayer.CommitTransaction() ingo@2853: except e: ingo@2853: print "Exception while committing transaction." ingo@2853: ingo@2853: return geomType ingo@2853: