Course contentsShow
← Data Science
Lesson 1311 of 2,14533. Advanced Visualization TechniquesFree lesson

Working with Shapefiles and GeoJSON

Loading and manipulating geographic boundary data in standard formats for use in visualizations.

Working with Shapefiles and GeoJSON

You'll learn how to load and manipulate geographic boundary data in standard formats to power your map visualizations.

What Are Shapefiles and GeoJSON?

When creating choropleth or point maps, you need boundary data that defines where regions begin and end—like country borders, state outlines, or neighborhood zones. Think of these files as cookie cutters that tell your visualization software how to draw geographic shapes on a map.

Shapefiles (.shp + companion files) are an older, industry-standard format developed by Esri. They actually come as a set of files (.shp, .shx, .dbf) that work together—you need all of them in the same folder.

GeoJSON is a newer, web-friendly format built on JSON. It's human-readable text, making it easier to inspect and share. A single .geojson file contains both geometry (the shapes) and properties (like region names and IDs).

Loading Geographic Data

In Python, you typically use libraries like geopandas to read these files:

import geopandas as gpd

# Load a shapefile
states = gpd.read_file('us_states.shp')

# Load GeoJSON
cities = gpd.read_file('major_cities.geojson')

# Preview the data
print(states.head())

Both methods return a GeoDataFrame—like a pandas DataFrame, but with a special geometry column containing the actual shapes.

When to Use Each Format

  • Shapefiles: Common in government datasets, GIS software
  • GeoJSON: Better for web applications, version control, simpler structure

You can convert between them after loading—choose based on your data source, not destination.

Key Takeaway: Shapefiles and GeoJSON are the standard formats for storing geographic boundaries; loading them into a GeoDataFrame gives you both the shapes and associated data you need for mapping visualizations.