I am working with a shapefile that has many polygons.
How do I add one more field named "area_sqkm" and calculate area for each polygon in the shapefile?
I am working with a shapefile that has many polygons.
How do I add one more field named "area_sqkm" and calculate area for each polygon in the shapefile?
You can do
library(raster)
x <- shapefile('file.shp')
crs(x)
x$area_sqkm <- area(x) / 1000000
Or with terra
library(terra)
x <- vect('file.shp')
x$area_sqkm <- expanse(x) / 1000000
That works for both an angular CRS (longitude/latitude) and for planar CRSs. It is generally best to use a longitude/latitude CRS to compute area a planar CRS can distort size with a lot (How much? It depends on the CRS and on the location and extent of your spatial data).
raster::area()
with unprojected data more accurate than rgeos::gArea()
with projected data?
Commented
Apr 30, 2019 at 22:45
geosphere
vignette).
Commented
May 1, 2019 at 0:14