diff flys-backend/contrib/shpimporter/utils.py @ 5128:a020100ee6a1

SCHEME CHANGE: Merge branch dami into default. A summary on the scheme changes: HWS and Lines tables are dropped and will be replaced by HWS_Lines and HWS_Points. The catchment table removed and will be replaced by a WMS Service. Hydr_boundaries has an added reference to boundary_kind sectie_kind and sobek_kind objects. Dem has a new column srid.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 28 Feb 2013 11:48:17 +0100
parents c5187ab9f571
children 04eb62eae722
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/utils.py	Thu Feb 28 11:06:20 2013 +0100
+++ b/flys-backend/contrib/shpimporter/utils.py	Thu Feb 28 11:48:17 2013 +0100
@@ -1,8 +1,17 @@
 import os
 import sys
-from shpimporter import DEBUG, INFO, ERROR
+import logging
+
+try:
+    from osgeo import ogr
+except ImportError:
+    import ogr
+
+logger = logging.getLogger("utils")
 
 SHP='.shp'
+SQL_SELECT_RIVER_ID="SELECT id FROM rivers WHERE name = %s"
+SQL_SELECT_RIVER_ID_ORA="SELECT id FROM rivers WHERE name = :s"
 
 def findShapefiles(path):
     shapes = []
@@ -11,7 +20,7 @@
         if len(files) == 0:
             continue
 
-        DEBUG("Processing directory '%s' with %i files " % (root, len(files)))
+        logger.debug("Processing directory '%s' with %i files " % (root, len(files)))
 
         for f in files:
             idx = f.find(SHP)
@@ -20,6 +29,36 @@
 
     return shapes
 
+def getRiverId(dbconn, name, oracle):
+    """
+    Returns the id of the river "name"
+    Dbconn must be a python database connection api compliant object
+    """
+    cur = dbconn.cursor()
+    if oracle:
+        # This is stupid and shoudl not be neccessary. But I don't
+        # know how to make it work both ways. aheinecke - 02/2013
+        stmt = SQL_SELECT_RIVER_ID_ORA
+    else:
+        stmt = SQL_SELECT_RIVER_ID
+    cur.execute(stmt, (name,))
+    row = cur.fetchone()
+    if row:
+        return row[0]
+    else:
+        return 0
+
+def getUTF8(string):
+    """
+    Tries to convert the string to a UTF-8 encoding by first checking if it
+    is UTF-8 and then trying cp1252
+    """
+    try:
+        return unicode.encode(unicode(string, "UTF-8"), "UTF-8")
+    except UnicodeDecodeError:
+        # Probably European Windows names so lets try again
+        return unicode.encode(unicode(string, "cp1252"), "UTF-8")
+
 def getUTF8Path(path):
     """
     Tries to convert path to utf-8 by first checking the filesystemencoding
@@ -31,3 +70,30 @@
     except UnicodeDecodeError:
         # Probably European Windows names so lets try again
         return unicode.encode(unicode(path, "cp1252"), "UTF-8")
+
+WKB_MAP = {
+    ogr.wkb25Bit :                'wkb25Bit',
+    ogr.wkbGeometryCollection :   'wkbGeometryCollection',
+    ogr.wkbGeometryCollection25D :'wkbGeometryCollection25D',
+    ogr.wkbLineString :           'wkbLineString',
+    ogr.wkbLineString25D :        'wkbLineString25D',
+    ogr.wkbLinearRing :           'wkbLinearRing',
+    ogr.wkbMultiLineString :      'wkbMultiLineString',
+    ogr.wkbMultiLineString25D :   'wkbMultiLineString25D',
+    ogr.wkbMultiPoint :           'wkbMultiPoint',
+    ogr.wkbMultiPoint25D :        'wkbMultiPoint25D',
+    ogr.wkbMultiPolygon :         'wkbMultiPolygon',
+    ogr.wkbMultiPolygon25D :      'wkbMultiPolygon25D',
+    ogr.wkbNDR :                  'wkbNDR',
+    ogr.wkbNone :                 'wkbNone',
+    ogr.wkbPoint :                'wkbPoint',
+    ogr.wkbPoint25D :             'wkbPoint25D',
+    ogr.wkbPolygon :              'wkbPolygon',
+    ogr.wkbPolygon25D :           'wkbPolygon25D',
+    ogr.wkbUnknown :              'wkbUnknown',
+    ogr.wkbXDR :                  'wkbXDR'
+}
+
+def getWkbString(type):
+    return WKB_MAP.get(type) or "Unknown"
+

http://dive4elements.wald.intevation.org