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()<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 ; line << 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();
参考链接:QT(5.12)+Qgis(3.10) 点图层及线图层渲染 - 灰信网(软件开发博客聚合) (freesion.com)