Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Basic Spatial Operations | Introduction to Geospatial Data
Geospatial Analysis with Python

Basic Spatial Operations

Swipe to show menu

Working with geospatial data often begins with the ability to filter, select, and visualize features based on their attributes or spatial properties. Using the geopandas library, you can perform these basic spatial operations efficiently. Filtering allows you to focus on features of interest within a larger dataset, while plotting helps you interpret the spatial patterns and relationships visually.

To filter spatial data, you commonly use boolean indexing and the .loc accessor in geopandas. This enables you to select rows that meet specific criteria, such as all features with a certain attribute value.

12345678910111213141516171819202122
import geopandas as gpd import matplotlib.pyplot as plt # 1. Load the dataset using a direct URL to the public Natural Earth GeoJSON world_url = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_0_countries.geojson" world = gpd.read_file(world_url) # 2. Filter: Select only countries in South America south_america = world[world['CONTINENT'] == 'South America'] # 3. Plotting ax = world.plot(color='lightgray', edgecolor='white', figsize=(10, 6)) # Overlay South American countries in green south_america.plot(ax=ax, color='forestgreen', edgecolor='black') # Customize the map plt.title("South America Highlighted on World Map", fontsize=14) plt.axis('off') # Hide the lat/long grid lines for a cleaner look # Show the plot plt.show()

After filtering your data, visualizing it on a map is an essential step in geospatial analysis. geopandas integrates seamlessly with matplotlib, allowing you to create rich, informative maps. You can customize the colors of features based on attribute values and add legends to make your plots more meaningful.

123456789101112
import matplotlib.pyplot as plt # Plot all world countries in light gray ax = world.plot(color='lightgray', edgecolor='white', figsize=(10, 6)) # Overlay South American countries in green with a legend south_america.plot(ax=ax, color='forestgreen', edgecolor='black', label='South America') # Add a title and legend plt.title("Countries of South America") plt.legend() plt.show()
question mark

Which of the following statements best describes what you would expect to see in the plot after filtering for South American countries and customizing the visualization as shown above?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 4
some-alt