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.WellKnownText
以POLYGON(..
开头的值
但最近我意识到一些城镇包含更多的多边形,WellKnownText
以MULTIPOLYGON(..
开始,STPolyFromText
函数以错误结束。
我已经把它放在try{}
块,但在catch{}
-如果值是多多边形-是否有可能以某种方式获得单个多边形?我知道有方法STMPolyFromText
,但我无法访问单独的多边形,只有方法STPolyFromText
中的点。
我的目标是将多个多边形分割成多个多边形,然后对每个多边形执行与上面相同的方法。
解决了,在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
}