Last active
May 17, 2019 14:33
-
-
Save henryjameslau/7dca5a303778c5a3f35a51687b6c33db to your computer and use it in GitHub Desktop.
Find nearest road
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import requests | |
| from itertools import islice | |
| import pandas as pd | |
| import numpy as np | |
| import os | |
| from tqdm import tqdm | |
| filepath = 'destination.csv' | |
| df = pd.read_csv(filepath) | |
| df = df.replace(np.nan, '', regex=True) | |
| nearest_lon_col = [] | |
| nearest_lat_col = [] | |
| for index, row in tqdm(df.iterrows(), total=df.shape[0]): | |
| lon = row['lon'] | |
| lat = row['lat'] | |
| url = 'http://0.0.0.0:5000/nearest/v1/driving/{},{}'.format(lon,lat) | |
| response = requests.get(url) | |
| data = response.json() | |
| try: | |
| nearest_lon = data['waypoints'][0]['location'][0] | |
| nearest_lon_col.append(nearest_lon) | |
| nearest_lat = data['waypoints'][0]['location'][1] | |
| nearest_lat_col.append(nearest_lat) | |
| except KeyError: | |
| nearest_lon = '' | |
| nearest_lon_col.append(nearest_lon) | |
| nearest_lat = '' | |
| nearest_lat_col.append(nearest_lat) | |
| new_columns = pd.DataFrame({'nearest_lon': nearest_lon_col,'nearest_lat': nearest_lat_col}) | |
| df_final = df.merge(new_columns, left_index = True, right_index = True) | |
| df_final.drop("lon",axis=1,inplace=True) | |
| df_final.drop("lat",axis=1,inplace=True) | |
| df_final.rename(columns={'nearest_lon': 'lon', 'nearest_lat': 'lat'}, inplace=True) | |
| os.remove("destination.csv") | |
| df_final.to_csv('destination.csv', index=False, encoding='utf8') | |
| filepath = 'origin.csv' | |
| df = pd.read_csv(filepath) | |
| df = df.replace(np.nan, '', regex=True) | |
| nearest_lon_col = [] | |
| nearest_lat_col = [] | |
| for index, row in tqdm(df.iterrows(), total=df.shape[0]): | |
| lon = row['lon'] | |
| lat = row['lat'] | |
| url = 'http://localhost:5000/nearest/v1/driving/{},{}'.format(lon,lat) | |
| response = requests.get(url) | |
| data = response.json() | |
| try: | |
| nearest_lon = data['waypoints'][0]['location'][0] | |
| nearest_lon_col.append(nearest_lon) | |
| nearest_lat = data['waypoints'][0]['location'][1] | |
| nearest_lat_col.append(nearest_lat) | |
| except KeyError: | |
| nearest_lon = '' | |
| nearest_lon_col.append(nearest_lon) | |
| nearest_lat = '' | |
| nearest_lat_col.append(nearest_lat) | |
| new_columns = pd.DataFrame({'nearest_lon': nearest_lon_col,'nearest_lat': nearest_lat_col}) | |
| df_final = df.merge(new_columns, left_index = True, right_index = True) | |
| df_final.drop("lon",axis=1,inplace=True) | |
| df_final.drop("lat",axis=1,inplace=True) | |
| df_final.rename(columns={'nearest_lon': 'lon', 'nearest_lat': 'lat'}, inplace=True) | |
| os.remove("origin.csv") | |
| df_final.to_csv('origin.csv', index=False, encoding='utf8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment