查询地理编码位置(纬度/经度)周围的周长
如何搜索特定范围内的地理物体?例如,我的数据库中存储了几个带有纬度/经度坐标的对象。现在我想检索位于给定点周围特定周长(10 英里或 20 英里)内的所有对象。
我想我要做的就是形成一些查询,例如:
SELECT * FROM objects o where o.lat < (myPositionLat+x) AND o.lon < (myPositionLon+y)
这是正确的吗?如何确定/设置 x 和 y 的值?
how can I search for geo-objects in a certain perimeter ? E.g. I have several objects with Lat/Lon coordinates stored in my DB. Now I want to retrieve all the objects which lie in a specific perimeter (10 miles or 20 miles) around a given point.
I guess what I would have to do is to form some query like:
SELECT * FROM objects o where o.lat < (myPositionLat+x) AND o.lon < (myPositionLon+y)
Is this right? How do I determine/set the values for x and y?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您必须记住,经度之间的距离不是恒定的。纬度是平行的,相距约 111 公里(69 英里),但经度在赤道最宽,为 111 公里,在两极逐渐收敛到零。
在北纬 40° 或南纬 40° 处,经度之间的距离约为 85 公里(53 英里),而纬度之间的距离大致相同,为 111 公里 (来源)。如果您的地理对象仅限于某个区域,并且可以接受非常粗略的过滤器,则您可以使用此信息来确定
x
和y
偏移量。但是,如果您的对象在全球范围内稀疏,您应该计算大圆距离 相反。幸运的是,使用 haversine 公式,这相对容易。您可能需要进一步阅读并查看计算距离、方位等的实现纬度/经度点之间,作者:Chris Veness。
如果您只有少数地理对象,则可以简单地计算从您的点到每个地理对象的大圆距离。然后简单地按距离对结果列表进行排序,并按某个以英里或公里为单位的阈值进行过滤。
但是,如果您将拥有许多地理对象,则应考虑使用具有空间索引 能力。 MySQL,PostgreSQL 和 SQL Server 2008 都具有地理空间功能(本机或通过扩展),其中包括空间索引和半正弦公式的实现。
You have to keep in mind that the distance between longitudes is not constant. Degrees of latitude are parallel and are approximately 111 kilometers (69 miles) apart, but a degree of longitude is widest at the equator at 111 kilometers and gradually converges to zero at the poles.
At 40° north or south the distance between a degree of longitude is about 85 kilometers (53 miles), while the distance between latitudes remains roughly the same at 111 kilometers (Source). If your geo-objects will be limited to a certain area only, and a very rough filter is acceptable, you can use this information to determine your
x
andy
offsets.However if your objects will be sparse around the globe, you should be calculating the great-circle distance instead. Luckily this is relatively easy with the haversine formula. You may want to read further and check out the implementation at Calculate distance, bearing and more between Latitude/Longitude points by Chris Veness.
If you will only have a handful of geo-objects, you can simply calculate the great-circle distance from your point to each geo-object. Then simply sort the result list by the distance, and filter by some threshold in miles or kilometers.
However, if you will be having many geo-objects, you should consider using a database with spatial indexing capabilities. MySQL, PostgreSQL and SQL Server 2008 all have geo-spatial features (either natively or via extensions), which include spatial indexing and implementations of the haversine formula.