Today's lab will focus on looping through data files to make maps.
Software tools needed: web browser and Python IDLE programming environment.
We can use the techniques from last lab to map and different scales. Let's start by mapping cities of the world. Locations in the world are usually indicated by their latitude and longitude.
In the trinket below, map the following cities:
Check to see if you have folium already:
import foliumIf not, go to the terminal, and download it:
pip install folium
To make a map in folium, the process is:
Here's our first program:
#Import the folium package for making maps import folium #Create a map, centered (0,0) and zoomed out a bit: mapWorld = folium.Map(location=[0, 0],zoom_start=3) #Save the map: mapWorld.save(outfile='tempMap.html')
Save this file and run it. It will create a file called tempMap.html. Open your favorite browser (Chrome, Firefox, IE, etc.) and choose "Open File" and then choose the file tempMap.html. You should see a map of the world.
Let's make another map, focused on New York City. To do that, when we set up the map object, we need to reset the location to New York City and the increase the zoom level:
import folium mapCUNY = folium.Map(location=[40.75, -74.125],zoom_start=10)
Let's add in a marker for Lehman College:
folium.Marker(location = [40.873442, -73.889361], popup = "Lehman College").add_to(mapCUNY)
and create the .html file:
Let's make an interactive map of the the CUNY campuses. Instead of typing every location in by hand, we can use a file. First, download a CSV file from data.ny.gov:
Let's use Pandas to read in the file. We will need to import pandas and folium:
import folium from folium.plugins import MarkerCluster import pandas as pdNote that we're explicitly importing MarkerCluster from the Folium plugins (we're going to use it to cluster markers together as the map zooms out).
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. First, we'll create empty lists to hold the locations, the popup messages, and the icons:
#Create lists to hold coordinates and popups: coords =  popups =  icons = We're going to iterate through the rows of dataframe to create the markers:
#For each row in the CSV file: for index,row in cuny.iterrows(): #Extract the data: lat = row["Latitude"] lon = row["Longitude"] name = row["Campus"] #Add the [lat,lon] to list of coordinates: coords.append([lat,lon]) #Add the names to the popup list> popups.append(name) if row["College or Institution Type"] == "Senior Colleges": icons.append(folium.Icon(icon='cloud')) else: icons.append(folium.Icon(color='green'))Note that we're putting in different icons, depending on whether its a senior college or not.
Once we have everything set up, let's add all the markers at once:
#Add all the markers at once: mapCUNY.add_children(MarkerCluster(locations=coords, popups = popups, icons=icons))
Lastly, let's save our map:
To view your map, open a browser. From the browser, open the file: cunyLocations.html.
If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.