We calculate the nearest objects by coordinates

I was developing one real estate project and the task was to show objects located within a radius of 20 km with a viewable one. Those. we have an object, in our case it is a village, and we need to display the nearby villages from our database within a radius of 20 km, while having only the coordinates of their location.





Study

So, to solve the problem, "googling" began. And the first thing that was googled was the algorithm for calculating the distances between two points on the ball, the article went to Wikipedia . The article is certainly interesting, but how does it help in my business !? As it turned out, it would be useful, but not immediately. The point is that the calculation of distances by coordinates is good, but how to make a selection from the base and calculate the coordinates "on the fly" !? Probably in this way in any way. Of course, the decision in the forehead was somehow to calculate how much you can move from the coordinate in order to get the desired distance and query through some SQL BETWEEN.





Google again and google the QUESTION on Habr Q&A. The best answer has a solution, but it indicates that our length of one degree in kilometers is equal to 111 km, but this is far from always the case. From here it was clear that the solution was too inaccurate. We read on and there a certain @ alex40 offers a solution just with between but choose by the square. The essence of his solution is to take a range of coordinates in a square and not in a circle and request a selection just with the BETWEEN operator. And looking at the elegance of this solution, I realized that I had to do something like this, but the question remained that the square introduces too much inaccuracy.





Solution scheme

Take the original coordinate point, and add to it the number of degrees corresponding to the distance required for the sample.





If we connect these points to which we added the original coordinate, we get a square (sorry, I didn’t draw exactly).





Then, with the help of the same Wikipedia, I discovered several formulas, and one of them is exactly what I need. This is the formula for calculating the length of a degree . Remember in the beginning I said that 111 km. this is too abstract from the first solution in the question. So, this formula allows you to calculate the length of a degree directly on a specific meridian or parallel, you just need to know the radius of our ball. There, on the Wikipedia page, there is pre-calculated data by which you can check your formula.





φ





Δ 1

lat





Δ 1

long





0 °





110.574 km





111.320 km





15 °





110.649 km





107.551 km





30 °





110.852 km





96.486 km





45 °





111,133 km





78.847 km





60 °





111,412 km





55.800 km





75 °





111.618 km





28.902 km





90°





111.694 km





0.000 km





. , :





  •  R = 6371210 .





  •  R = 6378,245 .





  •  R = 6356,830 .





. , - , , .





, , .





const EART_RADIUS = 6371210; // 
const DISTANCE = 20000; //  

//https://en.wikipedia.org/wiki/Longitude#Length_of_a_degree_of_longitude
function computeDelta(degrees) {
  return Math.PI / 180 * EART_RADIUS * Math.cos(deg2rad(degrees));
}

function deg2rad(degrees) {
  return degrees * Math.PI / 180;
}

const latitude = 55.460531; //   
const longitude = 37.210488; //   

const deltaLat = computeDelta(latitude); //   
const deltaLon = computeDelta(longitude); //   

const aroundLat = DISTANCE / deltaLat; //     
const aroundLon = DISTANCE / deltaLon; //     

console.log(aroundLat, aroundLon);
      
      



, , 1000, , , .





. , . , . , .





1 --- 63046.689652997775

X --- 200000





1 , 63046.689652997775 ( ), 20000 X. , , . 1, `DISTANCE / deltaLat`. .



0.31722522007226484 0.22583381380662185. , , .



SQL , , :





select
	name,
	latitude,
	longitude
from
	villages
where
	latitude between 55.460531 - 0.31722522007226484 and 55.460531 + 0.31722522007226484
	and longitude between 37.210488 - 0.22583381380662185 and 37.210488 + 0.22583381380662185;
      
      



7 . . 20. , , ?! , . , , , 20 .





. , 20 , , 20 . - , . , 20 . .. . , - . , 20 , .





. . "" "" , . , , , , , , .





https://en.wikipedia.org/wiki/Longitude#Length_of_a_degree_of_longitude -





https://en.wikipedia.org/wiki/Great-circle_distance -





https://gis-lab.info/qa/great-circles.html -





http://osiktakan.ru/geo_koor.htm - ,








All Articles