[Dive4elements-commits] [PATCH 04 of 15] (importer) Add utility function to convert paths to utf-8
Wald Commits
scm-commit at wald.intevation.org
Mon Jan 28 12:27:31 CET 2013
# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1359106869 -3600
# Node ID b1d7e600b43bdf7cbb878de10c808cf031f20775
# Parent 2b371e42a9af5551470b4b1d9111d2e3df733b81
(importer) Add utility function to convert paths to utf-8
- Add utility function to generically copy fields
diff -r 2b371e42a9af -r b1d7e600b43b flys-backend/contrib/shpimporter/utils.py
--- 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))
More information about the Dive4elements-commits
mailing list