Skip to content

Instantly share code, notes, and snippets.

@hydr
Created December 19, 2012 17:47
Show Gist options
  • Save hydr/4338698 to your computer and use it in GitHub Desktop.
Save hydr/4338698 to your computer and use it in GitHub Desktop.
ToGoogleMapsPolygon
public static List<List<Coordinate>> ToGoogleMapsPolygon(this DbGeography dbGeo)
{
// contains all geo data for one postal code / admin area code
// conversion to SqlGeography because DbGeography does not offer properties to determine rings within geometries. we need those ring information to display areas with holes
var sqlGeo = (SqlGeography) dbGeo.ProviderValue;
var numberOfGeometries = sqlGeo.STNumGeometries();
var result = new List<List<Coordinate>>((int) numberOfGeometries);
// at first we iterate over all geometries within our sqlGeo object to get all polygones in case we have to deal with a multipolygone
for (int k = 1; k <= numberOfGeometries; k++)
{
var sqlGeography = sqlGeo.STGeometryN(k);
// if we one of our geometries has rings defined on it, we know that this area contain holes
if (!sqlGeography.NumRings().IsNull)
{
var numberOfRings = sqlGeography.NumRings();
// we now add one path for each ring
for (int i = 1; i <= numberOfRings; i++)
{
var ringGeography = sqlGeography.RingN(i);
var numberOfCoordinatesInRing = ringGeography.STNumPoints();
var path = new List<Coordinate>((int) numberOfCoordinatesInRing);
// a path is a list of coordinates; a coordinate consists of a Latitude and Longitude value
for (int n = 1; n <= numberOfCoordinatesInRing; n++)
{
path.Add(new Coordinate
{
Latitude = (double) ringGeography.STPointN(n).Lat,
Longitude = (double) ringGeography.STPointN(n).Long,
});
}
result.Add(path);
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment