2017-02-03 114 views
3

不知道如何解決這個問題。我有2分地理列SQL Server空間查找剩餘距離

CREATE TABLE #Trip 
... 
LegRoute geography NULL, 
GPSPoint geography NULL, 
GPSPointToLegRouteDistance Float NULL, 
... 
  • LegRoute包含折線
  • GPSPoint包含由點到線點

我可以得到距離(英里)。這是GPS相對於路徑的位置。

UPDATE T 
SET GPSPointToLegRouteDistance = LegRoute.STDistance(GPSPoint)/1609.344    
FROM #Trip T 

我需要找到的是這個距離計算的POINT。然後,我需要一些方法來計算從該點到折線結束的距離。

現實世界中的描述:

車輛可能會偏離路線(總是會)。但我需要在路線上找到最近的點以及剩餘行程距離。

回答

0

好的,我確實解決了這個問題。隨意評論和建議更好/更快的方式。經過一番研究,似乎沒有辦法通過內置的SQL Server功能來實現。所以我訴諸使用CLR集成。

有一些「假設」,從數學的角度來看,解決方案並不完全準確。但我的目標是速度。所以,我做了我所做的。此外,在我們的數據庫中 - 我們將GPS和路線存儲爲緯度/經度和絃(類似於多個網絡服務返回的那些弦,只是一系列點)

僅通過查看我們擁有哪些數據 - 「超過1/2-1英里。我估計最多隻有0.2英里的誤差。這是我們所做的事情(估計道路卡車的剩餘行駛距離/時間)沒有任何意義。比較我們的嘗試與地理類型的表現是非常不錯的。但如果您對提高C#代碼速度有所建議 - 請做..

public class Functions 
{ 
    /// <summary> 
    /// Function receives path and current location. We find remaining distance after 
    /// matching position. Function will return null in case of error. 
    /// If everything goes well but last point on route is closest to GPS - we return 0 
    /// </summary> 
    /// <param name="lat"> 
    /// Latitude of current location 
    /// </param> 
    /// <param name="lon"> 
    /// Longitude of current location 
    /// </param> 
    /// <param name="path"> 
    /// Path as a series of points just like we have it in RouteCache 
    /// </param> 
    /// <param name="outOfRouteMetersThreshhold"> 
    /// Out of route distance we can tolerate. If reached - return NULL 
    /// </param> 
    /// <returns> 
    /// Meters to end of path. 
    /// </returns> 
    [SqlFunction] 
    public static double? RemainingDistance(double lat, double lon, string path, double outOfRouteThreshhold) 
    { 
     var gpsPoint = new Point { Lat = lat, Lon = lon }; 

     // Parse path into array of points 
     // Loop and find point in sequence closest to our input GPS lat/lon 
     // IMPORTANT!!! There is some simplification of issue here. 
     // We assume that linestring is pretty granular with small-ish segments 
     // This way we don't care if distance is to segment. We just check distance to each point. 
     // This will give better performance but will not be too precise. For what we do - it's OK 
     var closestPointIndex = 0; 
     double distance = 10000; 
     var pointArrayStr = path.Split(','); 
     if (pointArrayStr.Length < 2) return null; 

     for (var i = 0; i < pointArrayStr.Length; i++) 
     { 
      var latLonStr = pointArrayStr[i].Split(' '); 
      var currentDistance = DistanceSqrt(
       gpsPoint, 
       new Point { Lat = double.Parse(latLonStr[1]), Lon = double.Parse(latLonStr[0]) }); 
      if (currentDistance >= distance) continue; 

      distance = currentDistance; 
      closestPointIndex = i; 
     } 

     // Closest point known. Let's see what this distance in meters and handle out of route 
     var closestPointStr = pointArrayStr[closestPointIndex].Split(' '); 
     var closestPoint = new Point { Lat = double.Parse(closestPointStr[1]), Lon = double.Parse(closestPointStr[0]) }; 
     var distanceInMeters = DistanceMeters(gpsPoint, closestPoint); 
     if (distanceInMeters > outOfRouteThreshhold) return null; 

     // Last point closest, this is "complete" route or something wrong with passed data 
     if (closestPointIndex == pointArrayStr.Length - 1) return 0; 

     // Reconstruct path string, but only for remaining points in line 
     var strBuilder = new StringBuilder(); 
     for (var i = closestPointIndex; i < pointArrayStr.Length; i++) strBuilder.Append(pointArrayStr[i] + ","); 
     strBuilder.Remove(strBuilder.Length - 1, 1); 

     // Create geography linestring and calculate lenght. This will be our remaining driving distance 
     try 
     { 
      var geoPath = SqlGeography.STGeomFromText(new SqlChars($"LINESTRING({strBuilder})"), 4326); 
      var dist = geoPath.STLength().Value; 
      return dist; 
     } 
     catch (Exception) 
     { 
      return -1; 
     } 
    } 

    // Compute the distance from A to B 
    private static double DistanceSqrt(Point pointA, Point pointB) 
    { 
     var d1 = pointA.Lat - pointB.Lat; 
     var d2 = pointA.Lon - pointB.Lon; 

     return Math.Sqrt(d1 * d1 + d2 * d2); 
    } 

    private static double DistanceMeters(Point pointA, Point pointB) 
    { 
     var e = Math.PI * pointA.Lat/180; 
     var f = Math.PI * pointA.Lon/180; 
     var g = Math.PI * pointB.Lat/180; 
     var h = Math.PI * pointB.Lon/180; 

     var i = Math.Cos(e) * Math.Cos(g) * Math.Cos(f) * Math.Cos(h) 
      + Math.Cos(e) * Math.Sin(f) * Math.Cos(g) * Math.Sin(h) 
      + Math.Sin(e) * Math.Sin(g); 

     var j = Math.Acos(i); 
     var k = 6371 * j; // 6371 earth radius 

     return k * 1000; 
    } 

    private struct Point 
    { 
     public double Lat { get; set; } 

     public double Lon { get; set; } 
    } 
}