Saturday 21 December 2019

python - How do I fill a combobox in QGIS with only points vector layers?


I'm new to writing plugins in python for QGIS. I've been searching on internet for a while and wrote the code below. Even though it fills my combobox, all layers (rasters, vectors of all geometry types) get added. What am I doing wrong here?


#Populate the combo box with vector layers
layers = QgsMapLayerRegistry.instance().mapLayers().values()
for layer in layers:
if layer.type() == QgsMapLayer.VectorLayer and layer.geometryType() == QGis.Point:
self.dlg.mMapLayerComboBox.addItem(layer.name(), layer)

Answer



This should help:



  if layer.type() == QgsMapLayer.VectorLayer and (layer.wkbType()==QGis.WKBPoint or layer.wkbType() == QGis.WKBMultiPoint):

But it is not the best approach. Just use QgsMapLayerComboBox https://qgis.org/api/classQgsMapLayerComboBox.html


use:


comboBox.setFilters(QgsMapLayerProxyModel.PointLayer)

On your combobox object.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...