PYTHON

A Tutorial On geopandas

Date Published:
Last Modified:

Overview

geopandas is a Python library that adds extra functionality to pandas to make it more useful for dealing with geospatial data.

Most of the code examples below assume you have imported geopandas into the current module with:

import geopandas

Plotting

geopandas makes it very easy to plot geospatial data onto graphs.

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.plot(figsize=(10,10))
The plot generated by the 'naturalearth_lowres' dataset that is bundled with geopandas.

The plot generated by the 'naturalearth_lowres' dataset that is bundled with geopandas.

If you want to save the plot, you can import matplotlib.pyplot and use the standard save_fig() function:

import matplotlib.pyplot as plt
plt.save_fig('plot.png')

Common Warnings/Errors

Missing package rtree

/usr/local/lib/python3.7/site-packages/geopandas/base.py:116: UserWarning: Cannot generate spatial index: Missing package `rtree`.

You are missing the package rtree. rtree can not usually easily be installed with pip alone because it depends on a number of dependencies that pip does not manage. It is best to use your systems package manager to install rtree instead.

No module named descartes

ModuleNotFoundError: No module named 'descartes'

Running pip install geopandas does not install the dependency descartes automatically. descartes is needed when plotting, for example when you call .plot(). Install with:

pip install descartes

Like this page? Upvote with shurikens!

Related Content:

Tags:

comments powered by Disqus