Adam Knights
Adam Knights

Reputation: 2151

Create MultiPolygon SqlGeography from Polygon SqlGeographys

I have a list of SqlGeographys that are Polygons. I want to combine them into one SqlGeography of type multipolygon.

List<SqlGeography> areaPolygons = GetAreaPolygons()
SqlGeography multiPoly = null;

foreach (SqlGeography geog in areaPolygons)
{
    /// Combine them somehow?
}

Upvotes: 4

Views: 1315

Answers (1)

Adam Knights
Adam Knights

Reputation: 2151

I found a way using SqlGeographyBuilder, there may be a more efficient way but this works:

List<SqlGeography> areaPolygons = GetAreaPolygons()
SqlGeography multiPoly = null;

SqlGeographyBuilder sqlbuilder = new SqlGeographyBuilder();
sqlbuilder.SetSrid(4326);
sqlbuilder.BeginGeography(OpenGisGeographyType.MultiPolygon);

foreach (SqlGeography geog in areaPolygons)
{
    sqlbuilder.BeginGeography(OpenGisGeographyType.Polygon);

    for (int i = 1; i <= geog.STNumPoints(); i++)
        {
            if (i == 1)
                sqlbuilder.BeginFigure((double)geog.STPointN(i).Lat, (double)geog.STPointN(i).Long);
            else
                sqlbuilder.AddLine((double)geog.STPointN(i).Lat, (double)geog.STPointN(i).Long);
        }

        sqlbuilder.EndFigure();
        sqlbuilder.EndGeography();
}

sqlbuilder.EndGeography();
multiPoly = sqlbuilder.ConstructedGeography;

Upvotes: 5

Related Questions