view flys-backend/contrib/shpimporter/importer.py @ 2859:ed13816047b3

Removed superfluous imports. flys-backend/trunk@4323 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 27 Apr 2012 15:52:08 +0000
parents bd9e76e0b55d
children b0132e1b9719
line wrap: on
line source
import ogr

class Importer:

    def __init__(self, dbconn, river_id):
        self.dbconn   = dbconn
        self.river_id = river_id


    def getKind(self, path):
        raise NotImplementedError("Importer.getKind is abstract!")


    def getPath(self, base):
        raise NotImplementedError("Importer.getPath is abstract!")


    def getTablename(self):
        raise NotImplementedError("Importer.getTablename is abstract!")


    def IsFieldSet(self, feat, name):
        try:
            isset = feat.GetField(name)
            return isset is not None
        except:
            return False


    def IsDoubleFieldSet(self, feat, name):
        try:
            isset = feat.GetFieldAsDouble(name)
            return isset is not None
        except:
            return False


    def isShapeRelevant(self, name, path):
        return True


    def walkOverShapes(self, shape):
        (name, path) = shape
        if not self.isShapeRelevant(name, path):
            print "Skip shapefile '%s'" % name
            return

        shp = ogr.Open(shape[1])
        if shp is None:
            print "Shapefile '%s' could not be opened!" % path
            return

        print "Opened shapefile '%s'" % path
        srcLayer = shp.GetLayerByName(name)

        if srcLayer is None:
            print "Layer '%s' was not found!" % name
            return

        return self.shape2Database(srcLayer, name, path)


    def shape2Database(self, srcLayer, name, path):
        table     = ogr.Open(self.dbconn)
        destLayer = table.GetLayerByName(self.getTablename())

        if srcLayer is None:
            print "Shapefile is None!"
            return -1

        if destLayer is None:
            print "No destination layer given!"
            return -1

        count = srcLayer.GetFeatureCount()
        print "Try to add %i features to database." % count

        srcLayer.ResetReading()

        geomType    = -1
        success     = 0
        unsupported = 0
        featureDef  = destLayer.GetLayerDefn()

        for feat in srcLayer:
            geom     = feat.GetGeometryRef()
            geomType = geom.GetGeometryType()

            if self.isGeometryValid(geomType):
                newFeat = self.createNewFeature(featureDef,
                                                feat,
                                                name=name,
                                                path=path)

                if newFeat is not None:
                    res = destLayer.CreateFeature(newFeat)
                    if res is None or res > 0:
                        print "Error while inserting feature: %r" % res
                    else:
                        success = success + 1
            else:
                unsupported = unsupported + 1

        print "Inserted %i features" % success
        print "Found %i unsupported features" % unsupported

        try:
            destLayer.CommitTransaction()
        except e:
            print "Exception while committing transaction."

        return geomType

http://dive4elements.wald.intevation.org