3

I have polygon layer which consists of many circles with different radius (see below) and I want to convert it to centroids and write new attribute into attribute table of centroids with value of radius.

enter image description here

EDIT: I would rather prefer not using calculation from circle area as I would like to extend the model later to use it with irregular polygons. So I am more looking for some kind of function which could work also for other geometries (not only for circles).

So far I've tried v.distance and other build-in functions I've also tried Field Calculator (but it obviously doesn't work for two separated layers).

8
  • 1
    Well, what is the connection between circle area and radius?
    – Erik
    Commented Mar 19, 2021 at 11:51
  • I'm not sure if I completely understand your question, so sorry if I'll answer something else. But I want to calculate the distance from centroid to boundary of polygon - and as the polygon is circle, then the distance equals radius and is same in every direction.
    – daky
    Commented Mar 19, 2021 at 11:55
  • 1
    So will post the answer as comment.... run Execute SQL or Virtual Layer with a query like this: select distance(centroid(geometry),point_n(geometry,1)) as radius, centroid(geometry) as geometry from input1
    – MrXsquared
    Commented Mar 19, 2021 at 12:05
  • 1
    The trigonometry here is pretty basic. If the circular polygons have a regular vertex density, the zeroth and (n-1)/2 vertex should be opposites, and the midpoint of that is the center (and half that segment the radius). Or you can simply average the Xs and Ys all but the closing vertex. This task could be complicated if the spatial reference is not projected. Please Edit the question to include your coding attempt, or the question is likely to be closed.
    – Vince
    Commented Mar 19, 2021 at 12:06

1 Answer 1

4

To calculate the distance from a centroid of any kind of (regular or irregular) polygon to the closest point on the outer boundaray of that polygon (this is what you want to get, right?), you can use this expression on the polygon layer:

length (
    make_line (
        centroid ($geometry), 
        closest_point (
            boundary ($geometry), 
            centroid ($geometry)
        )
    )
)

Screenshot: here, I used the expression from above as a label (and added a round() function to get rid of many decimals). The red line, created dynamically with geometry generator with an expression based on the one above, shows the line from the centroid to the closest point on the outer boundary of the polygon:

enter image description here

Edit: By the way, to have the answer to the question from the comments here: the following expression gives the length of the longest line inside the polygon, starting from the centroid:

make_line (
    centroid ($geometry),
    closest_point ( 
        boundary (
            minimal_circle( $geometry)
        ),
        $geometry
    )
)

See screenshot - the red line is created dynamically with this expression (minus the length part):

enter image description here

4
  • Thank you, it works perfectly. I was using shortest_line instead of closest_point and that was the problem. May I have one more further question please? Most of the time I'll use shortest distance, but sometimes I need the longest one - to the furthest point. But there is no expression like furthest_point when I search it in QGIS Documentation. Is there any easy way (just change one parameter) or it will be far more complicated? Thank you again.
    – daky
    Commented Mar 22, 2021 at 7:32
  • Spontaneously, I don't have an answer to the question about the longest distance. However, there have been similar questions here an GIS SE, search for someting like "longest distance QGIS" to see it meets your expectations. Otherwise, it would be worth posting this as a new question. As a first idea: in the case of the polygons above,the longest distance will be one from the centroid to one of the corners. So calculate the distance to each corner and keep the longest one. OK for simple shapes. For complex ones (countries) this might not be ideal. The line might leave the polygon: is this OK?
    – Babel
    Commented Mar 22, 2021 at 7:59
  • Thank you. Yes, it is absolutely helpful, I've already checked all old questions here on forum and any solution actually worked for me. I will post new question. Again, thank you a lot for you help.
    – daky
    Commented Mar 22, 2021 at 8:05
  • I feel bad now that I didn't invent it myself. Thanks again, it works great!
    – daky
    Commented Mar 22, 2021 at 9:27

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.