6

Is there a possibility to get the centroid of each line?

enter image description here

When using Vector > Analysis Tools > Mean Coordinates I always get a centroid of the whole lines. What I need is the centroid of each line without having to select individual lines.

Taras
  • 32,823
  • 4
  • 66
  • 137
Ma Fo
  • 671
  • 7
  • 15

3 Answers3

9

In the Field Calculator, create a new String field and set the length to be something high like 99. Then use the expression:

geomToWKT(centroid($geometry))

This should give centroid coordinate values like Point (-4.28930643 55.86506837).

Taras
  • 32,823
  • 4
  • 66
  • 137
Joseph
  • 75,746
  • 7
  • 171
  • 282
9

In tool "Mean coordinate(s)" from the QGIS's Processing Toolbox (Ctrl+Alt+T) use Unique "ID" field.

enter image description here

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
tomalos
  • 191
  • 3
1

There is a possibility using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer....

Let's assume there is a polyline layer called 'lines_test2', see image below.

input

With the following query, it is possible * to get the centroid of each line*.

SELECT
    "id",
    st_centroid(geometry) AS geom
FROM
    "lines_test2"

The output point layer with its attribute table will look like:

result

However, keep in mind the difference between Centroid | Midpoint | Center, more details here: Getting the line middle point with SQL in QGIS.


References:

Taras
  • 32,823
  • 4
  • 66
  • 137