44

I have a polygon vector and I want to quickly attain the centroid of this polygon for insertion in a text document. I am working with QGIS. I searched through other questions and although some are somewhat close they don't quite answer my question.

Ideally, the centroid coordinates would be in the attribute table of the polygon and I could just copy and paste the coordinates.

I don't want to have to create a separate point vector file that represents the centroid and then find these coordinates and copy and paste them.

2
  • What format is your text document?
    – Nathan W
    Jan 6, 2013 at 22:21
  • It is a MS word document but really it could be anything.
    – David
    Jan 10, 2013 at 16:05

10 Answers 10

47

A centroid is per definition a point layer and not a polygon. Therefor you need to create a new layer, which is easy as pie in QGIS 1.8 and higher versions.

  • Load in your polygon
  • Go to the menu -> Vector -> Geometry tools -> polygon centroid and create a centroid point layer
  • Export the coordinates of the created centroid to the attribute table by clicking on -> vector Menu -> Geometry tools -> Export/Add geometry columns.

Afterwards you could make a spatial join to add the centroids columns to the polygons and delete the point layer again.

2
  • Thanks for the reply I appreciate the help. Still, this is a lot of work simple to know what the coordinates of the centroid is. Too bad there isn't a simpler way in QGIS.
    – David
    Jan 5, 2013 at 20:26
  • If you still want it easier you might take a look at PostGIS and the ST_Centroid function mentioned below. This way you don't need to create temporary files. Alternatively you could write yourself a python script for QGIS or use the SEXTANTE Modeler.
    – Curlew
    Jan 5, 2013 at 22:09
15

A more robust soulution to mike's answer: long = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*')) lat = toreal(regexp_substr(geom_to_wkt(centroid($geometry)), '-?\\d+\\.?\\d* (-?\\d+\\.?\\d*)'))

1
11

Really, making a new shapefile is one of your easiest options. However, you don't need to copy and paste coordinates. Do this:

  • Make your centroids file using the Polygon Centroids tool.
  • Open the centroids attribute table and make it editable (pencil icon at the bottom).
  • Open the field calculator (calculator icon at the bottom) and choose the Create New Field option and call it something meaningful (e.g. Easting).
  • From the functions List expand the Geometry menu and choose $x then repeat with a new column for $y (calling it Northing, say).
  • Save your edits and now you have all the coordinates as part of the attribute table complete with the original polygon ID and attributes if you used the centroids tool.

Alternatively you will have to import your data into PostGIS and then iterate over the polygons, using the ST_Centroid function and store the result in a new field. The work involved is about the same but the second option means you don't have a separate centroids point file.

1
  • Thanks for the answer. I appreciate your help. It seems like Curlew's answer is a bit easier for me. Still both of these answers are a lot more work that I wanted. In the end you end up with various shapefiles that you have to name, delete, edit, etc.
    – David
    Jan 5, 2013 at 20:28
11

I just came across this post nearly 5 years late (!), but here's what I do to calculate polygon centroids at version 2.18.14:

  1. Begin editing the polygon layer
  2. Create a new text field, call it centroidxy
  3. Using the field calculator, calculate centroidxy = x($geometry) || ' , ' || y($geometry)
  4. The result looks something like: 397640.915545362 , 2126924.53637653
  5. Save your edits.
  6. Your polygon layer now contains a field with x,y centroid values
2
  • simple and superb
    – Kazuhito
    Dec 9, 2017 at 6:19
  • 1
    1. I was wondering if that answer was already here. 2. I would make two new columns with separate x and y, but that's personal choice. It's just as copyable to excel when it's in two cols. 3. No matter what your map units are, you're calculating the coordinates of the centroid molecule or atom with that many decimals. Two or three are usually more than enough. :D
    – thymaro
    Apr 27, 2020 at 21:59
8

How about using this in the 2.2 Field Calculator?

Long field = substr(geomToWKT( centroid( $geometry )), 7, 12)

Lat field = substr(geomToWKT( centroid( $geometry )), strpos(geomToWKT( centroid( $geometry )), ' ')+2, 12)

Seems this is addressed in 2.6 with the xmin option xmin(centroid( $geometry ))

5

Came across this post and was hard to follow, suggested update:

Note: This will calculate the latitude and longitude in your projection coordinates (which may be meters). If you want the coordinates in decimal degrees (with I assume you do or they are mostly unintelligible) first save the layer in this CRS: EPSG: 4326, WGS 84.

  1. Open field calculator
  2. Create new field
  3. Enter name (either Latitude or Longitude)
  4. Select decimal as output field type
  5. Enter this expression for latitude: y($geometry) and this expression for Longitude: x($geometry)
  6. Press OK
3

Associate the centroid points table with the table of the polygons and then out put the coordinates with the polygon name and shape. Polygons don't have one lat long in and of themselves

1
  • Thanks for your answer. Yes I realize polygons don't have one lat long in and of themselves it simply something that can be calculated. Unfortunately, it isn't easier than this in QGIS. Thanks again everyone for the answers. And so fast too. Thanks!
    – David
    Jan 5, 2013 at 20:30
0

A more robust solution to @mike's and @SzieberthAdam's answers:

lon

toreal(regexp_substr(geom_to_wkt(transform(centroid($geometry),@layer_crs,'EPSG:4326')),'(-?\\d+\\.?\\d*) -?\\d+\\.?\\d*'))

lat

toreal(regexp_substr(geom_to_wkt(transform(centroid($geometry),@layer_crs,'EPSG:4326')), '-?\\d+\\.?\\d* (-?\\d+\\.?\\d*)'))

0

You may want to use the Pole of inaccessibility for which there is a tool in QGIS:

enter image description here

This creates a separate Point point layer from your original polygon layer. Perhaps surprisingly, the attribute table for this layer is the same as your original layer but with only a dist_pole column added.

If you want to add the x and y (latitude and longitude) positions to the attribute table of your original polygon layer, first run the Add X/Y field to layer on your new point layer:

enter image description here

This helpfully gives you new x and y coordinate columns which you wanted in a new Added fields point layer. To take these columns back to your polygon layer, use Join attributes by field value and specify your polygon layer and the Added fields point layer:

enter image description here

2
  • Centroid ≠ Pole of inaccessibility
    – Taras
    Jul 7 at 9:22
  • @Taras yes strictly speaking you are correct. However I came across this name and solution after searching for centroid since that was the closest term I knew. I discovered that poles of inaccessibility are more practical than centroids since they work well with concave polygons.
    – MSC
    Jul 9 at 8:30
-2

lon = ToReal(regexp_substr(geomToWKT(centroid($geometry)), '([\s]')) lat = ToReal(regexp_substr(geomToWKT(centroid($geometry)), '\s[)]'))

1
  • 2
    This is no answer at all. Please visit the tour to get a better understanding how to answer a question...
    – Nightwatch
    Nov 20, 2017 at 13:57

Your Answer

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

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