addArea: Adds the gridcell area to a spatial Field or...

addAreaR Documentation

Adds the gridcell area to a spatial Field or data.table/data.frame

Description

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.

Usage

addArea(
  input,
  unit = "m^2",
  ellipse = FALSE,
  verbose = TRUE,
  tolerance = NULL,
  lon_centres = NULL,
  lat_centres = NULL
)

Arguments

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 copyLayers. Defines how close the longitudes and latitudes of the gridcells in input and the internally calculated area data.table need to be to the coordinates in order to get a match. Can be a single numeric (for the same tolerance for both lon and lat) or a vector of two numerics (for lon and lat separately). #' Default is no rounding (value is NULL) and so is fine for most regular spaced grids. This is a technical detail, you only need to use it if you have troubles because of coordinates with a few decimal places.

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 aggregateSpatial but it can be utilised by the user for any other purpose.

Value

same class as input

Author(s)

Joerg Steinkamp joerg.steinkamp@senckenberg.de

Examples



 
# 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)



MagicForrest/DGVMTools documentation built on Aug. 23, 2024, 8:05 a.m.