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: # <dbfield> : (<csvfield>, conversion function)
aheinecke@4975: DGM_MAP = {
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@4993:     "srid"            : "SRID",
aheinecke@4975:     "path"            : ("Pfad_Bestand", lambda x: x),
aheinecke@4975:     }
aheinecke@4975: 
aheinecke@5085: SQL_INSERT_DGT = "INSERT INTO dem (river_id, name, " + ", ".join(DGM_MAP.keys()) + \
aheinecke@4993:         ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
aheinecke@5085: SQL_INSERT_DGT_ORA = "INSERT INTO dem (river_id, name, " + ", ".join(DGM_MAP.keys()) + \
aheinecke@5036:         ") VALUES (:s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s)"
aheinecke@4975: 
aheinecke@5036: def insertRiverDgm(dbconn, dgmfile, river_name, dry_run, oracle):
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@5036:         river_id = utils.getRiverId(dbconn, river_name, oracle)
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@5085:                 name = "%s KM %s - %s" % (river_name, fields[namedict["km_von"]],
aheinecke@5085:                         fields[namedict["km_bis"]])
aheinecke@4975:                 cur = dbconn.cursor()
aheinecke@5036:                 if oracle:
aheinecke@5036:                     stmt = SQL_INSERT_DGT_ORA
aheinecke@5036:                 else:
aheinecke@5036:                     stmt = SQL_INSERT_DGT
aheinecke@5036: 
aheinecke@5085:                 cur.execute(stmt, [river_id, name] + values)
aheinecke@4975: 
aheinecke@4975:         if not dry_run:
aheinecke@4975:             dbconn.commit()
aheinecke@4975: