[R] Calculate depth from regular xyz grid for any coordinate within the grid

Michael Sumner mdsumner at gmail.com
Mon Jul 28 21:31:56 CEST 2014


The raster package can readily provide bilinear interpolation:

library(raster)
r <- rasterFromXY(df)
## due diligence, just a guess here you should check
## projection(r) <- "+proj=utm +zone=32 +datum=WGS84"

## coordinates to extract
m <- matrix(c( 3454263, 5970687), ncol = 2)

extract(r, m, method = "bilinear")
[1] -1.686059

## compare with
extract(r, m, method = "simple")
-1.6877

See ?extract - simplest usage is a query matrix of XY coordinates in
the projection used by your raster, it will helpfully transform
queries such as a "Spatial*DataFrame" if needed, as long as both
raster x and query y have sufficient projection metadata (and it's up
to you to make sure that's set right).

(Generally building a raster from "XYZ" data is sub-optimal since
there's so much redundancy in the XY coordinates, and so much room for
things to go wrong in between. But sometimes there's no better option.
)

Cheers, Mike.

On Mon, Jul 28, 2014 at 11:07 PM, Kulupp <kulupp at online.de> wrote:
> Dear R-experts,
>
> I have a regular grid dataframe (here: the first 50 rows) :
>
> # data frame (regular grid) with x, y (UTM-coordinates) and z (depth)
> # x=UTM coordinates (easting, zone 32)
> # y=UTM coordinates (northing, zone 32)
> # z=river-depth (meters)
> df <- data.frame(x=c(3454240, 3454240, 3454240, 3454240, 3454240, 3454250,
> 3454250, 3454250, 3454250, 3454250, 3454250, 3454250, 3454250, 3454250,
> 3454250, 3454250,
>                      3454250, 3454250, 3454260, 3454260, 3454260, 3454260,
> 3454260, 3454260, 3454260, 3454260, 3454260, 3454260, 3454260, 3454260,
> 3454260, 3454260,
>                      3454260, 3454260, 3454260, 3454260, 3454260, 3454260,
> 3454260, 3454260, 3454270, 3454270, 3454270, 3454270, 3454270, 3454270,
> 3454270, 3454270,
>                      3454270, 3454270),
>                  y=c(5970610, 5970620, 5970630, 5970640, 5970650, 5970610,
> 5970620, 5970630, 5970640, 5970650, 5970660, 5970670, 5970680, 5970690,
> 5970700, 5970710,
>                      5970720, 5970730, 5970610, 5970620, 5970630, 5970640,
> 5970650, 5970660, 5970670, 5970680, 5970690, 5970700, 5970710, 5970720,
> 5970730, 5970740,
>                      5970750, 5970760, 5970770, 5970780, 5970790, 5970800,
> 5970810, 5970820, 5970610, 5970620, 5970630, 5970640, 5970650, 5970660,
> 5970670, 5970680,
>                      5970690, 5970700),
>                  z= c(-1.5621, -1.5758, -1.5911, -1.6079, -1.6247, -1.5704,
> -1.5840, -1.5976, -1.6113, -1.6249, -1.6385, -1.6521, -1.6658, -1.6794,
> -1.6930, -1.7067,
>                       -1.7216, -1.7384, -1.5786, -1.5922, -1.6059, -1.6195,
> -1.6331, -1.6468, -1.6604, -1.6740, -1.6877, -1.7013, -1.7149, -1.7285,
> -1.7422, -1.7558,
>                       -1.7694, -1.7831, -1.7967, -1.8103, -1.8239, -1.8376,
> -1.8522, -1.8690, -1.5869, -1.6005, -1.6141, -1.6278, -1.6414, -1.6550,
> -1.6686, -1.6823,
>                       -1.6959, -1.7095))
> head(df)
> plot(df[,1:2], las=3)   # to show that it's a regular grid
>
> My question: is there a function to calculate the depth of any coordinate
> pair (e.g. x=3454263, y=5970687) within the grid, e.g. by bilinear
> interpolation or any other meaningful method?
>
> Thanks a lot for your help in anticipation
>
> Best wishes
>
> Thomas
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-help mailing list