Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract coordinates falling in a bbox from a geopandas data frame?

I have a geopandas dataframe with coordinates and long with data frame I have a bbox. Now I want to apply the bbox on the data frame and extract the coordinates that’s falling in that bbox. I tried using gpd.clip to extract but it is returning an empty data frame. What is the best way to extract the coordinates that are falling inside a bbox?

from shapely.geometry import box
import geopandas as gpd
from shapely.geometry import Point

print(df)
    
         lat           lon         geometry
    0   30.302228   -87.475474  POINT (-87.4754735 30.3022278)
    1   30.302249   -87.475305  POINT (-87.4753053 30.3022495)
    2   30.302268   -87.475203  POINT (-87.4752034 30.3022676)
    3   30.302284   -87.475118  POINT (-87.4751181 30.3022838)
    4   30.299260   -87.473474  POINT (-87.473474 30.2992603)
    5   30.299501   -87.473526  POINT (-87.473526 30.299501)
    6   30.299285   -87.481937  POINT (-87.4819365 30.299285)
    7   31.176753   -86.579765  POINT (-86.5797648 31.1767528)
    8   31.176670   -86.579352  POINT (-86.5793519 31.1766701)
    9   31.176644   -86.579243  POINT (-86.5792434 31.1766441)
    10  31.176596   -86.579159  POINT (-86.5791589 31.1765959)
    11  31.176503   -86.579115  POINT (-86.5791153 31.1765032)
    12  31.173518   -86.578724  POINT (-86.578724 31.173518)
    13  31.170868   -86.578374  POINT (-86.578374 31.170868)
    14  31.170122   -86.578270  POINT (-86.57827 31.170122)
    15  31.161356   -86.577077  POINT (-86.577077 31.161356)
    16  31.160598   -86.576931  POINT (-86.576931 31.160598)
    17  31.160147   -86.576831  POINT (-86.576831 31.160147)
    18  31.109081   -85.516056  POINT (-85.516056 31.109081)
    19  31.109327   -85.515871  POINT (-85.515871 31.109327)
    20  31.161638   -85.736218  POINT (-85.736218 31.161638)
    21  31.169062   -85.741498  POINT (-85.7414983 31.1690619)
    22  31.109349   -85.056092  POINT (-85.0560924 31.1093492)
    23  27.713963   -82.679369  POINT (-82.6793689 27.7139633)
    24  27.714265   -82.679379  POINT (-82.6793793 27.7142646)
    25  30.299501   -81.619310  POINT (-81.61931 30.299501)

bbox = box(*[30.902576115004003,-85.72642861167968,31.072530650777363,-85.57774194396336])
geometry = [Point(xy) for xy in zip(nodes.lon, nodes.lat)]
gdf = gpd.GeoDataFrame(nodes, crs="EPSG:4326", geometry=geometry)
df_clipped = gpd.clip(gdf, mask=bbox)
print(df_clipped)
lon lat geometry

    

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

gpd.clip will work fine. The issue is that like all shapely objects, the order of arguments for shapely.geometry.box is x then y, in this case, xmin, ymin, xmax, ymax. You’ve used latitude, longitude order, but these should be reversed. So your bounding box should be:

bbox = box(
    -85.72642861167968,
    30.902576115004003,
    -85.57774194396336,
    31.072530650777363,
)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading