1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-21 19:16:30 +00:00

Make polygons generation optional

This commit is contained in:
Thibaud Gasser 2018-12-14 19:43:13 +01:00
parent 2c38eb051e
commit 6dec467e52
2 changed files with 50 additions and 26 deletions

View File

@ -107,39 +107,49 @@ def load_polyconvert_template(osm_file, type_file, scenario_name):
return polyconfig return polyconfig
def load_sumoconfig_template(simulation_name, routefiles=()): def load_sumoconfig_template(simulation_name, routefiles=(), generate_polygons=False):
routefiles = routefiles or (f'{simulation_name}.rou.xml',) routefiles = routefiles or (f'{simulation_name}.rou.xml',)
sumoconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg')) sumoconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
root = sumoconfig.getroot() root = sumoconfig.getroot()
root.find('input/net-file').set('value', f'{simulation_name}.net.xml') root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
root.find('input/route-files').set('value', ','.join(routefiles)) root.find('input/route-files').set('value', ','.join(routefiles))
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml') if generate_polygons:
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
root.find('report/log').set('value', f'{simulation_name}.log') root.find('report/log').set('value', f'{simulation_name}.log')
return sumoconfig return sumoconfig
def generate_scenario(osm_file, out_path, scenario_name): def generate_scenario(osm_file, out_path, scenario_name, generate_polygons=False):
net_template = load_netconvert_template(osm_file, scenario_name) net_template = load_netconvert_template(osm_file, scenario_name)
poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name)
with tempfile.TemporaryDirectory() as tmpdirname: with tempfile.TemporaryDirectory() as tmpdirname:
# Generate POLYCONVERT and NETCONVERT configuration # Generate NETCONVERT configuration
netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg') netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg')
polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg')
net_template.write(netconfig) net_template.write(netconfig)
poly_template.write(polyconfig)
# Copy typemaps to tempdir # Copy typemaps to tempdir
shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap')) shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
# Call POLYCONVERT and NETCONVERT # Call NETCONVERT
polyconvert_cmd = ['polyconvert', '-c', polyconfig] print("Generate network...")
netconvertcmd = ['netconvert', '-c', netconfig] netconvertcmd = ['netconvert', '-c', netconfig]
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(polyconvert_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) # Optionaly generate polygons
if generate_polygons:
generate_polygons_(osm_file, scenario_name, tmpdirname)
# Move files to destination # Move files to destination
ignore_patterns = shutil.ignore_patterns('*.polycfg', '*.netcfg', 'typemap') ignore_patterns = shutil.ignore_patterns('*.polycfg', '*.netcfg', 'typemap')
shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns) shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns)
def generate_polygons_(osm_file, scenario_name, dest):
polyconfig = os.path.join(dest, f'{scenario_name}.polycfg')
poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name)
poly_template.write(polyconfig)
# Call POLYCONVERT
print('Generate polygons...')
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
subprocess.run(polyconvert_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def generate_mobility(out_path, name, vclasses): def generate_mobility(out_path, name, vclasses):
netfile = f'{name}.net.xml' netfile = f'{name}.net.xml'
netpath = os.path.join(out_path, netfile) netpath = os.path.join(out_path, netfile)
@ -156,17 +166,19 @@ def generate_mobility(out_path, name, vclasses):
return routefiles return routefiles
def generate_sumo_configuration(routefiles, path, scenario_name): def generate_sumo_configuration(routefiles, path, scenario_name, generate_polygons=False):
sumo_template = load_sumoconfig_template(scenario_name, routefiles=routefiles) sumo_template = load_sumoconfig_template(scenario_name, routefiles=routefiles, generate_polygons=False)
sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg')) sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg'))
def generate_all(osm_file, path, simulation_name, vclasses): def generate_all(args):
simulation_dir = os.path.join(path, simulation_name) simulation_name = args.name
simulation_dir = os.path.join(args.path, simulation_name)
osm_file = args.osmfile
logs_dir = os.path.join(simulation_dir, 'log') logs_dir = os.path.join(simulation_dir, 'log')
generate_scenario(osm_file, simulation_dir, simulation_name) generate_scenario(osm_file, simulation_dir, simulation_name, args.generate_polygons)
routefiles = generate_mobility(simulation_dir, simulation_name, vclasses) routefiles = generate_mobility(simulation_dir, simulation_name, args.vclasses)
generate_sumo_configuration(routefiles, simulation_dir, simulation_name) generate_sumo_configuration(routefiles, simulation_dir, simulation_name, args.generate_polygons)
# Move all logs to logdir # Move all logs to logdir
move_logs(simulation_dir, logs_dir) move_logs(simulation_dir, logs_dir)
@ -185,20 +197,21 @@ def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('osmfile', help='Path to the .osm file to convert to a SUMO simulation') parser.add_argument('osmfile', help='Path to the .osm file to convert to a SUMO simulation')
parser.add_argument('--path', help='Where to generate the files') parser.add_argument('--path', help='Where to generate the files')
parser.add_argument('--name', required=True, help='Name of the scenario to generate') parser.add_argument('--name', required=True, help='Name of the SUMO scenario to generate')
parser.add_argument('--generate-polygons', default=False)
parser.add_argument('--vclass', dest='vclasses', action=StoreDictKeyPair, parser.add_argument('--vclass', dest='vclasses', action=StoreDictKeyPair,
nargs="+", metavar="VCLASS=DENSITY", nargs="+", metavar="VCLASS=DENSITY",
help='Generate this vclass with given density, in pair form vclass=density. The density is ' help='Generate this vclass with given density, in pair form vclass=density. The density is '
'given in vehicles per hour per kilometer.') 'given in vehicles per hour per kilometer.')
args = parser.parse_args() options = parser.parse_args()
# If no vehicle classes are specified, use 'passenger' as a default # If no vehicle classes are specified, use 'passenger' as a default
vclasses = args.vclasses or ('passenger',) vclasses = options.vclasses or ('passenger',)
# FIXME Delete simul_dir if it already exists # FIXME Delete simul_dir if it already exists
simul_dir = os.path.join(args.path, args.name) simul_dir = os.path.join(options.path, options.name)
if os.path.isdir(simul_dir): if os.path.isdir(simul_dir):
input(f'{simul_dir} already exists ! Press Enter to overwrite...') input(f'{simul_dir} already exists ! Press Enter to overwrite...')
shutil.rmtree(simul_dir) shutil.rmtree(simul_dir)
generate_all(args.osmfile, args.path, args.name, vclasses) generate_all(options)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -8,6 +8,7 @@ from configurator import configurator
# Absolute path of the directory the script is in # Absolute path of the directory the script is in
SCRIPTDIR = os.path.dirname(__file__) SCRIPTDIR = os.path.dirname(__file__)
class TemplateTests(unittest.TestCase): class TemplateTests(unittest.TestCase):
def setUp(self): def setUp(self):
self.sim_name = 'test_simulation' self.sim_name = 'test_simulation'
@ -24,15 +25,20 @@ class TemplateTests(unittest.TestCase):
tree = configurator.load_sumoconfig_template(self.sim_name) tree = configurator.load_sumoconfig_template(self.sim_name)
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml') self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
self.assertEqual(tree.find('input/route-files').get('value'), f'{self.sim_name}.rou.xml') self.assertEqual(tree.find('input/route-files').get('value'), f'{self.sim_name}.rou.xml')
self.assertEqual(tree.find('input/additional-files').get('value'), f'{self.sim_name}.poly.xml')
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log') self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
def test_load_sumoconfig_template_with_polygons(self):
tree = configurator.load_sumoconfig_template(self.sim_name, generate_polygons=True)
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
self.assertEqual(tree.find('input/route-files').get('value'), f'{self.sim_name}.rou.xml')
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
self.assertEqual(tree.find('input/additional-files').get('value'), f'{self.sim_name}.poly.xml')
def test_load_sumoconfig_template_with_routefiles(self): def test_load_sumoconfig_template_with_routefiles(self):
routefiles = (f'{self.sim_name}.bus.rou.xml', f'{self.sim_name}.passenger.rou.xml') routefiles = (f'{self.sim_name}.bus.rou.xml', f'{self.sim_name}.passenger.rou.xml')
tree = configurator.load_sumoconfig_template(self.sim_name, routefiles) tree = configurator.load_sumoconfig_template(self.sim_name, routefiles)
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml') self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
self.assertEqual(tree.find('input/route-files').get('value'), ','.join(routefiles)) self.assertEqual(tree.find('input/route-files').get('value'), ','.join(routefiles))
self.assertEqual(tree.find('input/additional-files').get('value'), f'{self.sim_name}.poly.xml')
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log') self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
def test_load_polyconvert_template(self): def test_load_polyconvert_template(self):
@ -61,7 +67,12 @@ class GenerationTests(unittest.TestCase):
def test_generate_scenario(self): def test_generate_scenario(self):
osm_file = os.path.join(SCRIPTDIR, 'sample.osm') osm_file = os.path.join(SCRIPTDIR, 'sample.osm')
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name) configurator.generate_scenario(osm_file, self.sim_path, self.sim_name, generate_polygons=False)
self.assert_is_file(os.path.join(self.sim_path, f'{self.sim_name}.net.xml'))
def test_generate_scenario_with_polygons(self):
osm_file = os.path.join(SCRIPTDIR, 'sample.osm')
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name, generate_polygons=True)
self.assert_is_dir(self.sim_path) self.assert_is_dir(self.sim_path)
generated_files = [ generated_files = [
f'{self.sim_name}.poly.xml', f'{self.sim_name}.poly.xml',
@ -75,7 +86,7 @@ class GenerationTests(unittest.TestCase):
osm_file = os.path.join(SCRIPTDIR, 'sample.osm') osm_file = os.path.join(SCRIPTDIR, 'sample.osm')
trips_file = os.path.join(self.sim_path, f'{self.sim_name}.trips.xml') trips_file = os.path.join(self.sim_path, f'{self.sim_name}.trips.xml')
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name) configurator.generate_scenario(osm_file, self.sim_path, self.sim_name)
routefiles = configurator.generate_mobility(self.sim_path, self.sim_name) routefiles = configurator.generate_mobility(self.sim_path, self.sim_name, vclasses=('passenger', 'truck'))
self.assert_is_file(trips_file) self.assert_is_file(trips_file)
for f in routefiles: for f in routefiles: