0

尝试从外部图形链接生成 SVG,但输出的 SVG 图像未正确呈现。要使用以下代码生成 SVG,

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

}

预期输出:- 预期产出

最终输出:- 最终渲染图像

因此,在最终输出中,SVG 标记从右侧被剪切。

帮助将不胜感激。

提前致谢

这与上一个问题不同。

4

1 回答 1

0

这发生在图层边界附近。您可以在图层配置页面中手动放大图层的边界。

于 2022-02-08T13:13:16.263 回答