如何使用leaflet-knn计算到给定点的最接近点? [英] how to calculate the closest points to a given point using leaflet-knn?
问题描述
我有两个点层(客户,供应商),我想在用户输入的距离内找到特定客户的所有最近的供应商,我听说了传单knn,但找不到完整的示例来理解它好吧,有什么建议吗? 这是我的代码,是通过ajax获取客户数据的部分
I have two point Layers (Customers,Suppliers), i want to get all the nearest suppliers to a specific customer within a distance entered by user,i heared about leaflet knn but i couldn't find a full example to understand it well,any suggestions? This is my code in the part of getting the customers data through ajax
$.ajax({
type: "POST",
url: 'Customers_geojson.php',
dataType: 'json',
success: function (response) {
geojsonLayer = L.geoJson(
response,
{
pointToLayer: function (feature, latlng)
{
return L.circleMarker(latlng, geojsonMarkerOptions1);
}
},
{
onEachFeature: function (feature, layer)
{
layer.bindPopup('<label>Nick Name:</label>' + feature.properties.nick_name_);
}
}).addTo(mymap);
$("#info").fadeOut(500);
var gj = L.geoJson(response);
var nearest = leafletKnn(gj).nearest(L.latLng(8.71224, 125.692), 10000);
alert(nearest);
}
});
推荐答案
如何使用leaflet-knn计算到给定点的最接近点?
How to calculate the closest points to a given point using leaflet-knn?
答案是你不能".
一种最近的邻居搜索算法,例如 leaflet-knn
)实现,可帮助您回答以下查询:
A nearest neighbour search algorithm, such as the one that rbush-knn
(and the much older leaflet-knn
) implements, helps you answer queries like:
我住在(x,y);离我家最近的 5家商店是什么?
或
我在(x,y); 最近地铁站在哪里?
甚至
我在(x,y); 最近地铁站在哪里? 将限制在500米以内,因为我不会走那么远.
I'm at (x,y); where is the closest subway station? Limit the search to 500 meters because I won't bother walking that far.
...但是您要回答的查询 非常不同(强调我的意思):
...but the query you want to answer is very different (emphasis mine):
我想所有在用户输入的距离内最接近特定客户的供应商.
I want to get all the nearest suppliers to a specific customer within a distance entered by user.
使用最近的搜索算法是该工作的错误工具.您想要的是:
Using a nearest-neighbour search algorithm is the wrong tool for the job. What you want is either:
- 给出一个点,生成一个以该点为中心的圆,半径为
N
.该圆将包含与中心的距离等于或小于N
的所有所有点, - 给出一个点,生成一个以该点为中心的正方形,其边长为
N*2
.该正方形将包含所有个点,这些点与中心的距离等于或小于N
,而加则不是.
- Given a point, generate a circle centred on that point, with radius
N
. That circle will contain all points that are at a distance equal or less thanN
from the centre, and nothing else. - Given a point, generate a square centred on that point, with sides of length
N*2
. That square will contain all points that are at a distance equal or less thanN
from the centre, plus some which are not.
在GIS行话中,做一个圆被称为制作一个缓冲区,然后找到缓冲区的交点和您的点集.
In GIS jargon, making a circle is known as making a buffer, then finding the intersection of the buffer and your points set.
想法,制作一个正方形称为制作边界框,并在边界框内查询点.
Idem, making a square is known as making a bounding box, and querying points inside the bounding box.
制作缓冲区听起来最好,但是在计算上更昂贵.非常非常.创建一个边界框并查询一组点非常便宜(读为"fast"),并且可以通过对r-tree结构的简单查询来实现(请参见例如
Making a buffer sounds best, but is computationally more expensive. Very much so. Creating a bounding box and querying a set of points is very very cheap (read "fast") and can be achieved with a simple query to a r-tree structure (see e.g. the documentation and examples for rbush
).
这篇关于如何使用leaflet-knn计算到给定点的最接近点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!