0

I am using geotools to generate some images based on the shapefile data, it works well until I tried to add labels for the line features: the labels are rendred even there is no features in the images.

enter image description here enter image description here

As shown, the result is not what I expected, the labels are rendered without attached with the feature.

This is the core rendering codes:

public BufferedImage render(ShapefileDataStore dataStore, int x, int y, int zoom, int width, int height) {
    double[] box = new double[]{.....};
    ReferencedEnvelope bbox = new ReferencedEnvelope(box[0], box[2], box[1], box[3], crs);
    int rwidth = width, rheight = height;
    Rectangle imageSize = new Rectangle(0, 0, rwidth, rheight);
    BufferedImage image = new BufferedImage(rwidth, rheight, BufferedImage.TYPE_INT_ARGB);


    Graphics2D g = image.createGraphics();
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(0, 0, rwidth, rheight);
    g.setComposite(AlphaComposite.Src);

    MapContent mapContent = new MapContent();
    try {
        String schemaName = dataStore.getSchema().getName().toString();
        Style style=createStyle();
        mapContent.addLayer(new FeatureLayer(dataStore.getFeatureSource(), style));
        synchronized (renderer) {
            renderer.setMapContent(mapContent);
            renderer.paint(g, imageSize, bbox);
        }
    } catch (Exception e) {
    } finally {
        mapContent.dispose();
        g.dispose();
    }
    return image;
}

public Style createStyle(){
    SLDParser sldParser = new SLDParser(styleFactory, new File("style.sld").toURL());
    Style[] ss = sldParser.readXML();
    return ss[0];
}

And the style.sld:

<StyledLayerDescriptor version="1.0.0">
    <NamedLayer>
        <Name>def</Name>
        <UserStyle>
            <Name>fx</Name>
            <FeatureTypeStyle>
                <Rule>
                    <Name>Single symbol</Name>
                    <LineSymbolizer>
                        <Stroke>
                            <CssParameter name="stroke">#ffffff</CssParameter>
                            <CssParameter name="stroke-width">10</CssParameter>
                            <CssParameter name="stroke-dasharray">5 2</CssParameter>
                            <CssParameter name="stroke-linejoin">bevel</CssParameter>
                            <CssParameter name="stroke-linecap">square</CssParameter>
                        </Stroke>
                    </LineSymbolizer>
                    <LineSymbolizer>
                        <Stroke>
                            <CssParameter name="stroke">#e31a1c</CssParameter>
                            <CssParameter name="stroke-width">6</CssParameter>
                            <CssParameter name="stroke-dasharray">5 2</CssParameter>
                            <CssParameter name="stroke-linejoin">bevel</CssParameter>
                            <CssParameter name="stroke-linecap">square</CssParameter>
                        </Stroke>
                    </LineSymbolizer>
                    <TextSymbolizer>
                        <Label>
                            <ogc:PropertyName>name</ogc:PropertyName>
                        </Label>
                        <Font>
                            <CssParameter name="font-family">Arial</CssParameter>
                            <CssParameter name="font-size">18</CssParameter>
                        </Font>
                        <Fill>
                            <CssParameter name="fill">#a4bdc5</CssParameter>
                        </Fill>
                        <VendorOption name="followLine">true</VendorOption>
                    </TextSymbolizer>
                </Rule>
            </FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>

Did I miss anything?

  • something is off about your style as that image isn't generated by that SLD as there are no dashes. See docs.geoserver.org/stable/en/user/styling/sld-cookbook/… for an example of casing if that is what you are trying. – Ian Turton Aug 25 '15 at 10:19
  • to start with try not swallowing exceptions whole – Ian Turton Aug 25 '15 at 10:19
  • Thanks for your attention, and the dash array is not the point. The rendered labels without features is what I cared. – giser Aug 25 '15 at 23:52
  • but unless we can see the actual SLD you used we can't see what is wrong – Ian Turton Aug 26 '15 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.