4
votes

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.

1

1 Answers

6
votes

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
}