0

I have roughly 1000 points in one dataset "df1" which have coordinates scattered across the coordinate system. In another dataset "df2", I have 27 points placed across the coordinate system.

For each point in df1, I want to estimate the closest distance to a point in df2, located within the direction of 165-225° (in relation to the unit circle).

How can this be done in R?

If the direction did not matter, then I could solve the problem with the approach from Finding closest point from other data frame

This answer suggest to define your two dataset as data tables and then use the following function:

    dist <- function(a, b){
            dt <- data.table((df2$x-a)^2+(df2$y-b)^2)
            return(which.min(dt$V1))}
 results <- df1[, j = list(Closest =  dist(x, y)), by = 1:nrow(df1)]

However, I am unsure how to combine this with the direction criteria.

Thank you very much.

Best regards

Ditlev

  • 1
    If I understand your question correctly it seems like some basic Pythagorean geometry (a^2 + b^2 = c^2) will get you started as √(((Xdf1 - Xdf2)^2)+((Ydf1 - Ydf2)^2)) will give you the distances between your points, and then subset those falling within the arc somehow, then subset the minimum values, as an order of operations? Don't have a more detailed specific solution for you. Maybe you could provide some example data? *This also depends on the co-ordinates system units you are working with, for example UTM will work as 1x = 1y = 1 metre. – Roasty247 Feb 27 '19 at 00:46
  • The coordinates that I am working with is just (x,y) (-100;100,-100;100) m so I am not really using any specific type of coordinates. I am aware of the formulaes that will calculate the distances from one point to another point and also the formulaes for calculating the degree slope of a line, but I am lacking the programming skills to combine this technically in a smart way in R only for the shortest distance and for points in different data frames. – Ditlev Reventlow Feb 27 '19 at 08:48
  • 1
    If I am right, this constraint breaks the distance relation, and all the Voronoi machinery falls apart. I wonder if you can do much better than exhaustive comparisons. I you don't care about efficiency, first select the points of df2 located in the desired sector and find the closest distance. –  Feb 27 '19 at 13:04
  • Thank you @YvesDaoust. Is it still impossible even though the points in df2 are located in a specific pattern? – Ditlev Reventlow Feb 27 '19 at 14:20
  • Is what impossible ? –  Feb 27 '19 at 14:34
  • To make a coding that determines which point in df2 that is closest to each point in df1 when the points in df2 are not just distributed randomly but are located in a pattern. – Ditlev Reventlow Feb 27 '19 at 15:29

0 Answers0