Not able to generate SVG from an external graphic link using an SLD file. Please go through the below code snippet,
Sld file with an external link,
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>Grass</Name>
<UserStyle>
<FeatureTypeStyle>
<Rule>
<PointSymbolizer>
<Graphic>
<ExternalGraphic>
<OnlineResource xlink:href="https://upload.wikimedia.org/wikipedia/commons/8/88/Map_marker.svg" xlink:type="simple" />
<Format>image/svg+xml</Format>
</ExternalGraphic>
<Size>20</Size>
</Graphic>
</PointSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Export SVG code
private static Style loadStyle(String sldFilename) throws IOException {
StyleFactory factory = CommonFactoryFinder.getStyleFactory(null);
File file = new File("C:\\poc\\" + sldFilename);
URL url = file.toURI().toURL();
SLDParser stylereader = new SLDParser(factory, url);
Style style = stylereader.readXML()[0];
return style;
}
public static DefaultFeatureCollection createBoundingBox(Coordinate[] listofP){
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
b.setName("MyFeatureType");
b.add("location", Polygon.class);
final SimpleFeatureType TYPE = b.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
Polygon polygon = geometryFactory.createPolygon(listofP);
featureBuilder.add(polygon);
SimpleFeature feature = featureBuilder.buildFeature("polygon");
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
featureCollection.add(feature); //Add feature 1
return featureCollection;
}
public static void exportSVG(String fileName, Coordinate[] listofP)
throws IOException, ParserConfigurationException {
Style pStyle = loadStyle(fileName);
MapContent map = new MapContent();
DefaultFeatureCollection boundingbox = createBoundingBox(listofP);
Layer layer = new FeatureLayer(boundingbox, pStyle);
map.addLayer(layer);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Create an instance of org.w3c.dom.Document
Document document = db.getDOMImplementation().createDocument(null, "svg", null);
// Set up the map
SVGGeneratorContext ctx1 = SVGGeneratorContext.createDefault(document);
SVGGeneratorContext ctx = ctx1;
ctx.setComment("Generated by GeoTools2 with Batik SVG Generator");
SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);
Dimension canvasSize = new Dimension(1024, 1024);
g2d.setSVGCanvasSize(canvasSize);
StreamingRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
Rectangle outputArea = new Rectangle(g2d.getSVGCanvasSize());
ReferencedEnvelope dataArea = map.getMaxBounds();
renderer.paint(g2d, outputArea, dataArea);
File fileToSave = new File("C:\\poc\\" + "markers.svg");
OutputStreamWriter osw = null;
try {
OutputStream out = new FileOutputStream(fileToSave);
osw = null;
osw = new OutputStreamWriter(out, "UTF-8");
g2d.stream(osw);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (osw != null)
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mc.dispose();
}
Using the above code and SLDfile the final SVG is not getting rendered properly, its final output SVG image shows only a gray square. Instead it should show the exact marker present on the external link. Tried using the local SVG external link as well but still no luck.
map
in that? Where are the imports? Please read sscce.org and consider that if you are much more likely to get help if you make it easy to help you. Try to make a really small example that demonstrates the problem.