4

I managed to import a csv file containing lines in QGIS but unfortunately the x y aren't in the correct order. that is, the csv has a field where there is a sequence of lat (y), lon (x), while qgis apparently wants x,y. I become aware of this problem because my lines are plotted in a place which isn't my area under study. What can I do?

ID;geometry;name;definition;type;ID_linea
3;2;"LINESTRING(46.155628 13.17198, 46.155283 13.172648)";Ippovia Cormor in Comune di Tricesimo;qua1;1

(That should be somewhere in italy)

3
  • Where do you see it? Near Yemen or near Nigeria?
    – JGH
    Commented Feb 20, 2019 at 17:14
  • Yes its in Yemen
    – Geo_it
    Commented Feb 20, 2019 at 17:27
  • 2
    Accept one of the answers if he helped you with your question ... Commented Mar 8, 2019 at 18:19

3 Answers 3

9

Since you have successfully imported data, use the tool presented in the figure. QGIS 3.4

enter image description here

3

You can create a virtual layer that swaps the X-Y coordinates.

Go to the menu Layer / add layer / add-edit virtual layer.

Using true field and layer name, type a new query similar to:

select myID, myfieldABC, SwapCoordinates(geometry)
from a;

You can then either export this new layer to another format, or use it directly

3

Not a neat solution, but try:

  1. Load the CSV file as a plain text table;
  2. Use regex to rewrite the linestrings;
  3. Save the CSV file again, then load it as a vector layer using the corrected geometry.

The function (field calculator) to find pairs of coordinates and swap them in the linestrings would be:

 regexp_replace( geometry, '(\\d+\\.\\d+) (\\d+\\.\\d+)', '\\2 \\1' )

You could use this to alter the geometry column in place ('update existing field'), or create a new field which you use when importing later.

In fact, you can find the CSV file in the QGIS browser and double-click to add it - it will load as a non-spatial table. Then use the field calculator to create a new text field wkt with the function above, then save your edits. If you double-click the file in the browser it will now load the new geometry from the wkt field.

2
  • unfortunately the number of coordinates inside the string is variable. Here it's two, in other records it's 3,4,5, ect. Thank you.
    – Geo_it
    Commented Feb 20, 2019 at 19:30
  • @Geo_it - Did you try it? The regex I provide works for any number of coordinate pairs, as long as they're decimals separated by a space (i.e. normal WKT).
    – Simbamangu
    Commented Feb 21, 2019 at 7:11

Your Answer

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

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