58

I've got a layer in QGIS that is comprised of points. Its attribute table doesn't have (show) the corresponding x and y coordinates for each point. Is there a way for me to extract a list of coordinates for those points? (ideally into a CSV, but not required)

Also, for the exported list of coordinates (say, in a CSV file), how do I include the existing attribute table with it?

For your information, I am running QGIS on Mac OS X 10.6.7.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
hpy
  • 3,157
  • 13
  • 43
  • 43
  • 1
    Perhaps this is a duplicate of this question: http://gis.stackexchange.com/questions/7199/how-do-i-calculate-the-latitude-and-longitude-of-points-using-qgis – djq Apr 22 '11 at 15:42
  • 1
    You might be interested in my answer in another thread http://gis.stackexchange.com/questions/1721/which-gis-support-dxf-and-or-dwg – Willy Mar 28 '12 at 08:53
  • You could also use the MMQGIS plugin to export the geometry with the rest of the layer attributes to CSV file. – moutsinasg Jun 27 '17 at 09:59
  • Qgis has gdal, so you can use, from a command window, ogrinfo to do this, like 'ogrinfo -sql -dialect sqlite "select id,st_astext(geometry) from myshapefile" myshapefile.shp', where the single quotes should not be used but delimit the command. Other types of featureclass can be used, but need appropriate mods to the select, a connection string when using a db, etc. Nice is the ability to use sql on non-sql sources like shapefiles. – Jan Sep 21 '23 at 11:03

7 Answers7

58

Depending on your version, you should be able to right-click on the layer you want to export, and select Save As... Choose Comma Separated Value from the dropdown list of the dialog box that appears. Then, in the Layers part of the OGR creation options, type GEOMETRY=AS_XY or GEOMETRY=AS_XYZ depending on the dimensionality of your source data.

QGIS Save vector layer as... dialog

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
MerseyViking
  • 14,543
  • 1
  • 41
  • 75
  • That's great and it works! One more thing: Can I also export columns I already have in the original layer's into the CSV, along with the X, Y coordinated? Thanks. – hpy Apr 22 '11 at 16:34
  • 2
    Cool, glad it works! Make sure you have the "Skip attribute creation" checkbox unchecked. I'm not a fan of negative checkboxes like that, it always makes me think twice about what state I need it in. – MerseyViking Apr 22 '11 at 20:28
  • Very useful tips! – Heinz Jun 11 '13 at 09:27
  • For anyone else on Windows 10 (QGIS 4.3.1) confused about where this gets saved, check your "C:\OSGeo4W64\bin" folder. – Wassadamo Jan 11 '19 at 21:03
  • Note that if your geometry has somehow become a multipart geometry, GEOMETRY=AS_XY will silently fail. You can can fix this by processing the layer with 'Multipart to singleparts' – daviewales Aug 11 '21 at 06:12
  • This only works for point features. It doesn't work for polygons or line features. – Faustin Gashakamba Mar 28 '23 at 12:54
31

In the Field calculator in the Attribute Table you may use $x and $y to calculate your coordinates into a new attribute.

For that, open the Attribute Table, click on the Field calculator (top arrow) and enter $x or/and $y (middle arrow) in the expression pane. A preview of your newly created output is shown below (bottom arrow).

enter image description here

andschar
  • 1,255
  • 10
  • 13
mmartimo
  • 419
  • 4
  • 2
  • Is it possible to get the X Y in different coordinate system? I mean the XY of my points is in WGS 84 but I would like to list the XY values in different coordinate system in my attribute table. – Petr Nov 27 '19 at 13:46
  • this works, but note that you have a wrong column label in this example...$x is longitude and $y is latitude. – Luis Lizcano Aug 11 '23 at 15:02
9

You may like this for the added detail which is gathered

For QGIS 1.7 on Win7 I open the attribute table for the layer, use the buttons to; invert selection to select all rows, copy to clipboard

and paste into Excel. The first column contains Well Known Text information which maybe more useful to you.

Edit

I now use the plugin mmqgis to export geometry to csv, it is more desired by CAD users at the other end...

Kirk Kuykendall
  • 25,787
  • 8
  • 65
  • 153
BWill
  • 1,772
  • 1
  • 16
  • 27
  • BWill, should we merge your other account @Willy? That would help your reputation and simplify your interactions here. – whuber Apr 13 '12 at 14:42
  • Restored original edit. – Kirk Kuykendall Apr 16 '12 at 14:33
  • When copying from the attribute table and pasting into Excel, sometimes a feature will occupy multiple rows, and this results in overwriting any data in the row below. – csk Feb 26 '18 at 19:30
4

Virtual layers enables you to do exactly that. It is an icon on the Layers Toolbar or you can go Layer->Add layer->Add/Edit Virtual Layer...` Then you will face a window like the one below:

enter image description here

In the Query you can select geometry properties like x and y cooridnates and attributes values from the Attribute Table, like the id in the example.

Once you have a table with the desired fields, you can use save asand select csv as a format.

enter image description here

The underlying engine uses SQLite and Spatialite to operate, so the documentation for the queries could be found here: https://www.gaia-gis.it/fossil/libspatialite/wiki?name=misc-docs

Marco
  • 3,230
  • 14
  • 37
3

For your question if you have postgressql and built-in postgis you can do this adding new column which is named coord_x and coord_y. And sql to update this column is:

UPDATE table_name
      SET coord_x = X(the_geom),coord_y = Y(the_geom);

That is it.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
urcm
  • 22,533
  • 4
  • 57
  • 109
  • Do you think you can help me with this question https://gis.stackexchange.com/q/268888/36393? – Marco Jan 23 '18 at 10:30
3

I asked a similar question a few weeks ago as Calculating latitude and longitude of points using QGIS. It's only true for QGIS 1.7 though (which I'm running on OSX and it seems quite stable).

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
djq
  • 16,297
  • 31
  • 110
  • 182
0

How to add coordinates to attribute table
I was looking for an answer to the first part of the question as I just wanted to see the coordinates in QGIS. The information above helped me work out a solution so I am posting for other QGIS beginners. I am using Windows10 and QGIS 3.4

  1. Open Layer Properties
  2. Select Source Fields (initially only id existed)
    enter image description here
  3. Select Field Calculator
  4. Select Create new field, enter name and choose decimal number as Output field type
  5. Choose Geometry on lower right list

enter image description here
6. Select $x for x coodinate and save
enter image description here
7. Repeat by selecting $y for y coordinate and your coordinates will appear in the attribute table.

Roo
  • 143
  • 1
  • 10