comparison backend/contrib/shpimporter/utils.py @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-backend/contrib/shpimporter/utils.py@3f35d9db48c1
children
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 import os
2 import sys
3 import logging
4
5 try:
6 from osgeo import ogr
7 except ImportError:
8 import ogr
9
10 logger = logging.getLogger("utils")
11
12 SHP='.shp'
13 SQL_SELECT_RIVER_ID="SELECT id FROM rivers WHERE name = %s"
14 SQL_SELECT_RIVER_ID_ORA="SELECT id FROM rivers WHERE name = :s"
15
16 def findShapefiles(path):
17 shapes = []
18
19 for root, dirs, files in os.walk(path):
20 if len(files) == 0:
21 continue
22
23 logger.debug("Processing directory '%s' with %i files " % (root, len(files)))
24
25 for f in files:
26 idx = f.find(SHP)
27 if (idx+len(SHP)) == len(f):
28 shapes.append((f.replace(SHP, ''), root + "/" + f))
29
30 return shapes
31
32 def getRiverId(dbconn, name, oracle):
33 """
34 Returns the id of the river "name"
35 Dbconn must be a python database connection api compliant object
36 """
37 cur = dbconn.cursor()
38 if oracle:
39 # This is stupid and shoudl not be neccessary. But I don't
40 # know how to make it work both ways. aheinecke - 02/2013
41 stmt = SQL_SELECT_RIVER_ID_ORA
42 else:
43 stmt = SQL_SELECT_RIVER_ID
44 cur.execute(stmt, (getUTF8(name),))
45 row = cur.fetchone()
46 if row:
47 return row[0]
48 else:
49 return 0
50
51 def getUTF8(string):
52 """
53 Tries to convert the string to a UTF-8 encoding by first checking if it
54 is UTF-8 and then trying cp1252
55 """
56 try:
57 return unicode.encode(unicode(string, "UTF-8"), "UTF-8")
58 except UnicodeDecodeError:
59 # Probably European Windows names so lets try again
60 return unicode.encode(unicode(string, "cp1252"), "UTF-8")
61
62 def getUTF8Path(path):
63 """
64 Tries to convert path to utf-8 by first checking the filesystemencoding
65 and trying the default windows encoding afterwards.
66 Returns a valid UTF-8 encoded unicode object or throws a UnicodeDecodeError
67 """
68 try:
69 return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8")
70 except UnicodeDecodeError:
71 # Probably European Windows names so lets try again
72 return unicode.encode(unicode(path, "cp1252"), "UTF-8")
73
74 WKB_MAP = {
75 ogr.wkb25Bit : 'wkb25Bit',
76 ogr.wkbGeometryCollection : 'wkbGeometryCollection',
77 ogr.wkbGeometryCollection25D :'wkbGeometryCollection25D',
78 ogr.wkbLineString : 'wkbLineString',
79 ogr.wkbLineString25D : 'wkbLineString25D',
80 ogr.wkbLinearRing : 'wkbLinearRing',
81 ogr.wkbMultiLineString : 'wkbMultiLineString',
82 ogr.wkbMultiLineString25D : 'wkbMultiLineString25D',
83 ogr.wkbMultiPoint : 'wkbMultiPoint',
84 ogr.wkbMultiPoint25D : 'wkbMultiPoint25D',
85 ogr.wkbMultiPolygon : 'wkbMultiPolygon',
86 ogr.wkbMultiPolygon25D : 'wkbMultiPolygon25D',
87 ogr.wkbNDR : 'wkbNDR',
88 ogr.wkbNone : 'wkbNone',
89 ogr.wkbPoint : 'wkbPoint',
90 ogr.wkbPoint25D : 'wkbPoint25D',
91 ogr.wkbPolygon : 'wkbPolygon',
92 ogr.wkbPolygon25D : 'wkbPolygon25D',
93 ogr.wkbUnknown : 'wkbUnknown',
94 ogr.wkbXDR : 'wkbXDR'
95 }
96
97 def getWkbString(type):
98 return WKB_MAP.get(type) or "Unknown"
99
100 def convertToMultiLine(feature):
101 """
102 Converts a feature to a multiline feature.
103 """
104 geometry = feature.GetGeometryRef()
105 # SRS information is lost while forcing to multiline
106 srs = geometry.GetSpatialReference()
107 geometry = ogr.ForceToMultiLineString(geometry)
108 geometry.AssignSpatialReference(srs)
109 feature.SetGeometry(geometry)
110 return feature
111
112 def convertToMultiPolygon(feature):
113 """
114 Converts a feature to a multiline feature.
115 """
116 geometry = feature.GetGeometryRef()
117 # SRS information is lost while forcing to multiline
118 srs = geometry.GetSpatialReference()
119 geometry = ogr.ForceToMultiPolygon(geometry)
120 geometry.AssignSpatialReference(srs)
121 feature.SetGeometry(geometry)
122 return feature

http://dive4elements.wald.intevation.org