/// <summary> /// Writes a Geometry to the given binary wirter. /// </summary> /// <param name="geometry">The geometry to write.</param> /// <param name="file">The file stream to write to.</param> /// <param name="geometryFactory">The geometry factory to use.</param> public override void Write(IGeometry geometry, BinaryWriter file, IGeometryFactory geometryFactory) { // This check seems to be not useful and slow the operations... // if (!geometry.IsValid) // Trace.WriteLine("Invalid polygon being written."); IGeometryCollection multi; if (geometry is IGeometryCollection) multi = (IGeometryCollection) geometry; else { GeometryFactory gf = new GeometryFactory(geometry.PrecisionModel); multi = gf.CreateMultiPolygon(new IPolygon[] { (IPolygon) geometry, } ); } file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryType), ShapeType, "d"))); IEnvelope box = multi.EnvelopeInternal; IEnvelope bounds = GetEnvelopeExternal(geometryFactory.PrecisionModel, box); file.Write(bounds.MinX); file.Write(bounds.MinY); file.Write(bounds.MaxX); file.Write(bounds.MaxY); int numParts = GetNumParts(multi); int numPoints = multi.NumPoints; file.Write(numParts); file.Write(numPoints); // write the offsets to the points int offset = 0; for (int part = 0; part < multi.NumGeometries; part++) { // offset to the shell points IPolygon polygon = (IPolygon) multi.Geometries[part]; file.Write(offset); offset = offset + polygon.ExteriorRing.NumPoints; // offstes to the holes foreach (ILinearRing ring in polygon.InteriorRings) { file.Write(offset); offset = offset + ring.NumPoints; } } // write the points for (int part = 0; part < multi.NumGeometries; part++) { IPolygon poly = (IPolygon) multi.Geometries[part]; ICoordinate[] points = poly.ExteriorRing.Coordinates; WriteCoords(new CoordinateList(points), file, geometryFactory); foreach(ILinearRing ring in poly.InteriorRings) { ICoordinate[] points2 = ring.Coordinates; WriteCoords(new CoordinateList(points2), file, geometryFactory); } } }
internal static MultiPolygon ToNTSMultiPolygon(Geometries.MultiPolygon multiPolygon, GeometryFactory factory) { Polygon[] polygons = new Polygon[multiPolygon.Polygons.Count]; int index = 0; foreach (Geometries.Polygon polygon in multiPolygon.Polygons) polygons[index++] = ToNTSPolygon(polygon, factory); return factory.CreateMultiPolygon(polygons) as MultiPolygon; }