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.

3 Answers 3

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

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).

1
  • @MaFo - Most welcome, glad it was helpful :)
    – Joseph
    Mar 29, 2017 at 11:43
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:

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.