SqlGeography从多边形获取多边形

时间:2014-08-13 14:44:01

标签: c# polygon geography sqlgeography

我的表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.WellKnownText的值以POLYGON(..

开头

但是最近我意识到有些城镇包含更多的多边形,WellKnownTextMULTIPOLYGON(..开头,而函数STPolyFromText最终错误。

我已将其放置在try{}块中,但在catch{}中 - 如果值为多字形 - 是否可能以某种方式获取单个多边形?我知道有方法STMPolyFromText,但我无法在那里处理单个多边形,只能像方法STPolyFromText那样进行点。

我的目标是将多边形分割为多边形,而foreach多边形则采用与上述相同的方法。

1 个答案:

答案 0 :(得分:6)

解决方法,方法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
}