4

I have a (polygon) shapefile with multiple zones. Now, I would like to calculate the centroid of groups of these zones. The zones have to be grouped based on a value in the attribute table:

AreaNr  AreaName AreaChar 
   1      Amst       a
   2      Delf       a
   3      Zeis       b
   4      Apel       c
   5      Deve       c

Now, I would like to group them by their string value in 'AreaChar' and then calculate their centroid. What I would like to end up with is thus the 'AreaChar' value and an X and Y value for its centroid.

I usually use QGIS, but I am open to using other software if necessary.

2
  • After rewording my question, I quickly found that a 'dissolve' tool exists in qgis, which would, provided some extra steps are taken, do what I want.
    – Rogier
    Dec 30, 2019 at 15:00
  • The centroid of a multipart shape can sometimes be problematical. First there's the issue of "centroid" definition. Then there's the chance that the centroid won't be in any of the parts, and finally the complications of datelines and other arbitrary dividers, which could push the centroid into a different hemisphere. Additional clarity on your goal is necessary.
    – Vince
    Dec 30, 2019 at 15:13

1 Answer 1

5

Try the natively supported SpatiaLite SQL, either via Virtual Layer, or the DB Manager; run

SELECT "AreaChar",
       ST_Centroid(ST_Collect(geometry)) AS geometry
FROM   <your_layer>
GROUP BY
       "AreaChar"
;

to simply get the centroid as geometry, or

SELECT "AreaChar",
       ST_X(geometry) AS x,
       ST_Y(geometry) AS y,
       geometry
FROM   (
  SELECT "AreaChar",
         ST_Centroid(ST_Collect(geometry)) AS geometry
  FROM   <your_layer>
  GROUP BY
         "AreaChar"
) q;

to also retrieve the x and y values into separate columns.

Your Answer

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

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