1

This is desired output:

enter image description here

I am trying to construct the point at the top of each polygon (visible part of polygon) and point label to it.

EDIT:

How to get Y coordinate of the top of the visible polygon? I tried to combine the getY functions with the env function and the predefined variable wms_bbox but I didn't get the bbox from each polygon, I get the bbox from the whole visible image.

Here is part of my code, there is no validation or preview errors.

The grid is shown on the map but no labels are displayed

        <TextSymbolizer>
        <Geometry>
          <ogc:Function name="geomFromWKT">
            <ogc:Function name="Concatenate">
              <ogc:Literal>POINT(</ogc:Literal>
              <ogc:Function name="getX">
                <ogc:Function name="centroid">
                  <ogc:PropertyName>geom</ogc:PropertyName>
                </ogc:Function>
              </ogc:Function>
              <ogc:Literal><![CDATA[ ]]></ogc:Literal>
              <ogc:Function name="getY">
                  <ogc:Function name="env">
                    <ogc:Literal>wms_bbox</ogc:Literal>
                    <ogc:PropertyName>geom</ogc:PropertyName>
                </ogc:Function>
              </ogc:Function>
              <ogc:Literal>)</ogc:Literal>
            </ogc:Function>
          </ogc:Function>
        </Geometry>

        <Label>
          <ogc:PropertyName>date</ogc:PropertyName>
        </Label>
2
  • 1
    You've said what your desired output is but you haven't indicated what problem you're having. Do you have a screenshot of what your current output is?
    – Fezter
    Feb 21, 2022 at 22:36
  • currently you are using effectively the centroid (but slower) - you need to change your Y coordinate to be the top of the visible polygon - you'll want to add 1/2 the polygon height or the distance to the top of the map which ever is smaller.
    – Ian Turton
    Feb 22, 2022 at 8:22

1 Answer 1

1

Finally I find solution, works exactly as I wanted. Every time when map is rendered, the distance between the centroid of the visible part of the polygon and the edge of the visible part of the polygon will be recalculated and the label will always be positioned on the top of the visible part of the polygon

Steps:

  1. Get the visible part of the polygon

    -intersect between the geometry and the bbox(visible part of map)

  2. Create a new geometry (point) in the centroid of the visible part of the polygon

  3. Calculating the distance from the centroid of the visible part of polygon and the edge of the visible part of polygon ← value by which the point will move along the Y axis

  4. Set offset for point - move the point along the Y axis

  5. Place the label on the point

Here is the code:

<TextSymbolizer>

        <Geometry>  <!-- creating a point (geometry) to which the label will be attached  -->
          <ogc:Function name="offset"> <!-- moving a point along the y axis-->

            <!-- creating a point -->
            <ogc:Function name="Concatenate">
              <ogc:Literal>POINT(</ogc:Literal>
              <ogc:Function name="getX"> <!-- x coordinate of the new point -->
                <ogc:Function name="centroid"> <!-- position the new point in the centroid of the visible part of the polygon -->
                  <ogc:Function name="intersection"> <!--  get the visible part of the polygon by making an intersect between bbox(of full image) and geometry -->
                    <ogc:Function name="env">
                      <ogc:Literal>wms_bbox</ogc:Literal>
                    </ogc:Function>
                    <ogc:PropertyName>geom</ogc:PropertyName>
                  </ogc:Function>
                </ogc:Function>
              </ogc:Function>
              <ogc:Literal><![CDATA[ ]]></ogc:Literal>
              <ogc:Function name="getY"> <!-- Y coordinate of the new point -->
                <ogc:Function name="centroid">
                  <ogc:Function name="intersection">
                    <ogc:Function name="env">
                      <ogc:Literal>wms_bbox</ogc:Literal>
                    </ogc:Function>
                    <ogc:PropertyName>geom</ogc:PropertyName>
                  </ogc:Function>
                </ogc:Function>
              </ogc:Function>
              <ogc:Literal>)</ogc:Literal>
            </ogc:Function>

            <!-- offset along the x axis -->
            <ogc:Literal>0.0</ogc:Literal> <!-- there is no displacement along the x axis, it remains in the middle of the visible part of the polygon -->    

            <!-- offset along the y axis -->
            <ogc:Function name="distance"> <!-- the point should be moved along the y axis to the top (distance between the centroid of the visible polygon and the boundary of the geometry of the visible polygon) -->
              <ogc:Function name="centroid"> <!-- centroid of visible polygon -->
                <ogc:Function name="intersection">
                  <ogc:Function name="env">
                    <ogc:Literal>wms_bbox</ogc:Literal>
                  </ogc:Function>
                  <ogc:PropertyName>geom</ogc:PropertyName>
                </ogc:Function>
              </ogc:Function>
              <ogc:Function name="boundary"> <!-- boundary of geometry(of visible part of polygon) -->
                <ogc:Function name="intersection">
                  <ogc:Function name="env">
                    <ogc:Literal>wms_bbox</ogc:Literal>
                  </ogc:Function>
                  <ogc:PropertyName>geom</ogc:PropertyName>
                </ogc:Function>
              </ogc:Function>
            </ogc:Function>
          </ogc:Function>
        </Geometry>

        <Label>
          <ogc:PropertyName>date</ogc:PropertyName>
        </Label>
2
  • Don't forget to accept your own answer, unless you are waiting for more responses.
    – Matt
    Mar 2, 2022 at 15:14
  • 1
    @Matt Tnx :) I'm done with this :)
    – ncica
    Mar 2, 2022 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.