3.4. Writing data: JSON & GeoJSON¶
Available as a jupyter notebook or wiki page.
In [1]:
Copied!
import os
from genet import read_matsim
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()
import os
from genet import read_matsim
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
You can write the Network and it's Schedule (if applicable) to JSON:
In [2]:
Copied!
n.write_to_json("example_data/outputs/json")
n.write_to_json("example_data/outputs/json")
2022-07-14 15:38:02,012 - Saving Network to JSON in example_data/output_json 2022-07-14 15:38:03,912 - Saving Schedule to JSON in example_data/output_json
Writing the Network to JSON preserves all of the Network data - unlike writing the Network to CSV or MATSim format. For limitations of those formats head over to Sections/Notebooks:
- Writing CSV data
- Writing MATSim data
You can also choose to write the network to GeoJSON, GeoParquet or as a shapefile. This will produce spatial representation of the Network graph and the Schedule graph.
- The main difference for the Network graph outputs is that the link geometry is a
LINESTRING
, whereas in the JSON outputs, this geometry is an encoded polyline. - The biggest difference is for the Schedule graph. With JSON output you get the entire Schedule data saved to file. With GeoJSON you get only the spatial representation of the graph, nodes and edges, where nodes are the Stops in the Schedule and edges are the connections between Stops as defined by the Route and Service objects which use those Stops. It does not include any information about the vehicles, their IDs or modes, vehicle definitions or network routes (the edges are straight lines between the Stops)
In [3]:
Copied!
n.write_spatial("example_data/outputs/geojson", filetype="geojson")
n.write_spatial("example_data/outputs/geojson", filetype="geojson")
2022-07-14 15:38:05,560 - Saving Network to GeoJSON in example_data/output_geojson 2022-07-14 15:38:06,974 - Saving Schedule to GeoJSON in example_data/output_geojson