0

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.

Final Output Using SLD file

Expected output

7
  • Welcome to Stack Overflow! You need to turn up the logging level to developer in the global settings page, and then make the request again. Then edit your question with the relevant part of the log file.
    – Ian Turton
    Commented Feb 7, 2022 at 8:14
  • 1
    Also, it would help enormously if you make a small, self-contained, compileable example that demonstrates the problem. Your example code won't compile.
    – BradHards
    Commented Feb 7, 2022 at 8:22
  • @BradHards added the remaining code. To pass the value of coordinate please see below example, Coordinate[] listOfPoints = new Coordinate[5]; listOfPoints[0] = new Coordinate(-73.82, 41.24);//add random value of lat, long Commented Feb 7, 2022 at 9:44
  • 1
    That isn't what I meant. What is 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.
    – BradHards
    Commented Feb 7, 2022 at 9:54
  • 1
    OK. You still didn't provide a reproducable test case, but you can make an answer ("Your Answer", below) that explains what you needed to do to fix it.
    – BradHards
    Commented Feb 8, 2022 at 5:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.