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
aheinecke@5006: import logging
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
aheinecke@5353: from jetties import Jetties
aheinecke@4975: from dgm import insertRiverDgm
aheinecke@5545: from floodmarks import Floodmark
ingo@2798: 
aheinecke@5006: logger = logging.getLogger("shpimporter")
ingo@2798: 
aheinecke@5219: os.environ["NLS_LANG"] = ".AL32UTF8"
aheinecke@5219: 
aheinecke@5006: def initialize_logging(level):
aheinecke@5006:     """Initializes the logging system"""
aheinecke@5006:     root = logging.getLogger()
aheinecke@5006:     root.setLevel(level)
aheinecke@5006:     hdlr = logging.StreamHandler()
aheinecke@5006:     fmt = logging.Formatter("%(levelname)s %(name)s: %(message)s")
aheinecke@5006:     hdlr.setFormatter(fmt)
aheinecke@5006:     root.addHandler(hdlr)
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:         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@5353:         Jetties(river_id, dbconn, dry_run),
aheinecke@5545:         Floodmark(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_kms", type="int")
ingo@3654:     parser.add_option("--skip_uesgs", type="int")
aheinecke@4975:     parser.add_option("--skip_dgm", type="int")
aheinecke@5353:     parser.add_option("--skip_jetties", type="int")
aheinecke@5545:     parser.add_option("--skip_floodmarks", type="int")
ingo@3654:     (config, args) = parser.parse_args()
ingo@2798: 
aheinecke@5006:     if config.verbose > 1:
aheinecke@5006:         initialize_logging(logging.DEBUG)
aheinecke@5006:     elif config.verbose == 1:
aheinecke@5006:         initialize_logging(logging.INFO)
aheinecke@5006:     else:
aheinecke@5006:         initialize_logging(logging.WARN)
aheinecke@5006: 
ingo@3654:     if config.directory == None:
aheinecke@5006:         logger.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@5006:             logger.error("No database host specified!")
aheinecke@4871:             raise Exception("Invalid config")
aheinecke@4871:         if not config.user:
aheinecke@5006:             logger.error("No databaser user specified!")
aheinecke@4871:             raise Exception("Invalid config")
aheinecke@4871:         if not config.password:
aheinecke@5006:             logger.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@5011:     elif config.skip_hws_points == 1 and isinstance(importer, HWSPoints) and \
aheinecke@5011:             not isinstance(importer, HWSLines):
ingo@3654:         return True
ingo@3654:     elif config.skip_gauge_locations == 1 and isinstance(importer, GaugeLocation):
ingo@3654:         return True
aheinecke@5353:     elif config.skip_jetties == 1 and isinstance(importer, Jetties):
aheinecke@5353:         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
aheinecke@5545:     elif config.skip_floodmarks == 1 and isinstance(importer, Floodmark):
aheinecke@5545:         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:
aheinecke@5006:         logger.error("Unable to read config from command line!")
ingo@3654:         return
ingo@3654: 
ingo@3655:     if config.dry_run > 0:
aheinecke@5006:         logger.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@5036:     oracle = False # Marker if oracle is used.
aheinecke@4970:     if 'OCI:' in connstr:
aheinecke@5036:         oracle = True
aheinecke@4970:         try:
aheinecke@4970:             import cx_Oracle as dbapi
aheinecke@4970:             raw_connstr=connstr.replace("OCI:", "")
aheinecke@5077:         except ImportError:
aheinecke@5006:             logger.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@5036:             return -1
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@5006:             logger.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@5036:             return -1
aheinecke@4970: 
aheinecke@4970:     dbconn_raw = dbapi.connect(raw_connstr)
aheinecke@4879:     dbconn = ogr.Open(connstr)
aheinecke@4879: 
aheinecke@4879:     if dbconn == None:
aheinecke@5006:         logger.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@5005:         for file in [os.path.join(config.directory, d) for d in \
aheinecke@5005:                 os.listdir(config.directory)]:
aheinecke@4970:             if os.path.isdir(file):
aheinecke@5005:                 directories.append(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@5005:             river_name = utils.getUTF8Path(
aheinecke@5005:                     os.path.basename(os.path.normpath(directory)))
aheinecke@4970:         else:
aheinecke@4970:             river_name = config.river_name
aheinecke@5036:         river_id = utils.getRiverId(dbconn_raw, river_name, oracle)
ingo@2853: 
aheinecke@4970:         if not river_id:
aheinecke@5221:             logger.info(u"Could not find river in database. Skipping: %s"
aheinecke@5221:                   % unicode(utils.getUTF8(river_name), "UTF-8"))
aheinecke@4970:             continue
aheinecke@4970:         else:
aheinecke@5219:             logger.info(u"Importing River: %s" % unicode(
aheinecke@5219:                        utils.getUTF8(river_name), "UTF-8"))
aheinecke@4970: 
aheinecke@4970:         for importer in getImporters(river_id, dbconn, config.dry_run):
aheinecke@4970:             if skip_importer(config, importer):
aheinecke@5006:                 logger.info("Skip import of '%s'" % importer.getName())
aheinecke@4970:                 continue
aheinecke@4970: 
aheinecke@5006:             logger.info("Start import of '%s'" % importer.getName())
aheinecke@4970: 
aheinecke@4970:             shapes = utils.findShapefiles(importer.getPath(config.directory))
aheinecke@5006:             logger.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@5006:             logger.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@5006:                 logger.info("Could not find or access DGM file: %s \n"
aheinecke@4975:                      "Skipping DGM import." % dgmfilename)
aheinecke@4978:             else:
aheinecke@5006:                 logger.info("Inserting DGM meta information in 'dem' table.")
aheinecke@5036:                 insertRiverDgm(dbconn_raw, dgmfilename, river_name,
aheinecke@5036:                         config.dry_run, oracle)
aheinecke@5012:         else:
aheinecke@5012:             logger.info("Skip import of DGM.")
ingo@3654: 
ingo@3654: if __name__ == '__main__':
aheinecke@4872:     main()