3

Is there an expression in QGIS for array for_each to get the element from the output of geom_to_wkt($geometry) of the polygon and correspondingly transform the element (using the expression of LAT LONG Tool) from decdeg to ddmmss? i.e. array input:

51.06993546 25.58704192, 51.13739268 25.60501474, 51.15957073 25.54595078

desired output:

253513.35N 0510411.77E -
253618.05N 0510814.61E -
253245.42N 0510934.45E.
5

Edit

New expression for your desired output in degrees, minutes, seconds but without delimiter symbols (older solution below).

What you want to do indeed need installation of Lat Lon Tools plugin: this adds a new funtion to the expression editor: ddmmss. If you have a geometry with vertices 51.06993546 25.58704192, 51.13739268 25.60501474, 51.15957073 25.54595078, you can use the follwoging expression to get this output (coordinates for all vertices, from first to last) in string format, including line breaks. Simply switch xy to yx to change between lon/lat and lat/lon:

253513.35N 0510411.77E
253618.05N 0510814.61E
253245.42N 0510934.45E
replace (
    array_to_string (
        array_foreach (
            generate_series (1, num_points( $geometry)-1),
            right (ddmmss (y(point_n ($geometry,@element)), x (point_n ($geometry,@element)),'xy',2), 10)  || ' ' || 
            left (ddmmss (y(point_n ($geometry,@element)), x (point_n ($geometry,@element)),'xy',2), 11)  || '\n'
    )),
    ',',''
)

First solution

Use the function to_dms to convert coordinates to degree, minute, second format. If you have a geometry with vertices 51.06993546 25.58704192, 51.13739268 25.60501474, 51.15957073 25.54595078, you can use the follwoging expression to get this output (coordinates for all vertices, from first to last) in string format:

51°4′11.77″N 25°35′13.35″E,51°8′14.61″N 25°36′18.05″E,51°9′34.45″N 25°32′45.42″E

The expression:

array_to_string (
    array_foreach (
        generate_series (1, num_points( $geometry)-1),
        to_dms (x(point_n( $geometry,@element)),'y',2, 'suffix')  || ' ' || 
        to_dms(y(point_n( $geometry,@element)),'x',2, 'suffix') 
))

Variants:

  • To get a line break after each coordinates pair, add || '\n' at the end of the second last line.
  • To delete commas from the output, add replace ( at the very beginning of the expression and ,',','') at the very end.
replace (
    array_to_string (
        array_foreach (
            generate_series (1, num_points( $geometry)-1),
            to_dms (x(point_n( $geometry,@element)),'y',2, 'suffix')  || ' ' || 
            to_dms(y(point_n( $geometry,@element)),'x',2, 'suffix')  || '\n'
    )),
    ',',''
)

With this expression, you get the following output (including line breaks):

51°4′11.77″N 25°35′13.35″E
51°8′14.61″N 25°36′18.05″E
51°9′34.45″N 25°32′45.42″E
11
  • Thanks Babel for the reply! Just a follow up, is there a way to present it in LAT-LONG instead of LONG-LAT and without the symbols i.e. 253513.35N 0510411.77E? Sep 20 at 8:19
  • For lat/lon instead of lon/lat, simply change x and y in the expression (line 4 and 5). You should not use these values without the symbols as this is not correct as it looks like a numer with 6 decimals before and two after the point. If you still want to do it, use the second expression (the one including replace) and replace the second last line ( ',','' ) with this: array (',','°','′','″'),''
    – Babel
    Sep 20 at 8:28
  • Is there a way to use the Lat Lon Tools expression inside the array foreach? i.e. ddmmss(('y(point_n( $geometry,@element))' ,'x(point_n( $geometry,@element))', 'yx', 2, ' '), I can't seem to figure out how or it's not possible. Sep 20 at 9:04
  • What do you mean by "Lat Lon Tools" - this plugin? plugins.qgis.org/plugins/latlontools - You can't use a plugin inside an expression, if that is the question.
    – Babel
    Sep 20 at 9:16
  • You mean to say even though it has a corresponding expression in the field calculator? it cannot be used? Thanks for the clarification. Sep 20 at 9:31
2

Based on Babel's initial response this is the expression that provides the required output. You will notice I used regexp_replace to remove the degrees. minutes and seconds part.

Also since I am assuming you are in a location where your longitude is less than 100 you can hardcode the 0 as the lead, if not you will need to add a case statement to check if is less or more and apply the according format.

In your case you do not want to use 'suffix' but 'aligned' in order to get leading zero/padding for the minutes and seconds

regexp_replace(replace ( array_to_string ( array_foreach ( generate_series (1, num_points( $geometry)-1), to_dms (y(point_n( $geometry,@element)),'y',2, 'aligned') || ' 0' || to_dms(x(point_n( $geometry,@element)),'x',2, 'aligned') || '\n' )), ',','- ' ),'°|′|″','')

This is how it looks in the table

enter image description here

I recommend watching this video https://www.youtube.com/watch?v=8rOe-9A84O8 if you may want to implement what you are doing into an action as I am guessing this is not really for the attribute table that you need it

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.