Mercurial > dive4elements > river
comparison flys-backend/contrib/shpimporter/utils.py @ 5379:61bf64b102bc mapgenfix
Merge with default branch
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Fri, 22 Mar 2013 11:25:54 +0100 |
parents | 3f35d9db48c1 |
children |
comparison
equal
deleted
inserted
replaced
5175:cfc5540a4eec | 5379:61bf64b102bc |
---|---|
1 import os | 1 import os |
2 import sys | 2 import sys |
3 from shpimporter import DEBUG, INFO, ERROR | 3 import logging |
4 | |
5 try: | |
6 from osgeo import ogr | |
7 except ImportError: | |
8 import ogr | |
9 | |
10 logger = logging.getLogger("utils") | |
4 | 11 |
5 SHP='.shp' | 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" | |
6 | 15 |
7 def findShapefiles(path): | 16 def findShapefiles(path): |
8 shapes = [] | 17 shapes = [] |
9 | 18 |
10 for root, dirs, files in os.walk(path): | 19 for root, dirs, files in os.walk(path): |
11 if len(files) == 0: | 20 if len(files) == 0: |
12 continue | 21 continue |
13 | 22 |
14 DEBUG("Processing directory '%s' with %i files " % (root, len(files))) | 23 logger.debug("Processing directory '%s' with %i files " % (root, len(files))) |
15 | 24 |
16 for f in files: | 25 for f in files: |
17 idx = f.find(SHP) | 26 idx = f.find(SHP) |
18 if (idx+len(SHP)) == len(f): | 27 if (idx+len(SHP)) == len(f): |
19 shapes.append((f.replace(SHP, ''), root + "/" + f)) | 28 shapes.append((f.replace(SHP, ''), root + "/" + f)) |
20 | 29 |
21 return shapes | 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") | |
22 | 61 |
23 def getUTF8Path(path): | 62 def getUTF8Path(path): |
24 """ | 63 """ |
25 Tries to convert path to utf-8 by first checking the filesystemencoding | 64 Tries to convert path to utf-8 by first checking the filesystemencoding |
26 and trying the default windows encoding afterwards. | 65 and trying the default windows encoding afterwards. |
29 try: | 68 try: |
30 return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8") | 69 return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8") |
31 except UnicodeDecodeError: | 70 except UnicodeDecodeError: |
32 # Probably European Windows names so lets try again | 71 # Probably European Windows names so lets try again |
33 return unicode.encode(unicode(path, "cp1252"), "UTF-8") | 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 |