view flys-backend/contrib/shpimporter/importer.py @ 4878:82d00b0c7302

(importer) Avoid uneccessary error and improve debug output
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 25 Jan 2013 10:46:00 +0100
parents 852d4c508f33
children b457532dae63
line wrap: on
line source
try:
    from osgeo import ogr
except ImportErrror:
    import ogr
import osr
import shpimporter
import utils

class Importer:

    def __init__(self, config, dbconn):
        self.config = config
        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!")

    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):
        if feat.GetFieldIndex(name) == -1:
            return False # Avoids an Error in IsFieldSet
        return feat.IsFieldSet(feat.GetFieldIndex(name))

    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):
            shpimporter.INFO("Skip shapefile '%s'" % path)
            return

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

        shpimporter.INFO("Processing shapefile '%s'" % path)
        srcLayer = shp.GetLayerByName(name)

        if srcLayer is None:
            shpimporter.ERROR("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:
            shpimporter.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):
        destLayer = self.dbconn.GetLayerByName(self.getTablename())

        if srcLayer is None:
            shpimporter.ERROR("Shapefile is None!")
            return -1

        if destLayer is None:
            shpimporter.ERROR("No destination layer given!")
            return -1

        count = srcLayer.GetFeatureCount()
        shpimporter.DEBUG("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:
                shpimporter.DEBUG("Unkown Geometry reference for feature")
                continue

            geomType = geom.GetGeometryType()

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

                if newFeat is not None:
                    newFeat.SetField("path", utils.getUTF8Path(path))
                    newFeat = self.transform(newFeat)
                    res = destLayer.CreateFeature(newFeat)
                    if res is None or res > 0:
                        shpimporter.ERROR("Unable to insert feature. Error: %r" % res)
                    else:
                        success = success + 1
                else:
                    creationFailed = creationFailed + 1
            else:
                unsupported = unsupported + 1

        shpimporter.INFO("Inserted %i features" % success)
        shpimporter.INFO("Failed to create %i features" % creationFailed)
        shpimporter.INFO("Found %i unsupported features" % unsupported)

        try:
            if self.config.dry_run > 0:
                return geomType
            destLayer.CommitTransaction()
        except e:
            shpimporter.ERROR("Exception while committing transaction.")

        return geomType

http://dive4elements.wald.intevation.org