GeoDataFrame – Getting Bounding Box from Point Type GeoDataFrame for Osmnx

extentsgeopandaspython 3

I've a point geodataframe which has only point features inside of it.

fishnet_Id ... geometry
0 0 ... POINT (403486 4540640)
1 0 ... POINT (403736 4540640)

I want to create a bounding box polygon feature from the easternmost, southernmost, northernmost and westernmost point and then pass it into osmnx.gdf_from_bbox method.

I could do it via finding the biggest/smallest lat and biggest/smallest lon values and then make a column from these values and pass these values into the osmnx function manually but I want to do it more code-efficient way.

(Right now its look like this)

import geopandas as gpd
import pandas as pd
import osmnx as ox
from fiona.crs import from_epsg

origins = gpd.read_file(r"data\Origin_Clip.shp")
destinations = gpd.read_file(r"data\Dest_Clip.shp")

origins["geometry"] = origins["geometry"].to_crs(epsg=4326)
destinations["geometry"] = destinations["geometry"].to_crs(epsg=4326)

origins["lat"] = origins["geometry"].x
origins["lon"] = origins["geometry"].y
destinations["lat"] = destinations["geometry"].x
destinations["lon"] = destinations["geometry"].y

points = gpd.GeoDataFrame(pd.concat([origins, destinations], ignore_index=True))

xmin, xmax = points["lat"].min(), points["lat"].max()
ymin, ymax = points["lon"].min(), points["lon"].max()

G = ox.graph_from_bbox(ymax, ymin, xmax, xmin, clean_periphery=False)

Best Answer

You can use the total_bounds property for this.

Small example:

In [83]: from shapely.geometry import Point

In [84]: import geopandas

In [86]: import random

In [87]: df = geopandas.GeoDataFrame({'geometry': [Point(random.random(), random.random()) for _ in range(10)]})

In [93]: df
Out[93]: 
                                         geometry
0   POINT (0.2983451440678163 0.7557662257341221)
1    POINT (0.385473871438677 0.7633916702996563)
2  POINT (0.9540553623697344 0.03776388629695204)
3    POINT (0.9118544880582852 0.337026237508943)
4   POINT (0.0806616922186848 0.9095664267582581)
5   POINT (0.7583727419062042 0.1540564992911923)
6   POINT (0.8385360236296786 0.5697242268543217)
7   POINT (0.5537557739990847 0.4690105960129577)
8   POINT (0.2619102307461078 0.5456626866204806)
9   POINT (0.9953665099349714 0.1410054554344017)

In [96]: minx, miny, maxx, maxy = df.geometry.total_bounds

In [98]: minx, miny, maxx, maxy
Out[98]: 
(0.0806616922186848,
 0.037763886296952043,
 0.99536650993497144,
 0.90956642675825805)