3

I'm wanting to label polygons on a map that are above a certain area. These polygons are dervied from open streetmap and consist of either ways or multipart polygons ie If polygon is more than 160 sq m label it. I'm doing this using;

$area => 160

But nothing is being returned. The table has no specific area column but is comprised of polygons. On the fly transforms are enabled with all the source data in WGS 84 projected to a local national grid. The project CRS area is set to square metres as discussion in comments below.

What am I doing wrong?

  • 4
    Could also be projections, $area will be in the units of the CRS you are using. Also if you want bigger, then it is >= instead of <=, which is smaller. – HeikkiVesanto Apr 3 '17 at 12:14
  • Apologies there are no quotes that was me trying to use a back tic – mapping dom Apr 3 '17 at 12:20
  • 2
    I suspect it's just a typo, but you say you want to select for areas more than 160 square meters while your code selects for 1600. And as others have pointed out, you're using the less-than symbol (<) when you should be using greater-than (>). – Dan C Apr 3 '17 at 16:02
  • @Vesanto the CRS is definitely part of the puzzle if i select the feature i want to label it tells me that the area is 79 hectares but the CRS says its calculating areas in square metres. Even if i set CRS units to match those in the table it still doesnt work ie. no label added – mapping dom Apr 5 '17 at 7:09
  • You should label it with just $area, that will give you an idea of the units it is using. – HeikkiVesanto Apr 5 '17 at 8:11
7

Right-click on the layer from the Layers Panel and then go to Layer Properties > Labels and choose the label you want to use from the Label with drop-down list (after having selected the Show labels for this layer option).

Then, go to Expression Builder and try to use a CASE WHEN expression. Something like this:

CASE
WHEN
$area > 1600
THEN
"field_name_for_the_label"
END

In this way, the label will only appear if the area is larger than 1600 .

I don't know why you want to use a rule-based labeling (there are probably other rules that you didn't explain), but the same expression should work also for that case.

EDIT

As suggested from @GuiOm Clair, another way is using an if statement:

if( $area > 1600,  "field_name_for_the_label" , None)
  • 1
    Another way is to use an if statement. – GuiOm Clair Apr 3 '17 at 13:50
  • 1
    @GuiOmClair thanks for the hint! I hope your idea was what I reported in my edited answer. – mgri Apr 3 '17 at 14:01
  • 1
    This is absolutely what I was refering to. Thanks for the edit. – GuiOm Clair Apr 4 '17 at 6:39
  • Thankyou @mgri The expression works but still get errors as above – mapping dom Apr 5 '17 at 7:18
  • @mappingdom That's weird... Are you using layers with projected CRS? Setting the CRS is not enough for having it in a projected CRS. Starting from your layers in WGS84, it would be enough right-clicking on it, then going to 'Save as' and choosing a projected CRS. – mgri Apr 5 '17 at 11:41
3

You could try adding a new field in the table, with the area calculation and use that field instead of '$area' to give the labeling rule. Does it works?

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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.