aheinecke@4872: try: aheinecke@4872: from osgeo import ogr tom@4977: except ImportError: aheinecke@4872: import ogr ingo@2853: ingo@3654: import utils, optparse aheinecke@4970: import sys aheinecke@4970: import os ingo@3654: ingo@2853: from uesg import UESG ingo@2853: from axis import Axis ingo@2853: from km import KM ingo@2853: from fixpoints import Fixpoint ingo@2853: from buildings import Building ingo@2853: from crosssectiontracks import CrosssectionTrack ingo@2861: from floodplains import Floodplain ingo@2861: from boundaries import HydrBoundary, HydrBoundaryPoly aheinecke@4879: from hws import HWSLines, HWSPoints ingo@2862: from gauges import GaugeLocation ingo@2862: from catchments import Catchment aheinecke@4975: from dgm import insertRiverDgm ingo@2798: ingo@3654: ingo@3654: VERBOSE_DEBUG=2 ingo@3654: VERBOSE_INFO=1 ingo@2798: ingo@2798: ingo@3654: def DEBUG(msg): ingo@3654: config = getConfig() ingo@3654: if config.verbose >= VERBOSE_DEBUG: ingo@3654: print "DEBUG: %s" % msg ingo@3654: ingo@3654: def INFO(msg): ingo@3654: config = getConfig() ingo@3654: if config.verbose >= VERBOSE_INFO: ingo@3654: print "INFO: %s" % msg ingo@3654: ingo@3654: def ERROR(msg): ingo@3654: config = getConfig() ingo@3654: print "ERROR: %s" % msg ingo@3654: ingo@3654: aheinecke@4970: def getImporters(river_id, dbconn, dry_run): ingo@2853: return [ aheinecke@4970: Axis(river_id, dbconn, dry_run), aheinecke@4970: KM(river_id, dbconn, dry_run), aheinecke@4970: CrosssectionTrack(river_id, dbconn, dry_run), aheinecke@4970: Line(river_id, dbconn, dry_run), aheinecke@4970: Fixpoint(river_id, dbconn, dry_run), aheinecke@4970: Building(river_id, dbconn, dry_run), aheinecke@4970: Floodplain(river_id, dbconn, dry_run), aheinecke@4970: HydrBoundary(river_id, dbconn, dry_run), aheinecke@4970: HydrBoundaryPoly(river_id, dbconn, dry_run), aheinecke@4970: HWSLines(river_id, dbconn, dry_run), aheinecke@4970: HWSPoints(river_id, dbconn, dry_run), aheinecke@4970: GaugeLocation(river_id, dbconn, dry_run), aheinecke@4970: Catchment(river_id, dbconn, dry_run), aheinecke@4970: UESG(river_id, dbconn, dry_run) ingo@2861: ] ingo@2798: ingo@2798: ingo@3654: def getConfig(): ingo@3654: parser = optparse.OptionParser() ingo@3654: parser.add_option("--directory", type="string") ingo@3654: parser.add_option("--target_srs", type="int") ingo@3654: parser.add_option("--host", type="string") ingo@3654: parser.add_option("--user", type="string") ingo@3654: parser.add_option("--password", type="string") aheinecke@4970: parser.add_option("--river_name", type="string") ingo@3654: parser.add_option("--verbose", type="int", default=1) ingo@3655: parser.add_option("--dry_run", type="int", default=0) aheinecke@4871: parser.add_option("--ogr_connection", type="string") ingo@3654: parser.add_option("--skip_axis", type="int") ingo@3654: parser.add_option("--skip_hydr_boundaries", type="int") ingo@3654: parser.add_option("--skip_buildings", type="int") ingo@3654: parser.add_option("--skip_crosssections", type="int") ingo@3654: parser.add_option("--skip_fixpoints", type="int") ingo@3654: parser.add_option("--skip_floodplains", type="int") aheinecke@4879: parser.add_option("--skip_hws_lines", type="int") aheinecke@4879: parser.add_option("--skip_hws_points", type="int") ingo@3654: parser.add_option("--skip_gauge_locations", type="int") ingo@3654: parser.add_option("--skip_catchments", type="int") ingo@3654: parser.add_option("--skip_kms", type="int") ingo@3654: parser.add_option("--skip_uesgs", type="int") aheinecke@4975: parser.add_option("--skip_dgm", type="int") ingo@3654: (config, args) = parser.parse_args() ingo@2798: ingo@3654: if config.directory == None: ingo@3654: ERROR("No river directory specified!") ingo@3654: raise Exception("Invalid config") aheinecke@4871: if not config.ogr_connection: aheinecke@4871: if not config.host: aheinecke@4871: ERROR("No database host specified!") aheinecke@4871: raise Exception("Invalid config") aheinecke@4871: if not config.user: aheinecke@4871: ERROR("No databaser user specified!") aheinecke@4871: raise Exception("Invalid config") aheinecke@4871: if not config.password: aheinecke@4871: ERROR("No password specified!") aheinecke@4871: raise Exception("Invalid config") ingo@3654: ingo@3654: return config ingo@3654: ingo@3654: ingo@3654: def skip_importer(config, importer): ingo@3654: if config.skip_axis == 1 and isinstance(importer, Axis): ingo@3654: return True ingo@3654: elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundary): ingo@3654: return True ingo@3654: elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundaryPoly): ingo@3654: return True ingo@3654: elif config.skip_buildings == 1 and isinstance(importer, Building): ingo@3654: return True ingo@3654: elif config.skip_crosssections == 1 and isinstance(importer, CrosssectionTrack): ingo@3654: return True ingo@3654: elif config.skip_fixpoints == 1 and isinstance(importer, Fixpoint): ingo@3654: return True ingo@3654: elif config.skip_floodplains == 1 and isinstance(importer, Floodplain): ingo@3654: return True aheinecke@4963: elif config.skip_hws_lines == 1 and isinstance(importer, HWSLines): aheinecke@4963: return True aheinecke@4879: elif config.skip_hws_points == 1 and isinstance(importer, HWSPoints): ingo@3654: return True ingo@3654: elif config.skip_gauge_locations == 1 and isinstance(importer, GaugeLocation): ingo@3654: return True ingo@3654: elif config.skip_catchments == 1 and isinstance(importer, Catchment): ingo@3654: return True ingo@3654: elif config.skip_kms == 1 and isinstance(importer, KM): ingo@3654: return True ingo@3654: elif config.skip_uesgs == 1 and isinstance(importer, UESG): ingo@3654: return True ingo@3654: ingo@3654: return False ingo@3654: aheinecke@4872: def main(): ingo@3654: config=None ingo@3654: try: ingo@3654: config = getConfig() ingo@3654: except: aheinecke@4872: return -1 ingo@3654: ingo@3654: if config == None: ingo@3654: ERROR("Unable to read config from command line!") ingo@3654: return ingo@3654: ingo@3655: if config.dry_run > 0: ingo@3655: INFO("You enable 'dry_run'. No database transaction will take place!") ingo@3655: aheinecke@4872: if config.ogr_connection: aheinecke@4879: connstr = config.ogr_connection aheinecke@4872: else: aheinecke@4879: connstr = 'OCI:%s/%s@%s' % (config.user, config.password, config.host) aheinecke@4872: aheinecke@4970: if 'OCI:' in connstr: aheinecke@4970: try: aheinecke@4970: import cx_Oracle as dbapi aheinecke@4970: raw_connstr=connstr.replace("OCI:", "") aheinecke@4970: except ImportErrror: aheinecke@4970: ERROR("Module cx_Oracle not found in: %s\n" aheinecke@4970: "Neccessary to connect to a Oracle Database.\n" aheinecke@4970: "Please refer to the installation " aheinecke@4970: "documentation." % sys.path) aheinecke@4970: aheinecke@4970: else: # Currently only support for oracle and postgres aheinecke@4970: try: aheinecke@4970: import psycopg2 as dbapi aheinecke@4970: raw_connstr=connstr.replace("PG:", "") tom@4977: except ImportError: aheinecke@4970: ERROR("Module psycopg2 not found in: %s\n" aheinecke@4970: "Neccessary to connect to a Posgresql Database.\n" aheinecke@4970: "Please refer to the installation " aheinecke@4970: "documentation." % sys.path) aheinecke@4970: aheinecke@4970: dbconn_raw = dbapi.connect(raw_connstr) aheinecke@4879: dbconn = ogr.Open(connstr) aheinecke@4879: aheinecke@4879: if dbconn == None: aheinecke@4879: ERROR("Could not connect to database %s" % connstr) aheinecke@4872: return -1 aheinecke@4872: ingo@2798: types = {} ingo@2798: aheinecke@4970: directories = [] aheinecke@4970: if not config.river_name: aheinecke@4970: for file in os.listdir(config.directory): aheinecke@4970: if os.path.isdir(file): aheinecke@4970: directories.append(os.path.join(file)) aheinecke@4970: else: aheinecke@4970: directories.append(config.directory) ingo@3654: aheinecke@4970: for directory in directories: aheinecke@4970: if not config.river_name: aheinecke@4970: river_name = os.path.basename(os.path.normpath(directory)) aheinecke@4970: else: aheinecke@4970: river_name = config.river_name aheinecke@4970: river_id = utils.getRiverId(dbconn_raw, river_name) ingo@2853: aheinecke@4970: if not river_id: aheinecke@4970: INFO("Could not find river in database. Skipping: %s" aheinecke@4970: % river_name) aheinecke@4970: continue aheinecke@4970: else: aheinecke@4970: INFO("Importing River: %s" % river_name) aheinecke@4970: aheinecke@4970: for importer in getImporters(river_id, dbconn, config.dry_run): aheinecke@4970: if skip_importer(config, importer): aheinecke@4970: INFO("Skip import of '%s'" % importer.getName()) aheinecke@4970: continue aheinecke@4970: aheinecke@4970: INFO("Start import of '%s'" % importer.getName()) aheinecke@4970: aheinecke@4970: shapes = utils.findShapefiles(importer.getPath(config.directory)) aheinecke@4970: DEBUG("Found %i Shapefiles" % len(shapes)) aheinecke@4970: aheinecke@4970: for shpTuple in shapes: aheinecke@4970: geomType = importer.walkOverShapes(shpTuple) aheinecke@4970: try: aheinecke@4970: if geomType is not None: aheinecke@4970: num = types[geomType] aheinecke@4970: types[geomType] = num+1 aheinecke@4970: except: aheinecke@4970: types[geomType] = 1 ingo@2798: aheinecke@4975: for key in types: aheinecke@4975: DEBUG("%i x geometry type %s" % (types[key], key)) ingo@2798: aheinecke@4975: if not config.skip_dgm: aheinecke@4975: dgmfilename = os.path.join( aheinecke@4975: config.directory, "..", "DGMs.csv") aheinecke@4989: if not os.access(dgmfilename, os.R_OK) or not \ aheinecke@4989: os.path.isfile(dgmfilename): aheinecke@4978: INFO("Could not find or access DGM file: %s \n" aheinecke@4975: "Skipping DGM import." % dgmfilename) aheinecke@4978: else: aheinecke@4975: INFO("Inserting DGM meta information in 'dem' table.") aheinecke@4975: insertRiverDgm(dbconn_raw, dgmfilename, river_name, config.dry_run) ingo@3654: ingo@3654: if __name__ == '__main__': aheinecke@4872: main()