Check to see if you have folium already:
import foliumIf 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'))
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.