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

📄 maptools.cs

📁 包含详细例子的c#创建shape文件的开源码
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace MapTools
{
	/// <summary>
	/// .NET Framework wrapper for Shapefile C Library V1.2.10
	/// </summary>
	/// <remarks>
	/// Shapefile C Library is (c) 1998 Frank Warmerdam.  .NET wrapper provided by David Gancarz.  
	/// Please send error reports or other suggestions regarding this wrapper class to:
	/// dgancarz@cfl.rr.com or david.gancarz@cityoforlando.net
	/// </remarks>
 
	public class ShapeLib
	{

		/// <summary>
		/// Shape type enumeration
		/// </summary>
		public enum ShapeType
		{
			/// <summary>Shape with no geometric data</summary>
			NullShape = 0,			
			/// <summary>2D point</summary>
			Point = 1,		
			/// <summary>2D polyline</summary>
			PolyLine = 3,			
			/// <summary>2D polygon</summary>
			Polygon = 5,		
			/// <summary>Set of 2D points</summary>
			MultiPoint = 8,	
			/// <summary>3D point</summary>
			PointZ = 11,		
			/// <summary>3D polyline</summary>
			PolyLineZ = 13,		
			/// <summary>3D polygon</summary>
			PolygonZ = 15,	
			/// <summary>Set of 3D points</summary>
			MultiPointZ = 18,	
			/// <summary>3D point with measure</summary>
			PointM = 21,		
			/// <summary>3D polyline with measure</summary>
			PolyLineM = 23,		
			/// <summary>3D polygon with measure</summary>
			PolygonM = 25,	
			/// <summary>Set of 3d points with measures</summary>
			MultiPointM = 28,	
			/// <summary>Collection of surface patches</summary>
			MultiPatch = 31
		}

		/// <summary>
		/// Part type enumeration - everything but ShapeType.MultiPatch just uses PartType.Ring.
		/// </summary>
		public enum PartType
		{
			/// <summary>
			/// Linked strip of triangles, where every vertex (after the first two) completes a new triangle.
			/// A new triangle is always formed by connecting the new vertex with its two immediate predecessors.
			/// </summary>
			TriangleStrip = 0,	
			/// <summary>
			/// A linked fan of triangles, where every vertex (after the first two) completes a new triangle.
			/// A new triangle is always formed by connecting the new vertex with its immediate predecessor 
			/// and the first vertex of the part.
			/// </summary>
			TriangleFan = 1,	
			/// <summary>The outer ring of a polygon</summary>
			OuterRing = 2,	
			/// <summary>The first ring of a polygon</summary>
			InnerRing = 3,	
			/// <summary>The outer ring of a polygon of an unspecified type</summary>
			FirstRing = 4,	
			/// <summary>A ring of a polygon of an unspecified type</summary>
			Ring = 5
		}
		
		/// <summary>
		/// SHPObject - represents on shape (without attributes) read from the .shp file.
		/// </summary>
		[StructLayout(LayoutKind.Sequential)]
		public class SHPObject 
		{	
			///<summary>Shape type as a ShapeType enum</summary>	
			public ShapeType shpType;	
			///<summary>Shape number (-1 is unknown/unassigned)</summary>	
			public int nShapeId;	
			///<summary>Number of parts (0 implies single part with no info)</summary>	
			public int nParts;	
			///<summary>Pointer to int array of part start offsets, of size nParts</summary>	
			public IntPtr paPartStart;
			///<summary>Pointer to PartType array (PartType.Ring if not ShapeType.MultiPatch) of size nParts</summary>	
			public IntPtr paPartType;	
			///<summary>Number of vertices</summary>	
			public int nVertices;	
			///<summary>Pointer to double array containing X coordinates</summary>	
			public IntPtr padfX;	
			///<summary>Pointer to double array containing Y coordinates</summary>		
			public IntPtr padfY;	
			///<summary>Pointer to double array containing Z coordinates (all zero if not provided)</summary>	
			public IntPtr padfZ;	
			///<summary>Pointer to double array containing Measure coordinates(all zero if not provided)</summary>	
			public IntPtr padfM;	
			///<summary>Bounding rectangle's min X</summary>	
			public double dfXMin;	
			///<summary>Bounding rectangle's min Y</summary>	
			public double dfYMin;	
			///<summary>Bounding rectangle's min Z</summary>	
			public double dfZMin;	
			///<summary>Bounding rectangle's min M</summary>	
			public double dfMMin;	
			///<summary>Bounding rectangle's max X</summary>	
			public double dfXMax;	
			///<summary>Bounding rectangle's max Y</summary>	
			public double dfYMax;	
			///<summary>Bounding rectangle's max Z</summary>	
			public double dfZMax;	
			///<summary>Bounding rectangle's max M</summary>	
			public double dfMMax;
		}

		/// <summary>
		/// The SHPOpen() function should be used to establish access to the two files for 
		/// accessing vertices (.shp and .shx). Note that both files have to be in the indicated 
		/// directory, and must have the expected extensions in lower case. The returned SHPHandle 
		/// is passed to other access functions, and SHPClose() should be invoked to recover 
		/// resources, and flush changes to disk when complete.
		/// </summary>
		/// <param name="szShapeFile">The name of the layer to access.  This can be the name of either 
		/// the .shp or the .shx file or can just be the path plus the basename of the pair.</param>
		/// <param name="szAccess">The fopen() style access string. At this time only "rb" (read-only binary) 
		/// and "rb+" (read/write binary) should be used.</param>
		/// <returns>IntPtr</returns>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern IntPtr SHPOpen(string szShapeFile, string szAccess);

		/// <summary>
		/// The SHPCreate() function will create a new .shp and .shx file of the desired type.
		/// </summary>
		/// <param name="szShapeFile">The name of the layer to access. This can be the name of either 
		/// the .shp or the .shx file or can just be the path plus the basename of the pair.</param>
		/// <param name="shpType">The type of shapes to be stored in the newly created file. 
		/// It may be either ShapeType.Point, ShapeType.PolyLine, ShapeType.Polygon or ShapeType.MultiPoint.</param>
		/// <returns>IntPtr</returns>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern IntPtr SHPCreate(string szShapeFile, ShapeType shpType);

		/// <summary>
		/// The SHPGetInfo() function retrieves various information about shapefile as a whole. 
		/// The bounds are read from the file header, and may be inaccurate if the file was 
		/// improperly generated.
		/// </summary>
		/// <param name="hSHP">The handle previously returned by SHPOpen() or SHPCreate()</param>
		/// <param name="pnEntities">A pointer to an integer into which the number of 
		/// entities/structures should be placed. May be NULL.</param>
		/// <param name="pshpType">A pointer to an integer into which the ShapeType of this file 
		/// should be placed. Shapefiles may contain either ShapeType.Point, ShapeType.PolyLine, ShapeType.Polygon or 
		/// ShapeType.MultiPoint entities. This may be NULL.</param>
		/// <param name="adfMinBound">The X, Y, Z and M minimum values will be placed into this 
		/// four entry array. This may be NULL. </param>
		/// <param name="adfMaxBound">The X, Y, Z and M maximum values will be placed into this 
		/// four entry array. This may be NULL.</param>
		/// <returns>void</returns>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern void SHPGetInfo(IntPtr hSHP, ref int pnEntities, 
			ref ShapeType pshpType,  double[] adfMinBound, double[] adfMaxBound);

		/// <summary>
		/// The SHPReadObject() call is used to read a single structure, or entity from the shapefile. 
		/// See the definition of the SHPObject structure for detailed information on fields of a SHPObject. 
		/// </summary>
		/// <param name="hSHP">The handle previously returned by SHPOpen() or SHPCreate().</param>
		/// <param name="iShape">The entity number of the shape to read. Entity numbers are between 0 
		/// and nEntities-1 (as returned by SHPGetInfo()).</param>
		/// <returns>IntPtr to a SHPObject.  Use System.Runtime.InteropServices.Marshal.PtrToStructure(...)
		/// to create an SHPObject copy in managed memory</returns>
		/// <remarks>
		/// IntPtr's returned from SHPReadObject() should be deallocated with SHPDestroyShape(). 
		/// SHPReadObject() will return IntPtr.Zero if an illegal iShape value is requested. 
		/// Note that the bounds placed into the SHPObject are those read from the file, and may not be correct. 
		/// For points the bounds are generated from the single point since bounds aren't normally provided 
		/// for point types. Generally the shapes returned will be of the type of the file as a whole. 
		/// However, any file may also contain type ShapeType.NullShape shapes which will have no geometry. 
		/// Generally speaking applications should skip rather than preserve them, as they usually 
		/// represented interactively deleted shapes.
		/// </remarks>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern IntPtr SHPReadObject(IntPtr hSHP, int iShape);

		/// <summary>
		/// The SHPWriteObject() call is used to write a single structure, or entity to the shapefile. 
		/// See the definition of the SHPObject structure for detailed information on fields of a SHPObject.
		/// </summary>
		/// <param name="hSHP">The handle previously returned by SHPOpen("r+") or SHPCreate().</param>
		/// <param name="iShape">The entity number of the shape to write. 
		/// A value of -1 should be used for new shapes. </param>
		/// <param name="psObject">The shape to write to the file. This should have been created with SHPCreateObject(), 
		/// or SHPCreateSimpleObject().</param>
		/// <returns>int</returns>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern int SHPWriteObject(IntPtr hSHP, int iShape, IntPtr psObject);

		/// <summary>
		/// This function should be used to deallocate the resources associated with a SHPObject 
		/// when it is no longer needed, including those created with SHPCreateSimpleObject(), 
		/// SHPCreateObject() and returned from SHPReadObject().
		/// </summary>
		/// <param name="psObject">IntPtr of the SHPObject to deallocate memory.</param>
		/// <returns>void</returns>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern void SHPDestroyObject(IntPtr psObject);

		/// <summary>
		/// This function will recompute the extents of this shape, replacing the existing values 
		/// of the dfXMin, dfYMin, dfZMin, dfMMin, dfXMax, dfYMax, dfZMax, and dfMMax values based 
		/// on the current set of vertices for the shape. This function is automatically called by 
		/// SHPCreateObject() but if the vertices of an existing object are altered it should be 
		/// called again to fix up the extents.
		/// </summary>
		/// <param name="psObject">An existing shape object to be updated in place.</param>
		/// <returns>void</returns>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern void SHPComputeExtents(IntPtr psObject);

		/// <summary>
		/// The SHPCreateObject() function allows for the creation of objects (shapes). 
		/// This is normally used so that the SHPObject can be passed to SHPWriteObject() 
		/// to write it to the file.
		/// </summary>
		/// <param name="shpType">The ShapeType of the object to be created, such as ShapeType.Point, or ShapeType.Polygon.</param>
		/// <param name="nShapeId">The shapeid to be recorded with this shape.</param>
		/// <param name="nParts">The number of parts for this object. If this is zero for PolyLine, 
		/// or Polygon type objects, a single zero valued part will be created internally.</param>
		/// <param name="panPartStart">The list of zero based start vertices for the rings 
		/// (parts) in this object. The first should always be zero. This may be NULL if nParts is 0.</param>
		/// <param name="paPartType">The type of each of the parts. This is only meaningful for MultiPatch files. 
		/// For all other cases this may be NULL, and will be assumed to be PartType.Ring.</param>
		/// <param name="nVertices">The number of vertices being passed in padfX, padfY, and padfZ. </param>
		/// <param name="adfX">An array of nVertices X coordinates of the vertices for this object.</param>
		/// <param name="adfY">An array of nVertices Y coordinates of the vertices for this object.</param>
		/// <param name="adfZ">An array of nVertices Z coordinates of the vertices for this object. 
		/// This may be NULL in which case they are all assumed to be zero.</param>
		/// <param name="adfM">An array of nVertices M (measure values) of the vertices for this object. 
		/// This may be NULL in which case they are all assumed to be zero.</param>
		/// <returns>IntPtr to a SHPObject</returns>
		/// <remarks>
		/// The SHPDestroyObject() function should be used to free 
		/// resources associated with an object allocated with SHPCreateObject(). This function 
		/// computes a bounding box for the SHPObject from the given vertices.
		/// </remarks>
		[DllImport("shapelib.dll", CharSet=CharSet.Ansi)]
		public static extern IntPtr SHPCreateObject(ShapeType shpType, int nShapeId,
			int nParts, int[] panPartStart, PartType[] paPartType,
			int nVertices, double[] adfX, double[] adfY,
			double[] adfZ, double[] adfM );

		/// <summary>
		/// The SHPCreateSimpleObject() function allows for the convenient creation of simple objects. 
		/// This is normally used so that the SHPObject can be passed to SHPWriteObject() to write it 
		/// to the file. The simple object creation API assumes an M (measure) value of zero for each vertex. 
		/// For complex objects (such as polygons) it is assumed that there is only one part, and that it 
		/// is of the default type (PartType.Ring). Use the SHPCreateObject() function for more sophisticated 
		/// objects. 
		/// </summary>
		/// <param name="shpType">The ShapeType of the object to be created, such as ShapeType.Point, or ShapeType.Polygon.</param>
		/// <param name="nVertices">The number of vertices being passed in padfX, padfY, and padfZ.</param>

⌨️ 快捷键说明

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