I am wondering if there is a way to display coordinates for each vertex of a polygon by only using expressions, without creating a new layer. So this question is basically inspired by Displaying vertex coordinates of a polygon on a map in QGIS and enhancing it.
I thought about creating an array containing all vertices as geometry. So I have created this expression:
array_foreach( -- Go throuth every element of the following array
string_to_array(geom_to_wkt(nodes_to_points($geometry)),','), -- Create the array of WKT information splitted by comma [ 'MultiPoint ((0.2974026 51.8987013)', '(0.00909091 51.50… ]
make_point( -- Create a point geometry for every array content
to_real(if(
length(regexp_substr( -- Only create X coordinate if
regexp_substr(@element,'\\d.*\\d.[0-9]'),'([^ ]+)'))>0, -- everything between the Brackets of 'MultiPoint ((0.2974026 51.8987013)' before the whitespace '0.2974026' is not empty like ''
to_real(regexp_substr(regexp_substr(@element,'\\d.*\\d.[0-9]'),'([^ ]+)')), -- Extract the X coordinate of 'MultiPoint ((0.2974026 51.8987013)' --> 0.2974026
NULL -- If invalid coordinate use NULL, maybe replace with 0
)),
to_real(if(
length(regexp_substr( -- Only create Y coordinate if
regexp_substr(@element,'\\d.*\\d.[0-9]'),'(?<=\\s).*'))>0, -- everything between the Brackets of 'MultiPoint ((0.2974026 51.8987013)' after the whitespace '51.8987013' is not empty like ''
to_real(regexp_substr(regexp_substr(@element,'\\d.*\\d.[0-9]'),'(?<=\\s).*')), -- Extract the Y coordinate of 'MultiPoint ((0.2974026 51.8987013)' --> 51.8987013
NULL -- If invalid coordinate use NULL, maybe replace with 0
))
)
)
Which returns an array like [ <geometry: Point>, <geometry: Point>, <geometry: Point>, <geometry: Point>, ... ]
, which represents all the vertex of the polygon. One can easily extract X and Y coordinates out of it by adding x()
or y()
in front of make_point()
, or by just adding another array_foreach()
like array_foreach(*expression above*,x(@element))
.
How could I use such an array to label every vertex with its coordinates? I thought of extracting them from the array, but cant iterate over it. Would this be possible? Or something similar? I am kind of stuck now...
nodes_to_points($geometry)
) but can't have vertex coordinates.