public void EndGeometry() { GeometryType type = _CurrentType.Peek(); switch (type) { case GeometryType.Polygon: if (_Figures.Count > 0) { var g = new SmGeometries.Polygon( (SmGeometries.LinearRing)_Figures[0], _Figures.Skip(1).Cast <SmGeometries.LinearRing>().ToList <SmGeometries.LinearRing>() ); _Figures = new SmGeometries.Geometry[] { g }; } else { _Figures = new SmGeometries.Geometry[] { new SmGeometries.Polygon() } }; break; } _CurrentType.Pop(); if (_CurrentType.Count == 0) { _Figures[0].SpatialReference = CoordinateSystemUtils.Convert(_SpatialReference); } }
public void CreatingGeometryProviderWithWkbShouldContainSpecifiedGeometries() { Point[] points = new Point[] { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(0, 1), new Point(0, 0) }; Polygon p = new Polygon(new LinearRing(points)); byte[] wkb = p.AsBinary(); GeoProvider provider = new GeoProvider(wkb); Assert.AreEqual(1, provider.Geometries.Count); Assert.AreEqual(p, provider.Geometries[0]); }
protected override void OnRenderInternal(Map map, Polygon polygon, Graphics g) { // convert points var pts = /*LimitValues(*/polygon.TransformToImage(map)/*)*/; // clip if (UseClipping) pts = VectorRenderer.ClipPolygon(pts, map.Size.Width, map.Size.Height); // fill the polygon if (Fill != null) g.FillPolygon(Fill, pts); // outline the polygon if (Outline != null) g.DrawPolygon(Outline, pts); }
public void PolygonTest() { Polygon p = new Polygon(); Assert.IsTrue(p.IsEmpty()); Assert.AreEqual(0, p.NumInteriorRing); Assert.AreEqual(p.NumInteriorRing, p.InteriorRings.Count); Assert.IsFalse(p.Equals(null)); Assert.IsTrue(p.Equals(new Polygon())); Assert.IsNull(p.GetBoundingBox()); LinearRing ring = new LinearRing(); ring.Vertices.Add(new Point(10, 10)); ring.Vertices.Add(new Point(20, 10)); ring.Vertices.Add(new Point(20, 20)); Assert.IsFalse(ring.IsCCW()); ring.Vertices.Add(new Point(10, 20)); ring.Vertices.Add(ring.Vertices[0].Clone()); Assert.IsTrue(ring.IsPointWithin(new Point(15, 15))); Assert.AreNotSame(ring.Clone(), ring); p.ExteriorRing = ring; Assert.AreEqual(100, p.Area); LinearRing ring2 = new LinearRing(); ring2.Vertices.Add(new Point(11, 11)); ring2.Vertices.Add(new Point(19, 11)); ring2.Vertices.Add(new Point(19, 19)); ring2.Vertices.Add(new Point(11, 19)); ring2.Vertices.Add(ring2.Vertices[0].Clone()); p.InteriorRings.Add(ring2); Assert.AreEqual(100 + 64, p.Area); // Reverse() doesn't exist for Collections //ring2.Vertices.Reverse(); //Assert.AreEqual(100 - 64, p.Area); Assert.AreEqual(1, p.NumInteriorRing); Assert.AreEqual(new BoundingBox(10, 10, 20, 20), p.GetBoundingBox()); Polygon p2 = p.Clone(); Assert.AreEqual(p, p2); Assert.AreNotSame(p, p2); p2.InteriorRings.RemoveAt(0); Assert.AreNotEqual(p, p2); }
/// <summary> /// Writes a polygon. /// </summary> /// <param name="poly">The polygon to be written.</param> /// <param name="bWriter">Stream to write to.</param> /// <param name="byteorder">Byte order</param> private static void WritePolygon(Polygon poly, BinaryWriter bWriter, WkbByteOrder byteorder) { //Get the number of rings in this polygon. int numRings = poly.InteriorRings.Count + 1; //Write the number of rings to the stream (add one for the shell) WriteUInt32((uint)numRings, bWriter, byteorder); //Write the exterior of this polygon. WriteLineString((LineString)poly.ExteriorRing, bWriter, byteorder); //Loop on the number of rings - 1 because we already wrote the shell. foreach (LinearRing lr in poly.InteriorRings) //Write the (lineString)LinearRing. WriteLineString((LineString)lr, bWriter, byteorder); }
/// <summary> /// Transforms a <see cref="SharpMap.Geometries.Polygon"/>. /// </summary> /// <param name="p">Polygon to transform</param> /// <param name="transform">MathTransform</param> /// <returns>Transformed Polygon</returns> public static Polygon TransformPolygon(Polygon p, IMathTransform transform) { Polygon pOut = new Polygon(TransformLinearRing(p.ExteriorRing, transform)); //pOut.InteriorRings = new Collection<LinearRing>(p.InteriorRings.Count); //Pre-inialize array size for better performance pOut.InteriorRings = new Collection<LinearRing>(); for (int i = 0; i < p.InteriorRings.Count; i++) pOut.InteriorRings.Add(TransformLinearRing(p.InteriorRings[i], transform)); return pOut; }
private static Geometry FromShapeFilePolygon(EsriShapeBuffer shapeBuffer, out BoundingBox box) { box = null; if (shapeBuffer == null) return null; var hasZ = EsriShapeBuffer.HasZs(shapeBuffer.shapeType); var hasM = EsriShapeBuffer.HasMs(shapeBuffer.shapeType); using (var reader = new BinaryReader(new MemoryStream(shapeBuffer.shapeBuffer))) { var type = reader.ReadInt32(); if (!(type == 5 || type == 15 || type == 25)) throw new InvalidOperationException(); box = new BoundingBox(reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble()); var numParts = reader.ReadInt32(); var numPoints = reader.ReadInt32(); var allVertices = new List<Point>(numPoints); var parts = new int[numParts+1]; for (var i = 0; i < numParts; i++) parts[i] = reader.ReadInt32(); parts[numParts] = numPoints; var res = new MultiPolygon(); Polygon poly = null; for (var i = 0; i < numParts; i++) { var count = parts[i + 1] - parts[i]; var vertices = new List<Point>(count); for (var j = 0; j < count; j++) { var vertex = hasZ ? new Point3D(reader.ReadDouble(), reader.ReadDouble(), double.NaN) : new Point(reader.ReadDouble(), reader.ReadDouble()); vertices.Add(vertex); allVertices.Add(vertex); } var ring = new LinearRing(vertices); if (poly == null || !ring.IsCCW()) { poly = new Polygon(ring); res.Polygons.Add(poly); } else { poly.InteriorRings.Add(ring); } } if (hasZ) { var minZ = reader.ReadDouble(); var maxZ = reader.ReadDouble(); for (var i = 0; i < numPoints; i++) ((Point3D) allVertices[i]).Z = reader.ReadDouble(); } if (res.NumGeometries == 1) return res.Polygons[0]; return res; } }
/// <summary> /// Converts a Polygon to <Polygon Tagged Text> format, /// then Appends it to the writer. /// </summary> /// <param name="polygon">Th Polygon to process.</param> /// <param name="writer">The stream writer to Append to.</param> private static void AppendPolygonTaggedText(Polygon polygon, StringWriter writer) { writer.Write("POLYGON "); AppendPolygonText(polygon, writer); }
/// <summary> /// Creates a Polygon using the next token in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <Polygon Text>.</param> /// <returns>Returns a Polygon specified by the next token /// in the stream</returns> /// <remarks> /// ParseException is thown if the coordinates used to create the Polygon /// shell and holes do not form closed linestrings, or if an unexpected /// token is encountered. /// </remarks> private static Polygon ReadPolygonText(WktStreamTokenizer tokenizer) { Polygon pol = new Polygon(); string nextToken = GetNextEmptyOrOpener(tokenizer); if (nextToken == "EMPTY") return pol; pol.ExteriorRing = new LinearRing(GetCoordinates(tokenizer)); nextToken = GetNextCloserOrComma(tokenizer); while (nextToken == ",") { //Add holes pol.InteriorRings.Add(new LinearRing(GetCoordinates(tokenizer))); nextToken = GetNextCloserOrComma(tokenizer); } return pol; }
private void generatePolygons(Collection<Geometry> geometry, Random rndGen) { int numPolygons = rndGen.Next(10, 100); for (int polyIndex = 0; polyIndex < numPolygons; polyIndex++) { Polygon polygon = new Polygon(); Collection<GeoPoint> verticies = new Collection<GeoPoint>(); GeoPoint upperLeft = new GeoPoint(rndGen.NextDouble()*1000, rndGen.NextDouble()*1000); double sideLength = rndGen.NextDouble()*50; // Make a square verticies.Add(upperLeft); verticies.Add(new GeoPoint(upperLeft.X + sideLength, upperLeft.Y)); verticies.Add(new GeoPoint(upperLeft.X + sideLength, upperLeft.Y - sideLength)); verticies.Add(new GeoPoint(upperLeft.X, upperLeft.Y - sideLength)); polygon.ExteriorRing = new LinearRing(verticies); geometry.Add(polygon); } }
protected static GraphicsPath PolygonToGraphicsPath(Map map, Polygon polygon) { var gp = new GraphicsPath(FillMode.Alternate); gp.AddPolygon(polygon.TransformToImage(map)); return gp; }
/// <summary> /// Transforms a <see cref="SharpMap.Geometries.Polygon"/>. /// </summary> /// <param name="p">Polygon to transform</param> /// <param name="transform">MathTransform</param> /// <returns>Transformed Polygon</returns> public static Polygon TransformPolygon(Polygon p, IMathTransform transform) { Polygon pOut = new Polygon(TransformLinearRing(p.ExteriorRing, transform)); foreach (LinearRing ring in p.InteriorRings) pOut.InteriorRings.Add(TransformLinearRing(ring, transform)); return pOut; }
/// <summary> /// Creates a deep copy of the Polygon. /// </summary> /// <returns>A copy of the Polygon instance.</returns> public override Geometry Clone() { Polygon p = new Polygon(); p.ExteriorRing = ExteriorRing.Clone() as LinearRing; foreach (LinearRing ring in InteriorRings) { p.InteriorRings.Add(ring.Clone() as LinearRing); } return p; }
///// <summary> ///// Transforms the polygon to image coordinates, based on the map ///// </summary> ///// <param name="map">Map to base coordinates on</param> ///// <returns>Polygon in image coordinates</returns> //public RenderPoint[] TransformToView(SharpMap.Map map) //{ // int vertices = _ExteriorRing.Vertices.Count; // for (int i = 0; i < _InteriorRings.Count;i++) // vertices += _InteriorRings[i].Vertices.Count; // System.Drawing.PointF[] v = new System.Drawing.PointF[vertices]; // for (int i = 0; i < _ExteriorRing.Vertices.Count; i++) // v[i] = SharpMap.Utilities.Transform.WorldToMap(_ExteriorRing.Vertices[i], map); // int j = _ExteriorRing.Vertices.Count; // for (int k = 0; k < _InteriorRings.Count;k++) // { // for (int i = 0; i < _InteriorRings[k].Vertices.Count; i++) // v[j + i] = SharpMap.Utilities.Transform.WorldToMap(_InteriorRings[k].Vertices[i], map); // j += _InteriorRings[k].Vertices.Count; // } // return v; //} #region "Inherited methods from abstract class Geometry" /// <summary> /// Determines if this Polygon and the specified Polygon object has the same values /// </summary> /// <param name="p">Polygon to compare with</param> /// <returns></returns> public bool Equals(Polygon p) { if (ReferenceEquals(p, null)) { return false; } if (!p.ExteriorRing.Equals(ExteriorRing)) { return false; } if (p.InteriorRings.Count != InteriorRings.Count) { return false; } for (int i = 0; i < p.InteriorRings.Count; i++) { if (!p.InteriorRings[i].Equals(InteriorRings[i])) { return false; } } return true; }
/// <summary> /// Return a copy of this geometry /// </summary> /// <returns>Copy of Geometry</returns> public new Polygon Clone() { Polygon p = new Polygon(); p.ExteriorRing = (LinearRing) this._ExteriorRing.Clone(); for(int i=0;i<_InteriorRings.Count;i++) p.InteriorRings.Add(_InteriorRings[i].Clone() as LinearRing); return p; }
/// <summary> /// Determines if this Polygon and the specified Polygon object has the same values /// </summary> /// <param name="p">Polygon to compare with</param> /// <returns></returns> public bool Equals(Polygon p) { if (p == null) return false; if (!p.ExteriorRing.Equals(this.ExteriorRing)) return false; if (p.InteriorRings.Count != this.InteriorRings.Count) return false; for (int i = 0; i < p.InteriorRings.Count; i++) if (!p.InteriorRings[i].Equals(this.InteriorRings[i])) return false; return true; }
public void EndGeometry() { GeometryType type=_CurrentType.Peek(); switch (type) { case GeometryType.Polygon: if (_Figures.Count>0) { var g=new SmGeometries.Polygon( (SmGeometries.LinearRing)_Figures[0], _Figures.Skip(1).Cast<SmGeometries.LinearRing>().ToList<SmGeometries.LinearRing>() ); _Figures=new SmGeometries.Geometry[] { g }; } else _Figures=new SmGeometries.Geometry[] { new SmGeometries.Polygon() }; break; } _CurrentType.Pop(); if (_CurrentType.Count==0) _Figures[0].SpatialReference=CoordinateSystemUtils.Convert(_SpatialReference); }
/// <summary> /// Reads and parses the geometry with ID 'oid' from the ShapeFile /// </summary> /// <remarks><see cref="FilterDelegate">Filtering</see> is not applied to this method</remarks> /// <param name="oid">Object ID</param> /// <returns>geometry</returns> private Geometry ReadGeometry(uint oid) { brShapeFile.BaseStream.Seek(GetShapeIndex(oid) + 8, 0); //Skip record number and content length ShapeType type = (ShapeType) brShapeFile.ReadInt32(); //Shape type if (type == ShapeType.Null) return null; if (_ShapeType == ShapeType.Point || _ShapeType == ShapeType.PointM || _ShapeType == ShapeType.PointZ) { Point tempFeature = new Point(); return new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()); } else if (_ShapeType == ShapeType.Multipoint || _ShapeType == ShapeType.MultiPointM || _ShapeType == ShapeType.MultiPointZ) { brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box MultiPoint feature = new MultiPoint(); int nPoints = brShapeFile.ReadInt32(); // get the number of points if (nPoints == 0) return null; for (int i = 0; i < nPoints; i++) feature.Points.Add(new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); return feature; } else if (_ShapeType == ShapeType.PolyLine || _ShapeType == ShapeType.Polygon || _ShapeType == ShapeType.PolyLineM || _ShapeType == ShapeType.PolygonM || _ShapeType == ShapeType.PolyLineZ || _ShapeType == ShapeType.PolygonZ) { brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box int nParts = brShapeFile.ReadInt32(); // get number of parts (segments) if (nParts == 0) return null; int nPoints = brShapeFile.ReadInt32(); // get number of points int[] segments = new int[nParts + 1]; //Read in the segment indexes for (int b = 0; b < nParts; b++) segments[b] = brShapeFile.ReadInt32(); //add end point segments[nParts] = nPoints; if ((int) _ShapeType%10 == 3) { MultiLineString mline = new MultiLineString(); for (int LineID = 0; LineID < nParts; LineID++) { LineString line = new LineString(); for (int i = segments[LineID]; i < segments[LineID + 1]; i++) line.Vertices.Add(new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); mline.LineStrings.Add(line); } if (mline.LineStrings.Count == 1) return mline[0]; return mline; } else //(_ShapeType == ShapeType.Polygon etc...) { //First read all the rings List<LinearRing> rings = new List<LinearRing>(); for (int RingID = 0; RingID < nParts; RingID++) { LinearRing ring = new LinearRing(); for (int i = segments[RingID]; i < segments[RingID + 1]; i++) ring.Vertices.Add(new Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble())); rings.Add(ring); } bool[] IsCounterClockWise = new bool[rings.Count]; int PolygonCount = 0; for (int i = 0; i < rings.Count; i++) { IsCounterClockWise[i] = rings[i].IsCCW(); if (!IsCounterClockWise[i]) PolygonCount++; } if (PolygonCount == 1) //We only have one polygon { Polygon poly = new Polygon(); poly.ExteriorRing = rings[0]; if (rings.Count > 1) for (int i = 1; i < rings.Count; i++) poly.InteriorRings.Add(rings[i]); return poly; } else { MultiPolygon mpoly = new MultiPolygon(); Polygon poly = new Polygon(); poly.ExteriorRing = rings[0]; for (int i = 1; i < rings.Count; i++) { if (!IsCounterClockWise[i]) { mpoly.Polygons.Add(poly); poly = new Polygon(rings[i]); } else poly.InteriorRings.Add(rings[i]); } mpoly.Polygons.Add(poly); return mpoly; } } } else throw (new ApplicationException("Shapefile type " + _ShapeType.ToString() + " not supported")); }
protected abstract void OnRenderInternal(Map mpa, Polygon polygon, Graphics g);
/// <summary> /// Converts a Polygon to GeoJSON Polygon format, /// then Appends it to the writer. /// </summary> /// <param name="polygon">Th Polygon to process.</param> /// <param name="writer">The stream writer to Append to.</param> private static void AppendPolygonTaggedText(Polygon polygon, StringWriter writer) { //{ // "type": "Polygon", // "coordinates": [ // [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] // ] //} // with holes (inner rings) //{ // "type": "Polygon", // "coordinates": [ // [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ], // [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] // ] //} //{ //"type": "Polygon", //"coordinates": //[ [[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]], [[5, 5], [7, 5], [7, 7], [5, 7], [5, 5]]] ] //} writer.WriteLine("{"); writer.WriteLine("\"type\": \"Polygon\","); writer.WriteLine("\"coordinates\": "); //writer.Write("[ "); AppendPolygonText(polygon, writer); //writer.WriteLine(" ]"); //writer.WriteLine("]"); writer.WriteLine("}"); }
/// <summary> /// Returns the box filter string needed in SQL query /// </summary> /// <param name="bbox"></param> /// <returns></returns> private string GetBoxFilterStr(BoundingBox bbox) { //geography::STGeomFromText('LINESTRING(47.656 -122.360, 47.656 -122.343)', 4326); LinearRing lr = new LinearRing(); lr.Vertices.Add(new Point(bbox.Left, bbox.Bottom)); lr.Vertices.Add(new Point(bbox.Right, bbox.Bottom)); lr.Vertices.Add(new Point(bbox.Right, bbox.Top)); lr.Vertices.Add(new Point(bbox.Left, bbox.Top)); lr.Vertices.Add(new Point(bbox.Left, bbox.Bottom)); Polygon p = new Polygon(lr); string bboxText = Converters.WellKnownText.GeometryToWKT.Write(p); // ""; //string whereClause = GeometryColumn + ".STIntersects(geometry::STGeomFromText('" + bboxText + "', " + SRID + ")" + MakeValidString + ") = 1"; string whereClause = String.Format("{0}{1}.STIntersects({4}::STGeomFromText('{2}', {3})) = 1", GeometryColumn, MakeValidString, bboxText, SRID, _spatialObject); return whereClause; // strBbox; }
/// <summary> /// Transforms a <see cref="SharpMap.Geometries.Polygon"/>. /// </summary> /// <param name="p">Polygon to transform</param> /// <param name="from">Source Projection</param> /// <param name="to">Target Projection</param> /// <returns>Transformed Polygon</returns> public static Polygon TransformPolygon(Polygon p, ProjectionInfo from, ProjectionInfo to) { var pOut = new Polygon(TransformLinearRing(p.ExteriorRing, from, to)) {InteriorRings = new Collection<LinearRing>()}; //pOut.InteriorRings = new Collection<LinearRing>(p.InteriorRings.Count); //Pre-inialize array size for better performance for (var i = 0; i < p.InteriorRings.Count; i++) pOut.InteriorRings.Add(TransformLinearRing(p.InteriorRings[i], from, to)); return pOut; }
public static void DrawPolygon(Graphics g, Polygon pol, Brush brush, Pen pen, bool clip, Map map) { if (pol.ExteriorRing == null) return; if (pol.ExteriorRing.Vertices.Count > 2) { //Use a graphics path instead of DrawPolygon. DrawPolygon has a problem with several interior holes GraphicsPath gp = new GraphicsPath(); //Add the exterior polygon if (!clip) gp.AddPolygon(/*LimitValues(*/pol.ExteriorRing.TransformToImage(map)/*, ExtremeValueLimit)*/); else DrawPolygonClipped(gp, pol.ExteriorRing.TransformToImage(map), map.Size.Width, map.Size.Height); //Add the interior polygons (holes) for (int i = 0; i < pol.InteriorRings.Count; i++) if (!clip) gp.AddPolygon(/*LimitValues(*/pol.InteriorRings[i].TransformToImage(map)/*, ExtremeValueLimit)*/); else DrawPolygonClipped(gp, pol.InteriorRings[i].TransformToImage(map), map.Size.Width, map.Size.Height); // Only render inside of polygon if the brush isn't null or isn't transparent if (brush != null && brush != Brushes.Transparent) g.FillPath(brush, gp); // Create an outline if a pen style is available if (pen != null) g.DrawPath(pen, gp); } }
/// <summary> /// This method parses quickly without paying attention to /// context validation, polygon boundaries and multi-geometries. /// This accelerates the geometry parsing process, /// but in scarce cases can lead to errors. /// </summary> /// <param name="geometryType">The geometry type (Point, LineString, Polygon, MultiPoint, MultiCurve, /// MultiLineString (deprecated), MultiSurface, MultiPolygon (deprecated)</param> /// <returns>The created geometries</returns> internal virtual Collection<Geometry> createQuickGeometries(string geometryType) { // Ignore multi-geometries if (geometryType.Equals("MultiPointPropertyType")) geometryType = "PointPropertyType"; else if (geometryType.Equals("MultiLineStringPropertyType")) geometryType = "LineStringPropertyType"; else if (geometryType.Equals("MultiPolygonPropertyType")) geometryType = "PolygonPropertyType"; else if (geometryType.Equals("MultiCurvePropertyType")) geometryType = "CurvePropertyType"; else if (geometryType.Equals("MultiSurfacePropertyType")) geometryType = "SurfacePropertyType"; string serviceException = null; while (_XmlReader.Read()) { if (_CoordinatesNode.Matches(_XmlReader)) { try { switch (geometryType) { case "PointPropertyType": _Geoms.Add(ParseCoordinates(_XmlReader.ReadSubtree())[0]); break; case "LineStringPropertyType": case "CurvePropertyType": _Geoms.Add(new LineString(ParseCoordinates(_XmlReader.ReadSubtree()))); break; case "PolygonPropertyType": case "SurfacePropertyType": Polygon polygon = new Polygon(); polygon.ExteriorRing = new LinearRing(ParseCoordinates(_XmlReader.ReadSubtree())); _Geoms.Add(polygon); break; default: break; } } catch (Exception ex) { Trace.TraceError("An exception occured while parsing a " + geometryType + " geometry: " + ex.Message); throw ex; } continue; } if (_ServiceExceptionNode.Matches(_XmlReader)) { serviceException = _XmlReader.ReadInnerXml(); Trace.TraceError("A service exception occured: " + serviceException); throw new Exception("A service exception occured: " + serviceException); } } return _Geoms; }
private static Polygon CreateWKBPolygon(BinaryReader reader, WkbByteOrder byteOrder) { // Get the Number of rings in this Polygon. int numRings = (int) ReadUInt32(reader, byteOrder); Debug.Assert(numRings >= 1, "Number of rings in polygon must be 1 or more."); Polygon shell = new Polygon(CreateWKBLinearRing(reader, byteOrder)); // Create a new array of linearrings for the interior rings. for (int i = 0; i < (numRings - 1); i++) shell.InteriorRings.Add(CreateWKBLinearRing(reader, byteOrder)); // Create and return the Poylgon. return shell; }
/// <summary> /// This method produces instances of type <see cref="SharpMap.Geometries.Polygon"/>. /// </summary> /// <returns>The created geometries</returns> internal override Collection<Geometry> createGeometries() { Polygon polygon = null; XmlReader outerBoundaryReader = null; XmlReader innerBoundariesReader = null; IPathNode polygonNode = new PathNode(_GMLNS, "Polygon", (NameTable) _XmlReader.NameTable); IPathNode outerBoundaryNode = new PathNode(_GMLNS, "outerBoundaryIs", (NameTable) _XmlReader.NameTable); IPathNode exteriorNode = new PathNode(_GMLNS, "exterior", (NameTable) _XmlReader.NameTable); IPathNode outerBoundaryNodeAlt = new AlternativePathNodesCollection(outerBoundaryNode, exteriorNode); IPathNode innerBoundaryNode = new PathNode(_GMLNS, "innerBoundaryIs", (NameTable) _XmlReader.NameTable); IPathNode interiorNode = new PathNode(_GMLNS, "interior", (NameTable) _XmlReader.NameTable); IPathNode innerBoundaryNodeAlt = new AlternativePathNodesCollection(innerBoundaryNode, interiorNode); IPathNode linearRingNode = new PathNode(_GMLNS, "LinearRing", (NameTable) _XmlReader.NameTable); string[] labelValue = new string[1]; bool geomFound = false; try { // Reading the entire feature's node makes it possible to collect label values that may appear before or after the geometry property while ((_FeatureReader = GetSubReaderOf(_XmlReader, null, _FeatureNode)) != null) { while ((_GeomReader = GetSubReaderOf(_FeatureReader, labelValue, polygonNode)) != null) { polygon = new Polygon(); if ( (outerBoundaryReader = GetSubReaderOf(_GeomReader, null, outerBoundaryNodeAlt, linearRingNode, _CoordinatesNode)) != null) polygon.ExteriorRing = new LinearRing(ParseCoordinates(outerBoundaryReader)); while ( (innerBoundariesReader = GetSubReaderOf(_GeomReader, null, innerBoundaryNodeAlt, linearRingNode, _CoordinatesNode)) != null) polygon.InteriorRings.Add(new LinearRing(ParseCoordinates(innerBoundariesReader))); _Geoms.Add(polygon); geomFound = true; } if (geomFound) AddLabel(labelValue[0], _Geoms[_Geoms.Count - 1]); geomFound = false; } } catch (Exception ex) { Trace.TraceError("An exception occured while parsing a polygon geometry: " + ex.Message); throw ex; } return _Geoms; }
/// <summary> /// Converts a Polygon to <Polygon Text> format, then /// Appends it to the writer. /// </summary> /// <param name="polygon">The Polygon to process.</param> /// <param name="writer"></param> private static void AppendPolygonText(Polygon polygon, StringWriter writer) { if (polygon == null || polygon.IsEmpty()) writer.Write("EMPTY"); else { writer.Write("("); AppendLineStringText(polygon.ExteriorRing, writer); for (int i = 0; i < polygon.InteriorRings.Count; i++) { writer.Write(", "); AppendLineStringText(polygon.InteriorRings[i], writer); } writer.Write(")"); } }
private static Geometry ToSharpMapMultiPolygon(EsriShapeBuffer shapeBuffer) { if (shapeBuffer == null) return null; var multiPartShapeBuffer = shapeBuffer as EsriMultiPartShapeBuffer; if (multiPartShapeBuffer == null) { BoundingBox box; return FromShapeFilePolygon(shapeBuffer, out box); } var hasZ = EsriShapeBuffer.HasZs(shapeBuffer.shapeType); var res = new MultiPolygon(); Polygon poly = null; var offset = 0; for (var i = 0; i < multiPartShapeBuffer.NumParts; i++) { var vertices = new List<Point>(multiPartShapeBuffer.Parts[i]); for (var j = 0; j < multiPartShapeBuffer.Parts[i]; j++) { var index = offset + j; var point = multiPartShapeBuffer.Points[index]; vertices.Add(hasZ ? new Point3D(point.x, point.y, multiPartShapeBuffer.Zs[index]) : new Point(point.x, point.y)); } var ring = new LinearRing(vertices); if (poly == null || !ring.IsCCW()) { poly = new Polygon(ring); res.Polygons.Add(poly); } else { poly.InteriorRings.Add(ring); } offset += multiPartShapeBuffer.Parts[i]; } if (res.NumGeometries == 1) return res.Polygons[0]; return res; }