Recommended RV Parks Dataset¶
Reference: https://www.greatalaskanholidays.com/trip-planning/recommended-campgrounds/
Setup¶
!pip install polyline geocoder -q
import pandas as pd
Scrape Data¶
df_all = pd.read_html('https://www.greatalaskanholidays.com/trip-planning/recommended-campgrounds/')
len(df_all)
DataFrames¶
- Privately Owned Campgrounds
- State of Alaska Campgrounds
df = df_all[0].copy()
# df.head(5)
df.columns = ['CampgroundName', 'Sites', 'Website', 'BusinessAddress', 'Email']
df = df.fillna('')
df
import logging
import requests
import json
import polyline
import folium
from folium.plugins import MeasureControl
import geocoder
from functools import lru_cache
logger = logging.getLogger(__name__)
DEBUG = True
@lru_cache(maxsize=None)
def geocode(location):
return _geocode(location)
def _geocode(location):
import geocoder
# g = geocoder.osm(location)
g = geocoder.arcgis(location)
return g.latlng
@lru_cache(maxsize=None)
def get_route(olat, olng, dlat, dlng):
response = _get_route(olat, olng, dlat, dlng)
return response
def _get_route(olat, olng, dlat, dlng):
url = f'http://router.project-osrm.org/route/v1/driving/{olng},{olat};{dlng},{dlat}?alternatives=false&steps=false'
# logger.debug(url)
response = None
try:
logger.debug(f'====== OSRM: {url}')
response = requests.get(url, verify=False)
except Exception as ex:
raise
# logger.debug(response.text)
if response and response.text:
response_dict = json.loads(response.text)
#possible = pd.DataFrame([{'Distance': (route['distance'] / 1000) * 0.621371 , route['weight_name']: route['weight']} for route in response_dict['routes']])
return response_dict
else:
return None
def get_routing_map(origin, destination, zoom=5):
orig_latlng = geocode(origin)
dest_latlng = geocode(destination)
resp = get_route(orig_latlng[0], orig_latlng[1], dest_latlng[0], dest_latlng[1])
decoded = polyline.decode(resp["routes"][0]['geometry'])
distance = resp["routes"][0]['distance'] * 0.000621371
duration = resp["routes"][0]['duration'] / 60
map2 = folium.Map(location=(orig_latlng[0], orig_latlng[1]), zoom_start=zoom,
control_scale=True)
# map2.add_child(MeasureControl(
# primary_length_unit='miles',
# secondary_length_unit='meters',
# primary_area_unit='acres',
# secondary_area_unit='sqmeters')
# )
folium.PolyLine(locations=decoded, color="blue").add_to(map2)
print(f"{duration} minutes")
print(f"{distance} miles")
return map2
Geocode Private Campgrounds¶
# geocode('15640 Kingsley Rd, Ninilchik, AK 99639 ')
applied_df = df.apply(lambda row: geocode(row.BusinessAddress), axis='columns', result_type='expand')
df = pd.concat([df, applied_df], axis='columns')
df = df.rename(columns={0:'lat', 1: 'lng'})
# df
df[pd.isnull(df.lat)]
# 61.84590432308156, -147.62491071636222
df.loc[df.CampgroundName == 'Grand View RV Park', ['lat', 'lng']] = [61.84590432308156, -147.62491071636222]
# 63.33500236865673, -142.9639913435805
df.loc[df.CampgroundName == 'Tok RV Village', ['lat', 'lng']] = [63.33500236865673, -142.9639913435805]
df[pd.isnull(df.lat)]
anchorage = geocode('Anchorage, Alaska, USA')
# anchorage
def format_popup(row):
format_str=f"""
<b>{row['CampgroundName']}</b><br/>
{row['BusinessAddress']}<br/>
<a herf="{row['Website']}">{row['Website']}</a><br/>
"""
return format_str
map2 = folium.Map(location=(anchorage[0], anchorage[1]), zoom_start=5, control_scale=True)
# add marker one by one on the map
for idx, row in df.iterrows():
if not pd.isnull(row['lat']) and not pd.isnull(row['lng']):
folium.Marker(
location=[row['lat'], row['lng']],
popup=format_popup(row),
).add_to(map2)
Map of Privately Owned Campgrounds¶
map2
State of Alaska Campgrounds¶
df_1 = df_all[1]
df_1 = df_1[~(df_1['Campground'] == df_1['Winter Open'])].reset_index(drop=True)
df_1 = df_1.fillna('')
df_1.head(5)
df_1['Address'] = df_1['Campground'] + ', ' + df_1['Location'] + ', ' + df_1['Nearest Community'] + ', AK, USA'
# df_1.columns
df = df_1.copy()
applied_df = df.apply(lambda row: geocode(row.Address), axis='columns', result_type='expand')
df = pd.concat([df, applied_df], axis='columns')
df = df.rename(columns={0:'lat', 1: 'lng'})
df
# df[pd.isnull(df.lat)]
def format_popup(row):
format_str=f"""
<b>{row['Campground']}</b><br/>
{row['Address']}<br/>
"""
return format_str
anchorage = geocode('Anchorage, Alaska, USA')
map2 = folium.Map(location=(anchorage[0], anchorage[1]), zoom_start=5, control_scale=True)
# add marker one by one on the map
for idx, row in df.iterrows():
if not pd.isnull(row['lat']) and not pd.isnull(row['lng']):
folium.Marker(
location=[row['lat'], row['lng']],
popup=format_popup(row),
).add_to(map2)
Map of State of Alaska Campgrounds¶
map2