Route Layers from File
Route layers can be saved to and loaded from files. This can be useful if we want to save a route layer to a file for later use, or if we want to load a route layer from a file.
We can save and load route layers to/from YAML or JSON files. For JSON we do:
For YAML we do:
The saved files contain all the information needed to initialize new route layers. If we are using a remote index, we can use the sync features to keep the route layer in sync with the index.
Full Example
Here we will show how to save routers to YAML or JSON files, and how to load a route layer from file.
We start by installing the library:
Define Route
First let’s create a list of routes:
We define a route layer using these routes and using the Cohere encoder.
Test Route
Save To JSON
To save our route layer we call the to_json
method:
Loading from JSON
We can view the router file we just saved to see what information is stored.
It tells us our encoder type, encoder name, and routes. This is
everything we need to initialize a new router. To do so, we use the
from_json
method.
We can confirm that our layer has been initialized with the expected
attributes by viewing the RouteLayer
object: