Skip to content

Download the Jupyter notebook

Assignment 05: Raster Data and Environmental Covariates

BIO597 Spatial Analysis of Biodiversity

This assignment asks you to repeat and extend the raster workflow from Lab 05. You will clip WorldClim rasters to Maine, extract environmental values for selected amphibian occurrences, compare species, and document your predictor choices.

Deliverable

Submit a completed notebook containing code, raster maps, an occurrence-environment table, and species summaries.

Learning goals

By the end of this assignment, you should be able to:

  • inspect raster metadata and identify alignment problems
  • clip global rasters to a study-area polygon
  • extract raster values at point locations
  • recognize and retain NoData values
  • select predictors based on ecological meaning rather than availability alone
  • document a reproducible environmental-data workflow

Data

Use:

  • Amphibians_ME/Amphibians_ME.csv for occurrence records
  • /home/jovyan/shared-readwrite/bioclim for WorldClim 2.1 bioclimatic rasters
  • ../labs/ME_ShapeFiles/Maine_State_Boundary.shp for the clipping boundary

The WorldClim files represent 1970–2000 climate normals at 2.5 arc-minute resolution. Variable definitions are available from the WorldClim bioclimatic variable page.

1. Import packages

Import pandas, geopandas, rioxarray, and xarray using the same abbreviations as Lab 05. Also import Path from the pathlib module as we did previously

# Import the four packages here.

2. Load and combine the Maine boundary

Read Maine_State_Boundary.shp. Inspect its CRS and shape, then use dissolve() to combine the polygons.

boundary_path = "../labs/ME_ShapeFiles/Maine_State_Boundary.shp"

# Read the boundary.
# Print its CRS and shape.
# Dissolve it to one row.

3. Open annual mean temperature

Open wc2.1_2.5m_bio_1.tif with masked=True. Store the result as bio1.

bioclim_path = Path("/home/jovyan/shared-readwrite/bioclim")
bio1_path = bioclim_path / "wc2.1_2.5m_bio_1.tif"

bio1 = rxr.open_rasterio(
    __________,
    masked=__________,
)

bio1

4. Inspect annual mean temperature metadata

Report:

  • dimensions and shape
  • data type
  • CRS
  • resolution
  • bounds
  • NoData value

Add a brief comment beside each output explaining what it describes.

# Print the requested raster metadata here.

5. Clip BIO1 to Maine

Use rio.clip() with the dissolved boundary. Set drop=True and from_disk=True, then remove the one-band dimension with squeeze().

bio1_maine = bio1.rio.clip(
    __________.geometry,
    __________.crs,
    drop=__________,
    from_disk=__________,
)

bio1_maine = bio1_maine.__________()

6. Compare the global and clipped rasters

Display the shape, bounds, CRS, and resolution of both. State which properties changed and which remained the same.

# Compare global and clipped metadata here.

Your comparison:

7. Plot annual mean temperature for Maine

Use the DataArray's .plot() method and choose an appropriate colormap.

# Plot bio1_maine here.

8. Choose three additional bioclimatic variables

Choose three variables from BIO2 through BIO19. Include at least one temperature variable and at least one precipitation variable.

Do not choose variables only because they are available. Give a short biological reason for each choice.

Variable 1 and justification:

Variable 2 and justification:

Variable 3 and justification:

9. Open the first additional raster

Build its filename, open it with masked=True, and inspect its CRS, shape, resolution, and bounds.

# Open your first additional raster.
# Inspect its metadata.

10. Check alignment with BIO1

Compare CRS, shape, resolution, and bounds. Each comparison should return True before you treat the rasters as an aligned stack.

# Compare the first additional raster with bio1.

11. Clip and map the first additional raster

Use the same clipping settings as BIO1. Plot the Maine result with an appropriate colormap.

# Clip, squeeze, and plot the first additional raster.

12. Repeat for the second additional raster

Open the raster, confirm alignment, clip it to Maine, and plot it. Keep each part of the workflow visible in your code.

