ingo@2798: import os
aheinecke@4874: import sys
aheinecke@5006: import logging
aheinecke@5006: 
aheinecke@4970: try:
aheinecke@4970:     from osgeo import ogr
aheinecke@5077: except ImportError:
aheinecke@4970:     import ogr
ingo@2798: 
aheinecke@5006: logger = logging.getLogger("utils")
aheinecke@5006: 
ingo@2798: SHP='.shp'
aheinecke@4976: SQL_SELECT_RIVER_ID="SELECT id FROM rivers WHERE name = %s"
aheinecke@5036: SQL_SELECT_RIVER_ID_ORA="SELECT id FROM rivers WHERE name = :s"
ingo@2798: 
ingo@2798: def findShapefiles(path):
ingo@2798:     shapes = []
ingo@2798: 
ingo@2798:     for root, dirs, files in os.walk(path):
ingo@2798:         if len(files) == 0:
ingo@2798:             continue
ingo@2798: 
aheinecke@5006:         logger.debug("Processing directory '%s' with %i files " % (root, len(files)))
ingo@2798: 
ingo@2798:         for f in files:
ingo@2798:             idx = f.find(SHP)
ingo@2798:             if (idx+len(SHP)) == len(f):
ingo@2798:                 shapes.append((f.replace(SHP, ''), root + "/" + f))
ingo@2798: 
ingo@2798:     return shapes
ingo@2798: 
aheinecke@5036: def getRiverId(dbconn, name, oracle):
aheinecke@4970:     """
aheinecke@4970:     Returns the id of the river "name"
aheinecke@4970:     Dbconn must be a python database connection api compliant object
aheinecke@4970:     """
aheinecke@4970:     cur = dbconn.cursor()
aheinecke@5036:     if oracle:
aheinecke@5036:         # This is stupid and shoudl not be neccessary. But I don't
aheinecke@5036:         # know how to make it work both ways. aheinecke - 02/2013
aheinecke@5036:         stmt = SQL_SELECT_RIVER_ID_ORA
aheinecke@5036:     else:
aheinecke@5036:         stmt = SQL_SELECT_RIVER_ID
aheinecke@5219:     cur.execute(stmt, (getUTF8(name),))
aheinecke@4970:     row = cur.fetchone()
aheinecke@4970:     if row:
aheinecke@4970:         return row[0]
aheinecke@4970:     else:
aheinecke@4970:         return 0
aheinecke@4970: 
aheinecke@4935: def getUTF8(string):
aheinecke@4935:     """
aheinecke@4935:     Tries to convert the string to a UTF-8 encoding by first checking if it
aheinecke@4935:     is UTF-8 and then trying cp1252
aheinecke@4935:     """
aheinecke@4935:     try:
aheinecke@4935:         return unicode.encode(unicode(string, "UTF-8"), "UTF-8")
aheinecke@4935:     except UnicodeDecodeError:
aheinecke@4935:         # Probably European Windows names so lets try again
aheinecke@4935:         return unicode.encode(unicode(string, "cp1252"), "UTF-8")
aheinecke@4935: 
aheinecke@4874: def getUTF8Path(path):
aheinecke@4874:     """
aheinecke@4874:     Tries to convert path to utf-8 by first checking the filesystemencoding
aheinecke@4874:     and trying the default windows encoding afterwards.
aheinecke@4874:     Returns a valid UTF-8 encoded unicode object or throws a UnicodeDecodeError
aheinecke@4874:     """
aheinecke@4874:     try:
aheinecke@4874:         return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8")
aheinecke@4874:     except UnicodeDecodeError:
aheinecke@4887:         # Probably European Windows names so lets try again
aheinecke@4887:         return unicode.encode(unicode(path, "cp1252"), "UTF-8")
aheinecke@4995: 
aheinecke@4995: WKB_MAP = {
aheinecke@4995:     ogr.wkb25Bit :                'wkb25Bit',
aheinecke@4995:     ogr.wkbGeometryCollection :   'wkbGeometryCollection',
aheinecke@4995:     ogr.wkbGeometryCollection25D :'wkbGeometryCollection25D',
aheinecke@4995:     ogr.wkbLineString :           'wkbLineString',
aheinecke@4995:     ogr.wkbLineString25D :        'wkbLineString25D',
aheinecke@4995:     ogr.wkbLinearRing :           'wkbLinearRing',
aheinecke@4995:     ogr.wkbMultiLineString :      'wkbMultiLineString',
aheinecke@4995:     ogr.wkbMultiLineString25D :   'wkbMultiLineString25D',
aheinecke@4995:     ogr.wkbMultiPoint :           'wkbMultiPoint',
aheinecke@4995:     ogr.wkbMultiPoint25D :        'wkbMultiPoint25D',
aheinecke@4995:     ogr.wkbMultiPolygon :         'wkbMultiPolygon',
aheinecke@4995:     ogr.wkbMultiPolygon25D :      'wkbMultiPolygon25D',
aheinecke@4995:     ogr.wkbNDR :                  'wkbNDR',
aheinecke@4995:     ogr.wkbNone :                 'wkbNone',
aheinecke@4995:     ogr.wkbPoint :                'wkbPoint',
aheinecke@4995:     ogr.wkbPoint25D :             'wkbPoint25D',
aheinecke@4995:     ogr.wkbPolygon :              'wkbPolygon',
aheinecke@4995:     ogr.wkbPolygon25D :           'wkbPolygon25D',
aheinecke@4995:     ogr.wkbUnknown :              'wkbUnknown',
aheinecke@4995:     ogr.wkbXDR :                  'wkbXDR'
aheinecke@4995: }
aheinecke@4995: 
aheinecke@4995: def getWkbString(type):
aheinecke@4995:     return WKB_MAP.get(type) or "Unknown"
aheinecke@4995: 
aheinecke@5137: def convertToMultiLine(feature):
aheinecke@5137:     """
aheinecke@5137:     Converts a feature to a multiline feature.
aheinecke@5137:     """
aheinecke@5137:     geometry = feature.GetGeometryRef()
aheinecke@5137:     # SRS information is lost while forcing to multiline
aheinecke@5137:     srs = geometry.GetSpatialReference()
aheinecke@5137:     geometry = ogr.ForceToMultiLineString(geometry)
aheinecke@5137:     geometry.AssignSpatialReference(srs)
aheinecke@5137:     feature.SetGeometry(geometry)
aheinecke@5137:     return feature
aheinecke@5294: 
aheinecke@5294: def convertToMultiPolygon(feature):
aheinecke@5294:     """
aheinecke@5294:     Converts a feature to a multiline feature.
aheinecke@5294:     """
aheinecke@5294:     geometry = feature.GetGeometryRef()
aheinecke@5294:     # SRS information is lost while forcing to multiline
aheinecke@5294:     srs = geometry.GetSpatialReference()
aheinecke@5294:     geometry = ogr.ForceToMultiPolygon(geometry)
aheinecke@5294:     geometry.AssignSpatialReference(srs)
aheinecke@5294:     feature.SetGeometry(geometry)
aheinecke@5294:     return feature