[Dive4elements-commits] [PATCH 2 of 3] Add dgm file reader although an awk script should have been more appropiate
Wald Commits
scm-commit at wald.intevation.org
Wed Feb 13 17:49:55 CET 2013
# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1360773994 -3600
# Branch dami
# Node ID 9eea3cd22ee7262a09d04522d3665250167c5abd
# Parent a92241d402289bb97759e23e40c2a672b999803d
Add dgm file reader although an awk script should have been more appropiate
diff -r a92241d40228 -r 9eea3cd22ee7 flys-backend/contrib/shpimporter/dgm.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/contrib/shpimporter/dgm.py Wed Feb 13 17:46:34 2013 +0100
@@ -0,0 +1,59 @@
+# -*- coding: utf-8 -*-
+
+import codecs
+import utils
+
+def latin(string):
+ return unicode(string, "latin1")
+
+# <dbfield> : (<csvfield>, conversion function)
+DGM_MAP = {
+ "name" : latin("Gewässer"),
+ "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",
+ "path" : ("Pfad_Bestand", lambda x: x),
+ }
+
+SQL_INSERT_DGT = "INSERT INTO dem (river_id, " + ", ".join(DGM_MAP.keys()) + \
+ ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
+
+def insertRiverDgm(dbconn, dgmfile, river_name, dry_run):
+ 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)
+ 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"))
+ cur = dbconn.cursor()
+ cur.execute(SQL_INSERT_DGT, [river_id] + values)
+
+ if not dry_run:
+ dbconn.commit()
+
diff -r a92241d40228 -r 9eea3cd22ee7 flys-backend/contrib/shpimporter/shpimporter.py
--- a/flys-backend/contrib/shpimporter/shpimporter.py Wed Feb 13 17:45:35 2013 +0100
+++ b/flys-backend/contrib/shpimporter/shpimporter.py Wed Feb 13 17:46:34 2013 +0100
@@ -19,6 +19,7 @@
from hws import HWSLines, HWSPoints
from gauges import GaugeLocation
from catchments import Catchment
+from dgm import insertRiverDgm
VERBOSE_DEBUG=2
@@ -83,6 +84,7 @@
parser.add_option("--skip_catchments", type="int")
parser.add_option("--skip_kms", type="int")
parser.add_option("--skip_uesgs", type="int")
+ parser.add_option("--skip_dgm", type="int")
(config, args) = parser.parse_args()
if config.directory == None:
@@ -223,9 +225,22 @@
except:
types[geomType] = 1
- for key in types:
- DEBUG("%i x geometry type %s" % (types[key], key))
+ for key in types:
+ DEBUG("%i x geometry type %s" % (types[key], key))
+ if not config.skip_dgm:
+ dgmfilename = os.path.join(
+ config.directory, "..", "DGMs.csv")
+ try:
+ dgmfile = open(dgmfilename, "r")
+ dgmfile.close()
+ except IOError:
+ INFO("Could not find DGM file: %s \n"
+ "Skipping DGM import." % dgmfilename)
+ dgmfilename=None
+ if dgmfilename:
+ INFO("Inserting DGM meta information in 'dem' table.")
+ insertRiverDgm(dbconn_raw, dgmfilename, river_name, config.dry_run)
if __name__ == '__main__':
main()
More information about the Dive4elements-commits
mailing list