简体   繁体   English

基于距给定点的距离的最近GPS坐标

[英]Nearest GPS coordinate based on distance from a given point

I have a list of GPS locations in a MySQL server database. 我在MySQL服务器数据库中有一个GPS位置列表。 The user will be entering a GPS coordinate in the application and he should get the nearest GPS coordinate. 用户将在应用程序中输入GPS坐标,他应该获得最近的GPS坐标。

I don't mind the distance calculation is based on "crow's flight" or anything else. 我不介意距离计算是基于“乌鸦的飞行”或其他任何东西。 It should be fast enough to search thousands of GPS locations. 它应该足够快,可以搜索数千个GPS位置。

I prefer solution in C#, else I will try to get the logic and apply myself. 我更喜欢C#中的解决方案,否则我会尝试获取逻辑并应用自己。

There's one question on MySQL lat/long distance search in Need help optimizing a lat/Lon geo search for mysql 关于MySQL纬度/长距离搜索的一个问题需要帮助优化lat / Lon geo搜索mysql

For C# distance calculation, most sites use the Haversine formula - here's a C# implementation - http://www.storm-consultancy.com/blog/development/code-snippets/the-haversine-formula-in-c-and-sql/ - this also has a SQL (MS SQL) implementation too. 对于C#距离计算,大多数站点使用Haversine公式 - 这是一个C#实现 - http://www.storm-consultancy.com/blog/development/code-snippets/the-haversine-formula-in-c-and-sql / - 这也有一个SQL(MS SQL)实现。

/// <summary>
/// Returns the distance in miles or kilometers of any two
/// latitude / longitude points.
/// </summary>
/// <param name="pos1">Location 1</param>
/// <param name="pos2">Location 2</param>
/// <param name="unit">Miles or Kilometers</param>
/// <returns>Distance in the requested unit</returns>
public double HaversineDistance(LatLng pos1, LatLng pos2, DistanceUnit unit)
{
    double R = (unit == DistanceUnit.Miles) ? 3960 : 6371;
    var lat = (pos2.Latitude - pos1.Latitude).ToRadians();
    var lng = (pos2.Longitude - pos1.Longitude).ToRadians();
    var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) +
                  Math.Cos(pos1.Latitude.ToRadians()) * Math.Cos(pos2.Latitude.ToRadians()) *
                  Math.Sin(lng / 2) * Math.Sin(lng / 2);
    var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1)));
    return R * h2;
}

public enum DistanceUnit { Miles, Kilometers };

For most queries... you are probably OK splitting the work between C# and SQL 对于大多数查询......你可能可以在C#和SQL之间分配工作

  • use MySQL to select "close" lat/lng points, eg say where lat and lng are within 1.0 of your target 使用MySQL选择“关闭”lat / lng点,例如说lat和lng在你的目标的1.0之内
  • then use C# to calculate a more accurate distance and to select "the best". 然后使用C#计算更准确的距离并选择“最佳”。

If you were using MS SQL 2008 then I'd recommend using the MS SQL geography types as these have built-in optimised indexing and calculation features - I see that MySQL also has some extensions - http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html - but I've no experience with these. 如果您使用的是MS SQL 2008,那么我建议使用MS SQL地理类型,因为它们具有内置的优化索引和计算功能 - 我看到MySQL也有一些扩展 - http://dev.mysql.com/tech -resources / articles / 4.1 / gis-with-mysql.html - 但我对这些没有经验。

What you're trying to do is called a nearest-neighbor search and there are many good data structures which can speed up these sorts of searches. 你要做的事情被称为最近邻搜索,并且有许多良好的数据结构可以加速这些类型的搜索。 You may want to look into kd-trees, for example, as they can give expected sublinear time (O(√ n) in two dimensions) queries for the point in a data set nearest to some arbitrary test point. 例如,您可能希望查看kd树,因为它们可以给出最接近某个任意测试点的数据集中的点的预期次线性时间(二维中的O(√n))查询。 They're also surprisingly easy to implement if you're comfortable writing a modified binary search tree. 如果您习惯于编写修改后的二叉搜索树,那么它们也很容易实现。

Note that when dealing with spherical geometry our euclidean geometry isn't quite precise (a^2+b^2=c^2) but for small subparts of the earth it might be approximate enough. 请注意,在处理球面几何时,我们的欧几里德几何不是很精确(a ^ 2 + b ^ 2 = c ^ 2),但对于地球的小子部分,它可能足够近似。

Otherwise: http://en.wikipedia.org/wiki/Great-circle_distance 否则: http//en.wikipedia.org/wiki/Great-circle_distance

If you have coordinate data stored in a database, you might want to query the database directly, especially if there is a large amount of the data. 如果您具有存储在数据库中的坐标数据,则可能需要直接查询数据库,尤其是在存在大量数据的情况下。 However, you need specific database support for that (normal indexes do not help). 但是,您需要特定的数据库支持(普通索引没有帮助)。 I know MSSQL supports geography data , I did not test MySQL, but online documentation seems to suggest there is similar support, too. 我知道MSSQL支持地理数据 ,我没有测试MySQL,但在线文档似乎也暗示也有类似的支持。 As soon as you have built a spatial-aware database, you get your results with a simple query. 只要构建了空间感知数据库,就可以通过简单的查询获得结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM