3

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?

2
  • Good news: Shapefile includes a header record property with the layer envelope (4D). Bad news: It doesn't have to be correct.
    – Vince
    Jun 30, 2021 at 16:50
  • GDAL/OGR can give the extent of a layer directly with GetExtent gdal.org/doxygen/…
    – user30184
    Jul 1, 2021 at 13:10

2 Answers 2

4

You can apply bounds to each row, create a list of tuples [(xmin, ymin, xmax, ymax), (xmin, ...] and use itemgetter to find overall min and max:

import datetime as dt
import geopandas as gpd
from operator import itemgetter
start = dt.datetime.now()

df = gpd.read_file('/home/bera/GIS/Data/testdata/my_riks.shp')
bounds = df.geometry.apply(lambda x: x.bounds).tolist()

minx, miny, maxx, maxy = min(bounds, key=itemgetter(0))[0], min(bounds, key=itemgetter(1))[1], max(bounds, key=itemgetter(2))[2], max(bounds, key=itemgetter(3))[3] #https://stackoverflow.com/questions/13145368/find-the-maximum-value-in-a-list-of-tuples-in-python
print(minx, miny, maxx, maxy)
print('Processing time: {0} seconds'.format((dt.datetime.now()-start).seconds))

#245209.174 6111275.67 921093.74 7671055.56
#Processing time: 14 seconds

enter image description here

0
2

All you need to do is use total_bounds method:

bounds = gdf.total_bounds

It is going to give you the values that you want.

The result will be the same even if you get the bounds of each geometry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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