view flys-backend/contrib/shpimporter/dgm.py @ 4999:929ec3ed7dc2 dami

SCHEMA_CHANGE: Add lookup tables for sectie / sobek and boundary_kind
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 15 Feb 2013 12:38:25 +0100
parents 71e1b96f4794
children d6860fca89e4
line wrap: on
line source
# -*- 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"            : "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, %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()

http://dive4elements.wald.intevation.org