struct Point
{
Point()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
Point(double _x, double _y, double _z)
{
x = _x;
y = _y;
z = _z;
}
double x;
double y;
double z;
};
void readShapeFile()
{
// 获取shp文件名
std::string shp_fn = "line.shp";
SHPHandle hSHP;
DBFHandle hDBF;
int nShapeType, nEntities;
double adfMinBound[4], adfMaxBound[4];
hSHP = SHPOpen(shp_fn.c_str(), "r");
if (hSHP == NULL)
{
return;
}
char szDBF[MAX_PATH + 1];
strcpy_s(szDBF, shp_fn.c_str());
szDBF[strlen(szDBF) - 3] = '\0';
strcat_s(szDBF, "dbf");
hDBF = DBFOpen(szDBF, "rb");
if (!hDBF)
{
SHPClose(hSHP);
return;
}
SHPGetInfo(hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound);
SHPObject *psElem;
double *padfX, *padfY, *padfZ;
int nField;
nField = DBFGetFieldCount(hDBF);
for (int i = 0; i < nEntities; i++)
{
std::vector<Point> shapePoints;
psElem = SHPReadObject(hSHP, i);
padfX = new double[psElem->nVertices];
padfY = new double[psElem->nVertices];
padfZ = new double[psElem->nVertices];
for (int j = 0; j < psElem->nVertices; j++)
{
padfX[j] = psElem->padfX[j];
padfY[j] = psElem->padfY[j];
padfZ[j] = psElem->padfZ[j];
Point point(padfX[j], padfY[j], padfZ[j]);
shapePoints.push_back(point);
}
for (int j = 0; j < nField; j++)
{
DBFFieldType eType;
int nWidth, nDecimals;
char szTitle[20];
eType = DBFGetFieldInfo(hDBF, j, szTitle, &nWidth, &nDecimals);
switch (eType)
{
case FTString:
{
std::string value = DBFReadStringAttribute(hDBF, i, j);
}
break;
case FTInteger:
{
int value = DBFReadIntegerAttribute(hDBF, i, j);
}
break;
case FTDouble:
{
double value = DBFReadDoubleAttribute(hDBF, i, j);
}
break;
default:
break;
}
}
delete[] padfX;
delete[] padfY;
delete[] padfZ;
SHPDestroyObject(psElem);
}
// 关闭文件
SHPClose(hSHP);
DBFClose(hDBF);
}
void writeShapeFile()
{
// 获取shp文件名
std::string shp_fn = "line";
// 创建对应的.shp文件和.dbf文件
SHPHandle hShp = SHPCreate(string(shp_fn + ".shp").c_str(), SHPT_ARCZ);
DBFHandle hDbf = DBFCreate(string(shp_fn + ".dbf").c_str());
// 创建dbf文件表
DBFAddField(hDbf, "ID", FTInteger, 10, 0);
DBFAddField(hDbf, "Name", FTString, 10, 0);
DBFAddField(hDbf, "Length", FTDouble, 32, 0);
// dbf的记录数
int record_idx = DBFGetRecordCount(hDbf);
std::vector<Point> line_shp;
line_shp.push_back(Point(1.0, 2.0, 3.0));
line_shp.push_back(Point(1.5, 2.6, 3.9));
int nVertices = line_shp.size();
if (0 == nVertices)
{
return;
}
double* padfX = new double[nVertices];
double* padfY = new double[nVertices];
double* padfZ = new double[nVertices];
for (int i = 0; i < nVertices; ++i)
{
padfX[i] = line_shp.at(i).x;
padfY[i] = line_shp.at(i).y;
padfZ[i] = line_shp.at(i).z;
}
SHPObject* shpObject = SHPCreateObject(SHPT_ARCZ, -1, 0, NULL, NULL, nVertices, padfX, padfY, padfZ, NULL);
SHPWriteObject(hShp, -1, shpObject);
SHPDestroyObject(shpObject);
delete[] padfX;
delete[] padfY;
delete[] padfZ;
// 字段索引
int field_idx = 0;
DBFWriteIntegerAttribute(hDbf, record_idx, field_idx++, 1001);
DBFWriteStringAttribute(hDbf, record_idx, field_idx++, "polyline");
DBFWriteDoubleAttribute(hDbf, record_idx, field_idx++, 10.15);
DBFClose(hDbf);
SHPClose(hShp);
}
|