Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created February 23, 2016 15:15
Show Gist options
  • Save geotheory/ac898ccbb29c275b9c36 to your computer and use it in GitHub Desktop.
Save geotheory/ac898ccbb29c275b9c36 to your computer and use it in GitHub Desktop.
Get total areas of spatialPolygonsDataFrame WGS84 objects
require(geosphere)
get_areas = function(spdf){
polys = lapply(spdf@polygons, function(p) p@Polygons)
sapply(polys, function(p){
coords = lapply(p, function(p) p@coords)
ind_areas = lapply(coords, areaPolygon)
sum(unlist(ind_areas))
})
}
## example
# require(maptools)
# data(wrld_simpl)
# data.frame(id = wrld_simpl@data$NAME,
# area = get_areas(wrld_simpl)/1e+06)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment