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:
- renaming variables to make their purpose clearer
- grouping variables into more transparent data structures
- removing hard-coded information
- creating functions to take care of repeated tasks
- breaking down long functions into smaller ones
- simplifying complex logic flows
- streamlining repeated code.
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
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].

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
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.
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!
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.
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.
Write down the high-level steps you will implement in your refactoring.
3. Refactor!
- 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.404. 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.
- 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?