aheinecke@4975: # -*- coding: utf-8 -*- aheinecke@4975: aheinecke@4975: import codecs aheinecke@4975: import utils aheinecke@4975: aheinecke@4975: def latin(string): aheinecke@4975: return unicode(string, "latin1") aheinecke@4975: aheinecke@4975: # : (, conversion function) aheinecke@4975: DGM_MAP = { aheinecke@4975: "name" : latin("Gewässer"), aheinecke@4975: "lower" : ("km_von", lambda x: int(x)), aheinecke@4975: "upper" : ("km_bis", lambda x: int(x)), aheinecke@4975: "year_from" : "Jahr_von", aheinecke@4975: "year_to" : "Jahr_bis", aheinecke@4975: "projection" : "Projektion", aheinecke@4975: "elevation_state" : latin("Höhenstatus"), aheinecke@4975: "format" : "Format", aheinecke@4975: "border_break" : ("Bruchkanten", aheinecke@4975: lambda x: True if x.lower() == "Ja" else False), aheinecke@4975: "resolution" : (latin("Auflösung"), lambda x: x), aheinecke@4975: # "description" : aheinecke@4975: # : "SRID", aheinecke@4975: "path" : ("Pfad_Bestand", lambda x: x), aheinecke@4975: } aheinecke@4975: aheinecke@4975: SQL_INSERT_DGT = "INSERT INTO dem (river_id, " + ", ".join(DGM_MAP.keys()) + \ aheinecke@4975: ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" aheinecke@4975: aheinecke@4975: def insertRiverDgm(dbconn, dgmfile, river_name, dry_run): aheinecke@4975: with codecs.open(dgmfile, "r", "latin1") as csvfile: aheinecke@4975: firstline = csvfile.readline() aheinecke@4975: names = firstline.split(";") aheinecke@4975: namedict = {} aheinecke@4975: field_nr = 0 aheinecke@4975: for name in names: aheinecke@4975: namedict[name] = field_nr aheinecke@4975: field_nr += 1 aheinecke@4975: aheinecke@4975: river_id = utils.getRiverId(dbconn, river_name) aheinecke@4975: for line in csvfile: aheinecke@4975: fields = line.split(";") aheinecke@4975: if not fields: continue aheinecke@4975: if fields[namedict[latin("Gewässer")]] != river_name: aheinecke@4975: continue aheinecke@4975: else: aheinecke@4975: values=[] aheinecke@4975: for key, val in DGM_MAP.items(): aheinecke@4975: if isinstance(val, tuple): aheinecke@4975: values.append(val[1](fields[namedict[val[0]]])) aheinecke@4975: else: aheinecke@4975: values.append(unicode.encode( aheinecke@4975: fields[namedict[val]], "UTF-8")) aheinecke@4975: cur = dbconn.cursor() aheinecke@4975: cur.execute(SQL_INSERT_DGT, [river_id] + values) aheinecke@4975: aheinecke@4975: if not dry_run: aheinecke@4975: dbconn.commit() aheinecke@4975: