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

BERA
  • 72,339
  • 13
  • 72
  • 161
mrakoplas
  • 111
  • 8

1 Answers1

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

BERA
  • 72,339
  • 13
  • 72
  • 161
  • 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 '20 at 11:20
  • I added a screenshot to my initial question – mrakoplas Sep 29 '20 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 '20 at 11:25
  • Yes, I did exactly this. No error at all. I tried several times with max length for string (manually) or using format 'unlimited string' – mrakoplas Sep 29 '20 at 11:32
  • 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 '20 at 11:47
  • just out of curiosity, did you write that python code from scratch or were you using it before somewhere? – mrakoplas Sep 29 '20 at 12:30
  • 1
    From scratch. I knew which techniques to use to manipulate the string from doing similar tasks many times before. – BERA Sep 29 '20 at 12:32