Skip to content

Commit

Permalink
Merge pull request #20 from alexanderquispe/osrm_feature
Browse files Browse the repository at this point in the history
Osrm, fix some bugs
  • Loading branch information
TJhon authored Jul 6, 2023
2 parents 34ff8d0 + de896f5 commit 28937e9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ data/
examples/continent
examples/country
examples/subregion
examples/data-pbf
examples/data-pbf

*algo*
25 changes: 16 additions & 9 deletions osrmareas/areas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import requests, pandas as pd, alphashape
from tqdm import tqdm
import geopandas as gpd, numpy as np
from shapely.geometry import LineString, Point
from shapely import Polygon
from shapely.geometry import Polygon

crs_moll='EPSG:3857'
crs_lat='EPSG:4326'
Expand Down Expand Up @@ -40,13 +41,13 @@ def get_osrm_route(from_, to_, how='driving'):
# route.append([-66.744416, -54.975603])dd
line = gpd.GeoDataFrame(geometry=[LineString(route)]).set_crs(crs_lat)
line = line.assign(
dist_km=dist/1000, x = to_[0], y = to_[1],
x_begin = from_[0], y_begin = from_[1]
dist_driving_km=dist/1000, dest_lon = to_[0], dest_lat = to_[1],
origin_lon = from_[0], origin_lat = from_[1]
)
return line

def get_routes(
center_lat_long, radius_km=10, grid_km_size=1
center_lat_long, radius_km=10, grid_km_size=1, filter_km=True
):

n_grid = radius_km/grid_km_size * 2
Expand All @@ -69,15 +70,20 @@ def get_routes(
final_routes.append(final)
# osrm routes
all_routes_df = gpd.GeoDataFrame()
for final in final_routes:
for final in tqdm(final_routes):
center1 = list(reversed(center))
final1 = list(reversed(final))
all_routes_df = \
pd.concat([all_routes_df, get_osrm_route(center1, final1)])

if filter_km:
rows_inside = all_routes_df['dist_driving_km'] < radius_km
all_routes_df_crop = all_routes_df.loc[rows_inside]
return all_routes_df_crop

return all_routes_df

def get_ameba(df, max_km=None, alpha = 0, xcol='x', ycol='y', km_col='dist_km', crs=crs_lat):
def get_ameba(df, max_km=None, alpha = 0, xcol='dest_lon', ycol='dest_lat', km_col='dist_driving_km', crs=crs_lat):
if max_km is None:
max_km=np.mean(df[km_col])
crop_df = df.copy()
Expand All @@ -87,8 +93,9 @@ def get_ameba(df, max_km=None, alpha = 0, xcol='x', ycol='y', km_col='dist_km',
crop_df = df.loc[crop_df_rows]
border = []
for _, row in crop_df.iterrows():
border.append((row[xcol], row[ycol]))
border.append((row[ycol], row[xcol]))
bd = alphashape.alphashape(border, alpha)
gdf = gpd.GeoDataFrame(geometry=[bd]).\
set_crs(crs, allow_override=True)
gdf = gpd.GeoDataFrame(geometry=[bd])
if crs is not None:
gdf=gdf.set_crs(crs, allow_override=True)
return gdf
2 changes: 1 addition & 1 deletion osrmareas/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.5'
__version__ = '0.1.6'
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ numpy<=1.24.3
pandas
geopandas
alphashape
shapely
shapely
tqdm
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'geopandas',
'alphashape',
'shapely',
'tqdm'
],
long_description='''
Using OSRM to create areas and routes within a radius.
Expand Down

0 comments on commit 28937e9

Please sign in to comment.