1

I have shapes with attribute BLUE. I have shapes with attribute ORANGE.

The attribute table has two entries - BLUE, ORANGE - even though there are more isolated shapes ( islands in fact).

I wish to find the centroid of the dominant (largest by area) shape BLUE and the centroid of the dominant shape ORANGE. Red DOTS in image.

I suspect there must be a way to ignore the non-dominant island shapes and then find centroid of dominate shape.

The purpose is that I want to determine LAT-LONG of the centre of shape in order to place Capital name there.

enter image description here

6

There is an easier way, without any geoprocessing, just using the expression editor:

centroid(coalesce( order_parts( $geometry, area($geometry), False)))

with order_parts, you order the parts of every (multipart-)geometry ($geometry) according to their area-values (area($geometry)) and sort it descending (paramenter: False). With coalesce, you select the first, thus (after ordering them before) the biggest part of every multi-geometry. You now generate the centroids of this "biggest island" with centroid().

Paste this expression in the labeling dialogue in the positioning tab, select around center or distance from center and set data-driven override for centroid: here you past the expression above. The red dots in the screenshot are generated just for demonstration with an additional geometry-generated points-geoemtry in the styling dialogue, using exatly the same expression.

generate centroid of the biggest geometry-part with expression-editor

  • Brilliant-! Now I just need to get the latitude and longitude of the centroids. How does one export Lat and Long ? – Michel Sep 13 '19 at 21:14
  • That's very simple, just add x() and y() for x- and y-value (lat./long.): x(centroid(coalesce( order_parts( $geometry, area($geometry), False)))) and y(centroid(coalesce( order_parts( $geometry, area($geometry), False)))) – babel Sep 13 '19 at 21:19
  • But why do you need lat./long.? If you want to position your label at this point, you don't need that - you just define the centroid as showed on the screenshot and your label will be anchored to this point, without any need for you to know lat./lon. – babel Sep 13 '19 at 21:21
  • I require the LAT-LONG to export to CSV/EXCEL. The resulting file is to be used in a dashboard S/W similar to Tableau. – Michel Sep 16 '19 at 16:47
1

It looks like you have multi-part geometries so each record in the data table can refer to one or more shapes. Use the multi-part to single part tools to convert those data so single part features.

Next, use menu -> Vector -> Geometry tools -> polygon centroid to create the polygon centroid.

You may find that some of your islands have geometries such that the point created is not bound within the island polygon. If that is the case then consider this link to a plugin to ensure the point is within the polygon.

Of course, these tools will create points for all the islands. You can either select your dominate shapes before running these tools so that only points are created for the highlighted shapes or you could use the selection tools to find the point in the largest island by name (but that answer should be another post).

  • Hi.. Thank you for the quick response... However... I have hundreds of islands and manual selection would be impossible. So I need to sort shapes by color, find area of each isolated island for each colour, pick island with the largest area in said colour. Then place centroid on bigest island. – Michel Sep 13 '19 at 18:30
1

It sounds like you have multipart polygons.

  1. Use the tool multipart to singlepart to split the parts into separate features.
  2. Select the two large polygons.
  3. Use the centroids tool, with the "selected features only" option, to find the centroid of the selected polygons.

Update: based on the new information that you have hundreds of multipolygons, rather than only two as originally stated, manually selecting the large polygons in step 2 will be impractical. Instead, use select by expression to select the larger polygons with an expression like $area > area_value. Substitute an appropriate area value for the area_value placeholder term.

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.