View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import com.vividsolutions.jts.geom.*;
28  import com.vividsolutions.jts.io.ParseException;
29  import com.vividsolutions.jts.io.WKTReader;
30  import com.vividsolutions.jts.io.WKTWriter;
31  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * Utility Class for Geometry
37   * <p/>
38   * Created by Ludovic on 16/04/2015.
39   */
40  public class Geometries {
41  
42      public static final int DEFAULT_SRID = 4326;
43  
44      /**
45       * Logger.
46       */
47      private static final Log log = LogFactory.getLog(Geometries.class);
48  
49      /**
50       * <p>Constructor for Geometries.</p>
51       */
52      protected Geometries() {
53          // helper class does not instantiate
54      }
55  
56      private static final GeometryFactory geometryFactory = new GeometryFactory();
57  
58      /**
59       * <p>createPoint.</p>
60       *
61       * @param x a {@link java.lang.Double} object.
62       * @param y a {@link java.lang.Double} object.
63       * @return a {@link com.vividsolutions.jts.geom.Point} object.
64       */
65      public static Point createPoint(Double x, Double y) {
66          return geometryFactory.createPoint(new Coordinate(x, y));
67      }
68  
69      /**
70       * <p>createLine.</p>
71       *
72       * @param minX a {@link java.lang.Double} object.
73       * @param minY a {@link java.lang.Double} object.
74       * @param maxX a {@link java.lang.Double} object.
75       * @param maxY a {@link java.lang.Double} object.
76       * @return a {@link com.vividsolutions.jts.geom.LineString} object.
77       */
78      public static LineString createLine(Double minX, Double minY, Double maxX, Double maxY) {
79          return geometryFactory.createLineString(new Coordinate[]{
80                  new Coordinate(minX, minY),
81                  new Coordinate(maxX, maxY)});
82      }
83  
84      /**
85       * <p>createPolygon.</p>
86       *
87       * @param minX a {@link java.lang.Double} object.
88       * @param minY a {@link java.lang.Double} object.
89       * @param maxX a {@link java.lang.Double} object.
90       * @param maxY a {@link java.lang.Double} object.
91       * @return a {@link com.vividsolutions.jts.geom.Polygon} object.
92       */
93      public static Polygon createPolygon(Double minX, Double minY, Double maxX, Double maxY) {
94          return geometryFactory.createPolygon(new Coordinate[]{
95                  new Coordinate(minX, minY),
96                  new Coordinate(minX, maxY),
97                  new Coordinate(maxX, maxY),
98                  new Coordinate(maxX, minY),
99                  new Coordinate(minX, minY)});
100     }
101 
102     /**
103      * <p>getGeometry.</p>
104      *
105      * @param wktString a {@link java.lang.String} object.
106      * @return a {@link com.vividsolutions.jts.geom.Geometry} object.
107      */
108     public static Geometry getGeometry(String wktString) {
109         try {
110             return new WKTReader(geometryFactory).read(wktString);
111         } catch (ParseException e) {
112             log.error(e.getLocalizedMessage());
113             throw new QuadrigeTechnicalException(e);
114         }
115     }
116 
117     /**
118      * <p>getWKTString.</p>
119      *
120      * @param geometry a {@link com.vividsolutions.jts.geom.Geometry} object.
121      * @return a {@link java.lang.String} object.
122      */
123     public static String getWKTString(Geometry geometry) {
124         return new WKTWriter().write(geometry);
125     }
126 
127 }