Beispiel #1
0
def main():
    
    outYear = 2008
    user = sys.argv[1]
    startedb =sys.argv[2]
    outfilename = user+startedb+"UpdateVehComp.out"
    domainName=os.environ["AVDBNAME"]
    dmn = pyDomain.domain(domainName)

    ### Create pyroaddb and read from ascii!
    roaddb=pyRoaddb.pyRoaddb(dmn,user,startedb)
    roaddb.read(simair=True)
    
    
    ### Load veh composition table
    vehCompTablePath = "/usr/airviro/data/sim2/prod/Artemis/Uttag_2011/sammanstallning_fordonssammansattning_20110817.txt"
    vehCompTable = dataTable.DataTable()
    vehCompTable.read(vehCompTablePath)
    try:
    #Set columns used as unique row identifiers, raise error if they do not exist
        vehCompTable.setKeys(["Year","Vehicle","Tatort"])
        for fuel in ["bensin","ethanol","diesel","CNG","Totalt"]:
            vehCompTable.convertCol(fuel,float)

    except DataTableException,msg:
        logger.error("Could not find column header in vehCompTable:"+msg)
        sys.exit(1)
Beispiel #2
0
def main():
   
    ### Some settings since I'm too lazy to write an optionparser today...
    inYear = 2009 
    outYear = 2020
    user = sys.argv[1]
    startedb =sys.argv[2]
    region = sys.argv[3]
    outfilename = user+startedb+"UpdateVehComp.out"
    domainName=os.environ["AVDBNAME"]
    dmn = pyDomain.domain(domainName)
    updateADT = False
    updateTyres = False

    ### Create pyroaddb and read from ascii!
    roaddb=pyRoaddb.pyRoaddb(dmn,user,startedb)
    roaddb.read(simair=True)

    ### Table for vehicle (ADT) update
    updateFactorTablePath = "/usr/airviro/data/sim2/prod/Artemis/Uttag_2011/inputTables/TF-Matris_93-10_sammanstallning.txt"    
    ### Table for studded tyres 
    studdedTyreTablePath = "/usr/airviro/data/sim2/prod/Artemis/Uttag_2011/inputTables/studdedTyres_2011-08-20.txt"
 
    ### Load veh composition table
    #vehCompTablePath = "/usr/airviro/data/sim2/prod/Artemis/Uttag_2011/sammanstallning_fordonssammansattning_20110817.txt"
    vehCompTablePath = "/usr/airviro/data/sim2/prod/Artemis/Uttag_2011/inputTables/sammanstallning_fordonssammansattning_20110817.txt"
    vehCompTable = dataTable.DataTable()
    vehCompTable.read(vehCompTablePath)
    try:
    #Set columns used as unique row identifiers, raise error if they do not exist
        vehCompTable.setKeys(["Year","Vehicle","Tatort"])
        for fuel in ["bensin","ethanol","diesel","CNG","Totalt"]:
            vehCompTable.convertCol(fuel,float)

    except DataTableException,msg:
        logger.error("Could not find column header in vehCompTable:"+msg)
        sys.exit(1)
Beispiel #3
0
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)
Beispiel #4
0
        optparser.error("**** ERROR: need to provide user, edb and city code.")

########################################################################

if opts.domain is not None:
    dmn = pyDomain.domain( opts.domain  )
else:
    dmn = pyDomain.domain( os.environ["AVDBNAME"]  )

if opts.outfile is not None:
    outname = opts.outfile
else:
    outname = "roaddb_" + opts.user + "_" + opts.edb + "_" + opts.city + ".out"


roaddb=pyRoaddb.pyRoaddb(dmn,opts.user,opts.edb)
print "Reading existing road database...",
roaddb.read(simair=True)
print "       ...found " + str( len(roaddb.sources) ) + " roads."
outRoaddb=pyRoaddb.pyRoaddb(dmn,"dummy","dummy")

dbaspath=os.environ["DBAS_PATH"]
cmd="getrsrc "+dbaspath+"wnd/modell.par \"area."+opts.city+".limit\""
returnCode,outMsg,errMsg=utilities.execute(cmd)
if returnCode!=30:
    raise IOError,"Could not run: "+cmd+"\nstdout:"+outMsg+"\nstderr:"+errMsg

print "Bounding box is: " + errMsg
x1 = int( errMsg.split()[0] )
x2 = int( errMsg.split()[1] )
y1 = int( errMsg.split()[2] )
Beispiel #5
0
### Useful in SIMAIR since the customers never remember where an EDB came from...

from os import path
import os, logging, sys

from pyAirviro_dev.pyEdb import pyRoaddb, pyRoad, pyDomain, pyEdb

if len(sys.argv) < 4:
    sys.exit("Usage: " + str(sys.argv[0]) + " <user> <edb> <orgEDB> ")

user = sys.argv[1]
startedb = sys.argv[2]
orgEDB = sys.argv[3]
outfilename = user+startedb+"_OrgEdbALOB.out"
domainName = os.environ["AVDBNAME"]
dmn = pyDomain.domain(domainName)

### Create pyroaddb and read from ascii!
roaddb = pyRoaddb.pyRoaddb(dmn,user,startedb)
roaddb.read(simair=True)


### Go through all roads, and add the ALOB to them
for road in roaddb.sources:
    road.setALOB("ORG_EDB",orgEDB)



### save updated EDB as a ascii file
roaddb.writeAscii(outfilename)