从Polygon SqlGeographys创建MultiPolygon SqlGeography

时间:2014-07-07 07:32:51

标签: c# sqlgeography

我有一个多边形的SqlGeographys列表。我想将它们组合成一个多面体类型的SqlGeography。

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

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

1 个答案:

答案 0 :(得分:5)

我找到了一种使用SqlGeographyBuilder的方法,可能有一种更有效的方法,但这可行:

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;