diff flys-backend/contrib/shpimporter/dgm.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 4f46679e13d0
children f459911fdbfb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/contrib/shpimporter/dgm.py	Thu Feb 28 11:48:17 2013 +0100
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+
+import codecs
+import utils
+
+def latin(string):
+    return unicode(string, "latin1")
+
+# <dbfield> : (<csvfield>, conversion function)
+DGM_MAP = {
+    "lower"           : ("km_von", lambda x: int(x)),
+    "upper"           : ("km_bis", lambda x: int(x)),
+    "year_from"       : "Jahr_von",
+    "year_to"         : "Jahr_bis",
+    "projection"      : "Projektion",
+    "elevation_state" : latin("Höhenstatus"),
+    "format"          : "Format",
+    "border_break"    : ("Bruchkanten",
+        lambda x: True if x.lower() == "Ja" else False),
+    "resolution"      : (latin("Auflösung"), lambda x: x),
+#   "description"     : 
+    "srid"            : "SRID",
+    "path"            : ("Pfad_Bestand", lambda x: x),
+    }
+
+SQL_INSERT_DGT = "INSERT INTO dem (river_id, name, " + ", ".join(DGM_MAP.keys()) + \
+        ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
+SQL_INSERT_DGT_ORA = "INSERT INTO dem (river_id, name, " + ", ".join(DGM_MAP.keys()) + \
+        ") VALUES (:s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s)"
+
+def insertRiverDgm(dbconn, dgmfile, river_name, dry_run, oracle):
+    with codecs.open(dgmfile, "r", "latin1") as csvfile:
+        firstline = csvfile.readline()
+        names = firstline.split(";")
+        namedict = {}
+        field_nr = 0
+        for name in names:
+            namedict[name] = field_nr
+            field_nr += 1
+
+        river_id = utils.getRiverId(dbconn, river_name, oracle)
+        for line in csvfile:
+            fields = line.split(";")
+            if not fields: continue
+            if fields[namedict[latin("Gewässer")]] != river_name:
+                continue
+            else:
+                values=[]
+                for key, val in DGM_MAP.items():
+                    if isinstance(val, tuple):
+                        values.append(val[1](fields[namedict[val[0]]]))
+                    else:
+                        values.append(unicode.encode(
+                            fields[namedict[val]], "UTF-8"))
+                name = "%s KM %s - %s" % (river_name, fields[namedict["km_von"]],
+                        fields[namedict["km_bis"]])
+                cur = dbconn.cursor()
+                if oracle:
+                    stmt = SQL_INSERT_DGT_ORA
+                else:
+                    stmt = SQL_INSERT_DGT
+
+                cur.execute(stmt, [river_id, name] + values)
+
+        if not dry_run:
+            dbconn.commit()
+

http://dive4elements.wald.intevation.org