Generate SVG from a polygon.
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 17 May 2022 11:55:52 +0000 (13:55 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 17 May 2022 11:55:52 +0000 (13:55 +0200)
org.argeo.app.core/src/org/argeo/app/geo/GeoUtils.java

index 8da7c4efbd27c6e0d074aa0e90bb8e758775f3a6..1b9ce72555d86130b4f6b77dd2fae53c03bb531e 100644 (file)
@@ -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<String> shapes = new ArrayList<>();
+                       for (SimpleFeatureIterator it = features.features(); it.hasNext();) {
+                               SimpleFeature feature = it.next();
+                               StringBuffer sb = new StringBuffer();
+                               sb.append("<polyline style=\"stroke-width:1;stroke:#000000;fill:none;\" points=\"");
+
+                               Polygon p = (Polygon) feature.getDefaultGeometry();
+                               Point centroid = p.getCentroid();
+                               String code = "AUTO:42001," + centroid.getX() + "," + centroid.getY();
+                               CoordinateReferenceSystem auto = CRS.decode(code);
+
+                               MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);
+
+                               Polygon projed = (Polygon) JTS.transform(p, transform);
+
+                               for (Coordinate coord : projed.getCoordinates()) {
+                                       double x = coord.x;
+                                       if (x < minX)
+                                               minX = x;
+                                       if (x > 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("</polyline>\n");
+                               shapes.add(sb.toString());
+
+                       }
+                       double viewportHeight = maxY - minY;
+                       double viewportWidth = maxX - minX;
+                       out.write("<svg xmlns=\"http://www.w3.org/2000/svg\"\n");
+                       out.write(" width=\"" + width + "\"\n");
+                       out.write(" height=\"" + height + "\"\n");
+                       out.write(" viewBox=\"" + minX + " " + minY + " " + viewportWidth + " " + viewportHeight + "\"\n");
+                       out.write(" preserveAspectRatio=\"xMidYMid meet\"\n");
+                       out.write(">\n");
+                       for (String shape : shapes) {
+                               out.write(shape);
+                               out.write("\n");
+                       }
+                       out.write("</svg>");
+               } catch (IOException | FactoryException | MismatchedDimensionException | TransformException e) {
+                       throw new RuntimeException("Cannot export to SVG", e);
+               }
+       }
+
        /** Singleton. */
        private GeoUtils() {
        }