Coordinates of the closest points of two geometries in Shapely

The GIS term you are describing is linear referencing, and Shapely has these methods. # Length along line that is closest to the point print(line.project(p)) # Now combine with interpolated point on line p2 = line.interpolate(line.project(p)) print(p2) # POINT (5 7) An alternative method is to use nearest_points: from shapely.ops import nearest_points p2 = nearest_points(line, … Read more

Find K nearest neighbors, starting from a distance matrix

Try to use FastKNN CRAN package (although it is not well documented). It offers k.nearest.neighbors function where an arbitrary distance matrix can be given. Below you have an example that computes the matrix you need. # arbitrary data train <- matrix(sample(c(“a”,”b”,”c”),12,replace=TRUE), ncol=2) # n x 2 n = dim(train)[1] distMatrix <- matrix(runif(n^2,0,1),ncol=n) # n x … Read more

How to calculate distance from a point to a line segment, on a sphere?

Here’s my own solution, based on the idea in ask Dr. Math. I’d be happy to see your feedback. Disclaimer first. This solution is correct for spheres. Earth isn’t a sphere, and the coordinates system (WGS 84) doesn’t assume it’s a sphere. So this is just an approximation, and I can’t really estimate is error. … Read more

Measure distance between two HTML elements’ centers

Get their positions, and use the Pythagorean Theorem to determine the distance between them… function getPositionAtCenter(element) { const {top, left, width, height} = element.getBoundingClientRect(); return { x: left + width / 2, y: top + height / 2 }; } function getDistanceBetweenElements(a, b) { const aPosition = getPositionAtCenter(a); const bPosition = getPositionAtCenter(b); return Math.hypot(aPosition.x – … Read more

Fastest available algorithm for distance transform

The OpenCV library uses for its approximate cv::distanceTransform function a algorithm which passes the image from top left to bottom right and back. The algorithm is described in the paper “Distance transformations in digital images” from Gunilla Borgefors (Comput. Vision Graph. Image Process. 34 3, pp 344–371, 1986). The algorithm calculates the distance through a … Read more

How to find my distance to a known location in JavaScript

If your code runs in a browser, you can use the HTML5 geolocation API: window.navigator.geolocation.getCurrentPosition(function(pos) { console.log(pos); var lat = pos.coords.latitude; var lon = pos.coords.longitude; }) Once you know the current position and the position of your “target”, you can calculate the distance between them in the way documented in this question: Calculate distance between … Read more