Wechat: yu389741| Email: gisdqy@163.com

Shop:https://www.giserdqy.com/shop

GDAL矢量(.shp)文件读写与创建



gdal18版本读写矢量与创建,具体李明录老师的书本更详细
以下代码是求两个矢量的交集:

    GDALAllRegister();
    OGRRegisterAll();
    CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
    OGRDataSource *podata = OGRSFDriverRegistrar::Open(str_checkshp.c_str(),1);
    ///str_checkshp为矢量路径,字符串类
        if (podata == NULL)
        {
            cout <<"file:" <<str_checkshp << " open checkshp fail!!" << endl;
        }
        OGRLayer *poSourceLayer = podata->GetLayer(0);
        if (poSourceLayer == NULL)
        {
            cout << "file:" << str_checkshp << "open checkshp_layer fail!!" << endl;
        }
        poSourceLayer->ResetReading();
        OGRFeature* pFea = poSourceLayer->GetNextFeature();
        if (pFea == NULL)
        {
            cout << "file:" << str_checkshp << " feature is not exist!!" << endl;
        }
        OGRGeometry *poGeometry= poFeature->GetGeometryRef();
///获取矢量2的geo
OGRDataSource *podata2 = OGRSFDriverRegistrar::Open(str_checkshp2.c_str(),1);
///str_checkshp2为矢量路径,字符串类型
        if (podata2 == NULL)
        {
            cout <<"file:" <<str_checkshp2 << " open checkshp fail!!" << endl;
        }
        OGRLayer *poSourceLayer2 = podata2->GetLayer(0);
        if (poSourceLayer2 == NULL)
        {
            cout << "file:" << str_checkshp2 << "open checkshp_layer fail!!" << endl;
        }
        poSourceLayer2->ResetReading();
        OGRFeature* pFea2 = poSourceLayer2->GetNextFeature();
        if (pFea2 == NULL)
        {
            cout << "file:" << str_checkshp2 << " feature is not exist!!" << endl;
        }
        OGRGeometry *poGeometry2= poFeature2->GetGeometryRef();
        ///创建矢量
        OGRSFDriver *poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);///获取驱动
    if (poDriver == NULL)
    {
        cout << "驱动不可用" << endl;
        return FALSE;
    }
        OGRDataSource *poNewDS = poDriver->CreateDataSource(strShpNewfliedir.c_str(), NULL);
        OGRSpatialReference *sp = poSourceLayer2->GetSpatialRef();
    OGRSpatialReference* oSRS = sp;///获取空间参考
    OGRLayer *poNewLayer = poNewDS->CreateLayer("newlayer", oSRS, wkbPolygon, NULL);
OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
    OGRFeature *poFeatureIntersection = OGRFeature::CreateFeature(poDefn);
    OGRGeometry *poGeometry3 = poGeometry1->Difference(poGeometry2);
    poFeatureIntersection->SetGeometry(poGeometry3);
    poLayer->CreateFeature(poFeatureIntersection);
    OGRFeature::DestroyFeature(poFeatureIntersection);
    OGRFeature::DestroyFeature(pFea );
    OGRFeature::DestroyFeature(pFea2);

    GDALClose(poNewDS);
    GDALClose(podata2);
    GDALClose(podata);
    return 0;

GDAL201矢量读写与创建
暂时没有中文书籍,就记录了下来,方便拷贝,提高码代码效率

if (OGRGeometryFactory::haveGEOS() == false)
    {
        cout << "GDAL库未包含GEOS库" << endl;
        return 0;
    }
    // 打开栅格文件
    GDALDataset* poSrcDS1 = (GDALDataset*)GDALOpenEx(pszSrc1File, GDAL_OF_VECTOR, NULL, NULL, NULL);
    if (poSrcDS1 == NULL)
    {
        return 0;
    }
    OGRLayer* poLayer1 = poSrcDS1->GetLayer(0);
    poLayer1->ResetReading();
    OGRFeature *poFeature1 = poLayer1->GetNextFeature();
    OGRGeometry *poGeometry1 = poFeature1->GetGeometryRef();

    GDALDataset* poSrcDS2 = (GDALDataset*)GDALOpenEx(pszSrc21File, GDAL_OF_VECTOR, NULL, NULL, NULL);
    if (poSrcDS2 == NULL)
    {
        return 0;
    }
    OGRLayer* poLayer2 = poSrcDS2->GetLayer(0);
    poLayer2->ResetReading();
    OGRFeature *poFeature2 = poLayer2->GetNextFeature();
    OGRGeometry *poGeometry2= poFeature2->GetGeometryRef();

    OGRGeometry *poGeometry3 = poGeometry1->Difference(poGeometry2);

    // 创建输出矢量文件
    GDALDriver *poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
    if (poDriver == NULL)
    {
        printf("%s驱动不可用!\n", "ESRI Shapefile");
        GDALClose((GDALDatasetH)poSrcDS1);
        GDALClose((GDALDatasetH)poSrcDS2);
        return 0;
    }
    //根据文件名创建输出矢量文件
    GDALDataset* poDstDS = poDriver->Create(pszDstFile, 0, 0, 0, GDT_Unknown, NULL);
    if (poDstDS == NULL)
    {
        GDALClose((GDALDatasetH)poSrcDS1);
        GDALClose((GDALDatasetH)poSrcDS2);
        return 0;
    }
    // 定义空间参考,与输入图像相同
    OGRSpatialReference *poSpatialRef = new OGRSpatialReference(poSrcDS1->GetProjectionRef());
    if (poSpatialRef==NULL)
    {
        return 0;
    }
    OGRLayer* poLayer = poDstDS->CreateLayer("Result", poSpatialRef, wkbPolygon, NULL);
    if (poDstDS == NULL)
    {
        GDALClose((GDALDatasetH)poSrcDS1);
        GDALClose((GDALDatasetH)poSrcDS2);
        delete poSpatialRef;
        poSpatialRef = NULL;
        return 0;
    }
    OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();
    OGRFeature *poFeatureIntersection = OGRFeature::CreateFeature(poDefn);
    poFeatureIntersection->SetGeometry(poGeometry3);
    poLayer->CreateFeature(poFeatureIntersection);
    OGRFeature::DestroyFeature(poFeatureIntersection);
    OGRFeature::DestroyFeature(poFeature1);
    OGRFeature::DestroyFeature(poFeature2);

    GDALClose(poSrcDS1);
    GDALClose(poSrcDS2);
    GDALClose(poDstDS);
    return 0;

最后附上gdal201版本的编译完成的库,包含了geos。欢迎下载
http://download.csdn.net/download/u013230291/10000023

GDAL18下载地址:http://download.csdn.net/download/u013230291/9920097

转载自:https://blog.csdn.net/u013230291/article/details/78124785