]> git.argeo.org Git - gpl/argeo-suite.git/blob - argeo/app/geo/GeoShapeUtils.java
Prepare next development cycle
[gpl/argeo-suite.git] / argeo / app / geo / GeoShapeUtils.java
1 package org.argeo.app.geo;
2
3 import org.locationtech.jts.geom.Coordinate;
4 import org.locationtech.jts.geom.Geometry;
5 import org.locationtech.jts.geom.LineString;
6 import org.locationtech.jts.geom.Point;
7 import org.locationtech.jts.geom.Polygon;
8
9 /** Utilities around ODK's GeoShape format */
10 public class GeoShapeUtils {
11
12 /** Converts a {@link Geometry} with WGS84 coordinates to a GeoShape. */
13 public static String geometryToGeoShape(Geometry geometry) {
14 if (geometry instanceof Point point) {
15 Coordinate coordinate = point.getCoordinate();
16 StringBuilder sb = new StringBuilder();
17 appendToGeoShape(sb, coordinate.getX(), coordinate.getY(), coordinate.getZ());
18 return sb.toString();
19 } else if (geometry instanceof Polygon || geometry instanceof LineString) {
20 StringBuilder sb = new StringBuilder();
21 for (Coordinate coordinate : geometry.getCoordinates()) {
22 appendToGeoShape(sb, coordinate.getX(), coordinate.getY(), coordinate.getZ());
23 sb.append(';');
24 }
25 return sb.toString();
26 } else {
27 throw new IllegalArgumentException("Unsupported geometry " + geometry.getClass());
28 }
29 }
30
31 public static String geoPointToGeoShape(double lon, double lat, double alt) {
32 StringBuilder sb = new StringBuilder();
33 appendToGeoShape(sb, lon, lat, alt);
34 return sb.toString();
35 }
36
37 private static void appendToGeoShape(StringBuilder sb, double lon, double lat, double alt) {
38 sb.append(lat).append(' ');
39 sb.append(lon).append(' ');
40 if (alt != Double.NaN)
41 sb.append(alt).append(' ');
42 else
43 sb.append("0 ");
44 sb.append('0');
45 }
46
47 /** singleton */
48 private GeoShapeUtils() {
49 }
50
51 }