comparison 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
comparison
equal deleted inserted replaced
5126:e37b25628dd4 5128:a020100ee6a1
1 # -*- coding: utf-8 -*-
2
3 import codecs
4 import utils
5
6 def latin(string):
7 return unicode(string, "latin1")
8
9 # <dbfield> : (<csvfield>, conversion function)
10 DGM_MAP = {
11 "lower" : ("km_von", lambda x: int(x)),
12 "upper" : ("km_bis", lambda x: int(x)),
13 "year_from" : "Jahr_von",
14 "year_to" : "Jahr_bis",
15 "projection" : "Projektion",
16 "elevation_state" : latin("Höhenstatus"),
17 "format" : "Format",
18 "border_break" : ("Bruchkanten",
19 lambda x: True if x.lower() == "Ja" else False),
20 "resolution" : (latin("Auflösung"), lambda x: x),
21 # "description" :
22 "srid" : "SRID",
23 "path" : ("Pfad_Bestand", lambda x: x),
24 }
25
26 SQL_INSERT_DGT = "INSERT INTO dem (river_id, name, " + ", ".join(DGM_MAP.keys()) + \
27 ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
28 SQL_INSERT_DGT_ORA = "INSERT INTO dem (river_id, name, " + ", ".join(DGM_MAP.keys()) + \
29 ") VALUES (:s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s)"
30
31 def insertRiverDgm(dbconn, dgmfile, river_name, dry_run, oracle):
32 with codecs.open(dgmfile, "r", "latin1") as csvfile:
33 firstline = csvfile.readline()
34 names = firstline.split(";")
35 namedict = {}
36 field_nr = 0
37 for name in names:
38 namedict[name] = field_nr
39 field_nr += 1
40
41 river_id = utils.getRiverId(dbconn, river_name, oracle)
42 for line in csvfile:
43 fields = line.split(";")
44 if not fields: continue
45 if fields[namedict[latin("Gewässer")]] != river_name:
46 continue
47 else:
48 values=[]
49 for key, val in DGM_MAP.items():
50 if isinstance(val, tuple):
51 values.append(val[1](fields[namedict[val[0]]]))
52 else:
53 values.append(unicode.encode(
54 fields[namedict[val]], "UTF-8"))
55 name = "%s KM %s - %s" % (river_name, fields[namedict["km_von"]],
56 fields[namedict["km_bis"]])
57 cur = dbconn.cursor()
58 if oracle:
59 stmt = SQL_INSERT_DGT_ORA
60 else:
61 stmt = SQL_INSERT_DGT
62
63 cur.execute(stmt, [river_id, name] + values)
64
65 if not dry_run:
66 dbconn.commit()
67

http://dive4elements.wald.intevation.org