public static Dictionary <string, object> MultiPolygonToJson(Microsoft.SqlServer.Types.SqlGeometry multipolygon) { Dictionary <string, object> geometry = new Dictionary <string, object>(); geometry.Add("type", "MultiPolygon"); List <object> polygons = new List <object>(); int geometries = (int)multipolygon.MakeValid().STNumGeometries(); for (int k = 1; k <= geometries; k++) { Microsoft.SqlServer.Types.SqlGeometry polygon = multipolygon.MakeValid().STGeometryN(k); List <object> coordinates = new List <object>(); // exterior ring int expoints = (int)polygon.STExteriorRing().STNumPoints(); List <object> ring = new List <object>(); for (int j = 1; j <= expoints; j++) { List <object> coordinate = new List <object>(); coordinate.Add(Convert.ToDouble(polygon.STPointN(j).STX.Value)); coordinate.Add(Convert.ToDouble(polygon.STPointN(j).STY.Value)); ring.Add(coordinate); } coordinates.Add(ring); // interior rings ring = new List <object>(); int rings = (int)polygon.STNumInteriorRing(); for (int i = 1; i <= rings; i++) { int inpoints = (int)polygon.STInteriorRingN(i).STNumPoints(); List <object> inring = new List <object>(); for (int j = 1; j <= inpoints; j++) { List <object> coordinate = new List <object>(); coordinate.Add(Convert.ToDouble(polygon.STInteriorRingN(i).STPointN(j).STX.Value)); coordinate.Add(Convert.ToDouble(polygon.STInteriorRingN(i).STPointN(j).STY.Value)); ring.Add(coordinate); } coordinates.Add(ring); } polygons.Add(coordinates); } geometry.Add("coordinates", polygons); return(geometry); }
private static Dictionary <string, object> GeometryToJson(Microsoft.SqlServer.Types.SqlGeometry geometry) { if ((string)geometry.MakeValid().STGeometryType() == "Point") { return(PointToJson(geometry)); } if ((string)geometry.MakeValid().STGeometryType() == "LineString") { return(LineStringToJson(geometry)); } if ((string)geometry.MakeValid().STGeometryType() == "Polygon") { return(PolygonToJson(geometry)); } if ((string)geometry.MakeValid().STGeometryType() == "MultiPolygon") { return(MultiPolygonToJson(geometry)); } string a = (string)geometry.MakeValid().STGeometryType(); return(null); }