2

I have followed Label coordinate position of Grid lines to be able to draw meridians and parallels with labels. The meridian labels are placed at the Equator line and oriented in the direction of the meridians. Is there a way to move these labels to the bottom of the map? How can the labels be rotated 90 degrees to the right to become horizontal text at the same time?

1

To make this easier to follow and compare to the SLD documentation I added a quick debug method to convert the style object to an SLD document, there is an almost 1:1 correspondence between the GeoTools internal style objects and SLD elements.

  public void writeStyle(Style red) {
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();
    UserLayer layer = styleFactory.createUserLayer();
    layer.setLayerFeatureConstraints(new FeatureTypeConstraint[] { null });
    sld.addStyledLayer(layer);
    layer.addUserStyle(red);

    SLDTransformer styleTransform = new SLDTransformer();
    String xml;
    try {
      xml = styleTransform.transform(sld);
      System.out.println(xml);
    } catch (TransformerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

Applying this to the style used in the MapWithGrid example

<?xml version="1.0" encoding="UTF-8"?>                                                                                  
<sld:StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:sld="http://www.opengis.net/sld" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0">
  <sld:UserLayer>                                                                                                       
    <sld:LayerFeatureConstraints>                                                                                       
      <sld:FeatureTypeConstraint/>                                                                                      
    </sld:LayerFeatureConstraints>                                                                                      
    <sld:UserStyle>                                                                                                     
      <sld:Name>Default Styler</sld:Name>                                                                               
      <sld:FeatureTypeStyle>                                                                                            
        <sld:Name>name</sld:Name>                                                                                       
        <sld:Rule>                                                                                                      
          <sld:LineSymbolizer>                                                                                          
            <sld:Stroke>                                                                                                
              <sld:CssParameter name="stroke">#FF0000</sld:CssParameter>                                                
              <sld:CssParameter name="stroke-opacity">0.5</sld:CssParameter>                                            
            </sld:Stroke>                                                                                               
          </sld:LineSymbolizer>                                                                                         
          <sld:TextSymbolizer>                                                                                          
            <sld:Label>                                                                                                 
              <ogc:PropertyName>label</ogc:PropertyName>                                                                
            </sld:Label>                                                                                                
            <sld:Font>                                                                                                  
              <sld:CssParameter name="font-family">Arial</sld:CssParameter>                                             
              <sld:CssParameter name="font-size">12.0</sld:CssParameter>                                                
              <sld:CssParameter name="font-style">normal</sld:CssParameter>                                             
              <sld:CssParameter name="font-weight">normal</sld:CssParameter>                                            
            </sld:Font>                                                                                                 
            <sld:LabelPlacement>                                                                                        
              <sld:LinePlacement>                                                                                       
                <sld:PerpendicularOffset>4</sld:PerpendicularOffset>                                                    
              </sld:LinePlacement>                                                                                      
            </sld:LabelPlacement>                                                                                       
            <sld:Fill>                                                                                                  
              <sld:CssParameter name="fill">#FF0000</sld:CssParameter>                                                  
            </sld:Fill>                                                                                                 
          </sld:TextSymbolizer>                                                                                         
        </sld:Rule>                                                                                                     
      </sld:FeatureTypeStyle>                                                                                           
    </sld:UserStyle>                                                                                                    
  </sld:UserLayer>                                                                                                      
</sld:StyledLayerDescriptor>

So we start with a map like:

enter image description here

To change the angle of the labels we can remove the LinePlacement which makes the labels follow the lines. Simply comment out lines 164 & 165:

  LabelPlacement lp = sb.createLinePlacement(ff.literal(4));
  text.setLabelPlacement(lp);

Moving the attributes is slightly harder but if instead of labeling the line we label the endPoint of the line we can use one of GeoTools' functions to extract the end point of the line to use as the label point.

text.setGeometry(ff.function("endPoint", ff.property("element")));

This moves the labels to the top and right (you could use startPoint to get bottom and left).

enter image description here

1
  • writeStyle(blueStyle); and text.setGeometry(ff.function("endPoint", ff.property("element"))); to place the labels. Had to replace ff.propertyName with ff.property to compile code. When should writeStyle(blueStyle) called?
    – Isidora
    Apr 17 '20 at 20:46

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.