3

Is there a way in QGIS to show in linestring's labels the number of overlapping linestrings (with exact same geometries)?

I've got cables that have the same geometry (are in same pipeline) and I need to know how many of them are there as I only see one linestring...

1
  • You can also use the Identify tool to find out how many identical geometries you have. Click on one, and attributes for all the geometries at that location will be displayed in the Identify window.
    – csk
    Sep 5, 2018 at 18:34

3 Answers 3

3

Install the reffunctions plugin which provides the intersecting_geom_count function.

Label the layer with the function: intersecting_geom_count('layername')


This very simple method has a few downsides:

  1. It displays a count of intersecting features, not identical features. Lines that cross each other will be included in this count.

  2. Each line cluster will be labeled multiple times, once for each line. So if you have five overlapping lines, they will be labeled like this: 5 5 5 5 5. This issue can be fixed as follows:

    • Dissolve the line layer.
    • Run Split multiparts to single parts on the dissolved layer.
    • Label the resulting layer (which will have only one line instead of overlapping lines) with the exact same expression as above (use the name of the original line layer, not the new dissolved layer).
2
  • thanks for yor answer. It does not work in my case cause it is counting intersecting lines and no equal lines. I do not need manual processing as the layer is in postgis and need the layer to be "online".
    – Egidi
    Sep 7, 2018 at 11:02
  • I created a new function in the plugin that counts equal geometries from another layer. Added a pull request in reffunctions github repository: github.com/enricofer/refFunctions/pull/20
    – Egidi
    Sep 7, 2018 at 12:21
2

For those interested in using a native approach (QGIS 3.4.1):

# lines_1 is the layer which will receive the count
# it has an empty attribute column called "count" at index[1]

lines_1 = QgsProject.instance().mapLayersByName('lines_1')[0]
lines_2 = QgsProject.instance().mapLayersByName('lines_2')[0]

lines_2_feats = lines_2.getFeatures()

lines_2_geoms = []

for f in lines_2_feats:
    lines_2_geoms.append(f.geometry())

lines_1_feats = lines_1.getFeatures()

results = []

for f in lines_1_feats:
    count = []
    for g in lines_2_geoms:
        if str(f.geometry()) == str(g):
        # add features with matching geometries to a list    
        count.append(f)
    # create a list ('results') of dictionaries to send to the datProvider() 
    # format is {feature_number:{field_number:{attribute_value}}
    # ascertain "count" by getting the length of the list containing matching features
    results.append({f.id():{1:len(count)}})

l_prov = lines_1.dataProvider()

# iterate through the list of results and send the dictionaries to the dataProvider() 
for r in results:
    l_prov.changeAttributeValues(r)

In the image the red lines are layer 'lines_1'. The labels are for 'lines_1' displaying the "count" field and indicate how many features from the layer 'lines_2' are identical.The green and orange are lines from 'lines_2' that share exactly the same geometry (the size difference is purely symbology and not geometry)

identical lines from different layers

1

Keeping things simple.

You start with a line layer 'OriginalLines'

Run 'Delete Duplicate Geometeries' to get a second layer, call it 'UniqueLines'

Join Attributes by Location (Summary) 'UniqueLines' to 'OriginalLines'. Select a suitable field and choose count. The results layer will include a count for each unique line geometry.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.