mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-21 19:16:30 +00:00
Refactor code
This commit is contained in:
parent
ceea79035d
commit
f50a11e3e8
@ -26,34 +26,34 @@ vehicle_classes = {
|
||||
|
||||
|
||||
def load_netconvert_template(osm_input, out_name):
|
||||
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg'))
|
||||
root = tree.getroot()
|
||||
netconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg'))
|
||||
root = netconfig.getroot()
|
||||
root.find('input/osm-files').set('value', osm_input)
|
||||
root.find('output/output-file').set('value', f'{out_name}.net.xml')
|
||||
root.find('report/log').set('value', f'{out_name}.netconvert.log')
|
||||
return tree
|
||||
return netconfig
|
||||
|
||||
|
||||
def load_polyconvert_template(osm_file, type_file, scenario_name):
|
||||
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg'))
|
||||
root = tree.getroot()
|
||||
polyconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg'))
|
||||
root = polyconfig.getroot()
|
||||
root.find('input/osm-files').set('value', osm_file)
|
||||
root.find('input/net-file').set('value', f'{scenario_name}.net.xml')
|
||||
root.find('input/type-file').set('value', type_file)
|
||||
root.find('output/output-file').set('value', f'{scenario_name}.poly.xml')
|
||||
root.find('report/log').set('value', f'{scenario_name}.polyconvert.log')
|
||||
return tree
|
||||
return polyconfig
|
||||
|
||||
|
||||
def load_sumoconfig_template(simulation_name, routefiles=None):
|
||||
def load_sumoconfig_template(simulation_name, routefiles=()):
|
||||
routefiles = routefiles or (f'{simulation_name}.rou.xml',)
|
||||
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
|
||||
root = tree.getroot()
|
||||
sumoconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
|
||||
root = sumoconfig.getroot()
|
||||
root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
|
||||
root.find('input/route-files').set('value', ','.join(f for f in routefiles))
|
||||
root.find('input/route-files').set('value', ','.join(routefiles))
|
||||
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
|
||||
root.find('report/log').set('value', f'{simulation_name}.log')
|
||||
return tree
|
||||
return sumoconfig
|
||||
|
||||
|
||||
def generate_scenario(osm_file, out_path, scenario_name):
|
||||
@ -61,12 +61,14 @@ def generate_scenario(osm_file, out_path, scenario_name):
|
||||
poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
# Generate POLYCONVERT and NETCONVERT configuration
|
||||
netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg')
|
||||
polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg')
|
||||
net_template.write(netconfig)
|
||||
poly_template.write(polyconfig)
|
||||
# Copy typemaps to tempdir
|
||||
shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
|
||||
# Call POLYCONVERT and NETCONVERT
|
||||
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
|
||||
netconvertcmd = ['netconvert', '-c', netconfig]
|
||||
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
@ -76,19 +78,20 @@ def generate_scenario(osm_file, out_path, scenario_name):
|
||||
shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns)
|
||||
|
||||
|
||||
def generate_mobility(path, name):
|
||||
netfile = os.path.join(path, f'{name}.net.xml')
|
||||
output = os.path.join(path, f'{name}.trips.xml')
|
||||
def generate_mobility(out_path, name):
|
||||
netfile = f'{name}.net.xml'
|
||||
netpath = os.path.join(out_path, netfile)
|
||||
output = os.path.join(out_path, f'{name}.trips.xml')
|
||||
routefiles = []
|
||||
end_time = 200
|
||||
classes = ('passenger', 'bus')
|
||||
for veh_class in classes:
|
||||
# simname.bus.rou.xml, simname.passenger.rou.xml, ...
|
||||
routefile = f'{name}.{veh_class}.rou.xml'
|
||||
routepath = os.path.join(path, routefile)
|
||||
routepath = os.path.join(out_path, routefile)
|
||||
routefiles.append(routefile)
|
||||
options = {
|
||||
'--net-file': netfile,
|
||||
'--net-file': netpath,
|
||||
'--output-trip-file': output,
|
||||
'--route-file': routepath,
|
||||
'-e': end_time
|
||||
@ -104,7 +107,7 @@ def generate_random_trips(flags, options):
|
||||
|
||||
|
||||
def generate_sumo_configuration(routefiles, path, scenario_name):
|
||||
sumo_template = load_sumoconfig_template(routefiles, scenario_name)
|
||||
sumo_template = load_sumoconfig_template(scenario_name, routefiles=routefiles)
|
||||
sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg'))
|
||||
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
<input>
|
||||
<net-file value="simul.net.xml"/>
|
||||
<osm-files value="simul.raw.osm"/>
|
||||
<!--<osm.keep-full-type value="true"/>-->
|
||||
<type-file value="typemap/osmPolyconvert.typ.xml"/>
|
||||
</input>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user