diff --git a/geopandas/geoseries.py b/geopandas/geoseries.py index f1886bf..28e85d8 100644 --- a/geopandas/geoseries.py +++ b/geopandas/geoseries.py @@ -5,6 +5,7 @@ import fiona from fiona.crs import from_epsg import numpy as np from pandas import Series, DataFrame +from pandas.util.decorators import cache_readonly import pyproj from shapely.geometry import shape, Polygon, Point from shapely.geometry.collection import GeometryCollection @@ -295,7 +296,6 @@ class GeoSeries(Series): # Other operations # - # should this return bounds for entire series, or elementwise? @property def bounds(self): """Return a DataFrame of minx, miny, maxx, maxy values of geometry objects""" @@ -303,6 +303,21 @@ class GeoSeries(Series): return DataFrame(bounds, columns=['minx', 'miny', 'maxx', 'maxy'], index=self.index) + + @cache_readonly + def total_bounds(self): + """Return a single bounding box (minx, miny, maxx, maxy) for all geometries + + This is a shortcut for the following: + >>> aggregator = dict(minx=np.nanmin, miny=np.nanmin, + maxx=np.nanmax, maxy=np.nanmax) + >>> series.bounds.groupby(lambda x: 1).agg(aggregator) + """ + + aggregator = dict(minx=np.nanmin, miny=np.nanmin, + maxx=np.nanmax, maxy=np.nanmax) + bbox = self.bounds.groupby(lambda x: 1).agg(aggregator) + return tuple(bbox.values[0].tolist()) def buffer(self, distance, resolution=16): return GeoSeries([geom.buffer(distance, resolution) for geom in self], diff --git a/tests/test_geoseries.py b/tests/test_geoseries.py index 059af50..5137b38 100644 --- a/tests/test_geoseries.py +++ b/tests/test_geoseries.py @@ -285,4 +285,9 @@ class TestSeries(unittest.TestCase): res = self.g4.skew(ys=skew, origin=Point(0,0)) self.assertTrue(geom_almost_equals(self.g4, res.skew(ys=-skew, origin=Point(0,0)))) + + def test_total_bounds(self): + bbox = self.sol.x, self.sol.y, self.esb.x, self.esb.y + self.assertEqual(self.landmarks.total_bounds, bbox) + self.assertEqual(self.g1.total_bounds, (0, 0, 1, 1))