view flys-backend/contrib/shpimporter/shpimporter.py @ 4878:82d00b0c7302

(importer) Avoid uneccessary error and improve debug output
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 25 Jan 2013 10:46:00 +0100
parents a563e9f58f93
children 63f82bdd45f8
line wrap: on
line source
try:
    from osgeo import ogr
except ImportErrror:
    import ogr

import utils, optparse

from uesg  import UESG
from axis  import Axis
from km    import KM
from lines import Line
from fixpoints import Fixpoint
from buildings import Building
from crosssectiontracks import CrosssectionTrack
from floodplains import Floodplain
from boundaries import HydrBoundary, HydrBoundaryPoly
from hws import HWS
from gauges import GaugeLocation
from catchments import Catchment


VERBOSE_DEBUG=2
VERBOSE_INFO=1


def DEBUG(msg):
    config = getConfig()
    if config.verbose >= VERBOSE_DEBUG:
        print "DEBUG: %s" % msg

def INFO(msg):
    config = getConfig()
    if config.verbose >= VERBOSE_INFO:
        print "INFO: %s" % msg

def ERROR(msg):
    config = getConfig()
    print "ERROR: %s" % msg


def getImporters(config, dbconn):
    return [
        Axis(config, dbconn),
        KM(config, dbconn),
        CrosssectionTrack(config, dbconn),
        Line(config, dbconn),
        Fixpoint(config, dbconn),
        Building(config, dbconn),
        Floodplain(config, dbconn),
        HydrBoundary(config, dbconn),
        HydrBoundaryPoly(config, dbconn),
        HWS(config, dbconn),
        GaugeLocation(config, dbconn),
        Catchment(config, dbconn),
        UESG(config, dbconn)
        ]


def getConfig():
    parser = optparse.OptionParser()
    parser.add_option("--directory", type="string")
    parser.add_option("--target_srs", type="int")
    parser.add_option("--host", type="string")
    parser.add_option("--user", type="string")
    parser.add_option("--password", type="string")
    parser.add_option("--river_id", type="int")
    parser.add_option("--verbose", type="int", default=1)
    parser.add_option("--dry_run", type="int", default=0)
    parser.add_option("--ogr_connection", type="string")
    parser.add_option("--skip_axis", type="int")
    parser.add_option("--skip_hydr_boundaries", type="int")
    parser.add_option("--skip_buildings", type="int")
    parser.add_option("--skip_crosssections", type="int")
    parser.add_option("--skip_lines", type="int")
    parser.add_option("--skip_fixpoints", type="int")
    parser.add_option("--skip_floodplains", type="int")
    parser.add_option("--skip_hws", type="int")
    parser.add_option("--skip_gauge_locations", type="int")
    parser.add_option("--skip_catchments", type="int")
    parser.add_option("--skip_kms", type="int")
    parser.add_option("--skip_uesgs", type="int")
    (config, args) = parser.parse_args()

    if config.directory == None:
        ERROR("No river directory specified!")
        raise Exception("Invalid config")
    if not config.ogr_connection:
        if not config.host:
            ERROR("No database host specified!")
            raise Exception("Invalid config")
        if not config.user:
            ERROR("No databaser user specified!")
            raise Exception("Invalid config")
        if not config.password:
            ERROR("No password specified!")
            raise Exception("Invalid config")
    if config.river_id == None:
        ERROR("No river id specified!")
        raise Exception("Invalid config")

    return config


def skip_importer(config, importer):
    if config.skip_axis == 1 and isinstance(importer, Axis):
        return True
    elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundary):
        return True
    elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundaryPoly):
        return True
    elif config.skip_buildings == 1 and isinstance(importer, Building):
        return True
    elif config.skip_crosssections == 1 and isinstance(importer, CrosssectionTrack):
        return True
    elif config.skip_lines == 1 and isinstance(importer, Line):
        return True
    elif config.skip_fixpoints == 1 and isinstance(importer, Fixpoint):
        return True
    elif config.skip_floodplains == 1 and isinstance(importer, Floodplain):
        return True
    elif config.skip_hws == 1 and isinstance(importer, HWS):
        return True
    elif config.skip_gauge_locations == 1 and isinstance(importer, GaugeLocation):
        return True
    elif config.skip_catchments == 1 and isinstance(importer, Catchment):
        return True
    elif config.skip_kms == 1 and isinstance(importer, KM):
        return True
    elif config.skip_uesgs == 1 and isinstance(importer, UESG):
        return True

    return False


def main():
    config=None
    try:
        config = getConfig()
    except:
        return -1

    if config == None:
        ERROR("Unable to read config from command line!")
        return

    if config.dry_run > 0:
        INFO("You enable 'dry_run'. No database transaction will take place!")

    if config.ogr_connection:
        dbconn = ogr.Open(config.ogr_connection)
    else:
        dbconn = ogr.Open('OCI:%s/%s@%s' % (config.user, config.password, config.host))

    if not dbconn:
        shpimporter.ERROR("Could not connect to database %s" % self.dbconn)
        return -1

    importers = getImporters(config, dbconn)
    types = {}

    for importer in importers:
        if skip_importer(config, importer):
            INFO("Skip import of '%s'" % importer.getName())
            continue

        INFO("Start import of '%s'" % importer.getName())

        shapes = utils.findShapefiles(importer.getPath(config.directory))
        DEBUG("Found %i Shapefiles" % len(shapes))

        for shpTuple in shapes:
            geomType = importer.walkOverShapes(shpTuple)
            try:
                if geomType is not None:
                    num = types[geomType]
                    types[geomType] = num+1
            except:
                types[geomType] = 1

    for key in types:
        DEBUG("%i x geometry type %s" % (types[key], key))


if __name__ == '__main__':
    main()

http://dive4elements.wald.intevation.org