# [R] latitude longitude data

Gabor Grothendieck ggrothendieck at myway.com
Sun Aug 22 21:01:22 CEST 2004

```Mikkel Grum <mi2kelgrum <at> yahoo.com> writes:

:
: Dear R-helpers,
:
: I get GPS readings with bug counts (bugs meaning
: insects in this case) made along rows in crop fields
: and use these to make maps of bug distribution.  The
: GPS readings are not quite accurate enough for my
: purpose, so since I know what row each reading is made
: in, I adjust the latitudinal coordinate using:
:
:     grd<-lm(lat~lon+Row,data)
:     data\$lat<-predict(grd[,c("lon","Row")])
:
: which adjusts the latitude pretty well when the rows
: run East-West, but not at all when the rows run
: North-South, and it doesn't adjust the longitude at
: all.
:
: Is there a better approach I could use to adjust both
: longitude and latitude onto the nearest point in the
: row, whatever the direction of the rows? In other
: words, move the point onto the row in a direction that
: is perpendicular to the row?

If you have the lonlats spaced along the row in a matrix called actual
and the gps lonlats in matrix called gps then its just a matter of finding
the nearest actual row to each gps row.  rdist.earth from fields will do the
lonlat distance calculation:

require(fields)

# test data
gps <- ozone\$lon.lat[seq(3),]
actual <- ozone\$lon.lat[-seq(3),]

# use rdist.earth from fields to get distance from each gps to each actual
# and find least
f <- function(i) {
dst <- rdist.earth(gps[i,,drop=F], actual)
actual[order(dst),]
}
t(sapply(1:nrow(gps), f))

```