📄 geometry.cs
字号:
///GeoCon, free tool to create gml & svg from gis files.
///Copyright(C) 2005 Amri Rosyada
///Distributed under GNU-LGPL, see a copy of the license in root directory
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Drawing;
namespace gml
{
#region abstract geometries
/// <summary>
/// All geometry elements are derived from this abstract supertype;
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
[XmlIncludeAttribute(typeof(AbstractGeometryCollectionBaseType))]
[XmlIncludeAttribute(typeof(GeometryCollectionType))]
public abstract class AbstractGeometryType
{
/// <summary>
/// Geometry ID, geometry element may have an identifying attribute
/// </summary>
[XmlAttribute(DataType="ID")]
public string gid;
/// <summary>
/// Spatial Reference System.
/// </summary>
[XmlAttribute(DataType="anyURI")]
public string srsName;
[XmlIgnore]
public double PointRadius=0.01; //for drawing ellipse
public AbstractGeometryType(){ }
public AbstractGeometryType(string srsName)
{
this.srsName=srsName;
}
public abstract svg.SvgElement createSvgObject();
public abstract void DrawGraphics(ref Graphics g, ref Pen p, ref Brush b);
}
/// <summary>
/// This abstract base type for geometry collections just makes the srsName attribute mandatory.
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
[XmlIncludeAttribute(typeof(GeometryCollectionType))]
//use AbstractGeometryType as base class, so we can use this class instead of object for better reference;
public abstract class AbstractGeometryCollectionBaseType : AbstractGeometryType
{
public AbstractGeometryCollectionBaseType()
{
this.srsName=" "; //srsName is required in this class
}
public AbstractGeometryCollectionBaseType(string srsName) : base(srsName)
{
this.srsName = srsName;
}
}
#endregion
#region associative geom
/// <summary>
/// Group of attributes that can be attached to any element, thus allowing it to act as a pointer
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
//temporary : must write correct xlink objects
public class AssociationAttributeGroup //: xlink.simpleLink
{
//using attribute group as base class will strip the namespace out, ???
[XmlAttribute(DataType="string",Namespace="http://www.w3.org/1999/xlink")]
public const string type ="simple";
[XmlAttribute(DataType="anyURI",Namespace="http://www.w3.org/1999/xlink")]
public string href;
[XmlAttribute(DataType="anyURI",Namespace="http://www.w3.org/1999/xlink")]
public string role;
[XmlAttribute(DataType="anyURI",Namespace="http://www.w3.org/1999/xlink")]
public string arcrole;
[XmlAttribute(DataType="anyURI",Namespace="http://www.w3.org/1999/xlink")]
public string title;
private string _showString;
[XmlAttribute("show",Namespace="http://www.w3.org/1999/xlink")]
public string show
{
get {return _showString;}
set
{
string[] showVals = new string[5]{"new","replace","embed","other","none"};
if(Array.IndexOf(showVals,value,0,5)<0) throw new ArgumentException(value.ToString()+" value not acceptable for 'show' attribute.");
_showString=value;
}
}
private string _actuateString;
[XmlAttribute("actuate",Namespace="http://www.w3.org/1999/xlink")]
public string actuate
{
get {return _actuateString;}
set
{
string[] actuateVals = new string[4]{"onLoad","onRequest","other","none"};
if(Array.IndexOf(actuateVals,value,0,4)<0) throw new ArgumentException(value.ToString()+" value not acceptable for 'actuate' attribute.");
_actuateString=value;
}
}
/// <summary>
/// allows an element that carries link attributes to indicate that the element is declared in a remote schema rather than by the schema that constrains the current document instance.
/// </summary>
[XmlAttribute(DataType="anyURI")]
public string remoteSchema;
public AssociationAttributeGroup(){ }
}
/// <summary>
/// Encapsulates any primitive geometry element.
/// Array of this class instances ("geometryMember") serves as items in gml.GeometryCollectionType.geometryMember
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
[XmlRoot("geometryMember", Namespace="http://www.opengis.net/gml", IsNullable=false)]
//Alternatively, it can function as a simple link that points to a remote geometry.
public class GeometryAssociationType : gml.AssociationAttributeGroup
{
[XmlElement("Point", typeof(gml.PointType))]
[XmlElement("LineString", typeof(gml.LineStringType))]
[XmlElement("LinearRing", typeof(gml.LinearRingType))]
[XmlElement("Polygon", typeof(gml.PolygonType))]
[XmlElement("MultiPoint", typeof(gml.MultiPointType))]
[XmlElement("MultiLineString", typeof(gml.MultiLineStringType))]
[XmlElement("MultiPolygon", typeof(gml.MultiPolygonType))]
public gml.AbstractGeometryType Item; //minOccurs=0 , maxOccurs=1
//this item must also accept any type derived from abstractGeometry
//and use atributeOverrides on this member to handle derived objects
/// <summary>
/// Constructs new GeometryAssociationType
/// </summary>
public GeometryAssociationType(){}
/// <summary>
/// Constructs new GeometryAssociationType with certain geometry as item
/// </summary>
/// <param name="geom">Geometry to associate with</param>
public GeometryAssociationType(gml.AbstractGeometryType geom){
this.Item=geom;
}
/// <summary>
/// Creates SVG object associated with the Item field of this class.
/// </summary>
/// <returns></returns>
public svg.SvgElement createSvgElement()
{
return Item.createSvgObject();
}
/// <summary>
/// Draws shapes associated with the Item field of this object.
/// </summary>
/// <param name="g">Graphics object to draw to</param>
/// <param name="p">Pen used in drawing</param>
/// <param name="b">Brush used in drawing</param>
public void DrawGraphics(ref Graphics g, ref Pen p, ref Brush b)
{
Item.DrawGraphics(ref g, ref p, ref b);
}
}
#endregion
#region geometry utils
/// <summary>
/// Enumeration of coordinate type choice
/// </summary>
[Serializable]
[XmlType(IncludeInSchema = false)]
public enum CoordChoices
{
/// <summary>
/// indicating gml.CoordType
/// </summary>
coord,
/// <summary>
/// indicating gml.CoordinatesType
/// </summary>
coordinates
}
/// <summary>
/// simple wrapper for coordType and coordinatesType
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
public class CoordWrapper
{
public CoordWrapper(){}
}
/// <summary>
/// Represents a coordinate tuple in one, two, or three dimensions.
/// Currently 3D not supported.
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
[XmlRoot("coord", Namespace="http://www.opengis.net/gml", IsNullable=false)]
public class CoordType : CoordWrapper
{
[XmlElement(IsNullable=false)]
public System.Decimal X; //minOccurs=1
public System.Decimal Y; //minOccurs=0
public System.Decimal Z; //minOccurs=0
public CoordType() { }
public CoordType(decimal x)
{
}
public CoordType(decimal x,decimal y)
{
this.X=x;
this.Y=y;
}
public CoordType(decimal x,decimal y,decimal z)
{
this.X=x;
this.Y=y;
this.Z=z;
}
/// <summary>
/// Gets tuple string using specified coordinate separator.
/// </summary>
/// <param name="coordseparator">string value separating the coordinate values</param>
/// <returns>tuple string</returns>
public string getStringTuple(string coordseparator)
{
return this.X.ToString() + coordseparator + this.Y.ToString();
}
/// <summary>
/// Gets tuple string using specified coordinate separator and number format info
/// </summary>
/// <param name="coordseparator">string value separating the coordinate values</param>
/// <param name="nfi">Number format info to format the string</param>
/// <returns>tuple string</returns>
public string getStringTuple(string coordseparator,System.Globalization.NumberFormatInfo nfi)
{
return System.Convert.ToString(this.X,nfi) + coordseparator + System.Convert.ToString(this.Y,nfi);
}
/// <summary>
/// Gets point string
/// </summary>
/// <returns>string that can be used as polyline data in SVG</returns>
public string getPointString()
{
string str = this.X.ToString() + "," + this.Y.ToString();
if(this.Z.Equals(null)) str+= "," + this.Z.ToString();
return str;
}
/// <summary>
/// Gets new PointF from this CoordType
/// </summary>
/// <returns>PointF object to be used for drawing graphics</returns>
public PointF getPointF()
{
return new PointF((float)this.X,(float)this.Y);
}
}
#endregion
#region primitive geometries
/// <summary>
/// Geometry type for a single coordinate tuple.
/// </summary>
[Serializable]
[XmlType(Namespace="http://www.opengis.net/gml")]
[XmlRoot("Point", Namespace="http://www.opengis.net/gml", IsNullable=false)]
public class PointType : AbstractGeometryType
{
[XmlElement("coord", typeof(CoordType))]
[XmlElement("coordinates", typeof(CoordinatesType))]
public gml.CoordWrapper Item;
/// <summary>
/// Constructs new PointType with empty coordinate tuple
/// </summary>
public PointType() { }
/// <summary>
/// Constructs new PointType using specified CoordType
/// </summary>
/// <param name="coord"></param>
///
public PointType(CoordType coord)
{
Item = coord;
}
/// <summary>
/// Constructs new PointType using specified CoordinatesType
/// </summary>
/// <param name="coordinates"></param>
public PointType(CoordinatesType coordinates)
{
Item = coordinates;
}
/// <summary>
/// Creates a svg circle using the coordinate tuple as center
/// </summary>
/// <returns>a svg circle object</returns>
public override svg.SvgElement createSvgObject()
{
svg.circle c;
if(Item.GetType()==typeof(gml.CoordinatesType))
{
gml.CoordinatesType ct = (gml.CoordinatesType)this.Item;
c = new svg.circle(ct.doubleArray[0] ,ct.doubleArray[1], this.PointRadius);
}
else
{
c = new svg.circle((double)((gml.CoordType)this.Item).X,(double)((gml.CoordType)this.Item).Y,this.PointRadius);
}
return c;
}
/// <summary>
/// Draws ellipse using this point coordinate tuple as center
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -