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.
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:
def fx Single symbol #ffffff 10 5 2 bevel square #e31a1c 6 5 2 bevel square name Arial 18 #a4bdc5 true Did I miss anything?
أكثر...


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:
def fx Single symbol #ffffff 10 5 2 bevel square #e31a1c 6 5 2 bevel square name Arial 18 #a4bdc5 true Did I miss anything?
أكثر...