Table of Contents
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
# https://www.opendatanetwork.com/dataset/datahub.smcgov.org/dmz9-a27g
# df_clinic = pd.read_json('https://datahub.smcgov.org/resource/dmz9-a27g.json', orient='columns')
# https://dev.socrata.com/foundry/data.cdc.gov/pvgy-252u
# df_mal = pd.read_json('https://data.cdc.gov/resource/pvgy-252u.json', orient='columns')
# https://www.opendatanetwork.com/dataset/data.cityofnewyork.us/w9ei-idxz
df_fluvac = pd.read_json('https://data.cityofnewyork.us/resource/w9ei-idxz.json', orient='columns')
clinic_data = {
'X Coordinate' : [],
'Y Coordinate' : [],
'Phone' : [],
'Address' : [],
'Name' : [],
'ZIP Code' : [],
}
# Explanation for how to covert lon/lat to mercator coordinates
# https://gis.stackexchange.com/questions/156035/calculating-mercator-coordinates-from-lat-lon/156046
def merc(lat, lon):
r_major = 6378137.000
x = r_major * math.radians(lon)
scale = x/lon
y = 180.0/math.pi * math.log(math.tan(math.pi/4.0 + lat * (math.pi/180.0)/2.0)) * scale
return (x, y)
# Loop goes through all given vaccine clinics and parses the following data points into a dictionary
for i in range(len(df_fluvac['latitude'])):
# Takes given coordinates and coverts to mercator for OSM plotting
cord = merc(df_fluvac['latitude'][i], df_fluvac['longitude'][i])
# Clinic name
clinic_data['Name'].append(df_fluvac['facility_name'][i])
# Clinic phone number, address and ZIP code
clinic_data['Phone'].append(df_fluvac['phone'][i])
clinic_data['Address'].append(df_fluvac['address'][i])
clinic_data['ZIP Code'].append(df_fluvac['zip_code'][i])
# Coordinates for the given clinic
clinic_data['X Coordinate'].append(cord[0])
clinic_data['Y Coordinate'].append(cord[1])
# Stores dataset created above in a dataframe for easy interpretation
dataset = pd.DataFrame.from_dict(clinic_data)
dataset
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, output_notebook
from bokeh.models import HoverTool
from bokeh.tile_providers import get_provider, Vendors
from bokeh.io import show
output_notebook()
tile_provider = get_provider(Vendors.CARTODBPOSITRON)
source=ColumnDataSource(clinic_data)
# Determines size of map to create given the
# most extreme longitude and latitude from all clinic locations
x_range=(min(clinic_data['X Coordinate']),max(clinic_data['X Coordinate']))
y_range=(min(clinic_data['Y Coordinate']),max(clinic_data['Y Coordinate']))
# Determines how to construct the map and tools for user interaction
plot = figure(title='Disease Map',
# Tools the user can control from right sidebar
tools='pan,wheel_zoom',
# Range used to determine plot size
x_range=x_range, y_range=y_range,
# Type of coordinates to plot
x_axis_type="mercator", y_axis_type="mercator",
# Size of the map
width=600, height=600)
# Configure Open Street Maps to be our mapping provider
plot.add_tile(tile_provider)
# Plot circles at the coordinates for each given clinic
plot.circle(x="X Coordinate", y="Y Coordinate", size=5, fill_color="green", fill_alpha=0.8, source=source)
# When the cursor hovers over a given clinic, the name, phone number and address
# will be taken from our dataframe and referenced in the plot.
plot.add_tools(HoverTool(tooltips=[
("", "@Name"),
("Phone", "@Phone"),
("Address", "@Address"),
]))
# Display map inside of this page
show(plot, notebook_handle=True)
"""
Change this variable to any ZIP code within NYC to determine nearby clinics.
This will not work on a published page, the Jupyter Notebook server must be running
for code execution to occur.
"""
given_zip = 10016
# Dataset of all nearby clinics will be stored in this dictionary
nearby_clinics = {
'Clinic Name' : [],
'Address' : [],
}
# Parse all clinics displayed in the Bokeh OSM plot above to determine which ones
# are nearby according to ZIP code
for i in range(len(clinic_data['ZIP Code'])):
if clinic_data['ZIP Code'][i] == given_zip:
# Store dataframe clinic data in nearby clinic dictionary if nearby
nearby_clinics["Clinic Name"].append(clinic_data['Name'][i])
nearby_clinics["Address"].append(clinic_data['Address'][i])
# Produce new dataframe for our nearby clinics
dataset = pd.DataFrame.from_dict(nearby_clinics)
dataset