ingo@2798: 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 ingo@2862: from hws import HWS 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: ingo@3654: def getImporters(config): ingo@2853: return [ ingo@3654: Axis(config), ingo@3654: KM(config), ingo@3654: CrosssectionTrack(config), ingo@3654: Line(config), ingo@3654: Fixpoint(config), ingo@3654: Building(config), ingo@3654: Floodplain(config), ingo@3654: HydrBoundary(config), ingo@3654: HydrBoundaryPoly(config), ingo@3654: HWS(config), ingo@3654: GaugeLocation(config), ingo@3654: Catchment(config), ingo@3654: UESG(config) 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) 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") ingo@3654: parser.add_option("--skip_hws", 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") ingo@3654: elif config.host == None: ingo@3654: ERROR("No database host specified!") ingo@3654: raise Exception("Invalid config") ingo@3654: elif config.user == None: ingo@3654: ERROR("No databaser user specified!") ingo@3654: raise Exception("Invalid config") ingo@3654: elif config.password == None: ingo@3654: ERROR("No password specified!") ingo@3654: raise Exception("Invalid config") ingo@3654: elif 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 ingo@3654: elif config.skip_hws == 1 and isinstance(importer, HWS): 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: ingo@3654: def parse(): ingo@3654: config=None ingo@3654: try: ingo@3654: config = getConfig() ingo@3654: except: ingo@3654: return 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: ingo@3654: importers = getImporters(config) 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__': ingo@3654: parse()