Skip to content

Commit

Permalink
refactoring evacuation, refs #12
Browse files Browse the repository at this point in the history
git-svn-id: file:///home/behr_mi/git/sumo_synched/trunk@19055 afbd958f-9f77-42d5-a016-97a22340ccf4
  • Loading branch information
behrisch committed Oct 8, 2015
1 parent 6e26340 commit 553dc46
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 62 deletions.
46 changes: 22 additions & 24 deletions sumo/tools/projects/evacuation/evacuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,10 @@

def mergePopulationData(populationFile, regionFile, mergedFile):
csvReader = csv.reader(open(populationFile), delimiter=',' , quotechar ='"')
InhabitantArray = []
for row in csvReader:
InhabitantArray.append(row);

Polygon = ET.ElementTree(file=regionFile)
root = Polygon.getroot()

counter = 0
fortschritt = 0
inhabDict = {}
for entry in InhabitantArray[3:]:
for entry in csvReader:
if csvReader.line_num <= 3:
continue
while(len(entry[0]) < 2 ):
entry[0] = '0' + entry[0]
while(len(entry[2]) < 2 ):
Expand All @@ -65,21 +58,21 @@ def mergePopulationData(populationFile, regionFile, mergedFile):
entry[4] = '0' + entry[4]
inhabDict["".join(entry[:5])] = str(entry[6]).replace(' ','')

root = ET.ElementTree(file=regionFile).getroot()
for parents in root.findall("./*"):
for elem in parents.findall("param[7]"):
RSValue = str(elem.attrib)[11:23]
inhabitants = SubElement(parents, 'param')
if RSValue in inhabDict:
inhabitants.clear()
inhabitants.attrib = { 'key':"INHABITANTS", 'value': inhabDict[RSValue] }
counter = counter + 1
outstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ")
outstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ")
with open(mergedFile, 'w') as out:
out.write(outstr.encode('utf-8'))

def extract():
PopulationXML = ET.ElementTree(file=("PopulationPolygons.xml"))
EvacuationXML = ET.ElementTree(file=("ConvertedEvaSite.poly.xml"))
def extract(mergedPopulationFile, mappedSiteFile, intersectionFile):
PopulationXML = ET.ElementTree(file=mergedPopulationFile)
EvacuationXML = ET.ElementTree(file=mappedSiteFile)
PopRoot = PopulationXML.getroot()
EvaRoot = EvacuationXML.getroot()
Schnittmenge = []
Expand Down Expand Up @@ -114,7 +107,7 @@ def extract():
poly.attrib ={"id":identity , "shape":OutExterior, "inhabitants":G[1] }
i += 1
outstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent = " ")
with open("Schnittflachen.xml", 'w') as out:
with open(intersectionFile, 'w') as out:
out.write(outstr)

def buildEvaSite(inputFile, siteFile):
Expand All @@ -138,31 +131,36 @@ def buildEvaSite(inputFile, siteFile):


