⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miwrapper.cs

📁 mitab,读取MapInfo的地图文件
💻 CS
📖 第 1 页 / 共 2 页
字号:
// $Id: MiWrapper.cs,v 1.2 2005/03/24 17:02:06 dmorissette Exp $
//

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;
using System.Text;

namespace EBop.MapObjects.MapInfo {

	/*
	 * These classes wrap the mitab c api functions to produce a hierarchy of classes giving readonly 
	 * access to the feature points.
	 * 
	 * Requires mitab.dll (www.maptools.org)
	 * See http://mitab.maptools.org/
	 * 
	 * Graham Sims,
	 * Environment Bay of Plenty, Whakatane, New Zealand
	 * http://www.envbop.govt.nz		
	 */

	/// <summary>
	/// This is a helper class for our standard enumerator based on EnumImpl. Implementataions 
	/// (Features, Parts, Fields and Vertices) are array like structures that can provide
	/// an object at a given index between 0 and Count  - 1.
	/// </summary>
	interface IObjectProvider {
		int Count {
			get;
		}
		object GetObj(int idx);
	}

	/// <summary>
	/// Implementation of an enumeration scheme over an index (array like) structure. 
	/// This class provides an enumerator that will work over any IObjectProvider implementation
	/// (Features, Parts, Fields and Vertices).
	/// </summary>
	/// <remarks>
	/// Calls to the GetEnumerator method of Fields, Parts and Vertices will return
	/// an instance of this class. Calls to GetEnumerator of Features will return a descendant
	/// of this class (due to the fact that features don't necessarily have a sequential
	/// index).
	/// </remarks>
	public class IndexedEnum : IEnumerator {
		public readonly int Count;
		protected int eIdx = -1;
		private readonly IObjectProvider objProvider;

		internal IndexedEnum(IObjectProvider objProvider) {
			this.objProvider = objProvider;
		}

		public virtual void Reset() {
			eIdx = -1;
		}

		public object Current {
			get {
				return objProvider.GetObj(eIdx);
			}
		}

		public virtual bool MoveNext() {
			return (++eIdx < objProvider.Count);
		}
	}

	/// <summary>
	/// Partial implementation of IEnumerable over an indexed (aray like) structure.
	/// </summary>
	/// <remarks>
	/// Fields, Vertices, Parts and Features all descend from this class. It serves to
	/// provide the common functionality required to generate their enumerators.
	/// </remarks>
	public abstract class EnumImpl : IEnumerable, IObjectProvider {
		public readonly int count;

		protected EnumImpl(int count) {
			this.count = count;
		}

		public int Count {
			get {
				return count;
			}
		}

		public virtual IEnumerator GetEnumerator() {
			return new IndexedEnum(this);
		}

		public abstract object GetObj(int idx);
	}


	/// <summary>
	/// Represents a readonly view of field in a layer.
	/// </summary>
	/// <remarks>
	/// A field instance does not relate explicity to a single feature instance. Rather
	/// it represents all the features in the layer. To find the value of a field for a particular
	/// feature one of the GetValueAs methods should be called, passing the feature in.
	/// </remarks>
	public class Field {
		/// <summary>
		/// The field name
		/// </summary>
		public readonly string Name;
		/// <summary>
		/// The field type
		/// </summary>
		public FieldType Type;
		/// <summary>
		/// The index of the field within the layers set of fields.
		/// </summary>
		public readonly int Index;
		/// <summary>
		/// The field width
		/// </summary>
		public readonly int Width;
		/// <summary>
		/// The field precision
		/// </summary>
		public readonly short Precision;
		/// <summary>
		/// The layer this field belongs to.
		/// </summary>
		public readonly Layer Layer;

		protected internal Field(Layer layer, int i) {
			this.Layer = layer;
			this.Index = i;
			this.Name = MiApi.mitab_c_get_field_name(layer.Handle, i);
			this.Type = MiApi.mitab_c_get_field_type(layer.Handle, i);
			this.Precision = (short) MiApi.mitab_c_get_field_precision(layer.Handle, i);
			this.Width = MiApi.mitab_c_get_field_width(layer.Handle, i);
		}

