View Javadoc
1   package fr.ifremer.quadrige2.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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.quadrige2.core.exception.Quadrige2TechnicalException;
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      /**
43       * Logger.
44       */
45      private static final Log log = LogFactory.getLog(Geometries.class);
46  
47      /**
48       * <p>Constructor for Geometries.</p>
49       */
50      protected Geometries() {
51          // helper class does not instantiate
52      }
53  
54      private static GeometryFactory geometryFactory = new GeometryFactory();
55  
56      /**
57       * <p>createPoint.</p>
58       *
59       * @param x a {@link java.lang.Double} object.
60       * @param y a {@link java.lang.Double} object.
61       * @return a {@link com.vividsolutions.jts.geom.Point} object.
62       */
63      public static Point createPoint(Double x, Double y) {
64          return geometryFactory.createPoint(new Coordinate(x, y));
65      }
66  
67      /**
68       * <p>createLine.</p>
69       *
70       * @param minX a {@link java.lang.Double} object.
71       * @param minY a {@link java.lang.Double} object.
72       * @param maxX a {@link java.lang.Double} object.
73       * @param maxY a {@link java.lang.Double} object.
74       * @return a {@link com.vividsolutions.jts.geom.LineString} object.
75       */
76      public static LineString createLine(Double minX, Double minY, Double maxX, Double maxY) {
77          return geometryFactory.createLineString(new Coordinate[]{
78                  new Coordinate(minX, minY),
79                  new Coordinate(maxX, maxY)});
80      }
81  
82      /**
83       * <p>createPolygon.</p>
84       *
85       * @param minX a {@link java.lang.Double} object.
86       * @param minY a {@link java.lang.Double} object.
87       * @param maxX a {@link java.lang.Double} object.
88       * @param maxY a {@link java.lang.Double} object.
89       * @return a {@link com.vividsolutions.jts.geom.Polygon} object.
90       */
91      public static Polygon createPolygon(Double minX, Double minY, Double maxX, Double maxY) {
92          return geometryFactory.createPolygon(new Coordinate[]{
93                  new Coordinate(minX, minY),
94                  new Coordinate(minX, maxY),
95                  new Coordinate(maxX, maxY),
96                  new Coordinate(maxX, minY),
97                  new Coordinate(minX, minY)});
98      }
99  
100     /**
101      * <p>getGeometry.</p>
102      *
103      * @param wktString a {@link java.lang.String} object.
104      * @return a {@link com.vividsolutions.jts.geom.Geometry} object.
105      */
106     public static Geometry getGeometry(String wktString) {
107         try {
108             return new WKTReader(geometryFactory).read(wktString);
109         } catch (ParseException e) {
110             log.error(e.getLocalizedMessage());
111             throw new Quadrige2TechnicalException(e);
112         }
113     }
114 
115     /**
116      * <p>getWKTString.</p>
117      *
118      * @param geometry a {@link com.vividsolutions.jts.geom.Geometry} object.
119      * @return a {@link java.lang.String} object.
120      */
121     public static String getWKTString(Geometry geometry) {
122         return new WKTWriter().write(geometry);
123     }
124 
125 }