TransWikia.com

Centroid of multiple polygons based on value in attribute table

Geographic Information Systems Asked by Rogier on December 17, 2020

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.

One Answer

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.

Correct answer by geozelot on December 17, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP