1

I am trying to include the feature count when using the qgis2web PLugin. I have a point layer and I simply want to show the number of features for each specific field.

enter image description here

This is what I see in the layer window of QGIS 3.28.11

I would just like to show the same items with feature counts on the web map.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Dec 3, 2023 at 15:01

1 Answer 1

1

qgis2web does not have an option that adds feature count. You have to do this in qgis before exporting by hand.

I asked for help in this forum and you can see the discussion here. I'll summarize the content of the discussion by adding a way to reset the visibility of the feature count in the name.

To view the feature count in the name of a single layer you will need to activate the python console and run this script, indicating the name of your layer

 # imports
 from qgis.core import QgsProject
 
 # getting a layer by its name
 layer = QgsProject.instance().mapLayersByName("YOUR_LAYER_NAME")[0]
 
 # counting features in the layer
 n = layer.featureCount()
 
 # setting up a new name
 layer.setName(f"{layer.name()} [{n}]")

You will see that the layer name will capture the feature count. If the number of features changes you will have to run the command again.

enter image description here

If you want to write the feature count in the name of each vector layer you will have to run this script

 # imports
 from qgis.core import QgsProject, QgsVectorLayer
 
 # getting all vector layers' ids in the project 
 ids = [layer.id() for layer in QgsProject().instance().mapLayers().values() if isinstance(layer, QgsVectorLayer)]
 
 # looping over each layer's id
 for id in ids:
 # accessing a layer by its id
 layer = QgsProject.instance().mapLayer(id)
 
 # getting layer's name
 name = layer.sourceName()
 
 # counting features in the layer
 n = layer.featureCount()
 
 # setting up a new name
 layer.setName(f"{name} [{n}]")

If you want to update the feature count you will first need to remove the count with the following script and then run one of the two scripts written above

# imports
from qgis.core import QgsProject, QgsVectorLayer
# getting all vector layers' ids in the project 
ids = [layer.id() for layer in QgsProject().instance().mapLayers().values() if isinstance(layer, QgsVectorLayer)]
# looping over each layer's id
for id in ids:
 # accessing a layer by its id
 layer = QgsProject.instance().mapLayer(id)
 # getting layer's name
 name = layer.sourceName()
 # checking if the layer name has the pattern " [numero]"
 if " [" in name and name.endswith("]"):
 # removing the " [numero]" part
 new_name = name[:name.rfind(" [")]
 layer.setName(new_name)
answered Dec 14, 2023 at 20:59
3
  • Would be nice, if you could include the reference to the original post: gis.stackexchange.com/a/472951/99589 Commented Dec 14, 2023 at 21:22
  • Thanks, I added it, is it okay to write it like this? Commented Dec 14, 2023 at 21:40
  • Thank you andreaordonselli for your response. I am not familiar with python coding but I will certainly be taking a look. I suspect I might have a few more questions! :) Commented Dec 21, 2023 at 19:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.