View Javadoc
1   package net.sumaris.server.http.geojson;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
6    * %%
7    * Copyright (C) 2018 - 2019 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Helper for GeoJson conversion
36   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>*
37   */
38  public class GeoJsonGeometries {
39  
40      protected GeoJsonGeometries() {
41          // helper class
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  }