Wednesday, 16 October 2019

pyqgis - QGIS Extract by Expression with multiple polylines in Processing Modeler



I'm trying to get the matching features from an intersecting vector layer. I can manually open Extract by expression tool and put the expression like intersects($geometry, geometry(get_feature('Liness', 'id', '1'))) where 'Liness' is one of the polylines and I'm giving the other polyline as input. I would like to implement it using QGIS Processing Modeler.


Processing Modeler


Since in extract by expression can only give one input, I'm unable to get the desired output. Can anybody suggest a better method? I want to use the matching features to other further steps.


Or can I implement it using pyqgis? Is there any code sample to refer?


EDIT 1: As suggested by Gabriel De Luca, tried "Extract by Location". But it's selecting my input polyline in random order.


enter image description here enter image description here


Also, I would like to have each polyline with "id" of "Liness"(whose id changes based on the lines I plot for selection) to have a separate section. Why because, I would like to use its output to autoincrement function, where I can increase each of the input lines (whose selection came from different "id") values from zeros to a finite value based on the "Liness" intersection in order.



Answer



We start with single-part line vector layers: 'Input' and 'Lines'.
'Input' layer contain countour lines as features.

'Lines' layer contain some dumb lines as features, wich we will use to rotulate the features of 'Input' layer.
Both layers are GeoPackage and cointain one "fid" field, autocompleted.


1


We need to know what features of the 'Input' layer, intersect what features of the 'Lines' layer. Thereforoe, we need to Join Attributes by Location in the modeler.


2


It returns both Joined and Unjoined outputs.
Joined output is a layer with a new field: "Lines_fid", wich has the "fid" attribute value of the dumb line intersected for each contour line.
It's a condition that each countour line must intersect only one dumb line.




We can use an Extract algorithm here, using the new "Lines_fid" field values to the match.

Instead of that, what we do is add a Field Calculator algorithm to create a new field in the Joined ouput, populated with the distance along dumb lines to each intersection, with the folowing expression:


round( 
line_locate_point(
geometry:= geometry( get_feature( 'Lines', 'fid', attribute( 'Lines_fid'))),
point:= intersection(
$geometry, geometry( get_feature( 'Lines', 'fid', attribute( 'Lines_fid'))))),
2)

3





Now, we add a new Field Calculator algorithm to the model, to aggregate, as an array, the "dist" values, grouped by the "Lines_fid" value and ordered by "dist" values growing. To that array, we find the index position of the "dist" value of the current feature, and return it as an order number to the new field, named "order", with the following expression:


array_find( array_agg( "dist", group_by:= "Lines_fid", order_by:= "dist"), "dist")


4


We named the output as "Ordered" to get the new layer when run the model.


5




So, let's run it to see what it do.


6


The output is a new 'Ordered' layer, with only the features that intersect any dumb line, with new "Lines_fid", "dist" and "order" fields. We label them with the concatenation of the values in all the fields, with the expression: fid || '-' || Lines_fid || '-' || dist || '-' || order.


7



Here we can check if distances along dumb lines are correctly calculated and if the ordering is working fine.




Finally, this is the result, with just 'Ordered' layer labeled with "order" field values.


8


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...