Today's lab will focus on mapping data, using pandas, numpy, and folium.
Software tools needed: web browser and Python IDLE programming environment with the pandas, numpy, and folium package installed.
We are going to use Python to make maps. 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 (GIS coordinates are rounded to the nearest whole number):
(Folium is installed on the lab machines, To check to see if your home machine has it, type at the Python prompt:
If you get an error, go to the terminal, and download it:
pip install foliumand the package will download and install.)
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 Hunter College:
folium.Marker(location = [40.768731, -73.964915], popup = "Hunter College").add_to(mapCUNY)
and create the .html file:
mapCUNY.save(outfile='nycMap.html')Save your commands to a file and run via IDLE. Your program will create an HTML file called, nycMap.html. Open it in your favorite browser to make sure it creates a map of NYC with a marker for Hunter College. When you have a running program, see the Programming Problem List.
Let's make an interactive map of the CUNY campuses. We can 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 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"])Note: we saved our CSV file to 'cunyLocations.csv'. If you saved it to a different name, change the input parameters for read_csv() to the name of your file.
Next, let's set up a map, centered on Hunter College:
mapCUNY = folium.Map(location=[40.768731, -73.964915])
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"] newMarker = folium.Marker([lat, lon], popup=name) newMarker.add_to(mapCUNY)For each row in the file, we find the latitude, longitude, and name of the campus, and use those to create a new marker which we add to our map. We repeat for each row, until we have made markers for all 23 campuses in the file.
Lastly, let's save our map:
To view your map, open a browser. From the browser, open the file: cunyLocations.html.
In Lab 8, we introduced github which can be used to share and store code, websites, etc. (roughly, a "Google Docs" for programs). The website for this course as well as all the programs are stored in repositories (or "repos") at github.
The github site has both a web browser interface (which we used last lab), and the option to use the command line interface to interact with the site. For this course, we will only retrieve code from existing repos (in future courses, they will introduce how to share your code and merge your changes with others).
From Program #35 onward, all sample programs are available at the class repository (repo):
git clone https://github.com/stjohn/csci127This command will make a copy of all the programs in the class repo in directory called, csci127, in your current working directory.
If you have that directory already and would like to update it with new files that have been (we periodically update the repo), you can 'fetch' the changes:
git fetch https://github.com/stjohn/csci127
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.