GISPython

Spatial Analysis with GeoPandas — Easier Than You Think!

Spatial analysis doesn’t have to involve expensive GIS software or complex tools. With just Python and the GeoPandas library, you can perform geographic calculations, create maps, and analyze spatial relationships — all within minutes and with just a few lines of code. In this post, I’ll walk you through how to get started with GeoPandas using vector data and simple code examples.

To begin, install GeoPandas using the following command:

pip install geopandas

Once installed, we can load spatial data — for example, administrative boundaries of Polish provinces from a .shp file — using the read_file function:

import geopandas as gpd

provinces = gpd.read_file("wojewodztwa.shp")
print(provinces.head())

After loading the data, it’s a good idea to check the coordinate reference system (CRS). For example:

print(provinces.crs)

If the data is in a geographic CRS (like EPSG:4326) and we want to perform measurements in meters, we can convert it to a projected CRS like EPSG:2180 (commonly used in Poland) using:

provinces = provinces.to_crs("EPSG:2180")

Now let’s say we want to select just the Mazowieckie province and calculate its area. This can be done with simple filtering:

mazovia = provinces[provinces["nazwa"] == "mazowieckie"]
print(mazovia.geometry.area / 10**6)  # area in square kilometers

Let’s now add point data to our analysis — for instance, weather stations stored in a GeoJSON file:

points = gpd.read_file("stacje_pogodowe.geojson")
points = points.to_crs(provinces.crs)

We can check which stations are located within the Mazowieckie province using the within() method:

mazovia_points = points[points.within(mazovia.geometry.values[0])]

If we want to find objects within a 10 km radius of a given station, we can create a spatial buffer. Here’s how to do that for the first point:

buffer_10km = points.geometry.iloc[0].buffer(10000)

Then we can check which other points fall within this buffer:

nearby = points[points.geometry.within(buffer_10km)]

To calculate the distance between all stations and the center of the province, we compute the centroid and use the distance() method:

center = mazovia.geometry.centroid.values[0]
points["distance_km"] = points.geometry.distance(center) / 1000

Finally, it’s useful to visualize our data. Thanks to built-in integration with Matplotlib, just a few lines of code are enough to generate a simple map:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 10))
provinces.plot(ax=ax, edgecolor='black', facecolor='none')
mazovia.plot(ax=ax, facecolor='lightblue')
mazovia_points.plot(ax=ax, color='red', markersize=20)
plt.title("Weather Stations in Mazowieckie Province")
plt.show()

Save all weather stations in Mazowieckie to a new GeoJSON file like this:

mazovia_points.to_file("mazowieckie_stations.geojson", driver="GeoJSON")

As you can see, spatial data analysis in Python can be both intuitive and powerful. GeoPandas allows you to work with vector data in DataFrame format while providing access to spatial operations like overlays, area and distance calculations, filtering, and basic visualization — all within the comfort of the Python ecosystem.