Protected areas code refactoring

Week 7 - Discussion section

In this discussion section you will practice refactoring code. Refactoring means improving the structure or readability of your coude without changing what it does. The goal is to make code cleaner, easier to understand, and easier to maintain. For example, you might refactor by:

Though the main goal when we are programming is to make our code do what we want, refactoring is a good habit that can make it easier to build upon our work and share it with others.

Setup

  1. Download the notebook week7-refactoring.ipynb from the course’s Google Drive and move it inside your eds-220-sections directory.

  2. Download the zip file WDPA_Nov2025_Latam.zipfrom the course’s Google Drive, unzip it, and add it to the data directory in your eds-220-sections directory.

  3. Run the week7-refactoring.ipynb notebook to make sure you can access the data.

About the data

The World Database on Protected Areas (WDPA) is the only global database of natural protected areas, it can be viewed and downloaded at www.protectedplanet.net. WDPA is a joint project of the United Nations Environment Programme World Conservation Monitoring Centre (UNEP-WCMC) and the International Union for Conservation of Nature (IUCN) [1]. This dataset includes both marine and terrestrial protected areas, combining both spatial and attribute data [2].

Image from the WDPA data viewer at www.protectedplanet.net.

The data used in this exercise is a subset of the original dataset and it only includes polygons for protected areas in Latin America (the original dataset is global with both polygon and point data). This subset was prepared for educational purposes and can be downloaded from the course’s Google Drive through the zip file WDPA_Nov2025_Latam.zip. It maintains the file structure of the original complete dataset.

1. Data loading exploration

  1. This dataset is exceptionally well documented. Take time to look through the PDFs associated with the data and investigate what the values for each column represent.

  2. Use the data viewer in the WDPA website to briefly explore the data over Central America.

2. Plan refactoring

In this step you will not run or write any code. The goal is to help you inspect code and make a plan without the temptation to start coding or running any cells!

  1. Imagine you are given this preliminary analysis a colleague has been working and a last bit that they left unfinished. Take time to read the code to understand what is being done.

  2. This exploratory analysis has code that can be streamlined. With your group, discuss some ways in which you could streamline the code to avoid repetitions that may make this code harder to update. You could update the code in any way you want.

  3. Write down the high-level steps you will implement in your refactoring.

3. Refactor!

  1. Work with your group to refactor the code to calculate the percentage of protected areas and obtain the exact same print out:
Total protected area in Belize (sq. km): 10,181.87
Total protected area in Costa Rica (sq. km): 199,170.72
Total protected area in El Salvador (sq. km): 5,701.12
Total protected area in Guatemala (sq. km): 40,316.17
Total protected area in Honduras (sq. km): 96,103.89
Total protected area in Nicaragua (sq. km): 84,985.39
Total protected area in Panama (sq. km): 133,553.40

4. Finish percentage coverage

The colleague writing this code ran out of steam and the calculations for the percentage of protected area relative to each country’s land area was not finished.

  1. Update your code to print the corresponding percentages for each Central American country.

5. Investigate issues

Discuss the following with your team:

  • Do these percentages make sense? Why or why not?
  • Are there known data quality or use recommendations this colleague should have taken into account since the start of the analysis? Take some time to look through the WDPA_WDOECM_Manual file.
  • How should the analysis change if we wanted to calculate the percentage of terrestrial protected area per country accurately?

Remember: Geospatial analysis should be done carefully and with consideration of the multiple ways the data interact with each other. Rarely does a straightforward analysis capture the nuance that this kind of dataset may have!

References

[1]
UNEP-WCMC, User manual for the world database on protected areas and world database on other effective area-based conservation measures: Version 1.6. Cambridge, UK: UNEP-WCMC, 2019. Available: http://wcmc.io/WDPA_Manual
[2]
UNEP-WCMC and IUCN, “Protected planet: The world database on protected areas (WDPA) [on-line], november 2025.” https://www.protectedplanet.net; UNEP-WCMC and IUCN, Cambridge, UK, 2025.