Fix lat/lon order in SVG export
[gpl/argeo-suite.git] / org.argeo.app.geo / src / org / argeo / app / geo / GeoUtils.java
index 2c0bb39a76e29d641f7d9bf8939e69a4d216be1f..f13467821de3984e37a61e48b0458f6c97858d54 100644 (file)
@@ -14,33 +14,43 @@ import javax.measure.Quantity;
 import javax.measure.quantity.Area;
 import javax.xml.transform.TransformerException;
 
+import org.geotools.api.data.SimpleFeatureSource;
+import org.geotools.api.data.SimpleFeatureStore;
+import org.geotools.api.data.Transaction;
+import org.geotools.api.feature.simple.SimpleFeature;
+import org.geotools.api.feature.simple.SimpleFeatureType;
+import org.geotools.api.filter.Filter;
+import org.geotools.api.filter.FilterFactory;
+import org.geotools.api.filter.expression.Expression;
+import org.geotools.api.geometry.MismatchedDimensionException;
+import org.geotools.api.referencing.FactoryException;
+import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
+import org.geotools.api.referencing.operation.MathTransform;
+import org.geotools.api.referencing.operation.TransformException;
+import org.geotools.api.style.AnchorPoint;
+import org.geotools.api.style.Displacement;
+import org.geotools.api.style.FeatureTypeConstraint;
+import org.geotools.api.style.FeatureTypeStyle;
+import org.geotools.api.style.Fill;
+import org.geotools.api.style.Graphic;
+import org.geotools.api.style.GraphicalSymbol;
+import org.geotools.api.style.NamedLayer;
+import org.geotools.api.style.PointSymbolizer;
+import org.geotools.api.style.Rule;
+import org.geotools.api.style.Stroke;
+import org.geotools.api.style.Style;
+import org.geotools.api.style.StyleFactory;
+import org.geotools.api.style.StyledLayerDescriptor;
+import org.geotools.api.style.UserLayer;
 import org.geotools.data.DefaultTransaction;
-import org.geotools.data.Transaction;
 import org.geotools.data.collection.ListFeatureCollection;
 import org.geotools.data.shapefile.ShapefileDataStore;
 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
 import org.geotools.data.simple.SimpleFeatureCollection;
-import org.geotools.data.simple.SimpleFeatureIterator;
-import org.geotools.data.simple.SimpleFeatureSource;
-import org.geotools.data.simple.SimpleFeatureStore;
 import org.geotools.factory.CommonFactoryFinder;
 import org.geotools.geometry.jts.JTS;
 import org.geotools.referencing.CRS;
 import org.geotools.referencing.crs.DefaultGeographicCRS;
-import org.geotools.styling.AnchorPoint;
-import org.geotools.styling.Displacement;
-import org.geotools.styling.FeatureTypeConstraint;
-import org.geotools.styling.FeatureTypeStyle;
-import org.geotools.styling.Fill;
-import org.geotools.styling.Graphic;
-import org.geotools.styling.NamedLayer;
-import org.geotools.styling.PointSymbolizer;
-import org.geotools.styling.Rule;
-import org.geotools.styling.Stroke;
-import org.geotools.styling.Style;
-import org.geotools.styling.StyleFactory;
-import org.geotools.styling.StyledLayerDescriptor;
-import org.geotools.styling.UserLayer;
 import org.geotools.styling.css.CssParser;
 import org.geotools.styling.css.CssTranslator;
 import org.geotools.styling.css.Stylesheet;
@@ -52,23 +62,13 @@ import org.locationtech.jts.geom.Point;
 import org.locationtech.jts.geom.Polygon;
 import org.locationtech.jts.geom.util.GeometryFixer;
 import org.locationtech.jts.operation.polygonize.Polygonizer;
-import org.opengis.feature.simple.SimpleFeature;
-import org.opengis.feature.simple.SimpleFeatureType;
-import org.opengis.filter.Filter;
-import org.opengis.filter.FilterFactory2;
-import org.opengis.filter.expression.Expression;
-import org.opengis.geometry.MismatchedDimensionException;
-import org.opengis.referencing.FactoryException;
-import org.opengis.referencing.crs.CoordinateReferenceSystem;
-import org.opengis.referencing.operation.MathTransform;
-import org.opengis.referencing.operation.TransformException;
-import org.opengis.style.GraphicalSymbol;
 
 import si.uom.SI;
 import tech.units.indriya.quantity.Quantities;
 
 /** Utilities around geographical format, mostly wrapping GeoTools patterns. */
 public class GeoUtils {
+       public final static String EPSG_4326 = "EPSG:4326";
 
        /** In square meters. */
        public static Quantity<Area> calcArea(SimpleFeature feature) {
@@ -115,44 +115,46 @@ public class GeoUtils {
                }
        }
 
-       public static void exportToSvg(SimpleFeatureCollection features, Writer out, int width, int height) {
+       public static void exportToSvg(Geometry[] geometries, 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();
+                       for (Geometry geometry : geometries) {
                                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 + " ");
+                               if (geometry instanceof Polygon p) {
+                                       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);
+
+                                       // EPSG4326 are in lat/lon order, so we invert coordinates
+                                       for (Coordinate coord : projed.getCoordinates()) {
+                                               double x = coord.y;
+                                               if (x < minX)
+                                                       minX = x;
+                                               if (x > maxX)
+                                                       maxX = x;
+                                               double y = -coord.x;
+                                               if (y < minY)
+                                                       minY = y;
+                                               if (y > maxY)
+                                                       maxY = y;
+                                               sb.append(x + "," + y + " ");
+                                       }
+                                       sb.append("\">");
+                                       sb.append("</polyline>\n");
+                                       shapes.add(sb.toString());
+                               } else {
+                                       throw new IllegalArgumentException("Unsuppported geometry type " + geometry.getClass().getName());
                                }
-                               sb.append("\">");
-                               sb.append("</polyline>\n");
-                               shapes.add(sb.toString());
-
                        }
                        double viewportHeight = maxY - minY;
                        double viewportWidth = maxX - minX;
@@ -217,7 +219,7 @@ public class GeoUtils {
        public static Style createStyleFromCss(String css) {
                Stylesheet ss = CssParser.parse(css);
                CssTranslator translator = new CssTranslator();
-               org.opengis.style.Style style = translator.translate(ss);
+               Style style = translator.translate(ss);
 
 //             try {
 //                     SLDTransformer styleTransform = new SLDTransformer();
@@ -275,7 +277,7 @@ public class GeoUtils {
        public static String createTestSLD() {
 
                StyleFactory sf = CommonFactoryFinder.getStyleFactory();
-               FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
+               FilterFactory ff = CommonFactoryFinder.getFilterFactory();
 
                StyledLayerDescriptor sld = sf.createStyledLayerDescriptor();
                sld.setName("sld");