QGis的简单使⽤
创建画布(QgsMapCanvas)
QgsMapCanvas * canvas = new QgsMapCanvas;
canvas->setCanvasColor(Qt::white); //设置画布颜⾊
canvas->enableAntiAliasing(true); //启⽤抗锯齿
canvas->setExtent(vlayer->extent()); //设置图层范围 vlayer⽮量图层
canvas->setLayers(vLayer); //设置图层集合
canvas->setVisible(true); //设置是否可见
canvas->freeze(false); //设置是否冻结对图层的操作
canvas->refresh(); //刷新
canvas->show(); //显⽰
创建图层(QgsMapLayer)
//⼏何类型,"Point", "LineString", "Polygon", "MultiPoint","MultiLineString","MultiPolygon"
QString path = "multiPoint"
path.append( QString( "crs=EPSG:4326&")); //参照坐标系
path.append( QString( "field=id:integer&field=name:string(50)&")); //添加字段属性
path.append( QString( "index=yes&" )); //创建索引
path.append( QString( "memoryid=%1").arg(QUuid::createUuid().toString())); //临时编码
//创建⽮量图层
QgsVectorLayer *vLayer = new QgsVectorLayer(path, "MyPoint", "memory");
在图层插⼊点要素(QgsFeature)
//创建⼀个点图层
QgsVectorLayer* pointLayer = new QgsVectorLayer("Point?crs=epsg:4326", "MyPoint", "memory"); QgsVectorDataProvider* dataProvider = pointLayer->dataProvider();
//创建⼀个要素
QgsFeature MyFeature;
//设置在地图的插⼊点的坐标
MyFeature.setGeometry( QgsGeometry::fromPointXY( QgsPointXY(116.3215, 39.5416) ) );
dataProvider->addFeatures(QgsFeatureList()<<MyFeature);
pointLayer->updateExtents();
//pointLayer->setCoordinateSystem();
在图层插⼊线要素(QgsFeature)
//创建⼀个线图层。
QgsVectorLayer *lineLayer = new QgsVectorLayer("LineString?crs=epsg:4326", "MyLine", "memory"); QgsVectorDataProvider* dataProder = lineLayer->dataProvider();
//在指定坐标添加点
QgsFeature feature;
QgsGeometry geometry = QgsGeometry::fromPointXY( QgsPointXY(50,50) );
//通过点坐标进⾏画线
QgsPolyline line ;
svg canvasline << QgsPoint(100.4443, 32.2123) << QgsPoint(130.5557, 50.4445);
geometry = QgsGeometry::fromPolyline(line);
feature.setGeometry(geometry);
dataProder->addFeatures(QgsFeatureList()<<feature);
lineLayer->updateExtents();
//lineLayer->setCoordinateSystem();
在图层添加字段,并对指定要素设置属性(QgsField)
//创建图层
QgsVectorLayer alarmLayer = new QgsVectorLayer("Point?crs=epsg:4326", "alarmLayer", "memory");
//添加字段
alarmLayer->startEditing();
QgsField* filed = new QgsField(QStringLiteral("频率"), QVariant::String);
alarmLayer->addAttribute(*filed)); //也可通过alarmLayer->dataProvier()->addAttributes(QgsFields); alarmLayer->updateFields(); alarmLayer->commitChanges();
//根据id添加属性
alarmLayer->startEditing();
alarmLayer->changeAttributeValue(id, field, attribute);
alarmLayer->commitChanges();
在图层中删除要素
QgsVectorLayer planeLayer = new QgsVectorLayer("Point?crs=epsg:4326", "MyPoint", "memory");
//启⽤编辑
planeLayer->startEditing();
planeLayer->deleteFeature( QgsFeatureId(对应id) );
planeLayer->updateExtents();
//提交修改
planeLayer->commitChanges();
设置点样式(点图层渲染 QgsFeatureRenderer)
My_MapCanvas->freeze();
//获取SVG⽮量图标,绘制⽮量图层
QString Image_Path = QFileDialog::getOpenFileName(this,tr("Select"),"D:/svg",tr("Files(*.svg)")); QgsSvgMarkerSymbolLayer* svg_Marker =  new QgsSvgMarkerSymbolLayer(Image_Path); svg_Marker->setColor(QColor(0,0,0));
svg_Marker->setSize(10);
svg_Marker->setAngle(0);
//创建QgsSymbolLayerList
QgsSymbolLayerList Sym_Layer_List;
Sym_Layer_List.append(svg_Marker);
//构建QgsMarkerSymbol
QgsMarkerSymbol* MarkerSymbol = new QgsMarkerSymbol(Sym_Layer_List);
//构建QgsSingleSymbolRenderer, 即最终图层的渲染器
QgsSingleSymbolRenderer* Sym_Renderer = new QgsSingleSymbolRenderer( MarkerSymbol ); //设置当前图层的渲染器
CurrentLayer->setRenderer(Sym_Renderer);
//
My_MapCanvas->freeze(false);
My_MapCanvas->refresh();
设置线样式(线图层渲染 QgsFeatureRenderer)
My_MapCanvas->freeze();
//设置线图层的样式,包括color, width (in millimeters) and penStyle QgsSimpleLineSymbolLayer* Line_Renderer_Layer = new QgsSimpleLineSymbolLayer();
Line_Renderer_Layer->setColor(Qt::red);
Line_Renderer_Layer->setPenStyle(Qt::PenStyle::DotLine);
//创建⽮量图层列表,QgsSymbolLayerList就是重新定义了⼀个特殊的QList QgsSymbolLayerList Sym_Layer_List;
Sym_Layer_List.append(Line_Renderer_Layer);
//构建QgsLineSymbol
QgsLineSymbol* LineSymbol = new QgsLineSymbol(Sym_Layer_List);
//构建QgsSingleSymbolRenderer, 即最终图层的渲染器
QgsSingleSymbolRenderer* Sym_Renderer = new QgsSingleSymbolRenderer( LineSymbol );
//设置当前图层的渲染器
CurrentLayer->setRenderer( Sym_Renderer );
//
My_MapCanvas->freeze(false);
My_MapCanvas->refresh();
在图层中添加标签,显⽰某字段属性
QgsVectorLayer* vLayer = new QgsVectorLayer("D:/MSCities_Geo_Pts.shp", "Cities", "ogr");
//⽂本缓冲区设置
QgsTextBufferSettings* bufSettings = new QgsTextBufferSettings;
bufSettings->setEnabled(true); //启⽤⽂本缓冲区
bufSettings->setSize(1); //设置缓缓冲区⼤⼩
bufSettings->setColor(QColor("white"));
//⽂本样式设置
QgsTextFormat * textFormat = new QgsTextFormat;
textFormat->setFont( QFont("Arial", 12) );
textFormat->setSize(12);
textFormat->setBuffer(*bufSettings); //设置⽂本缓冲区
//图层属性设置
QgsPalLayerSettings* lSettings = new QgsPalLayerSettings;
lSettings->setFormat(*textFormat); //设置标签⽂本格式
lSettings->fieldName = "NAME10"; //要显⽰的字段名
lSettings->placement = QgsPalLayerSettings::Placement::Line; //设置标签样式
//lSettings->geometryGeneratorEnabled = true; //启⽤⼏何⽣成器
//创建label标签
QgsVectorLayerSimpleLabeling* LSsettings = new QgsVectorLayerSimpleLabeling(*lSettings); //
vLayer->setLabelsEnabled(true); //该图层启⽤label标签
vLayer->setLabeling(LSsettings); //设置标签配置
vLayer->triggerRepaint();
参考链接:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。