GDAL矢量(.shp)文件读写与创建
来源:互联网 发布:武功 知乎 编辑:程序博客网 时间:2024/05/26 15:54
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的geoOGRDataSource *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
阅读全文
0 0
- GDAL矢量(.shp)文件读写与创建
- GDAL读写shp文件
- node-gdal实践-创建shp文件
- GDAL读写矢量文件——Java
- GDAL读写矢量文件——C#
- GDAL读写矢量文件——Python
- GDAL读写矢量文件——Java
- GDAL读写矢量文件——C#
- Gdal读取矢量文件
- GDAL读写矢量数据-Python
- GDAL 2.0+OGR读写矢量
- C# Gdal 读取SHP文件
- node-gdal实践-读写shp数据
- GDAL修改shp矢量数据的属性值
- android 平台下通过GDAL创建SHP(ShapeFile)文件至SD卡
- C#、C++用GDAL读shp文件
- GDAL-OGR对shp文件的操作
- C#中Gdal读取SHP文件
- Netty系列三:netty线程模型
- 事务、视图和索引
- tldr安装
- Java内部类
- Jmeter
- GDAL矢量(.shp)文件读写与创建
- 一 iOS之 CALayer的基本介绍
- Java常见集合框架(八):List总结
- 数组演练(43)矩阵转置
- 详细解释CNN卷积神经网络各层的参数和链接个数的计算
- 【NOIP模板】 堆-stl
- Inteilj IDEA快捷键
- Android仿淘宝头条向上滚动广告条的效果ViewFlipper
- 存储过程