view flys-backend/contrib/shpimporter/utils.py @ 4971:fc7ebf104779 dami

Update documentation and example run script
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 13 Feb 2013 12:07:10 +0100
parents 174fbaa3d344
children a6ee62a070b0
line wrap: on
line source
import os
import sys
from shpimporter import DEBUG, INFO, ERROR
try:
    from osgeo import ogr
except ImportErrror:
    import ogr

SHP='.shp'
SQL_SELECT_ID="SELECT id FROM %s WHERE %s = '%s'"

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 getRiverId(dbconn, name):
    """
    Returns the id of the river "name"
    Dbconn must be a python database connection api compliant object
    """
    cur = dbconn.cursor()
    select_stmt = SQL_SELECT_ID % ("rivers", "name", name)
    cur.execute(select_stmt)
    row = cur.fetchone()
    if row:
        return row[0]
    else:
        return 0

def getUTF8(string):
    """
    Tries to convert the string to a UTF-8 encoding by first checking if it
    is UTF-8 and then trying cp1252
    """
    try:
        return unicode.encode(unicode(string, "UTF-8"), "UTF-8")
    except UnicodeDecodeError:
        # Probably European Windows names so lets try again
        return unicode.encode(unicode(string, "cp1252"), "UTF-8")

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 European Windows names so lets try again
        return unicode.encode(unicode(path, "cp1252"), "UTF-8")

http://dive4elements.wald.intevation.org