addArea | R Documentation |
Adds the gridcell area to a spatial Field or data.table/data.frame. Makes a new layer/column called "area". Unit conversion from "m^2" (default) to "km^2" and "ha" is supported.
addArea(
input,
unit = "m^2",
ellipse = FALSE,
verbose = TRUE,
tolerance = NULL,
lon_centres = NULL,
lat_centres = NULL
)
input |
a spatial Field or a data.frame/data.table with at least the columns Lon and Lat. |
unit |
area unit. Default "m^2", can also be "km^2" and "ha" |
ellipse |
If the eath should be assumed to be a ellipsoid instead of a sphere. |
verbose |
print some information. |
tolerance |
Numeric, passed to |
lon_centres |
Optional, a numeric vector of the longitudes of the centres of the full grid. This is useful for calculating gridcell areas on a regular but sparsely populated grid. |
lat_centres |
Optional, a numeric vector of the latitudes of the centres of the full grid. This is useful for calculating gridcell areas on a regular but sparsely populated grid. The main use of this function is to calculate gridcell areas internally for gridcells weighted sums and averages in |
same class as input
Joerg Steinkamp joerg.steinkamp@senckenberg.de
# Get an example Field
africa.dir <- system.file("extdata", "LPJ-GUESS_Runs", "CentralAfrica", package = "DGVMTools")
africa.Source <- defineSource(name = "LPJ-GUESS", dir = africa.dir, format = GUESS)
field <- getField(source = africa.Source, quant = "cmass", year.aggregate.method="mean")
# add area in m^2 - note "Area" column follows immediately after Lon and Lat
field.m2 <- addArea(input = field, unit = "m^2")
print(field.m2@data)
# add area in km^2
field.km2 <- addArea(input = field, unit = "km^2")
print(field.km2@data)
# plot area (also modify plot and legend title to be more meaningful)
p <- plotSpatial(field.km2, "Area", title = "Gridcell Area (km^2)", subtitle = NULL)
p <- p + guides(fill = guide_colourbar(title = "km^2"))
print(p)
# add area in km^2 using an ellipse
field.km2.ellipse <- addArea(input = field, unit = "km^2", ellipse = TRUE)
field.km2.ellipse <- renameLayers(field.km2.ellipse, "Area", "Area_ellipse")
# compare areas just for fun
comp.layer <- compareLayers(field1=field.km2, field2=field.km2.ellipse,
layers1="Area", layers2="Area_ellipse")
plot.title <- "Difference in gridcell area for spherical vs ellipsoid Earth"
p <- plotSpatialComparison(comp.layer, title= plot.title, subtitle = NULL)
p <- p + guides(fill = guide_colourbar(title = "km^2"))
print(p)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.