# Complete the workflow for the second additional raster.

13. Repeat for the third additional raster

Open the raster, confirm alignment, clip it to Maine, and plot it.

# Complete the workflow for the third additional raster.

14. Load the amphibian records

Read the tab-delimited occurrence file. Keep gbifID, species, longitude, latitude, and year.

amphibians = pd.read_csv(
    "Amphibians_ME/Amphibians_ME.csv",
    sep=__________,
    low_memory=False,
)

# Keep the five requested columns.

15. Prepare the occurrence coordinates

Convert longitude and latitude to numeric columns. Remove records missing species or coordinates.

# Convert both coordinate columns with pd.to_numeric().
# Drop rows missing species, longitude, or latitude.

16. Choose three amphibian species

Inspect the species counts, then choose three species with enough records for comparison.

amphibians["species"].value_counts().head(20)

Species 1:

Species 2:

Species 3:

selected_species = [
    "____________________________",
    "____________________________",
    "____________________________",
]

# Filter the occurrence table with .isin(selected_species).

17. Create an occurrence GeoDataFrame

Use longitude and latitude to create point geometry. Assign EPSG:4326.

# Create selected_amphibians_gdf here.

18. Confirm coordinate systems

Print the occurrence CRS and raster CRS. Explain what must be true before extracting raster values.

# Print both coordinate systems.

Your explanation:

19. Prepare coordinates for extraction

Create xarray.DataArray objects called point_x and point_y. Use one dimension named record.

# Create point_x from point longitude.
# Create point_y from point latitude.

20. Extract annual mean temperature

Use .sel() with x, y, and method="nearest". Add the extracted values to a clearly named column in the occurrence GeoDataFrame.

bio1_values = bio1_maine.sel(
    x=__________,
    y=__________,
    method=__________,
)

# Add the values to selected_amphibians_gdf.

21. Inspect missing extracted values

Count missing BIO1 values. Explain why coastal occurrences may be assigned NoData even when the coordinates are valid.

# Count missing extracted BIO1 values.

Your explanation:

22. Extract the three additional variables

Use the same point_x and point_y arrays. Add one clearly named column for each variable. The column names should include units where practical.

# Extract values from the first additional clipped raster.
# Add the values to selected_amphibians_gdf.
# Extract values from the second additional clipped raster.
# Add the values to selected_amphibians_gdf.
# Extract values from the third additional clipped raster.
# Add the values to selected_amphibians_gdf.

23. Create the occurrence-environment table

Remove the geometry column and display the first five rows. Confirm that the table contains species, coordinates, year, and four environmental variables.

# Create and display occurrence_environment.

24. Summarize environmental values by species

For each selected species, calculate:

  • number of records
  • mean of each environmental variable
  • standard deviation of each environmental variable
# Calculate species-level summaries here.

25. Check relationships among predictors

Create a correlation table for your four environmental variables. Identify any pair with a strong relationship.

# Select the four predictor columns and calculate .corr().

Interpretation: Which predictors contain similar information? Would you retain all of them in the same model? Explain your reasoning.

27. Save your occurrence-environment table

Save it as Assignment05_environmental_values.csv beside this notebook. Do not include the DataFrame index.

# Save occurrence_environment with .to_csv().

Final reflection

Answer each question in two or three sentences.

  1. How do raster resolution and occurrence-coordinate uncertainty interact?
  2. What environmental variation is lost when all occurrences in one raster cell receive the same value?
  3. Which predictor has the strongest biological justification for your selected species?
  4. Which preprocessing choice is most important to document for reproducibility?
  5. Name one additional environmental predictor you would seek for an amphibian study and explain why.

Submit your work

Run the notebook from top to bottom. Make sure all raster maps, tables, and written answers are complete. Then commit and push the completed notebook to your class GitHub repository.

git add docs/assignments/Assignment-05-RasterData.ipynb
git commit -m "Complete raster data assignment"
git push