changeset 4874:b1d7e600b43b

(importer) Add utility function to convert paths to utf-8 - Add utility function to generically copy fields
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 25 Jan 2013 10:41:09 +0100
parents 2b371e42a9af
children 37fa93f65971
files flys-backend/contrib/shpimporter/utils.py
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/utils.py	Fri Jan 25 10:34:47 2013 +0100
+++ b/flys-backend/contrib/shpimporter/utils.py	Fri Jan 25 10:41:09 2013 +0100
@@ -1,4 +1,5 @@
 import os
+import sys
 from shpimporter import DEBUG, INFO, ERROR
 
 SHP='.shp'
@@ -19,3 +20,31 @@
 
     return shapes
 
+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 Windows iso-8859-x names so lets try again
+        return unicode.encode(unicode(path, "iso-8859-15"), "UTF-8")
+
+def copyFields(src, target, mapping):
+    """
+    Checks the mapping dictonary for key value pairs to
+    copy from the source to the destination feature.
+
+    The Key is the attribute of the source feature to be copied
+    into the target attribute named by the dict's value.
+    """
+    for key, value in mapping.items():
+        if src.GetFieldIndex(key) == -1:
+            continue
+        if src.IsFieldSet(src.GetFieldIndex(key)):
+            if src.GetFieldType(key) == 2:
+                target.SetField(value, src.GetFieldAsDouble(key))
+            else:
+                target.SetField(value, src.GetField(key))

http://dive4elements.wald.intevation.org