5

I have a layer of lines that represents a layer of plots. I want to eliminate all the inner segments to have only the segments that go out into the street. Thus, I want to determine the sum of the segments that lead to the road. I started like this: enter image description here

  1. We removed the double segments with the function "Delete duplicate geometries"
  2. I used dissolve on the group field to form a single line for a single plot.
  3. I turned the lines into polygons.
  4. I dissolved again on the resulting polygons to remain only with the contour of the plots, without the inner lines. enter image description here
  5. I used "Extract by location", using the touch parameter to extract the face to the street (only the lines facing the street)

This is the end result. Do you have any idea why these lines are broken and do not return the outline of my plot? Do you have any idea if there is a difference between layers? Or if not, you know some other way to extract these lines, but extract it from the original file, to keep the tabular data and segment characteristics.

enter image description here

After buffering the layer, it looks like this: I set the tolerance of 0.00000001. However, I think it works even smaller.

enter image description here

  • Which line layer did you run the extract by location on? – Erik Jan 12 at 9:19
  • On the initial layer of lines. "parcel lines" I did the other functions to get to a layer to use in Extract by location. There I need a layer for the parameter: By comparing to the future from, where I used the layer with dissolve. – Tony Pasca Jan 12 at 9:23
  • Btw, you may be able to skip all the intermediate steps (1. - 4.) using DBScan docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/… with epsilon 0 and minpoints 1 as with a eps distance of 0, the function effectively clusters by intersection only. – Timothy Dalton Jan 12 at 9:36
  • I used the function you told me. I set the parameters the way you said "minPTS = 1" and eps "0". Then I used extraction by location again, but it returns the same result as before. @Timothy Dalton – Tony Pasca Jan 12 at 9:50
  • @TonyPasca if I understand correctly, would transforming your dissolved polygons to linestrings be enough? gis.stackexchange.com/questions/15819/… – Timothy Dalton Jan 12 at 10:04
2

Instead of step 5, create a new geometry by expression (go to Menu Processing / Toolbox), using the expression boundary ($geometry), see:

enter image description here

The result:

enter image description here

2

It is annoying when it is necessary to do an extraction of line segments and layer is LinesSring type (not Polygon type). It is because it depends of the way as lines were digitized. Your approach was correct but your "Extract by location" method doesn't have a "convexHull" option. In a PyQGIS script you can do that.

First, I downloaded your image and, it was arbitrarily projected with EPSG:32612 by me. Afterward, I digitized two blocks (LineString type); as it can be watched in following image. Observe that I also used the group field (in this case named block) to form a single line for a single plot with dissolve in my Python script.

enter image description here

Complete developed code looks as follows:

import processing

layer = iface.activeLayer()

parameters1 = { 'FIELD' : ['block'], 
                'INPUT' : layer, 
                'OUTPUT' : 'TEMPORARY_OUTPUT' }

result1 = processing.run('qgis:dissolve', 
                         parameters1)

parameters2 = { 'INPUT' : result1['OUTPUT'], 
                'OUTPUT' : 'TEMPORARY_OUTPUT' }

result2 = processing.run('qgis:fixgeometries', 
                          parameters2)

parameters3 = { 'INPUT' : result2['OUTPUT'], 
               'OUTPUT' : 'TEMPORARY_OUTPUT' }

result3 = processing.run('qgis:linestopolygons', 
                         parameters3)

geoms_hull = [ feat.geometry().convexHull().asWkt() for feat in result3['OUTPUT'].getFeatures() ]
perimeters = [ feat.geometry().length() for feat in result3['OUTPUT'].getFeatures() ]

epsg = layer.crs().postgisSrid()

uri = "Polygon?crs=epsg:" + str(epsg) + "&field=id:integer&field=length:double""&index=yes"

mem_layer = QgsVectorLayer(uri,
                           'Polygon',
                           'memory')
                           
prov = mem_layer.dataProvider()

feats = [ QgsFeature() for i in range(len(geoms_hull)) ]

for i, feat in enumerate(feats):
    feat.setAttributes([i, perimeters[i]])
    feat.setGeometry(QgsGeometry.fromWkt(geoms_hull[i]))

prov.addFeatures(feats)

QgsProject.instance().addMapLayer(mem_layer)

When above code was run in Python Console of QGIS, I got result of following image. Convex Hull geometries (polygon features) perfectly match with each grouped features plot. In the attributes table it were also included your desired sums of the segments that lead to the road.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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