[Shapefile C Library]读写shp图形(C++&.net Wapper)

ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用。ORG据说也是使用的ShapeLib实现的shp文件的读写。

官网:http://shapelib.maptools.org/

1. C++读取shpfile文件代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int main()
{
    //读取shp
    const char * pszShapeFile = "data\\LineSegments2.shp";
    SHPHandle hShp= SHPOpen(pszShapeFile, "r");
    int nShapeType, nVertices;
    int nEntities = 0;
    double* minB = new double[4];
    double* maxB = new double[4];
    SHPGetInfo(hShp, &nEntities, &nShapeType, minB, maxB);
    printf("ShapeType:%d\n", nShapeType);
    printf("Entities:%d\n", nEntities);
    for (int i = 0; i < nEntities;i++)
    {
        int iShape = i;
        SHPObject *obj = SHPReadObject(hShp, iShape);
        printf("--------------Feature:%d------------\n",iShape);
        int parts = obj->nParts;
        int verts=obj->nVertices;
        printf("nParts:%d\n", parts);
        printf("nVertices:%d\n", verts);
        for (size_t i = 0; i < verts; i++)
        {
            double x=obj->padfX[i];
            double y = obj->padfY[i];
            printf("%f,%f;", x,y);
        }
        printf("\n");
    }
    SHPClose(hShp);
    system("pause");
 
}

 输出结果:

 


 2. 以下是.net读取Shp文件中图形的代码:

复制代码
 1  private void button1_Click(object sender, EventArgs e)
 2         {
 3             OpenFileDialog dlg = new OpenFileDialog();
 4             dlg.Filter = "(*.shp)|*.shp";
 5             if (dlg.ShowDialog() == DialogResult.OK)
 6             {
 7                 string fileName = dlg.FileName;
 8                 txtFilePath.Text = fileName;
 9                 ReadSHP(fileName);
10             }
11         }
12 
13         private void ReadSHP(string FILENAME)
14         {
15             IntPtr hShp;
16             hShp = ShapeLib.SHPOpen(FILENAME, "rb+");
17 
18             // get shape info and verify shapes were created correctly
19             double[] minB = new double[4];
20             double[] maxB = new double[4];
21             int nEntities = 0;
22             ShapeLib.ShapeType shapeType = 0;
23             ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
24             listBox1.Items.Add(string.Format("Number Entries: {0}", nEntities));
25             listBox1.Items.Add(string.Format("ShapeType: {0}", shapeType));
26             listBox1.Items.Add(string.Format("Min XY: {0}, {1}", minB[0], minB[1]));
27             listBox1.Items.Add(string.Format("Max XY: {0}, {1}", maxB[0], maxB[1]));
28 
29             // test SHPReadObject on the first shape
30             for (int i = 0; i < nEntities; i++)
31             {
32                 int iShape = i;
33                 listBox1.Items.Add(string.Format("Shape({0}): ", iShape));
34                 IntPtr pshpObj = ShapeLib.SHPReadObject(hShp, iShape);
35 
36                 // Get the SHPObject associated with our IntPtr pshpObj
37                 // We create a new SHPObject in managed code, then use Marshal.PtrToStructure
38                 // to copy the unmanaged memory pointed to by pshpObj into our managed copy.
39                 ShapeLib.SHPObject shpObj = new ShapeLib.SHPObject();
40                 Marshal.PtrToStructure(pshpObj, shpObj);
41 
42                 listBox1.Items.Add(string.Format("Min XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMin, shpObj.dfYMin));
43                 listBox1.Items.Add(string.Format("Max XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMax, shpObj.dfYMax));
44                 listBox1.Items.Add(string.Format("Points of shape({0}): ({1})", iShape, shpObj.nVertices));
45                 int parts = shpObj.nParts;
46                 listBox1.Items.Add(string.Format("Parts of shape({0}): ({1})", iShape, parts));
47                 if (parts>0)
48                 {
49                     int[] partStart = new int[parts];
50                     Marshal.Copy(shpObj.paPartStart, partStart, 0, parts);
51                     for (int j = 0; j < partStart.Length; j++)
52                     {
53                         listBox1.Items.Add(string.Format("FirstPart of shape({0}): ({1})", iShape, partStart[j]));
54                     }              
55                     int[] partType = new int[parts];
56                     Marshal.Copy(shpObj.paPartType, partType, 0, parts);
57                     for (int j = 0; j < partType.Length; j++)
58                     {                  
59                         listBox1.Items.Add(string.Format("FirstPartType of shape({0}): ({1})", iShape, (MapTools.ShapeLib.PartType)partType[j]));
60                     }
61                 }
62                 
63                 ShapeLib.SHPDestroyObject(pshpObj);
64             }         
65             ShapeLib.SHPClose(hShp);
66             Console.WriteLine("\nPress any key to continue...");
67             Console.ReadLine();
68         }
复制代码

 

3.新建shp并保存属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//简化后保存
    const char* saveFileName = "data\\simplyRoom.shp";
    int nShpTpyeSave = SHPT_POLYGON;
    SHPHandle outShp = SHPCreate(saveFileName, nShpTpyeSave);
    DBFHandle dbf_h = DBFCreate(saveFileName);
    int fieldIdx=DBFAddField(dbf_h, "Shape", FTInteger, 2, 0);
    SHPObject *psShape;
 
    for (int ir=0;ir<rooms_.size();ir++)
    {
        printf("--------------Room:%d------------\n",ir);
        std::vector<Coordinate> coords=rooms_[ir].simplyCoords_;
        double *xCoords = new double[coords.size()];
        double *yCoords = new double[coords.size()];
        for (int ip=0;ip<coords.size();ip++)
        {
            double x=coords[ip].x;
            double y=coords[ip].y;
            xCoords[ip] = x;
            yCoords[ip] = y;
            printf("%f,%f;\n", x,y);
        }
        printf("\n");
        psShape = SHPCreateObject(nShpTpyeSave, -1, 0, NULL, NULL, coords.size(), xCoords, yCoords, NULL, NULL);
 
        std::cout << std::endl;
        int ishape=SHPWriteObject(outShp, -1, psShape);
        SHPDestroyObject(psShape);
        DBFWriteIntegerAttribute(dbf_h, ishape, 0, ishape);
    }
    SHPClose(outShp);
    DBFClose(dbf_h);

 

posted @   太一吾鱼水  阅读(2465)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
历史上的今天:
2014-06-24 [QT]抄—影像显示实验
点击右上角即可分享
微信分享提示