Spaces:
Sleeping
Sleeping
| import openrouteservice as ors | |
| import numpy as np | |
| from dotenv import load_dotenv | |
| import os | |
| def fetch_matrices(dfadr): | |
| load_dotenv() | |
| ors_apikey=os.environ.get("ors_apikey") | |
| client = ors.Client(key=ors_apikey) | |
| max_elements=3500 | |
| nb_loc=len(dfadr) | |
| nb_loc_per_step=max_elements//nb_loc | |
| nb_steps=int(np.ceil(nb_loc/nb_loc_per_step)) | |
| lonlat=list(zip(*(dfadr.geometry.x, dfadr.geometry.y))) | |
| lrep=[] | |
| for i in np.arange(nb_steps): | |
| idx_dest=np.arange(i*nb_loc_per_step, min([(i+1)*nb_loc_per_step, nb_loc])).tolist() | |
| lrep.append(ors.distance_matrix.distance_matrix(client=client, | |
| locations=lonlat, | |
| destinations=idx_dest, | |
| metrics=["distance", "duration"], | |
| units="km", | |
| resolve_locations=True)) | |
| mat_dist=np.concat([il["distances"] for il in lrep], axis=1) | |
| mat_dur=np.concat([il["durations"] for il in lrep], axis=1) | |
| return mat_dist, mat_dur | |