使用GDAL/OGR读取OGRMultiLineString类型数据
最近在开发的时候遇到需要将shp线数据转为点数据,也就是要把线上的系列点坐标读取出来,通常用arcgis能完成操作,但在开发中只能通过代码实现,在AE二次中有专门的类和接口,但用C++进行的二次开发就不那么好办,查阅发现GDAL/OGR可以帮助完成此项工作,这里与大家分享结果。
#include <iostream>
#include <QString>
#include <vector>
#include <QVector>
#include "gdal.h"
#include "gdal_priv.h"
#include "gdal_version.h"
#include "ogrsf_frmts.h"
using namespace std;
void Roam::GetPoints()
{
GDALAllRegister();
OGRRegisterAll();
GDALDataset *poDS;
poDS=(GDALDataset*);
GDALOpenEx("line.shp",GDAL_OF_VECTOR,NULL,NULL,NULL);
if(poDS==NULL)
{
cout<<"open failed "<<endl;
}else{
cout<<"is not null"<<endl;
}
OGRLayer *poLayer;
OGRFeature *poFeature;
OGRGeometry *poGeometry;
poLayer=poDS->GetLayer(0);
poFeature=poLayer->GetNextFeature();
poGeometry=poFeature->GetGeometryRef();
//判断类型,这里wkbMultiLineString表示的是MultiLineString型
if(poGeometry!=NULL&&wkbFlatten(poGeometry->getGeometryType())==wkbMultiLineString)
{
OGRMultiLineString *poMultiLineString=(OGRMultiLineString*)poGeometry;
int nGeoCount=poMultiLineString->getNumGeometries();
cout<<"poGeometry IS NOT NULL"<<endl;
cout<<nGeoCount<<endl;
OGRGeometry *poLineGeometry;
vector<double> arryX;
vector<double>arryY;
for(int iLine=0;iLine<nGeoCount;iLine++)
{
poLineGeometry=poMultiLineString->getGeometryRef(iLine);
OGRLineString* poLineString=(OGRLineString*)poLineGeometry;
int nnCount=poLineString->getNumPoints();
double x,y;
for(int i=0;i<nnCount;i++)
{
x=poLineString->getX(i);
y=poLineString->getY(i);
arryX.push_back(x);
arryY.push_back(y);
// cout<<"X:"<<x<<", Y:"<<y<<endl;
// cout<<"arryX:"<<arryX.size()<<", "<<"arryY:"<<arryY.size()<<endl;
// cout<<"nnCount:"<<nnCount<<endl;
}
// cout<<"iLine:"<<iLine<<endl;
}
}else{
cout<<"poGeometry IS NULL"<<endl;
}
}
转载自:https://blog.csdn.net/qq_38378235/article/details/81604971