4

I am new to Geopandas and I want the min and max values of both latitude and longitude for a single record. The Geodataframe is structured as so (not sure it matters):

STATEFP   STATENS     GEOID   STUSPS   NAME   geometry
49         48           48      TX     Texas  (POLYGON ((-94.718296 29.728855, -94.717207 29... 
8

Individual POLYGONs in geometry column are shapely objects, so you can use .bounds method on this object to get a tuple of (minx, miny, maxx, maxy).

from shapely.geometry import Polygon
poly = Polygon([[1,2], [2,5], [4,6], [5,3], [1,2]])
poly.bounds
# out: (1.0, 2.0, 5.0, 6.0)
0

If you're using ESRI shapefile as vectorfile, then

import geopandas as gpd
from shapely.geometry import Polygon

vector_file_path=r"C or D:\path\shapefilename.shp"
vector_data=gpd.read_file(vector_file_path)
vector_bound_coordinates= vector_data['geometry']
vector_bound_coordinates.bounds
out[]: 
      minx        miny         maxx        maxy
0   78.158751   19.172422   78.977808   19.916980
1   77.423083   26.741696   78.854721   27.409776
2   71.839711   21.984888   72.843705   23.508623
3   73.908371   25.647207   75.353247   26.978540
4   76.692215   20.274559   77.620988   21.247055
... ... ... ... ...
712 86.796486   23.409566   87.539374   23.880466
713 87.054093   21.763803   87.892579   22.951303
714 87.438626   22.932676   88.419780   23.843992
715 85.832496   22.703573   86.899489   23.697929
716 87.809878   25.241873   88.533093   26.496043
3
  • 2
    Nicely done) however I was wondering how your answer is different to the solutions? – Taras Jan 3 at 11:53
  • earlier solution did not showed how to import shapefile, and the way to use the column geometry, New users usually stuck at that before coming to the solution point. @Taras – Abhilash Singh Chauhan Jan 3 at 11:58
  • So, in that case I will suggest extending the provided answer with your ideas how to set up the path to the working dir/file – Taras Jan 3 at 15:57

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.