您是第: 个访问我网站的人!
站内搜索:
浏览信息 您当前的位置:首页 >> 浏览信息
shapelib读写shape文件
【字体: 】   【时间:2021-01-24】  【作者:不学无数】  【关 闭】  【打 印
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);
}


关闭页面】【页面顶部
本站所有资源未经许可不得转载复制,违者必究。
© Copyright 2020-2025 www.yuzhilin.com.cn All Right Reaserved. 不学无数之家 拥有所有版权
地址:中国·光谷 E-mail:zhilinyu@163.com  鄂ICP备20001861号 技术支持:不学无数之家