简体   繁体   English

使用 GeoTools(版本 26.2)渲染剪裁的 SVG 图像

[英]Rendering clipped SVG image using GeoTools (version 26.2)

Trying to generate SVG from an external graphic link but the output SVG image not rendering properly.尝试从外部图形链接生成 SVG 但 output SVG 图像未正确呈现。 To generate SVG using the below code,要使用以下代码生成 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();
}

} }

Expected Output:-预计 Output:-预期产出

Final Output:-最后 Output:-最终渲染图像

So, in the final output SVG marker is getting cut from the right-hand side.所以,在最后的 output SVG 标记中,从右侧被切掉了。

Help would be appreciated.帮助将不胜感激。

Thanks in advance提前致谢

This is different from the previous question.这与上一个问题不同。

This happens near the bounds of the layer.这发生在层的边界附近。 You can enlarge the layer's bounds manually in the layer config page.您可以在图层配置页面中手动放大图层的边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM