📄 polygon.cs
字号:
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpMap.Geometries
{
/// <summary>
/// A Polygon is a planar Surface, defined by 1 exterior boundary and 0 or more interior boundaries. Each
/// interior boundary defines a hole in the Polygon.
/// </summary>
/// <remarks>
/// Vertices of rings defining holes in polygons are in the opposite direction of the exterior ring.
/// </remarks>
[Serializable]
public class Polygon : Surface
{
private LinearRing _ExteriorRing;
private List<LinearRing> _InteriorRings;
/// <summary>
/// Instatiates a polygon based on one extorier ring and a collection of interior rings.
/// </summary>
/// <param name="exteriorRing">Exterior ring</param>
/// <param name="interiorRings">Interior rings</param>
public Polygon(LinearRing exteriorRing, List<LinearRing> interiorRings)
{
_ExteriorRing = exteriorRing;
_InteriorRings = interiorRings;
}
/// <summary>
/// Instatiates a polygon based on one extorier ring.
/// </summary>
/// <param name="exteriorRing">Exterior ring</param>
public Polygon(LinearRing exteriorRing) : this(exteriorRing, new List<LinearRing>()) { }
/// <summary>
/// Instatiates a polygon
/// </summary>
public Polygon() : this(new LinearRing(), new List<LinearRing>()) { }
/// <summary>
/// Gets or sets the exterior ring of this Polygon
/// </summary>
/// <remarks>This method is supplied as part of the OpenGIS Simple Features Specification</remarks>
public LinearRing ExteriorRing
{
get { return _ExteriorRing; }
set { _ExteriorRing = value; }
}
/// <summary>
/// Gets or sets the interior rings of this Polygon
/// </summary>
public List<LinearRing> InteriorRings
{
get { return _InteriorRings; }
set { _InteriorRings = value; }
}
/// <summary>
/// Returns the Nth interior ring for this Polygon as a LineString
/// </summary>
/// <remarks>This method is supplied as part of the OpenGIS Simple Features Specification</remarks>
/// <param name="N"></param>
/// <returns></returns>
public LinearRing InteriorRing(int N)
{
return _InteriorRings[N];
}
/// <summary>
/// Returns the number of interior rings in this Polygon
/// </summary>
/// <remarks>This method is supplied as part of the OpenGIS Simple Features Specification</remarks>
/// <returns></returns>
public int NumInteriorRing
{
get { return _InteriorRings.Count; }
}
/// <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 System.Drawing.PointF[] TransformToImage(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 (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;
}
/// <summary>
/// Serves as a hash function for a particular type. <see cref="GetHashCode"/> is suitable for use
/// in hashing algorithms and data structures like a hash table.
/// </summary>
/// <returns>A hash code for the current <see cref="GetHashCode"/>.</returns>
public override int GetHashCode()
{
int hash = ExteriorRing.GetHashCode(); ;
for (int i = 0; i < InteriorRings.Count; i++)
hash = hash ^ InteriorRings[i].GetHashCode();
return hash;
}
/// <summary>
/// If true, then this Geometry represents the empty point set,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -