使用经度和纬度查找彼此靠近的城市
我的数据库中的每个用户都与一个城市相关联(及其经度和纬度)
我将如何找出哪些城市彼此接近?
即在英国,剑桥距离伦敦相当近。
所以如果我有一个住在剑桥的用户。靠近他们的用户将是居住在附近城市的用户,例如伦敦、赫特福德等。
我有什么想法可以解决这个问题吗?而且,我如何定义什么是接近的?即在英国,收盘价会比在美国更接近,因为美国更加分散。
想法和建议。另外,您知道有哪些服务提供此类功能吗?
谢谢
Each user in my db is associated to a city (with it's longitude and latitude)
How would I go about finding out which cities are close to one another?
i.e. in England, Cambridge is fairly close to London.
So If I have a user who lives in Cambridge. Users close to them would be users living in close surrounding cities, such as London, Hertford etc.
Any ideas how I could go about this? And also, how would I define what is close? i.e. in the UK close would be much closer than if it were in the US as the US is far more spread out.
Ideas and suggestions. Also, do you know any services that provide this sort of functionality?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您可以调用外部 Web 服务,则可以使用 GeoNames API 来定位您定义的某个半径内的附近城市:
http://www.geonames.org/export/web-services.html
If you can call an external web service, you can use the GeoNames API for locating nearby cities within some radius that you define:
http://www.geonames.org/export/web-services.html
从城市名称获取坐标称为反向地理编码。谷歌地图有一个很好的 Api 。
还有 Geonames 项目,您可以在其中获得城市、邮政编码等及其坐标的庞大数据库,
但是如果您已经有了坐标,只需简单计算即可得到距离。
棘手的是获得它的一个性能良好的版本。您可能已将其存储在 mysql 数据库中,因此您需要在那里快速执行此操作。
这是绝对有可能的。我曾经做过一个包含该代码的项目,我将获取它并将其发布在这里。
然而,为了加快速度,我建议首先围绕中心坐标进行矩形选择。使用蜂树索引甚至更好的东西(如多维范围搜索),这是非常非常快的。然后,您可以在其中计算有限数据集的精确距离。
在矩形选择之外,方向是如此之大,以至于不需要如此精确地显示或计算。或者只是显示国家/地区、大陆或类似的内容。
我还在办公室,但当我回到家时,我可以为您获取代码。在此期间,如果您能告诉我您如何存储数据,那就太好了。
编辑:同时这里你有一个对我来说看起来很合适的函数(我在一个查询中没有函数的情况下完成了它......)
我从这里引用了这一点: http://sebastian-bauer.ws/en/2010/12/ 12/geo-kooperativen-mysql-funktion-zur-berechnung-des-abstands.html
这是另一个链接:http://www.andrewseward.co.uk/2010/04/sql-function-to-calculate-distance.html
Getting coordinates from City names is called reverse geo coding. Google maps has a nice Api fot that.
There is also the Geonames project where you get huge databases of cities, zip codes etc and their cooridnates
However if you already have the coordinates, its a simple calculation to get the distance.
The tricky thing is to get a nice performant version of it. You probably have it stored in a mysql database, so you need to do it there and fast.
It is absolutely possible. I once did a project including that code, I will fetch it and post it here.
However to speed things up I would recommend first doing a rectangular selection around the center coordinates. This is very, very fast using bee tree indexes or even better stuff like multidimensional range search. Then inside that you can then calculate the exact distances on a limited set of data.
Outside that recangular selection the directions are so vast that it does not need to be displayed or calculated so accurately. Or just display the country, continent or something like that.
I am still at the office but when i get home i can fetch the codes for you. Int he meantime it would be good if you could inform me how you store your data.
Edit: in the mean time here you have a function which looks right to me (i did it without a function in one query...)
i quoted this from here: http://sebastian-bauer.ws/en/2010/12/12/geo-koordinaten-mysql-funktion-zur-berechnung-des-abstands.html
and here is another link: http://www.andrewseward.co.uk/2010/04/sql-function-to-calculate-distance.html
最简单的方法是根据城市的纬度和经度以及距离(通过将距离转换为经度)来计算边界框。
获得该框(最小纬度、最大纬度、最小经度、最大经度)后,查询纬度和经度位于边界框内的其他城市。这将为您提供一个大概的列表,并且应该非常快,因为它将能够使用您在纬度和经度列上可能拥有的任何索引。
如果需要,您可以从那里使用真正的“球体上点之间的距离”函数缩小列表范围。
The simplest way to do this would be to calculate a bounding box from the latitude and longitude of the city and a distance (by converting the distance to degrees of longitude).
Once you have that box (min latitude, max latitude, min longitude, max longitude), query for other cities whose latitude and longitude are inside the bounding box. This will get you an approximate list, and should be quite fast as it will be able to use any indexes you might have on the latitude and longitude columns.
From there you can narrow the list down if desired using a real "distance between points on a sphere" function.
您需要空间索引或 GIS 功能。你使用什么数据库? MySQL 和 PostgreSQL 都具有 GIS 支持,允许您使用 SQL 查询找到 N 个最近的城市。
You need a spatial index or GIS functionality. What database are you using? MySQL and PostgreSQL both have GIS support which would allow you to find the N nearest cities using an SQL query.
您可能要考虑的另一个选择是将所有城市放入空间搜索树(如 kd 树)中。 Kd 树有效地支持最近邻搜索,以及快速搜索给定边界框中的所有点。然后,您可以通过搜索该城市的一些最近邻居来找到附近的城市,然后使用到这些邻居的距离来获取要搜索的边界框的估计大小。
Another option you might want to consider would be to put all of the cities into a spatial search tree like a kd-tree. Kd-trees efficiently support nearest-neighbor searches, as well as fast searches for all points in a given bounding box. You could then find nearby cities by searching for a few of the city's nearest neighbors, then using the distance to those neighbors to get an estimate size for a bounding box to search in.