Support geometry field in CSV
[gpl/argeo-suite.git] / org.argeo.app.geo / src / org / argeo / app / geo / GeoShapeUtils.java
diff --git a/org.argeo.app.geo/src/org/argeo/app/geo/GeoShapeUtils.java b/org.argeo.app.geo/src/org/argeo/app/geo/GeoShapeUtils.java
new file mode 100644 (file)
index 0000000..1db099b
--- /dev/null
@@ -0,0 +1,51 @@
+package org.argeo.app.geo;
+
+import org.locationtech.jts.geom.Coordinate;
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.LineString;
+import org.locationtech.jts.geom.Point;
+import org.locationtech.jts.geom.Polygon;
+
+/** Utilities around ODK's GeoShape format */
+public class GeoShapeUtils {
+
+       /** Converts a {@link Geometry} with WGS84 coordinates to a GeoShape. */
+       public static String geometryToGeoShape(Geometry geometry) {
+               if (geometry instanceof Point point) {
+                       Coordinate coordinate = point.getCoordinate();
+                       StringBuilder sb = new StringBuilder();
+                       appendToGeoShape(sb, coordinate.getX(), coordinate.getY(), coordinate.getZ());
+                       return sb.toString();
+               } else if (geometry instanceof Polygon || geometry instanceof LineString) {
+                       StringBuilder sb = new StringBuilder();
+                       for (Coordinate coordinate : geometry.getCoordinates()) {
+                               appendToGeoShape(sb, coordinate.getX(), coordinate.getY(), coordinate.getZ());
+                               sb.append(';');
+                       }
+                       return sb.toString();
+               } else {
+                       throw new IllegalArgumentException("Unsupported geometry " + geometry.getClass());
+               }
+       }
+
+       public static String geoPointToGeoShape(double lon, double lat, double alt) {
+               StringBuilder sb = new StringBuilder();
+               appendToGeoShape(sb, lon, lat, alt);
+               return sb.toString();
+       }
+
+       private static void appendToGeoShape(StringBuilder sb, double lon, double lat, double alt) {
+               sb.append(lat).append(' ');
+               sb.append(lon).append(' ');
+               if (alt != Double.NaN)
+                       sb.append(alt).append(' ');
+               else
+                       sb.append("0 ");
+               sb.append('0');
+       }
+
+       /** singleton */
+       private GeoShapeUtils() {
+       }
+
+}