0

I have a shapefile that has many polygons, here is an example: enter image description here

Now, what I want to do is show the polygons that are around one particular location. So I don't have to show all the polygons.

So I began to try something, like creating a circle (we can also do with a rectangle), and try to limit/cut the polygons, or at least, find the only polygons in the circle.

import folium
import geopandas as gpd
from shapely.geometry import Point
center_location = (48.8566, 2.3522)
m = folium.Map(location=center_location, zoom_start=12)
circle_radius = 5000
center_point = Point(center_location)
circle = center_point.buffer(circle_radius)
polygons_in_circle = gdf[gdf.geometry.intersects(circle)]
polygons_in_circle

Could you please help me?

2
  • Depending on what you need, you can also filter already when reading the file using the bbox parameter: df = geopandas.read_file("nybb.shp", bbox=(0, 0, 10, 20)). If your file has a spatial index this will also be relatively fast.
    – Pieter
    Commented Aug 24, 2023 at 18:15
  • What should I do when someone answers my question?
    – Bera
    Commented Sep 3, 2023 at 15:44

1 Answer 1

0

I dont know of a way to read a circular subset from a file without first reading the full dataset.

You can read a rectangular subset using the bbox parameter:

Filter features by given bounding box, GeoSeries, GeoDataFrame or a shapely geometry

import geopandas as gpd
import shapely

#Read all data from a gpkg
file = r"/home/bera/Desktop/gistest/hexgrid.gpkg"
df_no_box = gpd.read_file(file)
ax = df_no_box.plot(figsize=(10,10), color="yellow")

#Read a rectangular subset of the same gpkg
ymin, xmin = 6407201,393160
ymax, xmax = 6514843,570974
df_box = gpd.read_file(filename=file, bbox=(xmin, ymin, xmax, ymax))
df_box.plot(ax=ax, color="blue")

#Create a circle dataframe and spatial join to the full dataset to only keep intersecting features
circle = shapely.geometry.Point(587098, 6356559).buffer(30000)
circleframe = gpd.GeoDataFrame(geometry=[circle], crs=df_no_box.crs)
df_circle = gpd.sjoin(df_no_box, circleframe, how="inner")
df_circle.plot(ax=ax, color="red")

enter image description here

1
  • Check out the mask parameter. More info here
    – Pieter
    Commented 14 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.