Created
February 23, 2016 15:15
-
-
Save geotheory/ac898ccbb29c275b9c36 to your computer and use it in GitHub Desktop.
Get total areas of spatialPolygonsDataFrame WGS84 objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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