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@4970: SQL_SELECT_ID="SELECT id FROM %s WHERE %s = '%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@4970: select_stmt = SQL_SELECT_ID % ("rivers", "name", name) aheinecke@4970: cur.execute(select_stmt) 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")