Is there a way of labeling the number of points with the same coordinates?
Nice question! PyQGIS to the rescue!
Follow these steps to get a new field (in your original layer) called label
with the number of points that lie in the same location:
- Active (select) your layer in the QGIS ToC.
Run this code snippet in the QGIS Python console:
from PyQt4.QtCore import QVariant lyr = iface.activeLayer() # Create New Field lyr.dataProvider().addAttributes([QgsField("label", QVariant.Int)]) lyr.updateFields() fIdx = lyr.fieldNameIndex( 'label' ) # Create Spatial Index idx = QgsSpatialIndex() for f in lyr.getFeatures(): idx.insertFeature(f) # Use the Spatial Index attrFeatMap = {} for f in lyr.getFeatures(): if not f.id() in attrFeatMap: res = idx.intersects( f.geometry().boundingBox() ) for id in res: attrFeatMap[id] = { fIdx : len(res) } # Write calculated count to the label field lyr.dataProvider().changeAttributeValues( attrFeatMap )
Open layer properties and configure labels selecting the
label
field we've just populated.This is the result:
EDIT:
As you would also like to filter by an attribute, you can define an expression in the beginning of the code (for example, after the Create New Field
block):
expr = QgsExpression( '"string"=\'{}\''.format("abc") )
and replace the two calls to lyr.getFeatures()
by:
lyr.getFeatures( QgsFeatureRequest( expr ) )
That way you'll only get the count of the points with the same coordinates that fulfill the string=abc
condition.
If I understand, you want aggregate a layer by location. You will need SQL for that, so create a virtual layer (Qgis >= 2.14) with a query like :
SELECT geometry, count(*) AS count FROM your_layer GROUP BY geometry
You can now use the "count" field of the virtual layer as label.
For adding the filter by the text field :
SELECT
geometry,
count(*) AS count_total,
count(CASE WHEN filter_field = "abc" THEN 1 ELSE NULL END) as count_filter
FROM your_layer GROUP BY geometry
Adapt words to your layer : your_layer, filter_field and "abc".
you can use Rule-based labeling
shapefile point before the rule
all the points they label.
after the rule:
using rules you can specify which labels to show, even using conditional statements