0

I am pretty new to R and spatial data, and need some help! I have one data frame with station coordinates (lat-long), to which I need to apply the depth value at the closest coordinate of another data frame.

First data frame:

stations = c(stationID, Latitude, Longitude) (16 rows)

second data frame:

depths = c(Longitude, latitude, depth) (2943485 rows)

optimally, I need a new data frame like:

df = c(stationID, Latitude, Longitude, depth) (16 rows)

I am pretty lost, so any help would be appreciated!

Mose
  • 3
  • 1
  • Maybe [this answer](https://stackoverflow.com/a/40075289/3460670) might be helpful? – Ben Oct 30 '20 at 14:58

1 Answers1

0

You can use spatstat to do this quite efficiently if it is OK to treat your lat,lon data as flat coordinates. So we assume you don’t cross the date line (+/-180 degrees longitude) and that 1 degree latitude is equal to 1 degree longitude. That is only correct around equator. Away from the equator you should really multiply one of the coordinates with an appropriate factor or you should ideally project your data to a flat coordinate system.

With randomly generated data of 16 stations and almost 3 million depth measurements the code finding nearest neighbours below takes less than half a second on my laptop.

library(spatstat)
stations <- data.frame(stationID = 1:16, 
                       Latitude = runif(16, 0, 10), 
                       Longitude = runif(16, 0, 10))
Wstations <- bounding.box.xy(stations$Longitude, stations$Latitude)
Xstations <- ppp(stations$Longitude, stations$Latitude, window = Wstations)
N <- 2943485
depths <- data.frame(Longitude = runif(N, 0, 10), 
                     Latitude = runif(N, 0, 10), 
                     depth = runif(N, 0, 1000))
Wdepths <- bounding.box.xy(depths$Longitude, depths$Latitude)
Xdepths <- ppp(depths$Longitude, depths$Latitude, window = Wdepths)
id <- nncross(Xstations, Xdepths, what = "which")
stations$depths <- depths$depth[id]
head(stations)
#>   stationID  Latitude Longitude   depths
#> 1         1 5.7992147 5.6716015 435.1266
#> 2         2 9.2218643 6.0519959 154.5833
#> 3         3 7.6444158 3.2228619 963.7626
#> 4         4 3.8993755 0.9428149 204.9189
#> 5         5 7.9673171 0.9801396 933.5696
#> 6         6 0.9453616 8.0829834 603.0325
Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
  • Your are such a hero! It worked like a charm - thanks a million! – Mose Oct 31 '20 at 15:47
  • Glad it was of help. Do remember that it treats your coordinates as if they were plain x,y. It does not know anything about a spherical earth... – Ege Rubak Nov 01 '20 at 00:02