view flys-backend/contrib/shpimporter/importer.py @ 3654:59ca5dab2782

Shape importer: use python's OptionParse to read user specific configuration from command line. flys-backend/trunk@5231 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 22 Aug 2012 11:55:55 +0000
parents 998f1a7dcfde
children 8654e4480fc6
line wrap: on
line source
import ogr, osr

class Importer:

    def __init__(self, config):
        self.config = config
        self.dbconn   = 'OCI:%s/%s@%s' % (config.user, config.password, config.host)
        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!")


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


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


    def getName(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):
        print "---"
        (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 transform(self, feat):
        geometry = feat.GetGeometryRef()
        src_srs  = geometry.GetSpatialReference()

        if src_srs is None:
            print "Error: No source SRS given! No transformation possible!"
            return feat

        transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
        geometry.Transform(transformer)

        return feat


    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
        creationFailed = 0
        featureDef  = destLayer.GetLayerDefn()

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

            if geom is None:
                continue

            geomType = geom.GetGeometryType()

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

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

        print "Inserted %i features" % success
        print "Failed to create %i features" % creationFailed
        print "Found %i unsupported features" % unsupported

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

        return geomType

http://dive4elements.wald.intevation.org