2

I am working in Jupyter Notebook and overlaying two GeoJSON files. One is property, the other is council zones.

I am selecting the property id's and then clipping the zones that lie within the extent

 ITT_PROP_select = ITT_PROP.query("(PROPID == 40932672)")
 ITT_PROP_select

enter image description here

 select = gpd.clip(ITT_Zone, ITT_PROP_select)
 select 

enter image description here

However when using the code below, I am unable to add a new column and calculate the areas for each row.

from shapely.geometry import shape

for i in range(len(select)): select.loc[i, 'area_m2'] = shape(select.loc[i,'geometry']).area

The code returns:

KeyError: 0

Taras
  • 32,823
  • 4
  • 66
  • 137

1 Answers1

4

The usual way to calculate area and store it as a new column (attribute) is:

df["area"] = df.area

but you might see a useful warning:

UserWarning: Geometry is in a geographic CRS. Results from 'area' are likely incorrect. Use GeoSeries.to_crs() to re-project geometries to a projected CRS before this operation.

You can also use geometry_area_perimeter from PyProj >= 2.3.0 to calculate the area in m²:

import geopandas

df = geopandas.read_file("file.shp") geod = df.crs.get_geod() df["area_m2"] = df.geometry.apply(lambda g: abs(geod.geometry_area_perimeter(g)[0]))

Taras
  • 32,823
  • 4
  • 66
  • 137
Mike T
  • 42,095
  • 10
  • 126
  • 187