ingo@2798: import os aheinecke@4874: import sys ingo@3654: from shpimporter import DEBUG, INFO, ERROR ingo@2798: ingo@2798: SHP='.shp' 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@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@4874: # Probably Windows iso-8859-x names so lets try again aheinecke@4874: return unicode.encode(unicode(path, "iso-8859-15"), "UTF-8") aheinecke@4874: aheinecke@4874: def copyFields(src, target, mapping): aheinecke@4874: """ aheinecke@4874: Checks the mapping dictonary for key value pairs to aheinecke@4874: copy from the source to the destination feature. aheinecke@4874: aheinecke@4874: The Key is the attribute of the source feature to be copied aheinecke@4874: into the target attribute named by the dict's value. aheinecke@4874: """ aheinecke@4874: for key, value in mapping.items(): aheinecke@4874: if src.GetFieldIndex(key) == -1: aheinecke@4874: continue aheinecke@4874: if src.IsFieldSet(src.GetFieldIndex(key)): aheinecke@4874: if src.GetFieldType(key) == 2: aheinecke@4874: target.SetField(value, src.GetFieldAsDouble(key)) aheinecke@4874: else: aheinecke@4874: target.SetField(value, src.GetField(key))