perep1972
perep1972

Reputation: 147

R spatstat: Units of distances retrieved by nndist

I’m ashamed bothering you with a stupid (but very necessary to me) question. I’ve a bunch of lat/lon points distributed almost randomly within a rectangle of ca. two x three degrees (latitude x longitude).

I need to calculate the maximum distance to the second nearest neighbor as well as the maximum distance to the farthest neighbor. I calculated these using package spatstat,

d2 <- max(nndist(data[,2:3], k = 2)
dn <- max(nndist(data[,2:3], k=(nrow(data))-1))

, respectively, and the distances obtained were 0.3 to 4.2.

I need these distances in kilometers. So, I supposed that distances provided by nndist where expressed in radians.
So, if θ = a /r, where θ is the subtended angle in radians, a is arc length, and r is Earth radius), then, to calculate a the equations becomes: a = θr.

However, the distances transformed in such a way ranged from:

a = 6371 * 0.3 = 1911.3, and

a= 6371 * 4.2 = 2650.2

This is evidently wrong; since the maximum distance measured using – for example – Qgis between the farthest points is just 480 km…

Can anybody indicate me where am I mistaken?

Thanks a lot in advance!!!

Upvotes: 1

Views: 872

Answers (1)

mnel
mnel

Reputation: 115382

nndist is simply calculating the euclidean distance. It does no unit conversion. As such you have given it values in "degrees", and thus it will return a value whose units are degrees. (not radians).

Thus

6371*0.3*pi/180 = 33.36

will give an approximation of the distance between these points.

A better approach would be to use great circle distances (eg in geosphere or gstat packages or to project the lat/long coordinates onto an appropriate map projection. (rgdal::spTransform will do this) and then nndist will calculate your distances in metres.

Upvotes: 3

Related Questions