QGIS获取点图层中各个点的坐标信息

由于项目中需要获取点图层的所有点的位置,通过查看源码找到以下方法,经测试有效

	QgsMapCanvas*canvas = dynamic_cast<QgsMapCanvas*>(GlobalUseInst()->load2DMapCanvas());
	QList<QgsMapLayer*> layerList = canvas->layers();
	int numLayer = layerList.size();
	for (size_t i = 0; i < numLayer; i++) {
		QgsMapLayer* mapLayer = layerList.at(i);
		QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>(mapLayer);
		if (vectorLayer->geometryType() == QGis::Point && vectorLayer->name() == QStringLiteral("机场")) {//机场图层
			const QgsSingleSymbolRendererV2*rendererV2 = dynamic_cast<QgsSingleSymbolRendererV2*>(vectorLayer->rendererV2());

			//所有数据都保存在QgsVectorDataProvider中
			QgsVectorDataProvider*dataProvider = vectorLayer->dataProvider();
			//获取所有点的迭代器
			QgsFeatureIterator it = dataProvider->getFeatures();
			QgsFeature feature;
			QgsMultiPoint airPortPoint;

			int row = 0;

			while (!it.isClosed()) {
				//下一个点
				it.nextFeature(feature);

				AirPortItem airPortItem;

				QgsGeometry* geo = feature.geometry();
				QGis::GeometryType geoType = geo->type();
				const QgsFields* fields = feature.fields();
				int fieldCount = fields->count();
#if 1
				airPortPoint = geo->asMultiPoint();//使用这个函数
#else
				geo->asPoint();//不要使用这个函数
#endif
				airPortItem.setAirPortLon(airPortPoint[0].x());
				airPortItem.setAirPortLat(airPortPoint[0].y());
				QString airPortName = feature.attribute(0).toString();
				}
		}
	}

aaa

你可能感兴趣的:(GIS相关)