aheinecke@4872: try: aheinecke@4872: from osgeo import ogr aheinecke@4872: except ImportErrror: aheinecke@4872: import ogr ingo@2853: ingo@3654: import utils, optparse ingo@3654: ingo@2853: from uesg import UESG ingo@2853: from axis import Axis ingo@2853: from km import KM ingo@2853: from lines import Line 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 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@4872: def getImporters(config, dbconn): ingo@2853: return [ aheinecke@4872: Axis(config, dbconn), aheinecke@4872: KM(config, dbconn), aheinecke@4872: CrosssectionTrack(config, dbconn), aheinecke@4872: Line(config, dbconn), aheinecke@4872: Fixpoint(config, dbconn), aheinecke@4872: Building(config, dbconn), aheinecke@4872: Floodplain(config, dbconn), aheinecke@4872: HydrBoundary(config, dbconn), aheinecke@4872: HydrBoundaryPoly(config, dbconn), aheinecke@4879: HWSLines(config, dbconn), aheinecke@4879: HWSPoints(config, dbconn), aheinecke@4872: GaugeLocation(config, dbconn), aheinecke@4872: Catchment(config, dbconn), aheinecke@4872: UESG(config, dbconn) 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") ingo@3654: parser.add_option("--river_id", type="int") 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_lines", 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") 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") aheinecke@4871: if config.river_id == None: ingo@3654: ERROR("No river id specified!") ingo@3654: 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_lines == 1 and isinstance(importer, Line): 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: 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@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: aheinecke@4872: importers = getImporters(config, dbconn) ingo@2798: types = {} ingo@2798: ingo@2853: for importer in importers: ingo@3654: if skip_importer(config, importer): ingo@3654: INFO("Skip import of '%s'" % importer.getName()) ingo@3654: continue ingo@3654: ingo@3654: INFO("Start import of '%s'" % importer.getName()) ingo@3654: ingo@3654: shapes = utils.findShapefiles(importer.getPath(config.directory)) ingo@3655: DEBUG("Found %i Shapefiles" % len(shapes)) ingo@2853: ingo@2853: for shpTuple in shapes: ingo@2853: geomType = importer.walkOverShapes(shpTuple) ingo@2853: try: ingo@3655: if geomType is not None: ingo@3655: num = types[geomType] ingo@3655: types[geomType] = num+1 ingo@2853: except: ingo@2853: types[geomType] = 1 ingo@2798: ingo@2798: for key in types: ingo@3654: DEBUG("%i x geometry type %s" % (types[key], key)) ingo@2798: ingo@3654: ingo@3654: if __name__ == '__main__': aheinecke@4872: main()