[R-sig-Geo] Calculate shortest distance between points belonging to different polygons
Adrian Baddeley
adrian.baddeley at uwa.edu.au
Wed Feb 18 12:02:28 CET 2015
After you do
> nn.within.pol<-nndist(randp.df[,c(2,3)], by=factor(randp.df$Polygon_ID))
the result nn.within.pol is a matrix with one row for each data point, and
one column for each polygon. The entry on row i, column j is the distance
from the i-th data point to its nearest neighbour in polygon j.
It's still not clear to me what you wanted to calculate.
Do you want the minimum distance between any two points in different polygons?
For each possible pair of polygons? or... ?
The command
> aggregate(nn.within.pol, by=list(from=factor(randp.df$PolygonID)), min)
would yield a matrix, with one row and column for each polygon,
in which the [i,j] entry is the minimum distance between any point in polygon i
and any point in polygon j. On the diagonal the [i,i] entry would give the minimum
nearest-neighbour distance for the pattern of points within polygon i.
Prof Adrian Baddeley FAA
Curtin University
________________________________________
From: Ivan Palmegiani [pan.sapiens.it at gmail.com]
Sent: Wednesday, 18 February 2015 12:32 PM
To: Adrian Baddeley
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Calculate shortest distance between points belonging to different polygons
Dear Adrian,
Thank you very much!
I made the change you suggested and the outcome is not exactly the one I wanted...but it's very close to it!
> library(spatstat)
> nn.within.pol<-nndist(randp.df[,c(2,3)], by=factor(randp.df$Polygon_ID))
> nn.within.pol
1 2 3 4 5 6 7 8 9 10 11 12 13
[1,] 77758.488 71978.036 33715.961 62236.2421 84528.6758 47206.9703 26370.4913 39448.7910 40198.916 68559.0147 67769.69 39298.45411 2579.422
[2,] 63404.130 93535.612 64780.940 21597.4530 32485.1889 1391.8891 38473.2915 69642.7457 24374.408 15070.9926 70237.22 35217.62792 49217.807
[3,] 37861.018 53839.721 27587.333 59658.5379 58107.6351 35133.6718 48282.9013 75710.3428 48898.652 52273.7298 33882.71 59.85628 36756.945
...
[98,] 114435.572 111577.598 73735.852 65518.7127 102369.5236 65629.7084 29330.3308 955.2648 39271.208 81234.4457 107270.98 74005.63912 30371.918
[99,] 32283.616 52705.045 28666.796 61665.1649 55854.9164 36094.8633 53239.2478 81208.2781 52669.590 52158.4953 30242.29 3166.00894 42467.270
[100,] 38187.593 53418.183 27001.456 60050.0374 58795.8736 35555.5710 48200.6059 75445.7086 49047.448 52858.1932 33791.42 705.25663 36323.229
In fact, I realized that I do not need to make recourse to aggregate() because in the matrix above I already have the information I need. I just need to extract it.
Is there any way to prevent the function to calculate the distance between a point and its nn within the same group (i.e. distance [1, 13]; [2, 6]; [3, 12]; etc.)?
If not, I will just extract the second shortest distance per point (the shortest one is certainly within the same group) and I will get exactly the information I need.
Thanks again!
Sincerely,
Ivan
On 17-Feb-15 16:01, Adrian Baddeley wrote:
Ivan Palmegiani <pan.sapiens.it at gmail.com><mailto:pan.sapiens.it at gmail.com> writes:
I'm handling a SpatialPointsDataFrame with 100 ramdom points distributed
within 13 different polygons.
> randp
coordinates Point_ID Polygon_ ID
0 (690926.8, 7522595) 1_hs 13
1 (696727.1, 7576122) 2_hs 6
I need to calculate the shortest distance between points belonging to
different polygons. Basically I'd like to do what nndist {spatstat} does.
The difference is that the distance should be calculated between
groups of points instead of within a group of points.
Well, nndist {spatstat} does that too.
I tried to use "aggregate" as suggested below but it didn't work out for me.
http://www.inside-r.org/packages/cran/spatstat/docs/nndist
Please find my try below:
> randp.df<-data.frame(randp)
> randp.hs.df
Point_ID coords.x1 coords.x2 Polygon_ ID
0 1_hs 690926.8 7522595 13
1 2_hs 696727.1 7576122 6
2 3_hs 723480.7 7546594 12
library(spatstat)
# Calculate nearest neighbors within a polygon
> nn.within.pol<-nndist(randp.df[,c(2,3)],by=marks(randp.df$Polygon_ID))
'marks' is not appropriate here.
marks() is a function that extracts the marks from a marked point pattern (class 'ppp')
but you are applying it to a numeric vector. The result is NULL.
The argument 'by' should be a factor that determines the grouping.
Just change 'marks' to 'factor' so that the numeric polygon ID will be treated as a factor,
nn.within.pol<-nndist(randp.df[,c(2,3)],by=factor(randp.df$Polygon_ID))
and proceed as before (also changing the other instances of 'marks' to 'factor')
Adrian Baddeley
spatstat author
More information about the R-sig-Geo
mailing list