View Javadoc
1   package fr.ifremer.reefdb.dao.technical;
2   
3   /*
4    * #%L
5    * Reef DB :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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  import com.vividsolutions.jts.geom.Envelope;
27  import com.vividsolutions.jts.geom.Geometry;
28  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
29  import fr.ifremer.reefdb.dto.CoordinateDTO;
30  import org.apache.commons.lang3.StringUtils;
31  
32  import java.util.Objects;
33  
34  /**
35   * Geometry utility class
36   * <p/>
37   * Created by Ludovic on 16/04/2015.
38   */
39  public class Geometries extends fr.ifremer.quadrige3.core.dao.technical.Geometries {
40  
41      /**
42       * <p>getCoordinate.</p>
43       *
44       * @param position a {@link java.lang.String} object.
45       * @return a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
46       */
47      public static CoordinateDTO getCoordinate(String position) {
48  
49          CoordinateDTO coordinate = ReefDbBeanFactory.newCoordinateDTO();
50  
51          if (StringUtils.isNotBlank(position)) {
52  
53              Geometry geometry = getGeometry(position);
54              Envelope envelope = geometry.getEnvelopeInternal();
55  
56              coordinate.setMinLongitude(envelope.getMinX());
57              coordinate.setMinLatitude(envelope.getMinY());
58              if (envelope.getWidth() > 0 || envelope.getHeight() > 0) {
59                  coordinate.setMaxLongitude(envelope.getMaxX());
60                  coordinate.setMaxLatitude(envelope.getMaxY());
61              }
62          }
63  
64          return coordinate;
65      }
66  
67      /**
68       * <p>getPointPosition.</p>
69       *
70       * @param coordinate a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
71       * @return a {@link java.lang.String} object.
72       */
73      public static String getPointPosition(CoordinateDTO coordinate) {
74  
75          if (coordinate == null || !isPoint(coordinate)) {
76              return null;
77          }
78  
79          Geometry geometry = createPoint(coordinate.getMinLongitude(), coordinate.getMinLatitude());
80          return getWKTString(geometry);
81      }
82  
83      /**
84       * <p>getLinePosition.</p>
85       *
86       * @param coordinate a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
87       * @return a {@link java.lang.String} object.
88       */
89      public static String getLinePosition(CoordinateDTO coordinate) {
90  
91          if (coordinate == null || isPoint(coordinate)) {
92              return null;
93          }
94  
95          Geometry geometry = createLine(coordinate.getMinLongitude(), coordinate.getMinLatitude(), coordinate.getMaxLongitude(), coordinate.getMaxLatitude());
96          return getWKTString(geometry);
97      }
98  
99      /**
100      * <p>getAreaPosition.</p>
101      *
102      * @param coordinate a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
103      * @return a {@link java.lang.String} object.
104      */
105     public static String getAreaPosition(CoordinateDTO coordinate) {
106 
107         if (coordinate == null || isPoint(coordinate)) {
108             return null;
109         }
110 
111         Geometry geometry = createPolygon(coordinate.getMinLongitude(), coordinate.getMinLatitude(), coordinate.getMaxLongitude(), coordinate.getMaxLatitude());
112         return getWKTString(geometry);
113     }
114 
115     /**
116      * <p>isValid.</p>
117      *
118      * @param coordinate a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
119      * @return a boolean.
120      */
121     public static boolean isValid(CoordinateDTO coordinate) {
122         return coordinate != null &&
123                 ((coordinate.getMinLongitude() != null && coordinate.getMaxLongitude() == null && coordinate.getMinLatitude() != null && coordinate.getMaxLatitude() == null)
124                         || (coordinate.getMinLongitude() != null && coordinate.getMaxLongitude() != null && coordinate.getMinLatitude() != null && coordinate.getMaxLatitude() != null));
125 
126     }
127 
128     /**
129      * <p>isPoint.</p>
130      *
131      * @param coordinate a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
132      * @return a boolean.
133      */
134     public static boolean isPoint(CoordinateDTO coordinate) {
135 
136         if (coordinate.getMinLongitude() != null && coordinate.getMinLatitude() != null
137                 && coordinate.getMaxLongitude() == null && coordinate.getMaxLatitude() == null) {
138 
139             // coordinate has only min long/lat
140             return true;
141         }
142 
143         // true if min long/lat equals max log/lat
144         return coordinate.getMinLongitude() != null && coordinate.getMinLatitude() != null && coordinate.getMaxLongitude() != null && coordinate.getMaxLatitude() != null
145                 && Objects.equals(coordinate.getMinLongitude(), coordinate.getMaxLongitude()) && Objects.equals(coordinate.getMinLatitude(), coordinate.getMaxLatitude());
146 
147     }
148 
149     /**
150      * <p>equals.</p>
151      *
152      * @param source a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
153      * @param target a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
154      * @return a boolean.
155      */
156     public static boolean equals(CoordinateDTO source, CoordinateDTO target) {
157 
158         if (source == null && target == null) {
159             // assume both null are equals
160             return true;
161         } else if (source == null ^ target == null) {
162             // but if one is null, return false
163             return false;
164         }
165 
166         if (isPoint(source) && isPoint(target)) {
167             // if both are point, check only min long/lat
168             return Objects.equals(source.getMinLongitude(), target.getMinLongitude()) && Objects.equals(source.getMinLatitude(), target.getMinLatitude());
169         } else if (!isPoint(source) && !isPoint(target)) {
170             // check all values
171             return Objects.equals(source.getMinLongitude(), target.getMinLongitude()) && Objects.equals(source.getMinLatitude(), target.getMinLatitude())
172                     && Objects.equals(source.getMaxLongitude(), target.getMaxLongitude()) && Objects.equals(source.getMaxLatitude(), target.getMaxLatitude());
173         }
174 
175         // otherwise not equals
176         return false;
177 
178     }
179 
180     /**
181      * <p>equals.</p>
182      *
183      * @param source a {@link fr.ifremer.reefdb.dto.CoordinateDTO} object.
184      * @param targetPosition a {@link java.lang.String} object.
185      * @return a boolean.
186      */
187     public static boolean equals(CoordinateDTO source, String targetPosition) {
188 
189         CoordinateDTO target = getCoordinate(targetPosition);
190 
191         return equals(source, target);
192     }
193 
194 }