TransWikia.com

Polygon label parallel to line but inside polygon

Geographic Information Systems Asked by TJ Rockefeller on August 25, 2021

I am trying to add labels to a polygon layer hosted on GeoServer with CSS styling, and I would like the labels to be on the boundary running parallel, but always inside of the polygon.

For instance if there is a horizontal boundary, I want the label for the top polygon to be above the line, and the label for the bottom polygon to be below the line.

I am using the CSS plugin for GeoServer, and using this styling

[@sd >=0] [@sd <= 1000000] {
  font-family: "DejaVu Sans Book";
  font-size: 18;
  font-fill: #C3282D;
  label: [DIST_NM];
  label-anchor: 0.5 0.5;
  label-offset: -10;
  label-max-displacement: 50;
  halo-radius: 1;
  halo-color: white;
}

but the label offset of -10 appears to be relative to the line (above or below) and not the polygon (inside or outside).

In the scenario described above with this styling, the labels for the top polygon and the bottom polygon are both drawn below the line, so it is unclear what polygon the label belongs to.

For additional context, I was able to get this kind of styling with mapnik using this kind of rule

<Rule>
    <MaxScaleDenominator>1000000</MaxScaleDenominator>
    <MinScaleDenominator>0</MinScaleDenominator>
    <TextSymbolizer dy="-5" size="18" face-name="DejaVu Sans Book" spacing="500" placement="line" vertical-alignment="top" fill="#C3282D" halo-fill="white" halo-radius="1" >[DIST_NM]</TextSymbolizer>
</Rule>

One Answer

You can get something pretty close using a negative buffer on the boundary of the polygon. Here it is in SLD (as I don't have CSS installed currently):

    <TextSymbolizer>
        <Geometry>
            <ogc:Function name ="boundary">
              <ogc:Function name="buffer">
                <ogc:PropertyName>the_geom</ogc:PropertyName>
                <ogc:Literal>-0.1</ogc:Literal>
                </ogc:Function>
              </ogc:Function>
        </Geometry>
        
        <Label>
          <ogc:PropertyName>STATE_NAME</ogc:PropertyName>
        </Label>
        <Font>
          <CssParameter name="font-family">Times New Roman</CssParameter>
          <CssParameter name="font-style">Normal</CssParameter>
          <CssParameter name="font-size">14</CssParameter>
        </Font>
        <LabelPlacement>
          <LinePlacement/>
        </LabelPlacement>
        <VendorOption name="followLine">true</VendorOption>
        <VendorOption name="repeat">100</VendorOption>
      </TextSymbolizer>

enter image description here

In css it will look something like (this comes from this closed question):

*{
  fill: #E30613 ;
  fill-opacity: 0.2;

  font-family: "SansSerif.plain";
  font-fill: #194383;
  font-size: 20;

  label-geometry: [boundary(buffer(GEOM, -0.01))];
  stroke-geometry: [GEOM],[boundary(buffer(GEOM, -0.01))];

  label: 'Really very very very long label';
  label-follow-line: true;
  label-max-angle-delta: 180;
  label-remove-overlaps: true;
  label-conflict-resolution: true;
  label-allow-overruns: false;

  stroke: #194383, black;
  stroke-width: 2, 0.1;
}

Correct answer by Ian Turton on August 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP