Skip to content

Commit

Permalink
Merge pull request #19 from alexanderquispe/server
Browse files Browse the repository at this point in the history
Server
  • Loading branch information
alexanderquispe authored Jul 6, 2023
2 parents e7aa2e7 + e07a19b commit 34ff8d0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 71 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
data/
*__pycache__
*__pycache__
examples/continent
examples/country
examples/subregion
examples/data-pbf
8 changes: 5 additions & 3 deletions osrmareas/areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def get_osrm_route(from_, to_, how='driving'):
return line

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

n_grid = radius_km/grid_km_size * 2

center = list(reversed(center_lat_long))
radius_km = radius_km * 1000
initial_center = Point(center)

Expand All @@ -70,8 +70,10 @@ def get_routes(
# osrm routes
all_routes_df = gpd.GeoDataFrame()
for final in final_routes:
center1 = list(reversed(center))
final1 = list(reversed(final))
all_routes_df = \
pd.concat([all_routes_df, get_osrm_route(center, final)])
pd.concat([all_routes_df, get_osrm_route(center1, final1)])

return all_routes_df

Expand Down
111 changes: 111 additions & 0 deletions osrmareas/downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pandas as pd, numpy as np, os
import requests

continent_df = "https://raw.githubusercontent.com/alexanderquispe/osrm_python/data/data_ref/continent_df.csv"
country_df = 'https://github.com/alexanderquispe/osrm_python/raw/data/data_ref/country_df.csv'
region_df = 'https://raw.githubusercontent.com/alexanderquispe/osrm_python/data/data_ref/region_df.csv'

cnt = pd.read_csv(continent_df)
ctry = pd.read_csv(country_df)
region = pd.read_csv(region_df)

# https://download.geofabrik.de/antarctica-latest-free.shp.zip
# https://download.geofabrik.de/asia-latest.osm.bz2
latest ='-latest'
osrm_pbf = '.osm.pbf'
zip_ext = '-free.shp.zip'
osm_bz2 = '.osm.bz2'

def add_link_exte(where:list, ext='pbf'):

if ext=='pbf':
ext = osrm_pbf
elif ext=='zip':
ext=zip_ext
else:
ext=osm_bz2

list_where = where.copy()
for i, w in enumerate(where):
list_where[i] = w + latest + ext
return list_where


class GetPBF:
def __init__(self, save_into_dir=None):
print("Convert the country names to lowercase and replace spaces with dashes (-).")
print("source: https://download.geofabrik.de/")
if save_into_dir is None:
save_into_dir = "data-pbf"
if not os.path.exists(save_into_dir):
os.makedirs(save_into_dir)
self.dir_name = save_into_dir
def continent(self, where:list, save_dir=None):

ref=cnt.query('continent in @where').\
url_main.to_numpy()
ref = list(ref)
self.locations = ref
self.where = where
return self
def country(self, where:list, continent=None, save_dir=None):
self.where = where

if continent is None:
ctrs=ctry.query('country in @where').\
url_main.to_numpy()
self.locations = list(ctrs)
return self
ctrs_spe = ctry.query('continent in @continent and country in @where').\
url_main.to_numpy()
self.locations = list(ctrs_spe)
return self
def sub_region(self, where:list, continent=None, country=None, save_dir=None):
self.where = where

if continent is None and country is None:
sr = region.query('region in @where').url_main\
.to_numpy()
self.locations = list(sr)
return self
elif continent is not None and country is None:
sr = region.query('continent in @continent and region in @where').\
url_main.to_numpy()
self.locations = list(sr)
return self
elif continent is None and country is not None:
sr = region.query('country in @country and region in @where').\
url_main.to_numpy()
self.locations = list(sr)
return self
elif continent is not None and country is not None:
sr = region.query('continent in @continent and country in @country and region in @where').\
url_main.to_numpy()
self.locations = list(sr)
return self
def get(self, ext='pbf', save_dir=None):
if save_dir is not None:
if not os.path.exists(save_dir):
os.makedirs(save_dir)
location = self.locations
name_where = self.where

download_link = add_link_exte(location, ext)
file_name = add_link_exte(name_where)
for i, file in enumerate(file_name):
save_f = save_dir + '/' + file
self._download(download_link[i], save_f)
return self

def _download(self, from_, to_, force_download=False):
exists = os.path.exists(to_)
if not exists or force_download:
response = requests.get(from_)
with open(to_, "wb") as file:
file.write(response.content)






106 changes: 39 additions & 67 deletions osrmareas/osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,82 +32,54 @@ def __init__(self, pbf_file=None, pbf_file_path=None, osrm_path='C:\osrm', metho
self._prepare = False


def prepare_server(self):
if self._prepare:
return "Done!"
def gen_osrm_1(self):
print("Please run the following function only once for each .pbf file. (It takes some time to execute, and consume all the CPU)")

gen_osrm = self.gen_osrm_file
gen_route = self.gen_routes
osrm_file = self.osrm_file
pbf_file = self.pbf_file
comand = f'''
{gen_osrm}
{gen_route}
'''
comand1 = comand

run_first = 'no'
run_second = 'no'

fl = tempfile.NamedTemporaryFile(delete=False, suffix='.bat')
file = fl.name
osrm_gen_=os.path.exists(osrm_file)

out_f = open(file, 'w')
out_f.writelines('algo')
out_f.close()
run_first="No"

if os.path.exists(osrm_file):
print('Found osrm file')
run_first = input(f"An OSRM file named '{osrm_file}' was found. Do you want to run the following command again: `osrm_extract.exe -p car.lua {pbf_file}`? [yes, no] (This will take some time)")
if not osrm_gen_:
subprocess.Popen(f'{gen_osrm}', shell=True).wait()
else:
subprocess.Popen(['start', 'cmd', '/c', 'call', file], shell = True)
print(file)
# print(comand1)

if "n" in run_first:
comand = f'''
{gen_route}
'''

if os.path.exists(self._contract_file):
print('Found contract file')
run_second=input(f"Contract found, run again?")

if 'n' in run_second:
comand = ''
with open(file, 'w') as bt:
bt.write(comand)
subprocess.Popen(['start', 'cmd', '/c', 'call', file], shell = True)
self._prepare = True
print(f'Found {osrm_file} file')

run_first = input(f"An OSRM file named '{osrm_file}' was found. Do you want to run again? [yes, no] (This will take some time)")
if 'y' in run_first:
subprocess.Popen(f'{gen_osrm}', shell=True).wait()
print("Done, generate a osrm file")
return self
def run_server(self):
gen_server = self.gen_backend
comand = f'''
{gen_server}
'''
fl = tempfile.NamedTemporaryFile(delete=False, suffix='.bat')
file = fl.name

with open(file, 'w') as bt:
bt.write(comand)
subprocess.Popen(['start', 'cmd', '/c', 'call', file], shell = True)

def prepare_server_2(self):
print("Please run the following function only once for each .pbf file. (It takes some time to execute, and consume all the CPU)")

gen_route = self.gen_routes
contract_file = self._contract_file
contract_ = os.path.exists(contract_file)
run_second = "No"

if not contract_:
subprocess.Popen(f'{gen_route}', shell=True).wait()
else:
run_second = input(f"An HSGR file named '{contract_file}' was found. Do you want to run the following command again: [yes, no] (This will take some time)")
if 'y' in run_second:
subprocess.Popen(f'{gen_route}', shell=True).wait()
else:
pass
print('Done, I have generated the local OSRM server with `{server}.run_server()`.')
self._prepare = True
return self



def run_server(self):
ready=self._prepare
if not ready:
raise "The necessary files are not available, please run `Server(..).gen_osrm_1()` and `Server(...).prepare_server_2()` first."


gen_server = self.gen_backend
subprocess.Popen(f'{gen_server}', shell=True)
print("The server is running in the background, you can start making queries.")


# def make_grid(self, x, y, size_grid):
# return areas.make_grid(x, y, size_grid)
# def get_osrm_route_local(self, from_, to_, how_='driving'):
# return areas.get_osrm_route(from_, to_, how)
# def get_area_routes(self, center, radius_km, grid_km_size=1):
# routes_df = areas.get_routes(center, radius_km, grid_km_size)
# self.routes_df_area = routes_df
# return self
# def get_ameba(self, df, max_km=None, alpha=0, xcol='x', ycol='y', km_col='dist_km', crs=areas.crs_lat):
# ameba_shp = areas.get_ameba(df, max_km, alpha, xcol, ycol, km_col, crs)
# self.ameba = ameba_shp
# return self
return self

0 comments on commit 34ff8d0

Please sign in to comment.