Tuesday, 17 December 2019

qgis - Geometry generator line from centroid to edge of irregular polygon


I have a point layer styled with the following geometry generator formula for an irregular polygon. It is the union of (a) a circular buffer with (b) a tree canopy spread buffered by 1m (where that extends beyond (a)).


 union(buffer($geometry,"tpzbsrad_m"),buffer(make_polygon(make_line(
translate($geometry,0,max("N",0.2)),
translate($geometry,0.6*max("E",0.2),0.6*max("N",0.2)),
translate($geometry,max("E",0.2),0),
translate($geometry,0.6*max("E",0.2),-0.6*max("S",0.2)),

translate($geometry,0,-max("S",0.2)),
translate($geometry,-0.6*max("W",0.2),-0.6*max("S",0.2)),
translate($geometry,-max("W",0.2),0),
translate($geometry,-0.6*max("W",0.2),0.6*max("N",0.2)),
translate($geometry,0,max("N",0.2)))),1))

I would like to use the geometry generator to draw a 45° line from the tree point ($geometry) to the edge of this irregular polygon.


For most cases the following suffices - a line to the edge of the circular buffer.


make_line(
$geometry, make_point($x + "tpzbsrad_m"*cos(radians(45)),

$y - ("tpzbsrad_m"*sin(radians(45)))
))

But where the tree canopy extends beyond the buffer in that area it doesn't match up. See image below:


enter image description here


What expression can I use to get the line to be long enough to always touch the edge of the irregular polygon at 45°? shortest_line() results in angles all over the place, naturally.


(Looking for an expression that ideally works in QGIS 2.18 and 3.x)



Answer



Thanks for sharing your great work!


[Option 1]



An idea presented below would require:



  • The irregular (sky-blue) polygon is saved as a vector layer ( your_polygon)

  • refFunctions plugin (for geomwithin() function)


Then the expression is:


intersection(
make_line(
$geometry, make_point($x + 2*"tpzbsrad_m"*cos(radians(45)),
$y - 2*("tpzbsrad_m"*sin(radians(45)))

)),
geom_from_wkt(geomwithin('your_polygon', '$geometry'))
)

(A multiplier factor 2 to your "tpzbsrad_m" has no meaning but it ensures the line is extended long enough before it gets cut at the intersecting polygon boundary).




[Option 2] without refFunctions plugin:


Instead of using refFunctions plugin, there is an additional step to link your tree (point) and polygon.



  • Join by Location to have identical id fields in both of your layers (point and polygon).



Now revised expression is:


intersection(
make_line(
$geometry, make_point($x + 2*"tpzbsrad_m"*cos(radians(45)),
$y - 2*("tpzbsrad_m"*sin(radians(45)))
)),
geometry(get_feature('your_polygon', 'id', "id"))
)


This expression uses get_feature() function which links 'id' field from the polygon (your_polygon) and the "id" field in the point layer (your tree locations).


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...