comparison flys-backend/contrib/shpimporter/dgm.py @ 4975:9eea3cd22ee7 dami

Add dgm file reader although an awk script should have been more appropiate
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 13 Feb 2013 17:46:34 +0100
parents
children 71e1b96f4794
comparison
equal deleted inserted replaced
4974:a92241d40228 4975:9eea3cd22ee7
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 "name" : latin("Gewässer"),
12 "lower" : ("km_von", lambda x: int(x)),
13 "upper" : ("km_bis", lambda x: int(x)),
14 "year_from" : "Jahr_von",
15 "year_to" : "Jahr_bis",
16 "projection" : "Projektion",
17 "elevation_state" : latin("Höhenstatus"),
18 "format" : "Format",
19 "border_break" : ("Bruchkanten",
20 lambda x: True if x.lower() == "Ja" else False),
21 "resolution" : (latin("Auflösung"), lambda x: x),
22 # "description" :
23 # : "SRID",
24 "path" : ("Pfad_Bestand", lambda x: x),
25 }
26
27 SQL_INSERT_DGT = "INSERT INTO dem (river_id, " + ", ".join(DGM_MAP.keys()) + \
28 ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
29
30 def insertRiverDgm(dbconn, dgmfile, river_name, dry_run):
31 with codecs.open(dgmfile, "r", "latin1") as csvfile:
32 firstline = csvfile.readline()
33 names = firstline.split(";")
34 namedict = {}
35 field_nr = 0
36 for name in names:
37 namedict[name] = field_nr
38 field_nr += 1
39
40 river_id = utils.getRiverId(dbconn, river_name)
41 for line in csvfile:
42 fields = line.split(";")
43 if not fields: continue
44 if fields[namedict[latin("Gewässer")]] != river_name:
45 continue
46 else:
47 values=[]
48 for key, val in DGM_MAP.items():
49 if isinstance(val, tuple):
50 values.append(val[1](fields[namedict[val[0]]]))
51 else:
52 values.append(unicode.encode(
53 fields[namedict[val]], "UTF-8"))
54 cur = dbconn.cursor()
55 cur.execute(SQL_INSERT_DGT, [river_id] + values)
56
57 if not dry_run:
58 dbconn.commit()
59

http://dive4elements.wald.intevation.org