view backend/contrib/shpimporter/ @ 6955:94cb1845c667

(issue1452) Coalesce to zero to avoid NaN arithmetic.
author Andre Heinecke <>
date Fri, 30 Aug 2013 15:45:14 +0200
parents 5aa05a7a34b7
line wrap: on
line source
    from osgeo import ogr, osr
except ImportError:
    import ogr, osr
import utils
import re
import logging

logger = logging.getLogger("importer")

class Importer:

    def __init__(self, river_id, dbconn, dry_run):
        self.river_id = river_id
        self.dbconn = dbconn
        self.dry_run = dry_run
        self.dest_srs = osr.SpatialReference()
        self.handled_fields = []
        self.tracking_import = False
        self.srcLayer = None

    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.getName is abstract!")

    def isGeometryValid(self, geomType):
        raise NotImplementedError("Importer.isGeometryValid is abstract!")

    def createNewFeature(self, featureDef, feat, **args):
        raise NotImplementedError("Importer.createNewFeature is abstract!")

    def IsFieldSet(self, feat, name):
        if not name:
            return False
        if feat.GetFieldIndex(name) == -1:
            return False # Avoids an Error in IsFieldSet
        return feat.IsFieldSet(feat.GetFieldIndex(name))

    def searchValue(self, feat, regex):
        Searches for a value that matches regex in all attribute
        fields of a feature.

        @returns the name of the field where a match was found or None
        for val in feat.items():
            if not isinstance(feat.items()[val], basestring):
            match = re.match(regex, feat.items()[val], re.IGNORECASE)
            if match:
                return val

    def searchField(self, regex):
        Searches for a field in the current src layer that matches
        the expression regex.
        Throws an exception if more than one field matches
        @param feat: The feature to search for attributes
        @param regex: The regex to look for

        @returns: The field name as a string

        if not hasattr(self.srcLayer, "fieldnames"):
            self.srcLayer.fieldnames = []
            for i in range(0, self.srcLayer.GetLayerDefn().GetFieldCount()):

        result = None
        for name in self.srcLayer.fieldnames:
            match = re.match(regex, name, re.IGNORECASE)
            if match:
                if result:
                    raise Exception("More than one field matches: %s" % regex)
                    result =
        return result

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

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

    def walkOverShapes(self, shape):
        (name, path) = shape

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

        if not self.isShapeRelevant(name, path):
  "Skip shapefile: '%s' of Type: %s" % (path,
"Processing shapefile '%s'" % path)
        srcLayer = shp.GetLayerByName(name)

        if srcLayer is None:
            logger.error("Layer '%s' was not found!" % name)

        return self.shape2Database(srcLayer, name, path)

    def transform(self, feat):
        geometry = feat.GetGeometryRef()
        src_srs  = geometry.GetSpatialReference()

        if src_srs is None:
            logger.error("No source SRS given! No transformation possible!")
            return feat

        transformer = osr.CoordinateTransformation(src_srs, self.dest_srs)
        if geometry.Transform(transformer):
            return None

        return feat

    def handled(self, field):
        Register a field or a map of as handled during the import.

        There is a warning printed after the import for each unhandled field!
        if not field in self.handled_fields:

    def copyFields(self, src, target, mapping):
        Checks the mapping dictonary for key value pairs to
        copy from the source to the destination feature.
        The keys can be reguar expressions that are matched
        agains the source fieldnames

        The Key is the attribute of the source feature to be copied
        into the target attribute named by the dict's value.
        self.tracking_import = True
        for key, value in mapping.items():
            realname = self.searchField(key)
            if realname == None:
            if not realname in self.handled_fields:
            # 0 OFTInteger, Simple 32bit integer
            # 1 OFTIntegerList, List of 32bit integers
            # 2 OFTReal, Double Precision floating point
            # 3 OFTRealList, List of doubles
            # 4 OFTString, String of ASCII chars
            # 5 OFTStringList, Array of strings
            # 6 OFTWideString, deprecated
            # 7 OFTWideStringList, deprecated
            # 8 OFTBinary, Raw Binary data
            # 9 OFTDate, Date
            # 10 OFTTime, Time
            # 11 OFTDateTime, Date and Time
            if src.IsFieldSet(src.GetFieldIndex(realname)):
                if src.GetFieldType(realname) == 2:
                    target.SetField(value, src.GetFieldAsDouble(realname))
                    target.SetField(value, utils.getUTF8(src.GetField(realname)))

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

        if srcLayer is None:
            logger.error("Shapefile is None!")
            return -1

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

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

        self.srcLayer = srcLayer

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

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

            if geom is None:
                logger.debug("Unkown Geometry reference for feature")

            geomType = geom.GetGeometryType()

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

                if newFeat is not None:
                    newFeat.SetField("path", utils.getUTF8Path(path))
                    newFeat = self.transform(newFeat)
                    if newFeat:
                        res = destLayer.CreateFeature(newFeat)
                        if res is None or res > 0:
                            logger.error("Unable to insert feature. Error: %r" % res)
                            success = success + 1
                        logger.error("Could not transform feature: %s " % feat.GetFID())
                        creationFailed += 1
                    creationFailed = creationFailed + 1
                unsupported[utils.getWkbString(geomType)] = \
                        unsupported.get(utils.getWkbString(geomType), 0) + 1"Inserted %i features" % success)"Failed to create %i features" % creationFailed)
        for key, value in unsupported.items():
  "Found %i unsupported features of type: %s" % (value, key))

        if self.tracking_import:
            unhandled = []
            for i in range(0, srcLayer.GetLayerDefn().GetFieldCount()):
                act_field = srcLayer.GetLayerDefn().GetFieldDefn(i).GetNameRef()
                if not act_field in self.handled_fields:

            if len(unhandled):
      "Did not import values from fields: %s " % \
                        " ".join(unhandled))

            if self.dry_run:
                return geomType
            logger.error("Exception while committing transaction.")

        return geomType