1

I have a big project (whole country) and it contains smaller features (administrative units).

My project is in .shp.


How to export features' coordinates one by one or all at once as long as all features' coordinates are separate / distinctive by eye in order to copy-paste them separately?

I don't care how coordinates are formatted as long as I can easily copy-paste them without downloading some huge program in order to open exported file.


But if there's any change to control the output, my desired final format is this:

//             point 1                           point 2
[["lat"=>xx.xxxxx,"lng"=>yy.yyyyy],["lat"=>xx.xxxxx,"lng"=>yy.yyyyy],/*etc*/] 

Otherwise I could just program a JS function that formats the coordinates to my desired format.


All these features are on same layer:

enter image description here

0

right click on the layer you want to export and select save as and choose CSV (comma seperated value) then in the layers part of the OGR options type GEOMETRY=AS_XY

enter image description here

I am not sure how to export it to the exact format you want, but you can use excel to batch them into that format after the export.

also see:

How do I calculate the latitude and longitude of points using QGIS?

and

Using Qgis API and python, how can I return latitude and longitude of a point?

  • That's the problem, all features are on same layer.. – N00b Jan 19 '16 at 14:59
  • why is that a problem? They should all be in the same layer, all of these methods work for pulling the x,y from a layer. – ed.hank Jan 19 '16 at 15:00
  • I added an image to make myself more clear.. If I use your suggestion, will I get coordinates for all these separate features on my image? – N00b Jan 19 '16 at 15:04
  • Ok so first you need to select out only the selected feature you want. you can do this from the attribute table and the select options. see : docs.qgis.org/2.0/en/docs/user_manual/working_with_vector/… and russiansphinx.blogspot.com/2014/01/… and youtube.com/watch?v=ZbnCrfoWnNk ... first you will select only the feature you want, then you will export only that feature out, then do what i told you above – ed.hank Jan 19 '16 at 15:07
  • I have problems with saving coordinates - I do everything you say but it will only save feature names and code (basically attripute table content). – N00b Jan 19 '16 at 15:40
1

Make the layer you want to export the active layer, and paste following code into python console (or execute as script):

fn = 'E:\\test.txt'
f = open(fn, 'w')
for feat in layer.selectedFeatures():
    f.write('[')
    for p in feat.geometry().asPolygon()[0]:
        f.write('[\"lat\"=>{0:.5f}, \"lng\"=>{1:.5f}], '.format(p[0], p[1]))
    f.seek(-2, 2)
    f.write(']\n')
f.close()

It will write the coordinates of the outer shell of selected features to file. The output looks exactly as your desired format:

[["lat"=>-0.99581, "lng"=>-0.01990], ["lat"=>-1.00838, "lng"=>0.22723], ["lat"=>-0.89319, "lng"=>0.31937], ["lat"=>-0.35707, "lng"=>0.38639], ["lat"=>-0.36126, "lng"=>-0.09738], ["lat"=>-0.95393, "lng"=>-0.21675], ["lat"=>-1.12984, "lng"=>-0.14346], ["lat"=>-0.99581, "lng"=>-0.01990]] 
0

You could also create a new column via Field Calculator with the following expression:

'["lat"=>'||$y||', "lng"=>'||$x||'],'

Then, export your layer in CSV format (as indicated by @ed.hankins).

Open the CSV file with a text editor such as Notepad++ in which:

  • replace carriage returns by nothing in order to put all your coordinates on the same line
  • add start and end brackets to the string

And this is done!

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.