Assignment 01: More Practice With GeoPandas
BIO597 Spatial Analysis of Biodiversity
This assignment gives you more practice with the workflow from Lab 01:
- load a CSV file of species occurrence records
- turn longitude and latitude columns into point geometry
- plot occurrence records on an interactive map
- combine multiple species into one
GeoDataFrame - run a few simple spatial summaries
You should expect to write some code in this notebook. Some cells are partly filled in, but you will need to complete the missing pieces.
Remember, learning to code is often times about learning how to strategically copy/paste/modify. At first everything will seem arbitrary, but with repetition it will become common-place and will begin to make sense.
Assignment 01 is due by next Thursday Sept 10th at 5pm
Data
Use the coordinate files in ../labs/EasternSnakes. Each CSV file has the same basic structure:
ID: occurrence record identifierLongitude: east-west coordinateLatitude: north-south coordinate
Because this assignment notebook is in the assignments folder, the file paths below start with ../labs/EasternSnakes/.
1. Import the packages
We need geopandas for geospatial data and pandas for combining tables. Run this cell before running the rest of the notebook.
import geopandas as gpd
import pandas as pd
2. Load the first species table
Start with corn snake, Pantherophis guttatus. The first goal is only to read the CSV file and inspect the first few rows.
Complete the filename in the first line, then run the cell.
pguttatus_csv = "../labs/EasternSnakes/__________"
pguttatus_df = gpd.read_file(pguttatus_csv)
pguttatus_df.head()
3. Convert the first species to a GeoDataFrame
The table has longitude and latitude columns, but those columns are not yet geometry. Use gpd.points_from_xy() to make point geometry.
Fill in the longitude and latitude column names. Look back at the output from the previous cell if you need a reminder.
pguttatus_gdf = gpd.GeoDataFrame(
pguttatus_df,
geometry=gpd.points_from_xy(pguttatus_df["__________"], pguttatus_df["__________"]),
crs="EPSG:4326",
)
pguttatus_gdf["Species"] = "Pantherophis guttatus"
pguttatus_gdf.head()
4. Map the first species
Use .explore() to make a quick interactive map. This is a visual check that the coordinates are being interpreted correctly.
__________.explore()
5. Repeat the workflow for a second species
Now load common kingsnake, Lampropeltis getula. This cell should do two things: read the CSV and show the first few rows.
Fill in the filename.
lgetula_csv = "../labs/EasternSnakes/__________"
lgetula_df = gpd.read_file(lgetula_csv)
lgetula_df.head()
6. Convert the second species to a GeoDataFrame
Use the same pattern from the first species. Make sure the Species column contains the correct scientific name.
lgetula_gdf = gpd.GeoDataFrame(
lgetula_df,
geometry=gpd.points_from_xy(lgetula_df["Longitude"], lgetula_df["Latitude"]),
crs="EPSG:4326",
)
lgetula_gdf["Species"] = "__________"
lgetula_gdf.head()
7. Map the second species
Create an interactive map for Lampropeltis getula.
# Replace the blank with the GeoDataFrame you created above.
__________.explore()
8. Add a third species
Choose coachwhip, Masticophis flagellum. This time, write most of the code yourself.
Your cell should:
- load
Mflagellum_coords.csv - convert it to a
GeoDataFrame - add a
Speciescolumn - show the first few rows
# Write your code here.
# Use the same pattern from the first two species.
9. Map the third species
Make an interactive map for the third species.
# Write one line of code using .explore().
10. Combine the three species
Use pd.concat() to combine the three GeoDataFrames into one object. Keep this cell focused on combining the data and checking the result.
combined_snakes = pd.concat([__________, __________, __________])
combined_snakes.head()
11. Map all three species together
Map the combined data. Color the points by the Species column so the species are easier to compare. Choose a different colormap to color these points from the python colormap reference.
combined_snakes.explore(column="__________", cmap="__________")
12. Count the records for each species
A very basic summary is the number of occurrence records per species. Use groupby() with the Species column.
record_counts = combined_snakes.groupby("__________").size()
record_counts
13. Find the overall coordinate bounds
total_bounds gives the smallest rectangle that contains all the points. The output is four numbers: minimum longitude, minimum latitude, maximum longitude, and maximum latitude.
Run the cell, then answer the question in the markdown cell below it.
combined_snakes.total_bounds
Question: Based on total_bounds, what is the approximate western edge, eastern edge, southern edge, and northern edge of these combined records?
Your answer:
14. Project the data before measuring area
Longitude and latitude are measured in degrees. Area measured in degrees is not easy to interpret, so project the data to EPSG:5070 before calculating polygon areas.
combined_projected = combined_snakes.to_crs("__________")
combined_projected.crs
15. Make a convex hull for each species
A convex hull is a simple polygon drawn around a set of points. It is not the true species range, but it is a useful first practice tool.
This cell does three things: groups records by species, turns each group into a convex hull, and maps the hulls.
hulls_projected = combined_projected.dissolve(by="Species")
hulls_projected = hulls_projected.reset_index()
hulls_projected["geometry"] = hulls_projected.geometry.convex_hull
hulls = hulls_projected.to_crs(combined_snakes.crs)
hulls.explore(column="Species", cmap="rainbow")
16. Calculate convex hull area
hulls_projected.area gives area in square meters because EPSG:5070 uses meters. Divide by 1,000,000 to convert square meters to square kilometers.
Fill in the missing column name.
hulls["convex_hull_area_km2"] = hulls_projected.area / 1000000
hulls[["__________", "convex_hull_area_km2"]]
Question: Which species has the largest convex hull area in your result? Give one biological or sampling reason why a species might have a larger hull area than another species.
Your answer:
17. Choose one more species
Choose one additional CSV file from ../labs/EasternSnakes that you did not already use. Load it, convert it to a GeoDataFrame, add a Species column, and map it.
This is meant to check that you can repeat the workflow without step-by-step scaffolding.
# Write your code here.
18. What are one or two other interesting biological questions you could ask about this data?
# Write your questions here.
19. Submit your work
Before submitting, make sure you have run the notebook from top to bottom and answered the written questions.
Commit and push your completed notebook to your class GitHub repository.