最近真是懒得一X,直接上代码。。。。。
最近搞Qt,需要利用QGis实现一些地理坐标显示还有插值图渲染功能。折腾了一大圈,终于实现了,记录一下,分享给需要的同学们。( ̄︶ ̄)↗
安装QGis啥的就自己百度吧,很傻瓜,就是慢。。。
1. 引入相关头文件
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmapcanvas.h>
#include <qgsvectorlayer.h>
#include <qgsproject.h>
#include <qgsrasterlayer.h>
#include <QDialog>
#include <QFileDialog>
#include <QMessageBox>
#include "ogrsf_frmts.h"
// 功能函数的头文件
#include <qgssinglesymbolrenderer.h>
#include <qgsinterpolator.h>
#include <qgstininterpolator.h>
#include <qgsgridfilewriter.h>
#include "qgssinglebandpseudocolorrenderer.h"
#include <qgsrastershader.h>
2.修改主函数
注意此处用Release模式运行(不知道为啥,反正Debug就是错!!╮(╯_╰)╭)
#include <qgsapplication.h>
int main(int argc, char *argv[])
{
//QApplication a(argc, argv); 正常qt application的初始化
QgsApplication a(argc, argv, true);
QgsApplication::setPrefixPath("D:\\SoftWares\\QGIS\\install\\apps\\qgis\\plugins", true);
// 此处用自己的安装路径下面的库
QgsApplication::initQgis(); //初始化QGIS应用
APP w;
w.show();
return a.exec();
}
3. 添加图层向量
定义变量
QgsMapCanvas* mapCanvas; // 地图画布
//QList<QgsMapCanvasLayer> mapCanvasLayerSet; // 地图画布所用的图层集合
QList<QgsMapLayer*> layers;
QPushButton* train_button;
实现函数
void onClickedBtnAddVectorLayers()
{
QString filename = QFileDialog::getOpenFileName(this, tr("open vector"), "", "*.shp");
QStringList temp = filename.split(QDir::separator());
QString basename = temp.at(temp.size() - 1);
QgsVectorLayer* vecLayer = new QgsVectorLayer(filename, basename, "ogr");
if (!vecLayer->isValid())
{
QMessageBox::critical(this, "error", "layer is invalid");
return;
}
QgsProject::instance()->addMapLayer(vecLayer);
//mapCanvas->setVisible(true);
layers.append(vecLayer);
mapCanvas->setExtent(vecLayer->extent());
mapCanvas->setLayers(layers);
mapCanvas->zoomToFullExtent();
//mapCanvas->freeze(false);
mapCanvas->refresh();
}
4. 添加栅格图层
本实现方法将栅格属性中的颜色表进行了自定义,实现了彩色渐变。可以自行修改!!!
void onClickedBtnAddRasterLayers()
{
QString filename = QFileDialog::getOpenFileName(this, tr("open vector"), "", "*.tif");
QStringList temp = filename.split(QDir::separator());
QString basename = temp.at(temp.size() - 1);
QgsRasterLayer* rasterLayer = new QgsRasterLayer(filename, basename, "gdal");
if (!rasterLayer->isValid())
{
QMessageBox::critical(this, "error", "layer is invalid");
return;
}
// 寻找栅格图层数据中的最大值和最小值
double max_value = 0, min_value = 100000,op=0;
max_value = rasterLayer->dataProvider()->bandStatistics(1).maximumValue;
min_value = rasterLayer->dataProvider()->bandStatistics(1).minimumValue;
op = (max_value - min_value) / 4;
//ui.lineEdit->setText(QString::number(max_value));
//ui.lineEdit_2->setText(QString::number(min_value));
//获取当前默认Qgis工程
QgsProject* project = QgsProject::instance();
project->addMapLayer(rasterLayer);
//构建一个栅格ColorRampItem列表,每个ColorRampItem表示栅格数据一个值对应一个颜色
QList<QgsColorRampShader::ColorRampItem> crlist;
//添加填充色
crlist.append(QgsColorRampShader::ColorRampItem(min_value, QColor(143, 131, 86, 255), QString("Fill")));
//添加云色
crlist.append(QgsColorRampShader::ColorRampItem(min_value+op, QColor(171, 221, 164, 255), QString("Cloud")));
//添加海洋水体颜色
crlist.append(QgsColorRampShader::ColorRampItem(min_value+op*2, QColor(255, 255, 191, 255), QString("Sea")));
//添加陆地颜色
crlist.append(QgsColorRampShader::ColorRampItem(min_value + op * 3, QColor(253, 174, 97, 255), QString("Land")));
//添加积雪颜色
crlist.append(QgsColorRampShader::ColorRampItem(max_value, QColor(215, 25, 28, 255), QString("Snow")));
//构建新的QgsColorRampShader 着色器
QgsColorRampShader* crshader = new QgsColorRampShader(0, 100, nullptr, QgsColorRampShader::Type::Interpolated);
//将颜色列表添加到着色器中
crshader->setColorRampItemList(crlist);
//构建栅格着色器,我也不知道为啥要包装这么多层
QgsRasterShader* rasterShader = new QgsRasterShader();
//设置渲染函数为刚刚新建的QgsColorRampShader 着色器
rasterShader->setRasterShaderFunction(crshader);
//构建一个伪彩色单波段渲染器,使用前面的栅格着色器作为输入,我也不明白为什么这么麻烦
QgsSingleBandPseudoColorRenderer* pseudRender = new QgsSingleBandPseudoColorRenderer(
rasterLayer->dataProvider(), 1, rasterShader);
//栅格图层设置伪彩色单波段渲染器
rasterLayer->setRenderer(pseudRender);
layers.append(rasterLayer);
mapCanvas->setExtent(rasterLayer->extent());
mapCanvas->setLayers(layers);
mapCanvas->zoomToFullExtent();
mapCanvas->refresh();
}
5. 生成TIN插值图
void onClickedBtnGenerateTINMap()
{
// 路径可修改为自己的 .shp 文件路径
QgsVectorLayer* exp5Layer = new QgsVectorLayer("D:/newShp.shp", "shapefile", "ogr");
const int index = 0;
QgsInterpolator::LayerData ld5;
ld5.source = exp5Layer;
ld5.interpolationAttribute = index;
ld5.sourceType = QgsInterpolator::SourceType::SourcePoints;
ld5.valueSource = QgsInterpolator::ValueSource::ValueAttribute;
QList<QgsInterpolator::LayerData> lds;
lds.append(ld5);
QgsTinInterpolator* itp = new QgsTinInterpolator(lds);
QgsWkbTypes::Type wkbType = exp5Layer->wkbType();
QgsFields fields = exp5Layer->fields();
// 范围选择图层范围,参数也可按自己需求修改(不明白参数的意思,可以先用QGis的Desktop软件试一试,功能对应)
QgsRectangle rect = exp5Layer->extent();
int ncol = (rect.xMaximum() - rect.xMinimum()) / 5;
int nrow = (rect.yMaximum() - rect.yMinimum()) / 5;
double current_y_value = rect.yMaximum() - 0.1 / 2.0;
double current_x_value = rect.xMinimum() + 0.1 / 2.0;
double interpolatedValue;
QgsGridFileWriter* writer = new QgsGridFileWriter(itp, "D:/d.tif", rect, ncol, nrow);
writer->writeFile();
}
效果图如下
[参考链接]
https://zhuanlan.zhihu.com/p/268609488
https://docs.qgis.org/3.16/en/docs/training_manual/processing/interpolation.html?highlight=interpolation
https://blog.csdn.net/deirjie/article/details/50413117
http://caiyi.tech/post.html?postKey=qgis_dev_1_env_setup
https://www.codenong.com/cs106838694/