view flys-backend/contrib/shpimporter/utils.py @ 4882:e9880b224c2f

(Importer) Add comment about OFT types
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 25 Jan 2013 15:38:34 +0100
parents b1d7e600b43b
children b457532dae63
line wrap: on
line source
import os
import sys
from shpimporter import DEBUG, INFO, ERROR

SHP='.shp'

def findShapefiles(path):
    shapes = []

    for root, dirs, files in os.walk(path):
        if len(files) == 0:
            continue

        DEBUG("Processing directory '%s' with %i files " % (root, len(files)))

        for f in files:
            idx = f.find(SHP)
            if (idx+len(SHP)) == len(f):
                shapes.append((f.replace(SHP, ''), root + "/" + f))

    return shapes

def getUTF8Path(path):
    """
    Tries to convert path to utf-8 by first checking the filesystemencoding
    and trying the default windows encoding afterwards.
    Returns a valid UTF-8 encoded unicode object or throws a UnicodeDecodeError
    """
    try:
        return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8")
    except UnicodeDecodeError:
        # Probably Windows iso-8859-x names so lets try again
        return unicode.encode(unicode(path, "iso-8859-15"), "UTF-8")


# 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


def copyFields(src, target, mapping):
    """
    Checks the mapping dictonary for key value pairs to
    copy from the source to the destination feature.

    The Key is the attribute of the source feature to be copied
    into the target attribute named by the dict's value.
    """
    for key, value in mapping.items():
        if src.GetFieldIndex(key) == -1:
            continue
        if src.IsFieldSet(src.GetFieldIndex(key)):
            if src.GetFieldType(key) == 2:
                target.SetField(value, src.GetFieldAsDouble(key))
            else:
                target.SetField(value, src.GetField(key))

http://dive4elements.wald.intevation.org