主要參考了大神之作 https://github.com/Leopard-C/iCGIS
結果看圖
因為工作上會用到GIS顯示地圖,用的功能簡單,而用QGIS會顯得非常龐大,故考慮自己寫一個簡單的GIS,但能力素質實在偏低,終於看到了大神之作 https://github.com/Leopard-C/iCGIS。於是自個根據需求瞎改了一起,用GDAL讀地圖文件Shapefile,進而用opengl渲染。
這里需要一個gdal庫的支持,因為我安裝了qgis,所以已經配好了,可以利用osgeo4w-setup-x86_64.exe直接配置。
建了個類FileReader,寫個靜態函數readShapefile,將圖層保存到layers。
調用方法:FileReader::readShapefile("D:/work/shp/asia1.shp",layers);
void FileReader::readShapefile(QString filepath, QVector<OGRLayer*> &layers)
{
QByteArray bytes = filepath.toLocal8Bit();
const char* path = bytes.data();
GDALAllRegister();
GDALDataset* pDS = nullptr;
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
CPLSetConfigOption("SHAPE_ENCODING", "");
pDS = (GDALDataset*)GDALOpenEx(path, GDAL_OF_VECTOR, NULL, NULL, NULL);
if (!pDS) {
qDebug()<<"Open {0} error"<<path;
return ;
}
int layerCount = pDS->GetLayerCount();
if (layerCount == 0) {
qDebug()<<("poDsIn dosen't have any layer");
return ;
}
qDebug()<<"convertGDALDataset layerCount="<<layerCount;
for (int i = 0; i < layerCount; ++i) {
OGRLayer* pLayer = pDS->GetLayer(i);
layers.append(pLayer);
}
//GDALClose(poDS);//先不關
}
下一步就開始分析圖層里的圖元等。