简体   繁体   中英

SqlGeography get polygons from multipolygon

I have table Town with column TownBoundary that contains polygon of relevant town ( geography datatype).

For each town I get polygon data that I need to generate KML(XML) files like:

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
}

Values of town.TownBoundary.WellKnownValue.WellKnownText begins with POLYGON(..

But recently I have realised that some towns contains more polygons and WellKnownText begins with MULTIPOLYGON(.. and function STPolyFromText ends up in error.

I have placed this in try{} block, but in catch{} - if value is multipolygon - is it possible to somehow get individual polygons? I know there is method STMPolyFromText , but I can't acccess individual polygons there, only points as in method STPolyFromText .

My goal is to split multipolygon to polygons and than foreach polygon do the same method as above.

Solved it, in method STMPolyFromText I can get array of polygons using 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
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM