shapelib 读取DBF

1.配置shapelib环境:拷贝shapelib.lib 和shapefil.h到工程目录下面。并且配置lib

操作DBF文件:

BOOL CSecurityMatainWnd::ImportFromDBF(const CString sFileName)
{
    DBFHandle pdbf = DBFOpen((char *)C22<MAX_PATH>(sFileName), "r+b");
    if (pdbf)
    {
        //> 获取DBF行数和列数目
        int iColCount = DBFGetFieldCount(pdbf);
        int iRowCount = DBFGetRecordCount(pdbf);//行数仅仅是内容的行数,不包含行头
        DBFFieldType filedType[64];
        char szfiledName[64] = "";
        int nWidth = 0;
        int nDecimal = 0;

        //保存没一列的数据类型
        for (int iCol = 0; iCol < iColCount; ++iCol)
        {
            filedType[iCol] = DBFGetFieldInfo(pdbf, iCol, szfiledName, &nWidth, &nDecimal);
        }

        for (int iRow = 0; iRow < iRowCount; iRow++)
        {
            for (int iCol = 0; iCol < iColCount;++iCol)
            {
                ///> 通过列的数据类型读取数据
                switch (filedType[iCol])
                {
                case FTString:
                {
                    const char *val = DBFReadStringAttribute(pdbf, iRow, iCol);
                    int a = 0;
                }
                break;
                case FTInteger:
                {
                    int ival = DBFReadIntegerAttribute(pdbf, iRow, iCol);
                    int a = 0;
                }
                break;
                case FTDouble:
                {
                    double dval = DBFReadDoubleAttribute(pdbf, iRow, iCol);
                    int a = 0;
                }
                break;
                case FTLogical:
                {
                    const char *val = DBFReadLogicalAttribute(pdbf, iRow, iCol);
                    int a = 0;
                }
                break;
                default:
                    break;
                }
            }
        }

        //> 使用完关闭DBF文件
        DBFClose(pdbf);
        return TRUE;
    }
    return FALSE;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
读取 .dbf 文件可以使用 DBF 文件的开源库,例如 Dbasepp 或者 libdbf++。读取 .sbn、.shx、.sho、.abx 文件可以使用 ESRI Shapefile 的开源库,例如 GDAL 或者 shapelib。 加水印需要先在要输出的文件中插入水印,可以使用开源的图像处理库,例如 OpenCV。将水印作为图像,先读入要输出的文件,然后在图像上叠加水印,最后将加了水印的图像输出到文件中。 下面是一个简单的示例代码,仅供参考: ```c++ #include <iostream> #include <fstream> #include <vector> #include <string> #include "dbf.h" #include "shapefil.h" #include "opencv2/opencv.hpp" using namespace std; using namespace cv; int main() { // 读取.dbf文件 DBFHandle dbf = DBFOpen("data.dbf", "rb"); if (dbf == nullptr) { cerr << "Failed to open data.dbf." << endl; return -1; } int recordCount = DBFGetRecordCount(dbf); int fieldCount = DBFGetFieldCount(dbf); // 读取.shp文件 SHPHandle shp = SHPOpen("data.shp", "rb"); if (shp == nullptr) { cerr << "Failed to open data.shp." << endl; return -1; } int shapeType; int shapeCount; double minBound[4], maxBound[4]; SHPGetInfo(shp, &shapeCount, &shapeType, minBound, maxBound); // 读取水印图像 Mat watermark = imread("watermark.png", IMREAD_UNCHANGED); if (watermark.empty()) { cerr << "Failed to open watermark.png." << endl; return -1; } // 创建输出文件 SHPHandle outShp = SHPCreate("output.shp", shapeType); DBFHandle outDbf = DBFCreate("output.dbf"); // 复制.dbf文件的schema for (int i = 0; i < fieldCount; i++) { char fieldName[12]; int fieldType, fieldWidth, fieldDecimals; fieldType = DBFGetFieldInfo(dbf, i, fieldName, &fieldWidth, &fieldDecimals); DBFAddField(outDbf, fieldName, fieldType, fieldWidth, fieldDecimals); } // 复制.shp文件的geometry vector<SHPObject*> shapes; for (int i = 0; i < shapeCount; i++) { SHPObject* shape = SHPReadObject(shp, i); shapes.push_back(shape); } // 在每个geometry上加水印 for (int i = 0; i < shapeCount; i++) { SHPObject* shape = shapes[i]; // 读取geometry的bounding box double xMin = shape->dfXMin; double yMin = shape->dfYMin; double xMax = shape->dfXMax; double yMax = shape->dfYMax; // 在bounding box中心位置叠加水印 Point2d center((xMin + xMax) / 2, (yMin + yMax) / 2); Point2d topLeft(center.x - watermark.cols / 2, center.y - watermark.rows / 2); Point2d bottomRight(center.x + watermark.cols / 2, center.y + watermark.rows / 2); // 将geometry转换为OpenCV的轮廓 vector<Point> points; for (int j = 0; j < shape->nVertices; j++) { double x = shape->padfX[j]; double y = shape->padfY[j]; Point pt(x, y); points.push_back(pt); } vector<vector<Point>> contours; contours.push_back(points); // 创建mask Mat mask = Mat::zeros(watermark.size(), CV_8UC1); fillPoly(mask, contours, Scalar(255)); // 叠加水印 Mat image = imread("data.png", IMREAD_UNCHANGED); Mat roi = image(Rect(topLeft, bottomRight)); Mat maskedWatermark; watermark.convertTo(maskedWatermark, CV_8UC4, 255); vector<Mat> channels; split(maskedWatermark, channels); channels[3] = mask; merge(channels, maskedWatermark); addWeighted(roi, 1, maskedWatermark, 0.5, 0, roi); // 将加了水印的geometry写入输出文件 SHPObject* outShape = SHPCreateSimpleObject(shapeType, shape->nVertices, shape->padfX, shape->padfY, nullptr); DBFWriteIntegerAttribute(outDbf, i, 0, DBFReadIntegerAttribute(dbf, i, 0)); // 复制第一个字段(ID) for (int j = 1; j < fieldCount; j++) { DBFFieldType fieldType = DBFGetFieldInfo(dbf, j, nullptr, nullptr, nullptr); if (fieldType == FTDouble) { DBFWriteDoubleAttribute(outDbf, i, j, DBFReadDoubleAttribute(dbf, i, j)); } else if (fieldType == FTInteger) { DBFWriteIntegerAttribute(outDbf, i, j, DBFReadIntegerAttribute(dbf, i, j)); } else { DBFWriteStringAttribute(outDbf, i, j, DBFReadStringAttribute(dbf, i, j)); } } SHPWriteObject(outShp, -1, outShape); SHPDestroyObject(outShape); } // 关闭所有文件 SHPClose(shp); DBFClose(dbf); SHPClose(outShp); DBFClose(outDbf); return 0; } ``` 需要注意的是,上述示例代码仅支持点、线、面三种类型的geometry,如果要支持更多的geometry类型,需要根据实际情况修改代码。此外,示例代码中使用的水印是一个透明的PNG图像,如果要使用其他类型的水印,需要相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值