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

📄 program.cs

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

namespace NetTest2005
{
    /// <summary>
    /// A demonstration and test program for the use of
    /// ShapeLib, a .NET wrapper class for the Shapefile C Library V1.2.10
    /// </summary>
    class Class1
    {
        const string FILENAME = "test";
        const int NVERTICES = 11;
        const int NFIELDS = 5;
        const int NSHAPES = 3;

        [STAThread]
        static void Main(string[] args)
        {
            CreateSHP();
            CreateDBF();
            Console.WriteLine("\nPress any key to exit...");
            Console.ReadLine();
        }

        private static void CreateSHP()
        {

            ShapeLib.ShapeType shpType = ShapeLib.ShapeType.Polygon;
            Console.WriteLine("*****Creating {0}*****\n", ShapeLib.SHPTypeName(shpType));
            IntPtr hShp;
            double[] xCoord = new double[NVERTICES];
            double[] yCoord = new double[NVERTICES];

            // create an arbitrary geometric figure
            // note that our boundary is defined clockwise, according
            // to the ESRI shapefile rule that the neighborhood to the right 
            // of an observer walking along the ring in vertex order is
            // the neighborhood inside the polygon.  In contrast, holes are 
            // defined in counterclockwise order.
            for (int i = 0; i < NVERTICES; i++)
            {
                xCoord[i] = Math.Cos(Math.PI / 5 * (double)i);
                yCoord[i] = -Math.Sin(Math.PI / 5 * (double)i);
            }

            // ensure start and end point are equal (some roundoff err occurs in Sin(2PI))
            xCoord[NVERTICES - 1] = xCoord[0];
            yCoord[NVERTICES - 1] = yCoord[0];

            // create a new shapefile
            hShp = ShapeLib.SHPCreate(FILENAME, shpType);
            if (hShp.Equals(IntPtr.Zero))
                return;

            // add three shapes
            IntPtr pshpObj = ShapeLib.SHPCreateSimpleObject(shpType, NVERTICES,
                xCoord, yCoord, new double[NVERTICES]);

            int iRet = ShapeLib.SHPWriteObject(hShp, -1, pshpObj);
            ShapeLib.SHPDestroyObject(pshpObj);

            // next shape will be 2 units to the right
            for (int i = 0; i < NVERTICES; i++)
                xCoord[i] += 2;

            pshpObj = ShapeLib.SHPCreateSimpleObject(shpType, NVERTICES,
                xCoord, yCoord, new double[NVERTICES]);
            iRet = ShapeLib.SHPWriteObject(hShp, -1, pshpObj);
            ShapeLib.SHPDestroyObject(pshpObj);

            // next will be 2 units up
            for (int i = 0; i < NVERTICES; i++)
                yCoord[i] += 2;

            // this polygon will have a triangular hole in its center,
            // for the purpose of testing SHPCreateObject
            double[] aX = new double[NVERTICES + 4];
            double[] aY = new double[NVERTICES + 4];
            double[] aZ = new double[NVERTICES + 4];
            double[] aM = new double[NVERTICES + 4];

            Array.Copy(xCoord, 0, aX, 0, NVERTICES);
            Array.Copy(yCoord, 0, aY, 0, NVERTICES);
            // center of polygon is at (2,2) - center our triangular hole there.
            // Outer ring is defined clockwise; inner ring (hole) is defined ccwise
            aX[NVERTICES] = 2.5;
            aY[NVERTICES] = 2;
            aX[NVERTICES + 1] = 2;
            aY[NVERTICES + 1] = 2.5;
            aX[NVERTICES + 2] = 1.5;
            aY[NVERTICES + 2] = 1.5;
            aX[NVERTICES + 3] = aX[NVERTICES];
            aY[NVERTICES + 3] = aY[NVERTICES];
            int[] apartStart = new int[2] { 0, NVERTICES };
            ShapeLib.PartType[] apartType =
                new ShapeLib.PartType[2] { ShapeLib.PartType.Ring, ShapeLib.PartType.Ring };

            pshpObj = ShapeLib.SHPCreateObject(shpType, -1, 2, apartStart, apartType, NVERTICES + 4, aX, aY, aZ, aM);

            iRet = ShapeLib.SHPWriteObject(hShp, -1, pshpObj);
            ShapeLib.SHPDestroyObject(pshpObj);

            // we want to test SHPOpen, so we will close hShp then reopen it
            ShapeLib.SHPClose(hShp);

            hShp = ShapeLib.SHPOpen(FILENAME, "rb+");

            // get shape info and verify shapes were created correctly
            double[] minB = new double[4];
            double[] maxB = new double[4];
            int nEntities = 0;
            ShapeLib.ShapeType shapeType = 0;
            ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
            Console.WriteLine("Number Entries: {0}", nEntities);
            Console.WriteLine("ShapeType: {0}", shapeType);
            Console.WriteLine("Min XY: {0}, {1}", minB[0], minB[1]);
            Console.WriteLine("Max XY: {0}, {1}", maxB[0], maxB[1]);

            // test SHPReadObject on the first shape
            int iShape = 0;
            pshpObj = ShapeLib.SHPReadObject(hShp, iShape);

            // Get the SHPObject associated with our IntPtr pshpObj
            // We create a new SHPObject in managed code, then use Marshal.PtrToStructure
            // to copy the unmanaged memory pointed to by pshpObj into our managed copy.
            ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject();
            Marshal.PtrToStructure(pshpObj, shpObj);

            Console.WriteLine("Min XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMin, shpObj.dfYMin);
            Console.WriteLine("Max XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMax, shpObj.dfYMax);

            // Recover the vertices for this shape. Use Marshal.Copy to copy the memory pointed 
            // to by shpObj.padfX and shpObj.padfX (each an IntPtr) to a actual array.
            int iVertex = 2;
            Marshal.Copy(shpObj.padfX, xCoord, 0, NVERTICES);
            Marshal.Copy(shpObj.padfY, yCoord, 0, NVERTICES);
            Console.WriteLine("Vertex XY: ({0}, {1})", xCoord[iVertex], yCoord[iVertex]);

            // move some vertices
            for (int i = 1; i < NVERTICES; i += 2)
            {
                xCoord[i] = 0.4 * Math.Cos(Math.PI / 5 * (double)i);
                yCoord[i] = -0.4 * Math.Sin(Math.PI / 5 * (double)i);
            }

            // scale the figure
            for (int i = 0; i < NVERTICES; i++)
            {
                xCoord[i] *= 2;
                yCoord[i] *= 2;
            }

            // Set padfX and padfY to point to our updated array
            Marshal.Copy(xCoord, 0, shpObj.padfX, NVERTICES);
            Marshal.Copy(yCoord, 0, shpObj.padfY, NVERTICES);
            // Copy shpObj to unmanaged memory pointed to by pshpObj 
            // (shpObj is only a COPY at this point.  Use StructureToPtr to 
            // copy it back for use by shapelib.dll)
            Marshal.StructureToPtr(shpObj, pshpObj, true);

            // The .NET GC should clean up shpObj, but we'll force the cleanup anyway...
            shpObj = null;

            // recalculate extents
            ShapeLib.SHPComputeExtents(pshpObj);

            // write this updated shape to the shapefile, then destroy the SHPObject
            iRet = ShapeLib.SHPWriteObject(hShp, iShape, pshpObj);
            ShapeLib.SHPDestroyObject(pshpObj);

            // verify change was successful
            ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
            Console.WriteLine("New Min XY: {0}, {1}", minB[0], minB[1]);
            Console.WriteLine("New Max XY: {0}, {1}", maxB[0], maxB[1]);

⌨️ 快捷键说明

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