public static SingleRoute GetRouteFromMidRoad(string subquery1, string subquery2, MobileUnit obj, Road currentRoad, Crossing target) { // needs refactoring after testing string table = Model.Managers[obj.GetType()].GetTable(); string query1 = string.Format(queryRoadToCrossing, subquery1, table, currentRoad.ID, obj.ID); string query2 = string.Format(queryRoadToCrossing, subquery2, table, currentRoad.ID, obj.ID); DataTable dt1 = PostGisUtils.Query(query1); DataTable dt2 = PostGisUtils.Query(query2); SingleRoute route = new SingleRoute(); double length1 = double.MaxValue; double length2 = double.MaxValue; if (dt1.Rows.Count == 1) { length1 = (double)dt1.Rows[0].ItemArray[1]; } if (dt2.Rows.Count == 1) { length2 = (double)dt2.Rows[0].ItemArray[1]; } Console.WriteLine("route lengths: {0} {1}", length1, length2); if (length1 < length2) { route.Length = length1; string ewkb = ReverseIfNeeded((string)dt1.Rows[0].ItemArray[0], PostGisUtils.GetEWKB(obj)); route.Geom = GeoTypeExtensions.FromEWKB(ewkb) as LineString; } else if (length1 > length2) { route.Length = length2; string ewkb = ReverseIfNeeded((string)dt2.Rows[0].ItemArray[0], PostGisUtils.GetEWKB(obj)); route.Geom = GeoTypeExtensions.FromEWKB(ewkb) as LineString; } else { //throw new GsecException("can't find any route"); Console.WriteLine("NO ROUTE SHOULD THROW EXCEPTION"); return(null); } return(route); }
public static string ReverseIfNeeded(string ewkbRoute, string ewkbPosition) { string queryReverseRoute = string.Format(@" SELECT CASE WHEN ST_DWithin(ST_EndPoint('{0}'), '{1}', 0.0001) = TRUE THEN ST_Reverse('{0}') ELSE '{0}' END AS geom", ewkbRoute, ewkbPosition); DataTable dt = PostGisUtils.Query(queryReverseRoute); if (dt.Rows.Count == 0) { throw new GsecException("failed to reverse route"); } string ewkbFixed = (string)dt.Rows[0].ItemArray[0]; return(ewkbFixed); }
public static SingleRoute GetRouteFromCrossing(string subquery, MobileUnit obj, Crossing source, Crossing target) { string query = string.Format(queryCrossingToCrossing, subquery); DataTable dt = PostGisUtils.Query(query); if (dt.Rows.Count == 0 || dt.Rows[0].ItemArray[0] is DBNull) { //throw new GsecException("can't find any route"); Console.WriteLine("NO ROUTE SHOULD THROW EXCEPTION"); return(null); } string ewkb = (string)dt.Rows[0].ItemArray[0]; ewkb = ReverseIfNeeded(ewkb, PostGisUtils.GetEWKB(source)); SingleRoute route = new SingleRoute(); route.Geom = GeoTypeExtensions.FromEWKB(ewkb) as LineString; route.Length = (double)dt.Rows[0].ItemArray[1]; return(route); }