2

I am writing a Python script to calculate the area of polygons, as I have a small polygon, I am getting the area of polygon 0.000. A shapefile contains multiple shapefiles. I am thinking due to the area unit, it is printing 0.000. Shapefile projection is CRS EPSG:4326 - WGS 84 - Geographic. I have to change the unit of area to Hectare in the script to calculate the area. I need to change the unit of area hectare(ha) in the below script.

Below is my working code, please let me know, how to change the area unit the below code

# Import necessary modules
import geopandas as gpd

# Set filepath (fix path relative to yours)
fp = r"D:\Data\Merged_Shapefile.shp"

# Read file using gpd.read_file()
data = gpd.read_file(fp)
selection = data[0:200]
for index, row in selection.iterrows():
    poly_area = row['geometry'].area
    print("Polygon area at index {0} is: {1:.3f}".format(index, poly_area))  
1
  • after adding this command line I am getting this warning message and the area still the same 0.000 in the output. Warning message: Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.
    – SWAT
    Mar 19 at 9:22
0

I'm not sure if I fully understand you. I think you try to get the area of the polygons in hectares, isn it?

Well it will depend on your CRS, since I think it takes the unit system from it. If you are using cartesian coordinates (x and y), your area will be computed in squared meters, so I think it is as simple as divide it by 10000. If your problem is that the area is not computed in m², I suggest you to change your coordinate system to a cartesian one.

In addition, if the area of some polygons are returning 0s, that means you have wrong polygon topologies. In the simplest cases it can be solved by doing a buffer of 0 meters.

2
  • Yes, i am trying to get the polygon area in hectare. and my shapefile projection is CRS EPSG:4326 - WGS 84 - Geographic.
    – SWAT
    Mar 19 at 7:46
  • 3
    EPSG:4326 is geographical projection (i.e. in lat/lon coordinates). That means it will locate the coordinates in a sphere and the area is computed in degrees. If you're working in a local area, it would be good to change the CRS to the local one. If you are working on a global map to compute the areas, it is good to reproject to a cartesian projection with equal-area properties. Mar 19 at 8:14
0

For this I usually use the 'cea' projection, which (as I understand it) is an equal-area projection. Then I use the area property of the geometry. Dividing by 10**6 results in km2. So divide that by 100 to get ha.

data_copy = data.copy()
data_copy = data_copy.to_crs({'proj':'cea'})
data['poly_area'] = data_copy['geometry'].area/ 10**6 / 100

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.