Probablemente haya una solución simple para esto, pero aún no la he descubierto. Tengo una tabla de ID de ubicación con ubicaciones X e Y. Necesito identificar la ubicación más cercana y calcular la distancia euclidiana a esa ubicación.La distancia no parece demasiado onerosa, pero estoy luchando por encontrar la ubicación más cercana. Se adjunta archivo. Gracias.
Tabla KD () es probablemente lo que necesitas.
@Craige_Hales tiene una publicación de blog: Vecinos
Hola Peter, esto usa la plataforma KNN para obtener filas y distancias vecinas, luego escribe la ID del vecino en una nueva columna.
Names Default To Here( 1 );
dt = Current Data Table();
knn = K Nearest Neighbors(
X( :X Location, :Y Location ),
K( 1 ),
save near neighbor rows,
save near neighbor distances
);
knn << close window;
dt << New Column( "Neighbor ID", formula( dt:siteId[:rowNear 1] ) );
dt:Neighbor ID << delete formula;
Retomando 'KDTable ()' mencionado por @Jeff_Perkinson , aquí tienes un ejemplo:
Names Default To Here( 1 );
n = 100; // Size of square
// Make a table of x and y locations
dtx = AsTable((1::n)`, << Invisible);
dty = AsTable((1::n)`, << Invisible);
dt = dty << Join( With( dtx ), Cartesian Join );
Close(dtx, NoSave);
Close(dty, NoSave);
Column(dt, 1) << setName("x");
Column(dt, 2) << setName("y");
dt << deleteProperty("Source");
dt << setName("xy Locations");
// Put these locations into a matrix
mat = dt << getAsMatrix;
tbl = KDTable( mat );
// Pick a point in the interior, so we don't need to decide on any boundary conditions
myX = RandomInteger(2, n-1);
myY = RandomInteger(2, n-1);
myRow = Loc(mat[0, 1] == myX & mat[0, 2] == myY);
myRow = myRow[1];
// Find the 8 nearest neighbours to the chosen point
{rows, dist} = tbl << K nearest rows( 8, myRow );
Show( myRow, rows );
// Check it worked
myXvals = Column(dt, "x")[myRow];
myYvals = Column(dt, "y")[myRow];
dt << NewColumn("Distance", Numeric, Continuous, Formula(sqrt((:x - myXvals)^2 +(:y - myYvals)^2)));
dt << selectRows(VConcat(myRow, rows`));
gb = dt << Graph Builder(
Size( 529, 453 ),
Show Control Panel( 0 ),
Variables( X( :x ), Y( :y ), Color( :Distance ) ),
Elements( Points( X, Y, Legend( 5 ) ) )
);