ingo@2798: import os aheinecke@4874: import sys ingo@3654: from shpimporter import DEBUG, INFO, ERROR aheinecke@4970: try: aheinecke@4970: from osgeo import ogr aheinecke@4970: except ImportErrror: aheinecke@4970: import ogr ingo@2798: ingo@2798: SHP='.shp' aheinecke@4976: SQL_SELECT_RIVER_ID="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: ingo@3654: 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@4970: def getRiverId(dbconn, name): 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@4976: cur.execute(SQL_SELECT_RIVER_ID, (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: