TransWikia.com

Extraction of line segments in front of the street

Geographic Information Systems Asked by Tony Pasca on February 14, 2021

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

2 Answers

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

Correct answer by Babel on February 14, 2021

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

Answered by xunilk on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP