kms
kms

Reputation: 2024

geopandas : Obtain min , max lat and long for all geometries

I am reading a shapefile of a county and need to extract the min and max coordinates across all geometries. It seems like this is possible with each individual geometries in the shapefile with shapely but not across all geometries in shapefile.

sf_shp = os.getcwd() + '/data/map/San_Mateo/SAN_MATEO_COUNTY_STREETS.shp'
sfgeodata = gpd.read_file(sf_shp)

sfgeodata.total_bounds <-- for bounding box. 

Is there a property or a function to obtain this info in geopandas or any other packages?

Upvotes: 0

Views: 3118

Answers (1)

Brendan
Brendan

Reputation: 4011

total_bounds returns a tuple containing minx, miny, maxx, maxy values for the bounds of the series as a whole.

bounds returns a DataFrame with columns minx, miny, maxx, maxy values containing the bounds for each geometry.

Viz.——

import geopandas as gpd
from shapely.geometry import box
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.bounds
world.total_bounds

# bounds for individual geometries
poly_geom = world.bounds
b = poly_geom.apply(lambda row: box(row.minx, row.miny, row.maxx, row.maxy), axis=1)
boxes = gpd.GeoDataFrame(poly_geom, geometry=b)

# visualize
ax = world.plot()
boxes.boundary.plot(color='r', ax=ax)
plt.show()

# total bounds for all geometries
world_box = gpd.GeoSeries(box(*world.total_bounds))

# visualize
ax = world.plot()
world_box.boundary.plot(color='r', ax=ax)
plt.show()

bounds:

enter image description here

total_bounds:

enter image description here

Upvotes: 2

Related Questions