Learn R Programming

ursa (version 3.10.4)

identify: Get value and coordinates from location

Description

Functions to extract values of raster image from given location, specified by coordinates in raster projection, by cell position or by geogpaphical coordinates. Additional utils to convert cell position and planar coordinates mutually.

Usage

value_xy(obj, ...)

value_ll(obj, ...)

value_cr(obj, ...)

coord_xy(obj, ...)

coord_cr(obj, ...)

Value

For value.* numeric matrix of raster values. Band values for specific coordinates are by column. Set of specific coordinates are by row. rownames are band names, and colnames are index in internal value storage.

For coord.* numeric matrix of coordinates with a vector of couple coordinates, one coordinate per one row. rownames are returned coordinates, and colnames are index in internal value storage.

Arguments

obj

Object of class ursaRaster.

...

the set of arguments, which are recognized via their names (using regular expressions) and classes:

Matched pattern Function Used nameind
*_*indIndex (positive integer) in internal value storage.^c
*_crcolInteger of non-zero length. Index of column/sample Length of column and row indices should be the same for creating set of two-dimension coordinates.
^r*_crrowInteger of non-zero length. Index of row/line. Length of column and row indices should be the same for creating set of two-dimension coordinates.
^x*_xyxNumeric of non-zero length. X-axis coordinate in grid of obj. The length of X-axis and Y-axis coordinates should be the same for creating set of two-dimension coordinates.
^y*_xyyNumeric of non-zero length. Y-axis coordinate in grid of obj. The length of X-axis and Y-axis coordinates should be the same for creating set of two-dimension coordinates.
^lonvalue_lllonLongitude. The length of longitudes and latitudes should be the same for creating set of two-dimension coordinates.
^latvalue_lllatLatitude. The length of longitudes and latitudes should be the same for creating set of two-dimension coordinates.

Author

Nikita Platonov platonov@sevin.ru

Details

value_xy returns values for location, which is specified by planar coordinates (x, y).
value_cr returns values for location, which is specified by cell posisition (column, row) relative to upper-left corner of image .
value_ll returns values for location, which is specified by longitude and latitude (long, lat).

coord_xy transforms planar coordinates (x, y) to cell position (column, row).
coord_cr transforms cell position (column, row) to planar coordinates (x, y).

It is required to use a couple of coordinate vectors: (x, y), (c, r) or (lon, lat) of the same length. The unary argument is interpreted as index in internal value storage.

Position in column/row coordinates starts from upper-lever corner. The cell of upper-level corner has (1, 1) coordinates (in R indices starts from 1L), whereas in some GIS the same corner cell has (0, 0) coordinates.

The column names of returned matrix are character format of index in internal value storage. This index can be specify in any function as argument ind instead of coordinates (planar, geographical, cell position).

Examples

Run this code
session_grid(NULL)
set.seed(352)
a <- as.integer(ursa_dummy(3,min=0,max=999))
ind <- which(ursa_value(a[1])==890)
print(ind)
msk <- a[1]==890
am <- a[msk]
b <- as.data.frame(am)
b$jx <- b$x+runif(nrow(b),min=-1000,max=1000)
b$jy <- b$y+runif(nrow(b),min=-1000,max=1000)
print(b)
cr1 <- coord_xy(a,x=b$jx,y=b$jy)
cr2 <- coord_xy(a,y=b$y,x=b$x)
cr3 <- coord_xy(a,ind=ind)
print(cr1)
print(list('cr1 and cr2'=all.equal(cr1,cr2)
          ,'cr2 and cr3'=all.equal(cr2,cr3)
          ,'cr3 and cr1'=all.equal(cr3,cr1)))
xy1 <- coord_cr(a,c=cr1["c",],r=cr1["r",])
print(xy1)
print(list('in x'=identical(unname(xy1["x",]),b[,"x",drop=TRUE])
          ,'in y'=identical(unname(xy1["y",]),b[,"y",drop=TRUE])))
val1 <- value_xy(a,x=b$jx,y=b$jy)
val2 <- value_xy(a,x=b$x,y=b$y)
val3 <- value_cr(a,ind=ind)
val4 <- value_cr(a,c=cr1["c",],r=cr1["r",])
print(val1)
print(list('val1 and val2'=all.equal(val1,val2)
       ,'val2 and val3'=all.equal(val2,val3)
       ,'val3 and val4'=all.equal(val3,val4)
       ,'val4 and val1'=all.equal(val4,val1)))
ps <- pixelsize()
v <- value_ll(ps,lon=180,lat=70)
print(c('True scale'=v/with(ursa_grid(ps),1e-6*resx*resy)))

Run the code above in your browser using DataLab