Skip to content

Usage: 3.1. Writing Data: MATSim

Kasia Kozlowska edited this page Jul 14, 2022 · 5 revisions

Writing Network to MATSim

This page goes through the MATSim files that can be generated with GeNet. Available as a jupyter notebook or wiki page.

Supported output files

GeNet currently supports writing the following MATSim network files:

  • network.xml with the following network v2 dtd schema

    Data present on the nodes and edges of the graph will only persist to the network.xml file if it matches the required or optional attributes defined in the /variables.py, or is saved in a nested dictionary under attributes for network links in the following format: 'attributes': {'attribute_name' : {'name': 'attribute_name', 'class': 'java.lang.String', 'text': 'attribute_value'}}.

  • schedule.xml with the following schedule v2 dtd schema

    Similarly to the network, in the case any data is added to the genet.Schedule object's graph, only the allowed attributes for stops (graph nodes) defined in /variables.py will persist to the schedule.xml file.

  • vehicles.xml with the following vehicles v1 dtd schema

    GeNet will generate a vehicle.xml file using vehicles and vehicle_types attributes of the Schedule object. These are based on vehicle_definitions.yml by default but can be customised at the time of creating the Schedule or by accessing/modifying those attributes of the object directly.

  • roadpricing-file.xml with the following roadpricing v1 dtd schema

    How to generate data for this file is described in: Usage: 4. Using Network

Let's read in a sample MATSim network into GeNet's Network object.

from genet import read_matsim
import os

path_to_matsim_network = '../example_data/pt2matsim_network'

network = os.path.join(path_to_matsim_network, 'network.xml')
schedule = os.path.join(path_to_matsim_network, 'schedule.xml')
vehicles = os.path.join(path_to_matsim_network, 'vehicles.xml')
n = read_matsim(
    path_to_network=network, 
    epsg='epsg:27700', 
    path_to_schedule=schedule, 
    path_to_vehicles=vehicles
)
# you don't need to read the vehicles file, but doing so ensures all vehicles
# in the schedule are of the expected type and the definition of the vehicle
# is preserved
n.print()
Graph info: Name: 
Type: MultiDiGraph
Number of nodes: 1662
Number of edges: 3166
Average in degree:   1.9049
Average out degree:   1.9049 
Schedule info: Schedule:
Number of services: 9
Number of routes: 68
Number of stops: 118

To write the network into matsim format, use write_to_matsim

n.write_to_matsim(os.path.join(path_to_matsim_network, 'genet_output'))
2022-07-14 15:31:43,952 - Writing ../example_data/pt2matsim_network/genet_output/network.xml
2022-07-14 15:31:44,529 - Writing ../example_data/pt2matsim_network/genet_output/schedule.xml
2022-07-14 15:31:44,570 - Writing ../example_data/pt2matsim_network/genet_output/vehicles.xml

Note that saving your network to MATSim format will only include the attributes included in the MATSim network schema. All other attributes will not be saved to this format, but you may be interested in also saving your network to CSV, or (Geo/)JSON formats.