4

I have a sheet with the column with polygon information in WKT format. However, the data taken are from web mapping (OSM or google maps) and in the order of lat, lng. When I am using the solution from this thread (Visualising WKT geometry string in QGIS) which are what I need, I do not have another choice to switch x and y other than manually. But concerning the number of data, I need to find something more efficient.

Basically my polygon data is in the format

55.947686 -3.132133, 55.945932 -3.131232, 55.947878 -3.121362, 55.943168 -3.117714, 55.943841 -3.108273, 55.94771 -3.108745, 55.950161 -3.119216

but I need -

-3.132133 55.947686, -3.131232 55.945932, ....

There are so many items in the dataset so manual transfer is not an option.

Is there a tool somewhere (even outside QGIS environment) to do it for me?

Screenshot for an answer from BERA: enter image description here

1
  • 1
    Try swapping your coordinates, e.g. in QGIS there is a Plugin namely Swap XY
    – Taras
    Sep 29, 2020 at 10:57

1 Answer 1

7

You can flip them using Python:

import re

layer = iface.activeLayer() #Click layer in table of contents
unflipped_field = 'wktstring' #Change
flipped_field = 'wktflipped' #Change. This text field will be calculated

with edit(layer):
    for f in layer.getFeatures(): #For each row in the attribute table
        oldval = f[unflipped_field] #Fetch unflipped wkt string
        templist = re.findall(r"[-+]?\d*\.\d+|\d+", oldval) #Extract digits from string (https://stackoverflow.com/questions/4703390/how-to-extract-a-floating-number-from-a-string)
        templist = ['{0} {1}'.format(v2,v1) for (v1,v2) in zip(templist[::2], templist[1::2])] #Swap first and second val for all coordinate pairs
        newval = ', '.join(templist) #Join list back to a string
        f[flipped_field] = newval #Update new field
        layer.updateFeature(f)

enter image description here

7
  • That seems like the solution for what exactly I need. However I am running the code, it seems to run smoothly however nothing is filed up in my 'wktflipped' attribute, not sure why
    – mrakoplas
    Sep 29, 2020 at 11:20
  • I added a screenshot to my initial question
    – mrakoplas
    Sep 29, 2020 at 11:20
  • 1
    You get no error? You have added the field wktflipped as type text with a length of at least the length of wktstring field?
    – BERA
    Sep 29, 2020 at 11:25
  • 1
    Actually, I was only using one polygon, for now, to try the feasibility so I created it using QuickWKT plugin. Anyway, I tried again and it is now working. Thank you greatly! (maybe the reason why it was not working) was because I did not specify the length for the wktstring initially (but then filled it up manually)
    – mrakoplas
    Sep 29, 2020 at 11:47
  • 1
    From scratch. I knew which techniques to use to manipulate the string from doing similar tasks many times before.
    – BERA
    Sep 29, 2020 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.