[Shapefile C Library]读取shp图形(.net Wapper)

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

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

以下是.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         }

 

你可能感兴趣的:(library)