QGis C++ 开发之创建临时图层并添加要素
时间: 2023-09-04 10:11:57 浏览: 163
要在 QGIS C++ 开发中创建临时图层并添加要素,可以按照以下步骤进行:
1. 创建临时图层对象
```cpp
QString layerName = "Temp Layer"; // 临时图层名称
QgsVectorLayer *tempLayer = new QgsVectorLayer("Point", layerName, "memory");
```
2. 添加临时图层到地图中
```cpp
QgsMapLayerRegistry::instance()->addMapLayer(tempLayer, true); // 添加到地图中并显示
```
3. 创建要素并添加到临时图层中
```cpp
QgsFeature feature;
QgsGeometry geometry = QgsGeometry::fromPoint(QgsPoint(-122.7, 45.5)); // 创建点要素
feature.setGeometry(geometry);
tempLayer->addFeature(feature); // 添加到临时图层中
```
注意:临时图层在程序结束后会被自动删除,无需手动删除。
完整示例代码如下:
```cpp
QString layerName = "Temp Layer";
QgsVectorLayer *tempLayer = new QgsVectorLayer("Point", layerName, "memory");
QgsMapLayerRegistry::instance()->addMapLayer(tempLayer, true);
QgsFeature feature;
QgsGeometry geometry = QgsGeometry::fromPoint(QgsPoint(-122.7, 45.5));
feature.setGeometry(geometry);
tempLayer->addFeature(feature);
```
阅读全文