Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/polygon intersection #200

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/Itinero/Algorithms/Collections/BinaryHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public int Count
}

/// <summary>
/// Enqueues a given item.
/// Enqueues a given item. The lower the value, the higher the priority.
/// </summary>
public void Push(T item, float priority)
{
Expand Down
93 changes: 74 additions & 19 deletions src/Itinero/LocalGeo/Coordinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

using System;
using System.Resources;
using Itinero.Navigation.Directions;

namespace Itinero.LocalGeo
Expand Down Expand Up @@ -72,34 +73,36 @@ public Coordinate OffsetWithDirection(float distance, DirectionEnum direction)

var oldLat = this.Latitude.ToRadians();
var oldLon = this.Longitude.ToRadians();
var bearing = ((double)(int)direction).ToRadians();
var bearing = ((double) (int) direction).ToRadians();

var newLatitude = System.Math.Asin(
System.Math.Sin(oldLat) *
System.Math.Cos(ratioInRadians) +
System.Math.Cos(oldLat) *
System.Math.Sin(ratioInRadians) *
System.Math.Cos(bearing));
System.Math.Sin(oldLat) *
System.Math.Cos(ratioInRadians) +
System.Math.Cos(oldLat) *
System.Math.Sin(ratioInRadians) *
System.Math.Cos(bearing));

var newLongitude = oldLon + System.Math.Atan2(
System.Math.Sin(bearing) *
System.Math.Sin(ratioInRadians) *
System.Math.Cos(oldLat),
System.Math.Cos(ratioInRadians) -
System.Math.Sin(oldLat) *
System.Math.Sin(newLatitude));
System.Math.Sin(bearing) *
System.Math.Sin(ratioInRadians) *
System.Math.Cos(oldLat),
System.Math.Cos(ratioInRadians) -
System.Math.Sin(oldLat) *
System.Math.Sin(newLatitude));

var newLat = newLatitude.ToDegrees();
if (newLat > 180)
{
newLat = newLat - 360;
}

var newLon = newLongitude.ToDegrees();
if (newLon > 180)
{
newLon = newLon - 360;
}
return new Coordinate((float)newLat, (float)newLon);

return new Coordinate((float) newLat, (float) newLon);
}

/// <summary>
Expand All @@ -116,7 +119,8 @@ public static float DistanceEstimateInMeter(Coordinate coordinate1, Coordinate c
/// Returns an estimate of the distance between the two given coordinates.
/// </summary>
/// <remarks>Accuraccy decreases with distance.</remarks>
public static float DistanceEstimateInMeter(float latitude1, float longitude1, float latitude2, float longitude2)
public static float DistanceEstimateInMeter(float latitude1, float longitude1, float latitude2,
float longitude2)
{
var lat1Rad = (latitude1 / 180d) * System.Math.PI;
var lon1Rad = (longitude1 / 180d) * System.Math.PI;
Expand All @@ -128,7 +132,7 @@ public static float DistanceEstimateInMeter(float latitude1, float longitude1, f

var m = System.Math.Sqrt(x * x + y * y) * RadiusOfEarth;

return (float)m;
return (float) m;
}

/// <summary>
Expand All @@ -137,14 +141,26 @@ public static float DistanceEstimateInMeter(float latitude1, float longitude1, f
public static float DistanceEstimateInMeter(System.Collections.Generic.List<Coordinate> coordinates)
{
var length = 0f;
for(var i = 1; i < coordinates.Count; i++)
for (var i = 1; i < coordinates.Count; i++)
{
length += Coordinate.DistanceEstimateInMeter(coordinates[i - 1].Latitude, coordinates[i - 1].Longitude,
coordinates[i].Latitude, coordinates[i].Longitude);
}

return length;
}

/// <summary>
/// Return how much degrees the coordinate is away from b
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public float DistanceInDegrees(Coordinate b)
{
return (float) Math.Sqrt((Latitude - b.Latitude) * (Latitude - b.Latitude) +
((Longitude - b.Longitude) * (Longitude - b.Longitude)));
}

/// <summary>
/// Offsets this coordinate with a given distance.
/// </summary>
Expand All @@ -160,18 +176,57 @@ public Coordinate OffsetWithDistances(float meter)
return new Coordinate(this.Latitude + (meter / latDistance) * 0.1f,
this.Longitude + (meter / lonDistance) * 0.1f);
}

/// <summary>
/// Returns a description of this object.
/// </summary>
public override string ToString()
{
if (this.Elevation.HasValue)
{
return string.Format("{0},{1}@{2}m", this.Latitude.ToInvariantString(), this.Longitude.ToInvariantString(),
return string.Format("{0},{1}@{2}m", this.Latitude.ToInvariantString(),
this.Longitude.ToInvariantString(),
this.Elevation.Value.ToInvariantString());
}

return string.Format("{0},{1}", this.Latitude.ToInvariantString(), this.Longitude.ToInvariantString());
}

public static Coordinate operator +(Coordinate a, Coordinate b)
{
return new Coordinate(){Latitude = a.Latitude + b.Latitude, Longitude = a.Longitude + b.Longitude};;
}

public static Coordinate operator -(Coordinate a, Coordinate b)
{
return new Coordinate() {Latitude = a.Latitude - b.Latitude, Longitude = a.Longitude - b.Longitude};
}

public static Coordinate operator /(Coordinate a, float b)
{
return new Coordinate(a.Latitude / b, a.Longitude / b);
}

public static float DotProduct(Coordinate a, Coordinate b)
{
return a.Latitude * b.Latitude + a.Longitude * b.Longitude;
}
/*
public override bool Equals(object obj)
{
var coor = obj as Coordinate?;
if (coor == null)
{
return false;
}

var c = (Coordinate) coor;
return this.Latitude == c.Latitude && this.Longitude == c.Longitude;
}

public override int GetHashCode()
{
return Latitude.GetHashCode() + Longitude.GetHashCode();
}*/
}
}
Loading