Cleanup for matsim export factory

This commit is contained in:
Ruben1729 2024-02-14 17:06:21 -05:00
parent 268ceb5d0f
commit 5f245f2e7d

View File

@ -23,7 +23,7 @@ class Matsim:
'facility': [] 'facility': []
} }
def _export(self): def export(self):
self._export_facilities() self._export_facilities()
self._export_network() self._export_network()
self._export_population() self._export_population()
@ -96,19 +96,10 @@ class Matsim:
) )
gdf.to_file("input_files/buildings_shapefile.shp") gdf.to_file("input_files/buildings_shapefile.shp")
# Convert the Python dictionary to an XML string # Write xml content to file
xml_content = etree.tostring(facilities_xml, pretty_print=True, encoding='UTF-8').decode('utf-8') xml_content = etree.tostring(facilities_xml, pretty_print=True, encoding='UTF-8').decode('utf-8')
# Write the XML to the file
output_file = f"{self._output_file_path}/{self._city.name}_facilities.xml" output_file = f"{self._output_file_path}/{self._city.name}_facilities.xml"
with open(output_file, 'w') as file: _save_xml(output_file, xml_content, f"<!DOCTYPE facilities SYSTEM \"{FACILITIES_DTD}\">\n", True)
file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file.write(f"<!DOCTYPE facilities SYSTEM \"{FACILITIES_DTD}\">\n")
file.write(xml_content)
with open(output_file, 'rb') as f_in:
with gzip.open(output_file + '.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
def _export_network(self): def _export_network(self):
java_path = "java" java_path = "java"
@ -194,19 +185,10 @@ class Matsim:
id += 1 id += 1
# Convert the Python dictionary to an XML string # Write xml content to file
xml_content = etree.tostring(population, pretty_print=True, encoding='UTF-8').decode('utf-8') xml_content = etree.tostring(population, pretty_print=True, encoding='UTF-8').decode('utf-8')
# Write the XML to the file
output_file = f"{self._output_file_path}/{self._city.name}_population.xml" output_file = f"{self._output_file_path}/{self._city.name}_population.xml"
with open(output_file, 'w') as file: _save_xml(output_file, xml_content, f"<!DOCTYPE population SYSTEM \"{POPULATION_DTD}\">\n", True)
file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file.write(f"<!DOCTYPE population SYSTEM \"{POPULATION_DTD}\">\n")
file.write(xml_content)
with open(output_file, 'rb') as f_in:
with gzip.open(output_file + '.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
def _export_config(self): def _export_config(self):
root = etree.Element('config') root = etree.Element('config')
@ -377,14 +359,10 @@ class Matsim:
# Defines all the modes available, including chain-based modes, seperated by commas # Defines all the modes available, including chain-based modes, seperated by commas
_add_param(subtour_module, 'modes', 'car,walk') _add_param(subtour_module, 'modes', 'car,walk')
# Write xml content to file
xml_content = etree.tostring(root, pretty_print=True, encoding='UTF-8').decode('utf-8') xml_content = etree.tostring(root, pretty_print=True, encoding='UTF-8').decode('utf-8')
# Write the XML to the file
output_file = f"{self._output_file_path}/{self._city.name}_config.xml" output_file = f"{self._output_file_path}/{self._city.name}_config.xml"
with open(output_file, 'w') as file: _save_xml(output_file, xml_content, f"<!DOCTYPE config SYSTEM \"{CONFIG_DTD}\">\n")
file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file.write(f"<!DOCTYPE config SYSTEM \"{CONFIG_DTD}\">\n")
file.write(xml_content)
def _add_param(parent, name, value): def _add_param(parent, name, value):
@ -427,3 +405,21 @@ def _convert_schedules(building_schedules):
}) })
return converted_schedules return converted_schedules
def _save_xml(output_file, xml_content, xml_dtd=None, zipped=None):
if zipped is None:
zipped = False
if xml_dtd is None:
xml_dtd = ''
with open(output_file, 'w') as file:
file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file.write(xml_dtd)
file.write(xml_content)
if zipped:
with open(output_file, 'rb') as f_in:
with gzip.open(output_file + '.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)