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
8f69983a80
commit
dc4444a24e
@ -10,15 +10,6 @@ import randomTrips
|
|||||||
# 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__)
|
||||||
STATICDIR = os.path.join(SCRIPTDIR, 'static')
|
STATICDIR = os.path.join(SCRIPTDIR, 'static')
|
||||||
SIMNAME = 'simul'
|
|
||||||
NETCONVERTCMD = ['netconvert', '-c', os.path.join(STATICDIR, f'{SIMNAME}.netcfg')]
|
|
||||||
POLYCONVERTCMD = ['polyconvert', '-c', os.path.join(STATICDIR, f'{SIMNAME}.polycfg')]
|
|
||||||
|
|
||||||
|
|
||||||
def clean():
|
|
||||||
# [os.remove(os.path.join('simul', f)) for f in os.listdir('simul')]
|
|
||||||
[os.remove(os.path.join('static', f))
|
|
||||||
for f in os.listdir('static') if f.endswith('.log')]
|
|
||||||
|
|
||||||
|
|
||||||
def load_netconvert_template(osm_input, out_name):
|
def load_netconvert_template(osm_input, out_name):
|
||||||
@ -30,37 +21,50 @@ def load_netconvert_template(osm_input, out_name):
|
|||||||
return tree
|
return tree
|
||||||
|
|
||||||
|
|
||||||
def generate_network(osm_file, out_path, net_name):
|
def load_polyconvert_template(osm_file, type_file, scenario_name):
|
||||||
template = load_netconvert_template(osm_file, net_name)
|
tree = ElementTree.parse(os.path.join(STATICDIR, 'simul.polycfg'))
|
||||||
|
root = tree.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
|
||||||
|
|
||||||
|
|
||||||
|
def load_sumoconfig_template(simulation_name):
|
||||||
|
tree = ElementTree.parse(os.path.join(STATICDIR, 'simul.sumocfg'))
|
||||||
|
root = tree.getroot()
|
||||||
|
root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
|
||||||
|
root.find('input/route-files').set('value', f'{simulation_name}.rou.xml')
|
||||||
|
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
|
||||||
|
root.find('report/log').set('value', f'{simulation_name}.log')
|
||||||
|
return tree
|
||||||
|
|
||||||
|
|
||||||
|
def generate_scenario(osm_file, out_path, 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:
|
||||||
netconfig = os.path.join(tmpdirname, f'{net_name}.netcfg')
|
netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg')
|
||||||
template.write(netconfig)
|
polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg')
|
||||||
# copy typemaps to tempdir
|
net_template.write(netconfig)
|
||||||
|
poly_template.write(polyconfig)
|
||||||
|
# Copy typemaps to tempdir
|
||||||
shutil.copytree(os.path.join(STATICDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
|
shutil.copytree(os.path.join(STATICDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
|
||||||
print('Creating the network…')
|
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
|
||||||
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)
|
||||||
print('Copying files')
|
subprocess.run(polyconvert_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
shutil.copy(os.path.join(tmpdirname, f'{net_name}.net.xml'), os.path.join(out_path, f'{net_name}.net.xml'))
|
# Move files to destination
|
||||||
print('created')
|
files = [
|
||||||
|
f'{scenario_name}.net.xml',
|
||||||
|
f'{scenario_name}.netconvert.log',
|
||||||
def generate_scenario(out_path, name):
|
f'{scenario_name}.poly.xml',
|
||||||
print('Creating the network…')
|
f'{scenario_name}.polyconvert.log'
|
||||||
subprocess.run(NETCONVERTCMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
]
|
||||||
print('Extracting polygons…')
|
for f in files:
|
||||||
subprocess.run(POLYCONVERTCMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
shutil.move(os.path.join(tmpdirname, f), os.path.join(out_path, f))
|
||||||
print('Moving files')
|
|
||||||
shutil.move(os.path.join(STATICDIR, 'simul.net.xml'), os.path.join(out_path, f'{name}.net.xml'))
|
|
||||||
shutil.move(os.path.join(STATICDIR, 'simul.poly.xml'), os.path.join(out_path, f'{name}.poly.xml'))
|
|
||||||
shutil.copyfile(os.path.join(STATICDIR, 'simul.sumocfg'), os.path.join(out_path, f'{name}.sumocfg'))
|
|
||||||
# Move log files
|
|
||||||
logdir = os.path.join(out_path, 'log')
|
|
||||||
if not os.path.exists(logdir):
|
|
||||||
os.mkdir(logdir)
|
|
||||||
for f in os.listdir(STATICDIR):
|
|
||||||
if f.endswith('.log'):
|
|
||||||
shutil.move(os.path.join(STATICDIR, f), os.path.join(logdir, f))
|
|
||||||
|
|
||||||
|
|
||||||
def generate_mobility(path, name):
|
def generate_mobility(path, name):
|
||||||
@ -82,19 +86,12 @@ def generate_mobility(path, name):
|
|||||||
randomTrips.main(randomTrips.get_options(options))
|
randomTrips.main(randomTrips.get_options(options))
|
||||||
|
|
||||||
|
|
||||||
def generate_all(output_path, simulation_name):
|
def generate_all(osm_file, output_path):
|
||||||
output_path = os.path.join(output_path, simulation_name)
|
output_dir = os.path.dirname(output_path)
|
||||||
if not os.path.exists(output_path):
|
simulation_name = os.path.basename(output_path)
|
||||||
os.mkdir(output_path)
|
generate_scenario(osm_file, output_dir, simulation_name)
|
||||||
generate_scenario(output_path, simulation_name)
|
generate_mobility(output_dir, simulation_name)
|
||||||
generate_mobility(output_path, simulation_name)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# clean()
|
pass
|
||||||
# generate_all('/tmp/', 'simul')
|
|
||||||
output_file = '/tmp/simul'
|
|
||||||
if not os.path.exists(output_file):
|
|
||||||
os.mkdir(output_file)
|
|
||||||
osm_file = os.path.join(STATICDIR, 'simul.raw.osm')
|
|
||||||
generate_network(osm_file, output_file, 'test-sc')
|
|
||||||
|
@ -4,6 +4,7 @@ import tempfile
|
|||||||
import unittest
|
import unittest
|
||||||
from configurator import configurator
|
from configurator import configurator
|
||||||
|
|
||||||
|
|
||||||
class ConfiguratorTests(unittest.TestCase):
|
class ConfiguratorTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -14,11 +15,11 @@ class ConfiguratorTests(unittest.TestCase):
|
|||||||
shutil.rmtree(self.sim_path)
|
shutil.rmtree(self.sim_path)
|
||||||
|
|
||||||
def test_generate_scenario(self):
|
def test_generate_scenario(self):
|
||||||
configurator.generate_scenario(self.sim_path, self.sim_name)
|
osm_file = os.path.abspath('sample.osm')
|
||||||
|
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name)
|
||||||
self.assert_is_dir(self.sim_path)
|
self.assert_is_dir(self.sim_path)
|
||||||
self.assert_is_dir(os.path.join(self.sim_path, 'log'))
|
# self.assert_is_dir(os.path.join(self.sim_path, 'log'))
|
||||||
generated_files = [
|
generated_files = [
|
||||||
f'{self.sim_name}.sumocfg',
|
|
||||||
f'{self.sim_name}.poly.xml',
|
f'{self.sim_name}.poly.xml',
|
||||||
f'{self.sim_name}.net.xml'
|
f'{self.sim_name}.net.xml'
|
||||||
]
|
]
|
||||||
@ -27,11 +28,37 @@ class ConfiguratorTests(unittest.TestCase):
|
|||||||
|
|
||||||
def test_generate_mobility(self):
|
def test_generate_mobility(self):
|
||||||
# The scenario must be generated before the mobility
|
# The scenario must be generated before the mobility
|
||||||
configurator.generate_scenario(self.sim_path, self.sim_name)
|
osm_file = os.path.abspath('sample.osm')
|
||||||
|
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name)
|
||||||
configurator.generate_mobility(self.sim_path, self.sim_name)
|
configurator.generate_mobility(self.sim_path, self.sim_name)
|
||||||
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')
|
||||||
self.assert_is_file(trips_file)
|
self.assert_is_file(trips_file)
|
||||||
|
|
||||||
|
def test_load_netconvert_template(self):
|
||||||
|
tree = configurator.load_netconvert_template('test.osm', 'test_simulation')
|
||||||
|
self.assertEqual(tree.find('input/osm-files').get('value'), 'test.osm')
|
||||||
|
self.assertEqual(tree.find('output/output-file').get('value'), f'{self.sim_name}.net.xml')
|
||||||
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.netconvert.log')
|
||||||
|
|
||||||
|
def test_load_sumoconfig_template(self):
|
||||||
|
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/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')
|
||||||
|
|
||||||
|
def test_load_polyconvert_template(self):
|
||||||
|
tree = configurator.load_polyconvert_template(
|
||||||
|
osm_file=f'{self.sim_name}.osm',
|
||||||
|
type_file='typemap/test.typ.xml',
|
||||||
|
scenario_name=f'{self.sim_name}'
|
||||||
|
)
|
||||||
|
self.assertEqual(tree.find('input/osm-files').get('value'), f'{self.sim_name}.osm')
|
||||||
|
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
|
||||||
|
self.assertEqual(tree.find('input/type-file').get('value'), 'typemap/test.typ.xml')
|
||||||
|
self.assertEqual(tree.find('output/output-file').get('value'), f'{self.sim_name}.poly.xml')
|
||||||
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.polyconvert.log')
|
||||||
|
|
||||||
def assert_exists(self, path):
|
def assert_exists(self, path):
|
||||||
self.assertTrue(os.path.exists(path), msg=f'{path} does not exist')
|
self.assertTrue(os.path.exists(path), msg=f'{path} does not exist')
|
||||||
|
|
||||||
|
49966
tests/sample.osm
Normal file
49966
tests/sample.osm
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user