4

I have a polygon shapefile which I have imported into QGIS, however it does not display X Y coordinates. Is there a way that I can identify the coordinate for the centroid of the polygon and display this in the attribute table?

  • 2
    It's certainly possible, but first you need to identify the meaning of "centroid", since there are several options, and not all of them are "inside" the polygon. – Vince Nov 23 '16 at 12:00
6

The QGIS field calculator can do it with these built-in functions:

 x($geometry)
 y($geometry)

Make sure to create a new field of type real, with an appropriate accuracy. You can even use a virtual field to avoid changing the datasource.

  • @inc42, the expressions x(centroid($geometry)) and y(centroid($geometry)) were the right answer. – Gabriel De Luca Jan 27 at 20:03
  • @GabrielDeLuca the x() and y() functions return the x resp. y values of the centroid of non-point geometries. No need for calculating the centroid twice. – inc42 Jan 27 at 20:06
  • @inc42, In that case, seems to me better to include that information and new expressions, or a new answer. – Gabriel De Luca Jan 27 at 20:08
  • It would be great to add details to the answer. – inc42 Jan 27 at 21:46

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.