1 package net.sumaris.server.http.geojson;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import net.sumaris.core.exception.SumarisTechnicalException;
26 import org.geojson.Geometry;
27 import org.geojson.LngLatAlt;
28 import org.geojson.MultiPolygon;
29 import org.geojson.Polygon;
30
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33
34
35
36
37
38 public class GeoJsonGeometries {
39
40 protected GeoJsonGeometries() {
41
42 }
43
44 public static Geometry jtsGeometry(com.vividsolutions.jts.geom.Geometry geom) {
45
46 if (geom instanceof com.vividsolutions.jts.geom.Polygon) {
47 return new Polygon(
48 Stream.of(geom.getCoordinates()).map(coordinate ->
49 new LngLatAlt(coordinate.x, coordinate.y, coordinate.z)).collect(Collectors.toList())
50 );
51 }
52 if (geom instanceof com.vividsolutions.jts.geom.MultiPolygon) {
53 com.vividsolutions.jts.geom.Geometry firstPolygon = geom.getGeometryN(0);
54 return new MultiPolygon(new Polygon(
55 Stream.of(firstPolygon.getCoordinates()).map(coordinate ->
56 new LngLatAlt(coordinate.x, coordinate.y, coordinate.z)).collect(Collectors.toList())
57 ));
58 }
59
60 throw new SumarisTechnicalException(String.format("GeoJson conversion from %s not implement yet !", geom.getGeometryType()));
61 }
62
63 public static LngLatAlt jtsCoordinate(com.vividsolutions.jts.geom.Coordinate coordinate) {
64 return new LngLatAlt(coordinate.x, coordinate.y, coordinate.z);
65 }
66 }