3

There are answers for how to get the geometry of the smallest enclosing circle with QGIS, ArcGIS, and PostGIS, but I haven't come across any way to create a smallest enclosing circle with GeoPandas.

The closest I've found is GeoSeries.envelope method (link). But this creates a smallest enclosing rectangle, NOT a circle. (A similar method is GeoSeries.convex_hull but still does not return a circle.)

I've also tried using the GeoSeries.buffer method with a distance great enough to cover the entire polygon I'm trying to enclose in a circle. However, this was done with trial and error, and there should be a way to automate this as easily as one could with GeoSeries.envelope.

How could one go about creating a smallest enclosing circle with GeoPandas?

Austin Wolff
  • 205
  • 1
  • 6

2 Answers2

4

It could be done like this

import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
s = gpd.GeoSeries(
    [
        Polygon([(0.2, 0), (0, 0.8), (0.6, 1), (0.8, 0.2)])        
    ]
)
#generate geoseries of maxx and maxy of each polygon's envelope
t = gpd.GeoSeries(gpd.points_from_xy(s.envelope.bounds.maxx, s.envelope.bounds.maxy))
fig, ax = plt.subplots()
#circle with radius of distance from centroid of envelope to it's maxx, maxy 
s.envelope.centroid.buffer(s.envelope.centroid.distance(t)).plot(ax=ax, color='green', alpha=0.5)
#envelope of the polygon
s.envelope.plot(ax=ax, color='magenta', alpha=0.4)
#polygon
s.plot(ax=ax, color='red', alpha=0.4)
#points of envelope's maxx, maxy
t.plot(ax=ax, color='blue')
plt.show()
Pepe N O
  • 676
  • 4
  • 10
3

There's minimum bounding circle:

import geopandas as gpd
import matplotlib.pyplot as plt

df = gpd.read_file(r"C:/GIS/GIStest/zones.shp") df["circle"] = df.geometry.minimum_bounding_circle()

fig, ax = plt.subplots(figsize=(15,15)) df.plot(ax=ax, color="blue") df.circle.boundary.plot(ax=ax, color="red", linewidth=10)

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161