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

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

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

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

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"); }

输出结果:

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


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

  private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "(*.shp)|*.shp";
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName = dlg.FileName;
txtFilePath.Text = fileName;
ReadSHP(fileName);
}
} private void ReadSHP(string FILENAME)
{
IntPtr hShp;
hShp = ShapeLib.SHPOpen(FILENAME, "rb+"); // get shape info and verify shapes were created correctly
double[] minB = new double[];
double[] maxB = new double[];
int nEntities = ;
ShapeLib.ShapeType shapeType = ;
ShapeLib.SHPGetInfo(hShp, ref nEntities, ref shapeType, minB, maxB);
listBox1.Items.Add(string.Format("Number Entries: {0}", nEntities));
listBox1.Items.Add(string.Format("ShapeType: {0}", shapeType));
listBox1.Items.Add(string.Format("Min XY: {0}, {1}", minB[], minB[]));
listBox1.Items.Add(string.Format("Max XY: {0}, {1}", maxB[], maxB[])); // test SHPReadObject on the first shape
for (int i = ; i < nEntities; i++)
{
int iShape = i;
listBox1.Items.Add(string.Format("Shape({0}): ", iShape));
IntPtr 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); listBox1.Items.Add(string.Format("Min XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMin, shpObj.dfYMin));
listBox1.Items.Add(string.Format("Max XY of shape({0}): ({1}, {2})", iShape, shpObj.dfXMax, shpObj.dfYMax));
listBox1.Items.Add(string.Format("Points of shape({0}): ({1})", iShape, shpObj.nVertices));
int parts = shpObj.nParts;
listBox1.Items.Add(string.Format("Parts of shape({0}): ({1})", iShape, parts));
if (parts>)
{
int[] partStart = new int[parts];
Marshal.Copy(shpObj.paPartStart, partStart, , parts);
for (int j = ; j < partStart.Length; j++)
{
listBox1.Items.Add(string.Format("FirstPart of shape({0}): ({1})", iShape, partStart[j]));
}
int[] partType = new int[parts];
Marshal.Copy(shpObj.paPartType, partType, , parts);
for (int j = ; j < partType.Length; j++)
{
listBox1.Items.Add(string.Format("FirstPartType of shape({0}): ({1})", iShape, (MapTools.ShapeLib.PartType)partType[j]));
}
} ShapeLib.SHPDestroyObject(pshpObj);
}
ShapeLib.SHPClose(hShp);
Console.WriteLine("\nPress any key to continue...");
Console.ReadLine();
}

3.新建shp并保存属性

//简化后保存
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);
上一篇:C#读取shp文件并获取图形保存到sde要素类中(不使用ESRI的类库,纯c#实现)


下一篇:GDAL读取Shp问题解决:Unable to open EPSG support file gcs.csv