[GIS] Why is field calculator returning NULL for $x and $y in line and polygon layers

coordinatesfield-calculatorpolygonqgisxyz

I am trying to include XY coordinates in the attribute tables of several of my vector files; they are line and polygon files. I have tried two methods; first I used $x and $y in the field calculator (which I've used before and worked well). When this returned NULL, I tried saving the file as a .CSV and using the methods described in this post:

Get list of coordinates for points in a layer?
(top answer describes using "GEOMETRY=AS_XY" in the 'save as' window).

Neither of these methods are working; they aren't returning errors, they just aren't returning values. I have tried re-importing my shapefiles, I have changed the CRS they occupy to try and force QGIS to recognize the location with no luck.

I'm using Windows 7, QGIS 1.8.0 Lisboa.

Best Answer

The $x and $y values only work for point layers.

Create a layer for polygon centroids and then that will give the x and y values using the $x and $y commands.

Related Question