Classwork: folium

MHC 250/Seminar 4:
Shaping the Future of New York City
Spring 2017

Comparing Neighborhoods

There are multiple rankings of New York City Neighborhoods. For example, With your group:

Getting Started with Folium

Folium is a Python package that uses the javascript Leaflet.js library to make beautiful interactive maps. Instead of popping up a matplotlib window, folium creates an .html file that you can open (and view interactively) with a browser. After the program runs, open the corresponding html file in a web browser to see your map.

Check to see if you have folium already:

import folium
If not, go to the terminal, and download it:
pip install folium

Work through the folium examples through adding markers (stop before the Vincent/Vega Markers section-- we will cover more in future classes).

These have moved since this classwork was written, a similar version is at . Note that the rewrite of the examples assumes that you are using jupyter and displays in-line. To save an .html file to diplay, you need to save it (mapName.save(outfile='htmlFile.html'))

Challenges

Plotting from Files

We can combine the mapping of folium with the tools we have used for CSV files.

Let's make an interactive map of the the CUNY campuses. We can download a CSV file from data.ny.gov:

(Export as a .csv file and save in the same directory as your programs.) Open the file to make sure you have all the lines (should be 23) and to check if the column headings occur in the first row (they do, so no need to skip rows when reading in the file).

Let's use Pandas to read in the file. We will need to import pandas and folium:

import folium
import pandas as pd

To read in the CSV file, we'll use pandas' csv reader. We'll print out the campus locations to make sure that all were read in:

cuny = pd.read_csv('cunyLocations.csv')
print (cuny["Campus"])

Next, let's set up a map, centered on New York City:

mapCUNY = folium.Map(location=[40.75, -74.125])

We need to add markers for each campus. We're going to iterate through the rows of dataframe to create the markers:

for index,row in cuny.iterrows():
    lat = row["Latitude"]
    lon = row["Longitude"]
    name = row["Campus"]
    mapCUNY.simple_marker([lat, lon], popup=name)

Lastly, let's save our map:

mapCUNY.save(outfile='cunyLocations.html')

To view your map, open a browser. From the browser, open the file: cunyLocations.html.