print("building Evacuation Site")
prefix = "evacuationArea"
siteFile = "site.poly.xml"
mappedSiteFile = "mappedSite.poly.xml"
mergedPopulationFile = "population.poly.xml"
intersectionFile = "intersections.poly.xml"
if len(sys.argv) > 1:
buildEvaSite(sys.argv[1], siteFile)
else:
buildEvaSite("inputLocations.poi.xml", siteFile)
print("osm Get")
osmGet.get(["-x", siteFile])
print("osm Build")
osmOptions = ['-f', 'osm_bbox.osm.xml', '-p', 'testserveroutput', '--vehicle-classes', 'road',
osmOptions = ['-f', 'osm_bbox.osm.xml', '-p', prefix, '--vehicle-classes', 'road',
'-m', os.path.join(SUMO_HOME, 'data', 'typemap', 'osmPolyconvert.typ.xml')]
osmBuild.build(osmOptions)
print("polyconvert")
sys.stdout.flush()
subprocess.call([sumolib.checkBinary('polyconvert'), '-n', 'testserveroutput.net.xml', '--xml-files', siteFile, '-o', 'ConvertedEvaSite.poly.xml'])
subprocess.call([sumolib.checkBinary('polyconvert'), '-n', '%s.net.xml' % prefix, '--xml-files', siteFile, '-o', mappedSiteFile])
print("merging")
mergePopulationData("population.csv", 'regions.poly.xml', "PopulationPolygons.xml")
mergePopulationData("population.csv", 'regions.poly.xml', mergedPopulationFile)
print("extracting population data")
extract()
extract(mergedPopulationFile, mappedSiteFile, intersectionFile)
print("generating traffic")
generateTraffic.generate()
generateTraffic.generate('%s.net.xml' % prefix, mappedSiteFile, intersectionFile, '%s.rou.xml' % prefix)
print("calling sumo")
sys.stdout.flush()
sumo= sumolib.checkBinary('sumo')
sumoOptions = [sumo, '-n', "testserveroutput.net.xml" , '-a' , "testserveroutput.poly.xml,inputLocations.poi.xml,ConvertedEvaSite.poly.xml", '-r' , 'TRAFFIC.xml', '--ignore-route-errors', '--save-configuration', 'sumo.config']
sumoOptions = [sumo, '-n', "%s.net.xml" % prefix, '-a' , "%s.poly.xml,inputLocations.poi.xml,%s" % (prefix, mappedSiteFile),
'-r' , '%s.rou.xml' % prefix, '--ignore-route-errors', '--no-step-log', '--save-configuration', '%s.sumocfg' % prefix]
subprocess.call(sumoOptions)
subprocess.call([sumo ,'-c' , 'sumo.config'])
#subprocess.call([sumolib.checkBinary('sumo-gui') ,'-c' , 'sumo.config'])
subprocess.call([sumo, '%s.sumocfg' % prefix])
#subprocess.call([sumolib.checkBinary('sumo-gui'), '%s.sumocfg' % prefix])
print("done")
59 changes: 21 additions & 38 deletions sumo/tools/projects/evacuation/generateTraffic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
SUMO_HOME = os.environ.get("SUMO_HOME", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", ".."))
sys.path.append(os.path.join(SUMO_HOME, "tools"))


import edgesInDistricts as EID
import subprocess
import sumolib
def generate():


def generate(netFile, mappedSiteFile, intersectionFile, tripFile):
#EdgesInDistricts
#evakuierungsbereich
options = OptionParser()
Expand All @@ -37,55 +38,37 @@ def generate():
options.vclass = None
options.assign_from = True
options.verbose = False
options.net_file = "testserveroutput.net.xml"
options.taz_files = "Schnittflachen.xml"
options.output = "EdgesInSchnittflachen.taz.xml"
options.weighted = False
nets = options.net_file.split(",")
reader = EID.DistrictEdgeComputer(sumolib.net.readNet(nets[0]))
tazFiles = nets + options.taz_files.split(",")
net = sumolib.net.readNet(netFile)
reader = EID.DistrictEdgeComputer(net)
polyReader = sumolib.shapes.polygon.PolygonReader(True)
for tf in tazFiles:
parse(tf, polyReader)
parse(intersectionFile, polyReader)
reader.computeWithin(polyReader.getPolygons(), options)
reader.writeResults(options.output, options.weighted)
reader.writeResults("edgesInIntersections.taz.xml", False)

#Evakuierungsziele
options.net_file = "testserveroutput.net.xml"
options.taz_files = "ConvertedEvaSite.poly.xml"
options.output = "EvacuationsiteEdges.taz.xml"
options.weighted = False
nets = options.net_file.split(",")
reader = EID.DistrictEdgeComputer(sumolib.net.readNet(nets[0]))
tazFiles = nets + options.taz_files.split(",")
reader = EID.DistrictEdgeComputer(net)
polyReader = sumolib.shapes.polygon.PolygonReader(True)
for tf in tazFiles:
parse(tf, polyReader)
parse(mappedSiteFile, polyReader)
reader.computeWithin(polyReader.getPolygons(), options)
print(polyReader.getPolygons())
reader.writeResults(options.output, options.weighted)
reader.writeResults("evacuationsiteEdges.taz.xml", False)
print("EdgesInDistricts - done")

#O/D Matrix(V-fortmat)
outString = '$OR \n* From-Tome To-Time \n1.00 2.00\n* Factor \n1.00\n'
#O/D Matrix
import xml.etree.cElementTree as ET
Districts = ET.ElementTree(file='Schnittflachen.xml')
Districts = ET.ElementTree(file=intersectionFile)
root = Districts.getroot()
EVA = ET.ElementTree(file='EvacuationsiteEdges.taz.xml')
EVA = ET.ElementTree(file='evacuationsiteEdges.taz.xml')
EV = EVA.getroot()
EV.remove(EV[0])
for elem in root.findall("./poly"):
for ESite in EV.findall("./*"):
CarAmount = str(int(float(elem.attrib["inhabitants"])/float(3*(len(EV.findall("./*"))-1))))
outString += elem.attrib["id"] + '\t' + ESite.attrib["id"] + '\t' + CarAmount +'\n'
ODM = open('ODMatrix.xml','w')
ODM.write(outString)
ODM.close()
EV.remove(EV[0])
with open('ODMatrix.fma','w') as odm:
odm.write('$OR \n* From-Tome To-Time \n1.00 2.00\n* Factor \n1.00\n')
for elem in root.findall("./poly"):
for ESite in EV.findall("./*"):
CarAmount = str(int(float(elem.attrib["inhabitants"])/float(3*(len(EV.findall("./*"))-1))))
odm.write(elem.attrib["id"] + '\t' + ESite.attrib["id"] + '\t' + CarAmount + '\n')
print("OD Matrix - done")

#OD2TRIPS
od2t = sumolib.checkBinary('od2trips')
od2tOptions = [od2t ,'-d','ODMATRIX.xml', '-n', 'EdgesInSchnittflachen.taz.xml,EvacuationsiteEdges.taz.xml', '--output-prefix', 'TRAFFIC' , '-o' , '.xml']
od2tOptions = [od2t, '--no-step-log', '-d', odm.name, '-n', 'edgesInIntersections.taz.xml,evacuationsiteEdges.taz.xml', '-o', tripFile]
subprocess.call(od2tOptions)
if __name__ == "__main__":
generate()

0 comments on commit 553dc46

Please sign in to comment.