From: Mathieu Baudier Date: Tue, 17 May 2022 11:55:52 +0000 (+0200) Subject: Generate SVG from a polygon. X-Git-Tag: v2.3.8~103 X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=commitdiff_plain;h=0d802e48f2cafd249149f9b322fd11f96a83a947 Generate SVG from a polygon. --- diff --git a/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java b/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java index 8da7c4e..1b9ce72 100644 --- a/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java +++ b/org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java @@ -1,11 +1,19 @@ package org.argeo.app.geo; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + import javax.measure.Quantity; import javax.measure.quantity.Area; +import org.geotools.data.simple.SimpleFeatureCollection; +import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.geotools.referencing.crs.DefaultGeographicCRS; +import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; import org.opengis.feature.simple.SimpleFeature; @@ -38,6 +46,63 @@ public class GeoUtils { } } + public static void exportToSvg(SimpleFeatureCollection features, Writer out, int width, int height) { + try { + double minY = Double.POSITIVE_INFINITY; + double maxY = Double.NEGATIVE_INFINITY; + double minX = Double.POSITIVE_INFINITY; + double maxX = Double.NEGATIVE_INFINITY; + List shapes = new ArrayList<>(); + for (SimpleFeatureIterator it = features.features(); it.hasNext();) { + SimpleFeature feature = it.next(); + StringBuffer sb = new StringBuffer(); + sb.append(" maxX) + maxX = x; + double y = -coord.y; + if (y < minY) + minY = y; + if (y > maxY) + maxY = y; + sb.append(x + "," + y + " "); + } + sb.append("\">"); + sb.append("\n"); + shapes.add(sb.toString()); + + } + double viewportHeight = maxY - minY; + double viewportWidth = maxX - minX; + out.write("\n"); + for (String shape : shapes) { + out.write(shape); + out.write("\n"); + } + out.write(""); + } catch (IOException | FactoryException | MismatchedDimensionException | TransformException e) { + throw new RuntimeException("Cannot export to SVG", e); + } + } + /** Singleton. */ private GeoUtils() { }