Ultimate goal is to design routes as the contour of an image.
Here is the recipe:
- Extract the largest contour (list of coordinates) from an image
- Project that contour on a map.
- Get direction between successives points.
Have a look at the Starter Notebook for a first overview.
from stravart.polygone import Polygon
from stravart.search.operations import Projection
from stravart.contours.extraction import ContourExtractor
contour_extractor = ContourExtractor("path/to/img")
contour = contour_extractor.get_best_contour()
contour.close()
map_center = (48.8675, 2.3638)
radius = 0.03
poly = Polygon.from_list(coordinates_list=contour, system="cartesian")
projection = Projection(center=map_center, radius=radius, map_type="GPS")
gps_poly = projection.apply(poly)
final_contour, path_mapping = gps_poly.fill_paths_between_points()
plot_route(map_center=map_center, route=final_contour)
For natural image, check out the Contour Extraction.ipynb notebook.
We can try to look for the best possible route by:
- Applying multiple operations to the polygon (dilatation, rotation)
- Measure each feasible route quality - as the total difference area between the image contour and the suggested polygon.
- Select the best one according to the previous criterion.
Check out the Optimization notebook
import optuna
from stravart.search.optimization import generate_grid, objective
# Grid over Paris
lat_start, lat_end = 48.8156, 48.9022
lon_start, lon_end = 2.2241, 2.4699
city_grid = generate_grid(lat_start, lat_end, lon_start, lon_end, 10, 10)
study = optuna.create_study(direction='minimize')
study.optimize(lambda trial: objective(trial, poly=poly, city_grid=city_grid), n_trials=30)
best_trial = study.best_trial
best_final_contour = best_trial.user_attrs['final_contour']
map_center = city_grid[study.best_params['map_center_idx']]
Running the code above (contour extraction + optimization) results in this route which is around 30kms long:
To install StravArt, follow these steps:
- Clone the repository to your local machine
git clone https://github.com/yourusername/StravArt.git
cd StravArt
- Create a virtual env
python -m venv venv
source venv/bin/activate
- Install dependencies
pip install -r requirements.txt
- Set up API access tokens By default, we use Google Maps API. You need to get an access token first and set it as environment variable.
export GMAPS_KEY=youraccesstoken
or directly in Python
import os
os.environ['GMAPS_KEY']='youraccesstoken'