Skip to content
Prev 22340 / 29559 Next

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: