0

Trying to generate SVG from an external graphic link but the output SVG image not rendering properly. To generate SVG using the below code,

package org.geotools.tutorial.quickstart;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.*;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Expression;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.awt.*;
import java.awt.Dimension;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

public class SvgRendering {

static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
static FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2();

public static void main(String[] args) throws Exception {

    Coordinate[] listOfPoints = new Coordinate[5];
    listOfPoints[0] = new Coordinate(-73.82, 41.24);

    setupSVG(listOfPoints);
}

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;
}

private static void setupSVG(Coordinate[] listofP)
        throws IOException, ParserConfigurationException, URISyntaxException, TransformerException {

    //URL url = new URL("http://localhost:8080/");
    URL url = new URL("file:///C:/poc/mapreport/Map_marker.svg");
    //File file = new File(baseFilePath + "mapreport\\Map_marker.svg");
    //URL url = file.toURI().toURL();

    PointSymbolizer symb = styleFactory.createPointSymbolizer();
    ExternalGraphic eg = styleFactory.createExternalGraphic(url, "image/svg+xml");
    symb.getGraphic().graphicalSymbols().add(eg);
    Expression size = filterFactory.literal(54);
    symb.getGraphic().setSize(size);

    Rule rule = styleFactory.createRule();
    rule.symbolizers().add(symb);
    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(rule);
    Style style = styleFactory.createStyle();
    style.featureTypeStyles().add(fts);

    MapContent mc = new MapContent();

    DefaultFeatureCollection boundingbox = createBoundingBox(listofP);
    Layer layer = new FeatureLayer(boundingbox, style);
    mc.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(mc);
    Rectangle outputArea = new Rectangle(g2d.getSVGCanvasSize());
    ReferencedEnvelope dataArea = mc.getMaxBounds();
    dataArea.expandBy(5); // some of these have 0 size

    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();
}

}

Expected Output:- Expected Output

Final Output:- Final rendered image

So, in the final output SVG marker is getting cut from the right-hand side.

Help would be appreciated.

Thanks in advance

This is different from the previous question.

  • Does this answer your question? [Facing issue in external graphic using GeoTools(version 26.2)](https://stackoverflow.com/questions/71014637/facing-issue-in-external-graphic-using-geotoolsversion-26-2) – Ian Turton Feb 08 '22 at 10:24
  • please edit your existing question rather than asking another one. This is still not self contained. You need a `main` method that contains an example point that you are trying to draw, and a copy of the svg. – Ian Turton Feb 08 '22 at 10:25
  • @IanTurton this question is totally different from the previous question. In the previous question, SVG was not rendering, but in this, it's rendering but not properly. – Aniket Joshi Feb 08 '22 at 10:44
  • OK, then you need to make that clear while you add the missing code and the svg file. – Ian Turton Feb 08 '22 at 10:47
  • Added `main` code block. Please check now – Aniket Joshi Feb 08 '22 at 10:56
  • we still need the SVG file to test with - also if you answered your last question it would be clearer why this is a different question – Ian Turton Feb 08 '22 at 11:07

1 Answers1

0

This happens near the bounds of the layer. You can enlarge the layer's bounds manually in the layer config page.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 08 '22 at 16:05