We have a Feature Layer having styling with a symbol using some font create by us. But we need to apply Labels on this following is the result we expected from this:-

Result From Other tool

and following is the result we have achieve with the AnchorPoint ,Displacement and Rotation :-

Result from geotools

Code For Label Style:-

Rule rule = styleFactory.createRule();
TextSymbolizer textsym = Styling.styleFactory.createTextSymbolizer();
StyleBuilder sb = new StyleBuilder();
AnchorPoint anchorPoint = sb.createAnchorPoint(sb.attributeExpression("the_geom"),
Displacement displacement = sb.createDisplacement(filterFactory.literal(10), filterFactory.literal(4));
PointPlacement pointPlacement = sb.createPointPlacement(anchorPoint,displacement,filterFactory.property("RotationColumn"));
  • please add the code or sld that you are using to create the symbol
    – Ian Turton
    Feb 14 '17 at 13:46
  • we have added the code to generate Label styling please check updated question. Feb 14 '17 at 13:56
  • now we need to see some example values for ColumnName and RotationColumn
    – Ian Turton
    Feb 14 '17 at 13:59
  • ColumnName have the values for Label like 'BSCE45','BSCe46' and so on and RotationColumn could have any value between 0-360.Example File in this file any column is LabelColumn and azm is rotation column please check. Feb 14 '17 at 14:06
  • then what you see looks plausible for those values - why did you think they would be horizontal if you set a rotation?
    – Ian Turton
    Feb 14 '17 at 15:16

I can get closer to what you want with the following code:

StyleBuilder builder = new StyleBuilder();
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
int offset = 10;

TextSymbolizer textSymbolizer = builder.createTextSymbolizer(Color.black, builder.createFont("Arial", 10), "CELL");
Expression deg2rad = ff.literal(Math.PI/180);
Expression angle = ff.multiply(deg2rad, ff.property("azm"));
Expression x = ff.multiply(ff.function("cos", angle),ff.literal(offset));
Expression y = ff.multiply(ff.function("sin", angle),ff.literal(offset));
Displacement off = builder.createDisplacement(x, y);
AnchorPoint anchorPt = builder.createAnchorPoint(0, 0);//centre on point
LabelPlacement pointPlacement = builder.createPointPlacement(anchorPt , off, ff.literal(0));
textSymbolizer.setLabelPlacement(pointPlacement );

Style style = builder.createStyle(textSymbolizer);
//SLDTransformer transformer = new SLDTransformer();
//String xml = transformer.transform( style );


But it only draws the first label because the label placement system considers all labels to be drawn at the same point (I think) and so even if you set the <VendorOption name="conflictResolution">false</VendorOption> it won't draw them. If this is the case then you should file an improvement request on the Jira but do check out this guide (it's for GeoServer but the same applies to GeoTools).

Expression deg2rad = filterFactory.literal(Math.PI / 180);
        Expression angle = filterFactory.multiply(deg2rad,
                filterFactory.subtract(filterFactory.literal(90), filterFactory.property(mplayer.LayerRotation)));
        Expression x = filterFactory.multiply(filterFactory.function("cos", angle),
        Expression y = filterFactory.multiply(filterFactory.function("sin", angle),
        Displacement off = sb.createDisplacement(x, y);

After this create four rules for each quadrant 0-90 , 90-180, 180-270 ,270-360

// For 0-90
AnchorPoint anchorPt = sb.createAnchorPoint(0, 0);
Filter flt =CQL.toFilter(filterFactory.property(LayerRotation) + " <=90");
LabelPlacement pointPlacement = sb.createPointPlacement(anchorPt, off, filterFactory.literal(0));

above code is same for all the Quadrants just u need to change the anchor point for all the quadrants. For 90-180 anchor point is AnchorPoint anchorPt = sb.createAnchorPoint(0, 1); For 180-270 anchor point is AnchorPoint anchorPt = sb.createAnchorPoint(1, 1); For 270-360 anchor point is AnchorPoint anchorPt = sb.createAnchorPoint(1, 0);

@iant please share with your team or propose better idea if any.

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.