0

I tried the RANN package to extract the nearest coordinate points by comparing two files and then add the nearest extracted points to the another file.

My files -> fire

lat lon frp

30.037  80.572  38.5
23.671  85.008  7.2
22.791  86.206  11.4
23.755  86.421  5.6
23.673  86.088  4.2
23.768  86.392  8.4
23.789  86.243  7.8
23.805  86.327  6.4
23.682  86.085  7.8
23.68   86.095  5.7
21.194  81.41   19
16.95   81.912  8
16.952  81.898  11.5
16.899  81.682  10.6
12.994  79.651  16.1
9.2 77.603  14.5
12.291  77.346  20.5
17.996  79.708  13.9
17.998  79.718  29.6
16.61   81.266  6.6
16.499  81.2    6.8
19.505  81.784  22.4
18.322  80.555  7.7
19.506  81.794  28.2
21.081  81.957  8.7
21.223  82.127  9.4
20.918  81.025  6.3
19.861  82.123  9.3
20.62   75.049  11.6

and 2nd file -> wind

lat lon si10    u10 v10

40  60  3.5927058834376 -0.874587879393667  -0.375465368327018
40  60.125  3.59519876134577    -0.836646189656238  -0.388624092937835
40  60.25   3.59769163925393    -0.798704499918809  -0.401782817548651
40  60.375  3.6001845171621 -0.76076281018138   -0.414941542159468
40  60.5    3.60246965524458    -0.722821120443951  -0.428380239634345
40  60.625  3.60496253315275    -0.684585309080651  -0.441538964245162
40  60.75   3.60766315088659    -0.646937740969094  -0.454977661720038
40  60.875  3.60911732966636    -0.609878416109279  -0.468976304923035
40  61  3.608701850015  -0.575172064256437  -0.484934758174451
40  61.125  3.60807863053795    -0.540759834029467  -0.500893211425867
40  61.25   3.60787089071227    -0.506053482176625  -0.516851664677283
40  61.375  3.60745541106091    -0.471641251949655  -0.533090090792759
40  61.5    3.60703993140955    -0.437229021722684  -0.548768571180115
40  61.625  3.60662445175819    -0.402522669869843  -0.565006997295591
40  61.75   3.60454705350139    -0.398993210359384  -0.579285613362648
40  61.875  3.60163869594186    -0.411346318645989  -0.592724310837524
40  62  3.59873033838234    -0.423405305306722  -0.606163008312401
40  62.125  3.59540650117145    -0.435758413593327  -0.619601705787278
40  62.25   3.59249814361192    -0.44781740025406   -0.633320376126214
40  62.375  3.5895897860524 -0.460170508540664  -0.646759073601091
40  62.5    3.58668142849287    -0.471935373575526  -0.660197771075968
40  62.625  3.57546347790613    -0.509288820061212  -0.666357174085286
40  62.75   3.56445326714507    -0.546642266546898  -0.672236604230545
40  62.875  3.55323531655832    -0.584289834658455  -0.678675980103923
40  63  3.54201736597158    -0.621643281144141  -0.684835383113241
40  63.125  3.53100715521052    -0.658996727629827  -0.69099478612256
40  63.25   3.51978920462378    -0.696350174115513  -0.697154189131878
40  63.375  3.50005392118414    -0.726644701580281  -0.692954596170979
40  63.5    3.46266075256166    -0.743115512629088  -0.668037011269646

I want to add wind$si10 wind$u10 wind$v10 into the fire file with nearest coordinates corresponding to frp values. First, I tried only with variable si10 because in RANN package both fire and wind files should have the same number of columns. So I use the code with si10 only

library(RANN)
read.table(file.choose(), sep="\t", header = T) -> wind_jan
read.table(file.choose(), sep="\t", header = T) -> fire_jan
names(fire_jan)
names(wind_jan)

closest <- RANN::nn2(data = wind_jan, query = fire_jan, k = 1)
closest
fire_jan$wind_lat <- wind_jan[closest$nn.idx, "lat"]
fire_jan$wind_lon <- wind_jan[closest$nn.idx, "lon"]
fire_jan$WS <- wind_jan[closest$nn.idx, "si10"]

From the above code I am able to extract si10 values at the nearby coordinates of fire$frp but when I apply the same code for u10 and v10variables in wind file then I am not able to get the extracted values on the same coordinates as I got with si10.

How can I solve this query with this code?

Kunal
  • 1
  • 1
  • Related / possible duplicate: [*Geographic / geospatial distance between 2 lists of lat/lon points (coordinates)*](https://stackoverflow.com/q/31668163/2204410) – Jaap Oct 04 '18 at 15:31
  • @Jaap Thanks for the reply. But not able to solve my problem from your suggested code. – Kunal Oct 04 '18 at 15:41

1 Answers1

0

you call closest_u$nn.id that doesnt exist. Maybe there is an error with your label as well when reading wind df ?

could that be the error?

lemairev
  • 216
  • 3
  • 8
  • oh sorry it's `closest` not `closest_u` – Kunal Oct 04 '18 at 16:43
  • from the help of the function the number of column must be the same. Your choice is based on lon lat so you can use nn2(wind_jan[,c("lon","lat")],fire_jan[,c("lon","lat")], k=1) – lemairev Oct 04 '18 at 17:09
  • Thanks for this information. Now can get the more nearest value. But is it possible that to get the nearest values for all `si10` `u10` `v10` together in one line. – Kunal Oct 04 '18 at 17:31
  • not sure what you mean by in one line? actually you got the index in one line and then you just have to use cbind or something else like cbind(wind_jan[closest$nn.idx,],fire_jan) and you have everything no? – lemairev Oct 04 '18 at 18:01
  • yes, I have also did that `cbind`. anyways thanks for the help. – Kunal Oct 04 '18 at 19:35