		/// <summary>
		/// Returns a string representation of this fields value for the given feature.
		/// </summary>
		/// <param name="feature">The feature to find the fields value for.</param>
		/// <returns>A string representation of this fields value for the given feature</returns>
		public string GetValueAsString(Feature feature) {
			return MiApi.mitab_c_get_field_as_string(feature.Handle, this.Index);
		}

		/// <summary>
		/// Returns a double representation of this fields value for the given feature.
		/// </summary>
		/// <param name="feature">The feature to find the fields value for.</param>
		/// <returns>A double representation of this fields value for the given feature</returns>
		public double GetValueAsDouble(Feature feature) {
			return MiApi.mitab_c_get_field_as_double(feature.Handle, this.Index);
		}

		public override string ToString() {
			return this.Name+", "+this.Type;
		}

	}

	/// <summary>
	/// Contains the set of fields belonging to a layer.
	/// </summary>
	/// <remarks>
	/// This class descends EnumImpl, meaning the fields in the
	/// set can be iterated using foreach.
	/// It also has an index property allowing any field between 0 and Fields.Count-1
	/// to be accessed directly with Fields[idx]
	/// </remarks>
	public class Fields : EnumImpl {
		private Field[] fields;

		protected internal Fields(Layer layer):base(MiApi.mitab_c_get_field_count(layer.Handle)) {
			fields = new Field[Count];
			for (int i=0; i<Count; i++)
				fields[i] = CreateField(layer, i);
		}

		/// <summary>
		/// Override this to support descendants of the Field class.
		/// </summary>
		/// <returns>A Field, with the given index, belonging to the given Layer</returns>
		protected internal virtual Field CreateField(Layer layer, int index) {
			return new Field(layer, index);
		}

		public virtual Field this[int index] {
			get {
				return index < Count ? fields[index] : null;
			}
		}

		public override object GetObj(int idx) {
			return this[idx];
		}

		public override string ToString() {
			StringBuilder str = new StringBuilder();
			str.Append("Columns:\n");
			foreach (Field field in this)
				str.Append(field.ToString()+"\t");
			return str.ToString();
		}
	}


	/// <summary>
	/// Represents a single vertex with an X,Y point in geometric space.
	/// </summary>
	/// <remarks>
	/// This is the base building block of a feature
	/// </remarks>
	public class Vertex {
		public readonly double X,Y;
		protected internal Vertex(double x, double y) {
			this.X = x;
			this.Y = y;
		}
		
		public override string ToString() {
			return this.X+", "+this.Y;
		}
	}

	/// <summary>
	/// Contains the set of Vertices belonging to a single Part.
	/// </summary>
	/// <remarks>
	/// This class descends EnumImpl, meaning the vertices in the
	/// set can be iterated using foreach.
	/// It also has an index property allowing any vertice between 0 and Vertices.Count-1
	/// to be accessed directly with Vertices[idx]
	/// </remarks>
	public class Vertices : EnumImpl {
		public readonly Part Part;

		protected internal Vertices(Part part):
			base(MiApi.mitab_c_get_vertex_count(part.Feature.Handle, part.Index)) {
			this.Part = part;
		}

		/// <summary>
		/// Override this to support descendants of the Vertex class.
		/// </summary>
		/// <returns>A vertex with the given X,Y coordinates</returns>
		protected internal virtual Vertex CreateVertex(double x, double y) {
			return new Vertex(x ,y);
		}

		public virtual Vertex this[int index] {
			get {
				return index < Count ? CreateVertex(
					MiApi.mitab_c_get_vertex_x(Part.Feature.Handle, Part.Index, index),
					MiApi.mitab_c_get_vertex_y(Part.Feature.Handle, Part.Index, index)) : null;
			}
		}

		public override object GetObj(int idx) {
			return this[idx];
		}
		
		public override string ToString() {
			StringBuilder str = new StringBuilder();
			foreach (Vertex v in this)
				str.Append(v+"\t");
			return str.ToString();
		}
	}

	/// <summary>
	/// Represents a Part.
	/// </summary>
	/// <remarks>A feature will contain one or more parts.</remarks>
	public class Part {
		public readonly Feature Feature;
		public readonly Vertices Vertices;
		public readonly int Index;

		protected internal Part(Feature feature, int partIdx) {
			this.Index = partIdx;
			this.Feature = feature;
			this.Vertices = CreateVertices(this);
		}

		/// <summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -