SqlGeography从multipolygon获取多边形

本文关键字:多边形 获取 multipolygon SqlGeography | 更新日期: 2023-09-27 18:17:45

我有一个表Town,列TownBoundary包含相关城镇的多边形(geography数据类型)。

对于每个城镇,我获得生成KML(XML)文件所需的多边形数据,如:

sqlg = SqlGeography.STPolyFromText(new SqlChars(
town.TownBoundary.WellKnownValue.WellKnownText),
town.TownBoundary.CoordinateSystemId);
for (int i = 1; i <= sqlg.STNumPoints(); i++)
{
    SqlGeography point = sqlg.STPointN(i);
    var pLong = (point.Long).ToString().Replace(",", ".");
    var pLat = (point.Lat).ToString().Replace(",", ".");
    double dLong = double.Parse(pLong, CultureInfo.InvariantCulture);
    double dLat = double.Parse(pLat, CultureInfo.InvariantCulture);
    kmlCoordinates.Add(new Vector(dLat, dLong)); //one point od polygon
}

town.TownBoundary.WellKnownValue.WellKnownTextPOLYGON(..开头的值

但最近我意识到一些城镇包含更多的多边形,WellKnownTextMULTIPOLYGON(..开始,STPolyFromText函数以错误结束。

我已经把它放在try{}块,但在catch{} -如果值是多多边形-是否有可能以某种方式获得单个多边形?我知道有方法STMPolyFromText,但我无法访问单独的多边形,只有方法STPolyFromText中的点。

我的目标是将多个多边形分割成多个多边形,然后对每个多边形执行与上面相同的方法。

SqlGeography从multipolygon获取多边形

解决了,在STMPolyFromText方法中,我可以使用STNumGeometries获得多边形数组。

sqlg = SqlGeography.STMPolyFromText(
new SqlChars(town.TownBoundary.WellKnownValue.WellKnownText),
town.TownBoundary.CoordinateSystemId);
for (int i = 1; i <= sqlg.STNumGeometries(); i++)
{
 SqlGeography poly = sqlg.STGeometryN(i);
 //foreach poly
}