public static Tuple<double, double, double, double> GetEnvelopeFromPolygon(DbGeography polygon)
{
if (polygon == null)
{
return new Tuple<double, double, double, double>(0, 0, 0, 0);
}
var points = polygon.GetPointsFromPolygon();
double minLat = double.MaxValue;
double maxLat = double.MinValue;
double minLon = double.MaxValue;
double maxLon = double.MinValue;
foreach (Tuple<double, double> point in points)
{
double lat = point.Item1;
double lon = point.Item2;
if (lat < minLat)
{
minLat = lat;
}
if (lat > maxLat)
{
maxLat = lat;
}
if (lon < minLon)
{
minLon = lon;
}
if (lon > maxLon)
{
maxLon = lon;
}
}
var res = new Tuple<double, double, double, double>(minLat, maxLat, minLon, maxLon);
return res;
}
public static IEnumerable<Tuple<double, double>> GetPointsFromPolygon(this System.Data.Entity.Spatial.DbGeography geo)
{
for (int i = 1; i < geo.PointCount; i++)
{
var p = geo.PointAt(i);
yield return new Tuple<double, double>(p.Latitude.Value, p.Longitude.Value);
}
var pFinal = geo.PointAt(1);
yield return new Tuple<double, double>(pFinal.Latitude.Value, pFinal.Longitude.Value);
}