def main(): #-----------Setting up and unsing option parser----------------------- parser=OptionParser(usage= usage, version=version) logger=logging.getLogger("exportEmisGrids.py") parser.add_option("-l", "--loglevel", action="store",dest="loglevel",default=2, help="Sets the loglevel (0-3 where 3=full logging)") parser.add_option("-u", "--user", action="store", dest="user", default=None, help="Specify user manually") parser.add_option("-e", "--edb", action="store", dest="edb", default=None, help="Name of target edb") parser.add_option("-t", "--template", action="store",dest="cf",default=None, help="Generate default controlfile") parser.add_option("-f", "--force", action="store_true",dest="force",default=False, help="To start the process without confirming the domain") parser.add_option("--aggregate", action="store_true",dest="aggregate",default=False, help="Aggregate twin-links for correct use of street canyon models") parser.add_option("-a", "--attributeFilter", action="store",dest="attributeFilter",default=None, help="Set to filter out roads with the specified attribute value, attribute field name is set in controlfile") parser.add_option("-r", "--region", action="store",dest="region",default=None, help="Determines the percentage of spiked tyres, possible region names are found in studdedTyreTable") parser.add_option("-y", "--year", action="store",dest="outYear",default=None, help="Year from which traffic update factors and vehicle compsition will be taken") parser.add_option("-o","--outfile", action="store",dest="outfile",default=None, help="Output road ascii file") parser.add_option("-g","--geofilter", action="store",dest="geoFilter",default=None, help="Filter out roads within polygons with field value matching the geoFilter, specify shapefile in controlfile") (options, args) = parser.parse_args() #------------Setting up logging capabilities ----------- rootLogger=logger2.RootLogger(int(options.loglevel)) logger=rootLogger.getLogger(sys.argv[0]) if options.cf!=None: #Checks if the file already exists, prompt the user if overwrite is wanted, create file controlFile.generateCf(path.abspath(options.cf),controlFileTemplate) logger.info("Wrote default controlfile") sys.exit() if len(args)!=1: parser.error("Incorrect number of arguments") if options.edb ==None: parser.error("Need to specify edb using flag -e") if options.user ==None: parser.error("Need to specify user using flag -u") if options.region==None: parser.error("Need to specify region") if options.outYear is None: parser.error("Need to specify output year by -y <year>") if options.attributeFilter is not None: attributeFilter=options.attributeFilter else: attributeFilter=None #Get vagverksregion (used to select percentage of studded tyres) region=options.region domainName=os.environ["AVDBNAME"] dmn = pyDomain.domain(domainName) if not options.force: answer=raw_input("Chosen dbase is: "+domainName+",continue(y/n)?") if answer=="y": dmn=pyDomain.domain() else: sys.exit(1) if not dmn.edbExistForUser(options.edb,options.user): logger.error("Edb "+options.edb+" does not exist for user "+ options.user+" in domain "+domainName) sys.exit(1) #Creating edb andedb.rsrc objects edb=pyEdb.edb(dmn.name,options.user,options.edb) #Creating a roaddb object roaddb=pyRoaddb.pyRoaddb(dmn,options.user,edb.name) #Creating a control-file object (simple parser) cf=controlFile.controlFile(fileName=path.abspath(args[0]),codec="latin6") #Retrieving data from control file shapeFilePath = cf.findExistingPath("shapeFile:") artemisToSimairTablePath = cf.findExistingPath("artemisToSimairTable:") nvdbToArtemisTablePath = cf.findExistingPath("nvdbToArtemisTable:") updateFactorTablePath = cf.findExistingPath("updateFactorTable:") vehCompTablePath = cf.findExistingPath("vehicleCompositionTable:") studdedTyreTablePath= cf.findExistingPath("studdedTyreTable:") attributeFilterFieldName=cf.findString("attributeFilterFieldName:") #Loads driver to read shape-files using ogr-library driver = ogr.GetDriverByName('ESRI Shapefile') #If option for geoFilter is used, the polygon defining the boundaries #of the area to be filtered is read from geoFilterShapeFile if options.geoFilter is not None: gfShapeFilePath=cf.findExistingPath("geoFilterShapeFile:") gfFieldName=cf.findString("geoFilterFieldName:") gfShapeFile = driver.Open(str(gfShapeFilePath), update=0) if gfShapeFile is None: logger.error("Could not open data source: " +gfShapeFilePath) sys.exit(1) gfLayer = gfShapeFile.GetLayer() logger.info("Found %i features in geocode shapefile" %gfLayer.GetFeatureCount()) geoFilterPolys=[] gfFeature = gfLayer.GetNextFeature() while gfFeature: geocode=gfFeature.GetFieldAsString(str(gfFieldName)) if geocode==options.geoFilter: geoFilterPoly=gfFeature.GetGeometryRef() geoFilterPolys.append(geoFilterPoly) gfFeature = gfLayer.GetNextFeature() if len(geoFilterPolys)==0: logger.error("Could not find any polygon with field value matching the specified geoFilter in shapeFile: %s, field: %s" %(gfShapeFilePath,gfFieldName)) sys.exit(1) inYear=cf.findInt("inYear:") outYear=int(options.outYear) #Creating lookup table for the ARTEMIS-code nvdbToArtemisTable=dataTable.DataTable() nvdbToArtemisTable.keys=["vaghallare","tatort","hastighet","vagklass","vagtyp"] nvdbToArtemisTable.read(nvdbToArtemisTablePath) missingTSTable=dataTable.DataTable( desc=[ {"id":"vaghallare","type":str}, {"id":"tatort","type":str}, {"id":"hastighet","type":str}, {"id":"vagklass","type":str}, {"id":"vagtyp","type":str}, {"id":"antal","type":int}], keys=["vaghallare","tatort","hastighet","vagklass","vagtyp"]) #If input and output year is the same, no update is needed made for flow if inYear!=outYear: #Creating lookup table for the updateFactors updateFactorTable=dataTable.DataTable() updateFactorTable.read(updateFactorTablePath) try: #Set columns used as unique row identifiers, raise error if they do not exist updateFactorTable.setKeys(["start_year","vehicle","roadtype"]) except DataTableException,msg: logger.error("Could not find column header in updateFactorTable:"+msg) sys.exit(1) #Filters out not needed rows from the updateFactorTable (makes lookups faster) updateFactorTable=updateFactorTable.filtered({"start_year":str(inYear)}) #year no longer necessary as a row identifier updateFactorTable.setKeys(["vehicle","roadtype"]) if unicode(outYear) not in updateFactorTable.listIds() and inYear!=outYear: logger.error("No update factors to year %i found in %s" %(outYear,updateFactorTablePath)) sys.exit(1)
def main(): #-----------Setting up and unsing option parser----------------------- parser=OptionParser(usage= usage, version=version) logger=logging.getLogger("exportEmisGrids.py") parser.add_option("-l", "--loglevel", action="store",dest="loglevel",default=2, help="Sets the loglevel (0-3 where 3=full logging)") parser.add_option("-u", "--user", action="store", dest="user", default=None, help="Specify user manually") parser.add_option("-e", "--edb", action="store", dest="edb", default=None, help="Name of target edb") parser.add_option("-t", "--template", action="store",dest="cf",default=None, help="Generate default controlfile") parser.add_option("-f", "--force", action="store_true",dest="force",default=False, help="To start the process without confirming the domain") (options, args) = parser.parse_args() #------------Setting up logging capabilities ----------- rootLogger=logger2.RootLogger(int(options.loglevel)) logger=rootLogger.getLogger(sys.argv[0]) if options.cf!=None: generateCf(path.abspath(options.cf)) logger.info("Wrote default controlfile") sys.exit() if len(args)!=0: parser.error("Incorrect number of arguments") if options.edb ==None: parser.error("Need to specify edb using flag -e") if options.user ==None: parser.error("Need to specify user using flag -u") domainName=os.environ["AVDBNAME"] dmn = pyDomain.domain(domainName) if not options.force: answer=raw_input("Chosen dbase is: "+domainName+",continue(y/n)?") if answer=="y": dmn=pyDomain.domain() else: sys.exit("Interrupted by user") if not dmn.edbExistForUser(options.edb,options.user): logger.error("Edb "+options.edb+" does not exist for user "+ options.user+" in domain "+domainName) sys.exit() #---Creating edb object------------------ edb=pyEdb.edb(dmn.name,options.user,options.edb)