comparison flys-backend/contrib/shpimporter/utils.py @ 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 59ca5dab2782
children e9880b224c2f
comparison
equal deleted inserted replaced
4873:2b371e42a9af 4874:b1d7e600b43b
1 import os 1 import os
2 import sys
2 from shpimporter import DEBUG, INFO, ERROR 3 from shpimporter import DEBUG, INFO, ERROR
3 4
4 SHP='.shp' 5 SHP='.shp'
5 6
6 def findShapefiles(path): 7 def findShapefiles(path):
17 if (idx+len(SHP)) == len(f): 18 if (idx+len(SHP)) == len(f):
18 shapes.append((f.replace(SHP, ''), root + "/" + f)) 19 shapes.append((f.replace(SHP, ''), root + "/" + f))
19 20
20 return shapes 21 return shapes
21 22
23 def getUTF8Path(path):
24 """
25 Tries to convert path to utf-8 by first checking the filesystemencoding
26 and trying the default windows encoding afterwards.
27 Returns a valid UTF-8 encoded unicode object or throws a UnicodeDecodeError
28 """
29 try:
30 return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8")
31 except UnicodeDecodeError:
32 # Probably Windows iso-8859-x names so lets try again
33 return unicode.encode(unicode(path, "iso-8859-15"), "UTF-8")
34
35 def copyFields(src, target, mapping):
36 """
37 Checks the mapping dictonary for key value pairs to
38 copy from the source to the destination feature.
39
40 The Key is the attribute of the source feature to be copied
41 into the target attribute named by the dict's value.
42 """
43 for key, value in mapping.items():
44 if src.GetFieldIndex(key) == -1:
45 continue
46 if src.IsFieldSet(src.GetFieldIndex(key)):
47 if src.GetFieldType(key) == 2:
48 target.SetField(value, src.GetFieldAsDouble(key))
49 else:
50 target.SetField(value, src.GetField(key))

http://dive4elements.wald.intevation.org