GeoPandas: installation, first steps
GeoPandas combines the functionality of Pandas and Shapely, making it easy to work with geographic data.
In this post, I’ll show you how to install GeoPandas and how to load and save geospatial data.
GeoPandas requires several dependencies, including libraries such as fiona, shapely, and pyproj, which can sometimes be tricky to install. That’s why the safest method is to use conda.
If you have Anaconda or Miniconda installed:
conda install geopandas
This command will automatically install all the required dependencies.
If you prefer to use pip, run:
pip install geopandas
You may need to manually install some dependencies like fiona
, shapely
, or pyproj
, for example:
pip install fiona shapely pyproj
GeoPandas supports many file formats such as Shapefile, GeoJSON, GPKG, and KML. The most commonly used are .shp
and .geojson
.
Loading a Shapefile takes just one line of code:
import geopandas as gpd
# Load shapefile
gdf = gpd.read_file("C:/data/area.shp")
# Preview data
print(gdf.head())
print(gdf.crs) # coordinate reference system
Loading a GEOJSON file works the same way:
gdf = gpd.read_file("C:/data/areas.geojson")
After processing, spatial data can easily be saved to a new file using:
gdf.to_file("C:/output/modified_areas.shp")
gdf.to_file("C:/output/modified_areas.geojson", driver="GeoJSON")
GeoPandas is a powerful tool for working with geospatial data that allows you to:
- Easily read and write data in various formats
- Work with DataFrames that include geometry, similar to Pandas
- Integrate with libraries like Matplotlib, Folium, or Contextily