1 package fr.ifremer.dali.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.vividsolutions.jts.geom.Coordinate;
27 import com.vividsolutions.jts.geom.Envelope;
28 import com.vividsolutions.jts.geom.Geometry;
29 import fr.ifremer.dali.dto.CoordinateDTO;
30 import fr.ifremer.dali.dto.DaliBeanFactory;
31 import fr.ifremer.quadrige3.core.dao.technical.Assert;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.geotools.geometry.jts.JTS;
36 import org.geotools.referencing.crs.DefaultGeographicCRS;
37 import org.opengis.referencing.operation.TransformException;
38
39 import java.util.Objects;
40
41
42
43
44
45
46 public class Geometries extends fr.ifremer.quadrige3.core.dao.technical.Geometries {
47
48 private static final Log LOG = LogFactory.getLog(Geometries.class);
49
50
51
52
53
54
55
56 public static CoordinateDTO getCoordinate(String position) {
57
58 CoordinateDTO coordinate = DaliBeanFactory.newCoordinateDTO();
59
60 if (StringUtils.isNotBlank(position)) {
61
62
63 coordinate.setWkt(position);
64
65
66 Geometry geometry = getGeometry(position);
67
68 switch (geometry.getNumPoints()) {
69 case 1:
70 coordinate.setMinLongitude(geometry.getCoordinate().x);
71 coordinate.setMinLatitude(geometry.getCoordinate().y);
72 break;
73 case 2:
74 coordinate.setMinLongitude(geometry.getCoordinates()[0].x);
75 coordinate.setMinLatitude(geometry.getCoordinates()[0].y);
76 coordinate.setMaxLongitude(geometry.getCoordinates()[1].x);
77 coordinate.setMaxLatitude(geometry.getCoordinates()[1].y);
78 break;
79 default:
80 Envelope envelope = geometry.getEnvelopeInternal();
81 coordinate.setMinLongitude(envelope.getMinX());
82 coordinate.setMinLatitude(envelope.getMinY());
83 if (envelope.getWidth() > 0 || envelope.getHeight() > 0) {
84 coordinate.setMaxLongitude(envelope.getMaxX());
85 coordinate.setMaxLatitude(envelope.getMaxY());
86 }
87 break;
88 }
89 }
90
91 return coordinate;
92 }
93
94 public static Geometry getGeometry(CoordinateDTO coordinate) {
95 Assert.isTrue(isValid(coordinate), "the CoordinateDTO object is invalid : " + toString(coordinate));
96 return getGeometry(getPosition(coordinate));
97 }
98
99 public static String getPosition(CoordinateDTO coordinate) {
100 if (!isValid(coordinate)) return null;
101
102 if (isPoint(coordinate)) {
103 Geometry geometry = createPoint(coordinate.getMinLongitude(), coordinate.getMinLatitude());
104 return getWKTString(geometry);
105 } else {
106 Geometry geometry = createLine(coordinate.getMinLongitude(), coordinate.getMinLatitude(), coordinate.getMaxLongitude(), coordinate.getMaxLatitude());
107 return getWKTString(geometry);
108 }
109 }
110
111
112
113
114
115
116
117 public static boolean isValid(CoordinateDTO coordinate) {
118 return coordinate != null && (
119 (coordinate.getMinLongitude() != null && coordinate.getMaxLongitude() == null && coordinate.getMinLatitude() != null && coordinate.getMaxLatitude() == null)
120 || (coordinate.getMinLongitude() != null && coordinate.getMaxLongitude() != null && coordinate.getMinLatitude() != null && coordinate.getMaxLatitude() != null)
121 );
122
123 }
124
125
126
127
128
129
130
131 public static boolean isPoint(CoordinateDTO coordinate) {
132
133 if (coordinate.getMinLongitude() != null && coordinate.getMinLatitude() != null
134 && coordinate.getMaxLongitude() == null && coordinate.getMaxLatitude() == null) {
135
136
137 return true;
138 }
139
140
141 return coordinate.getMinLongitude() != null && coordinate.getMinLatitude() != null && coordinate.getMaxLongitude() != null && coordinate.getMaxLatitude() != null
142 && Objects.equals(coordinate.getMinLongitude(), coordinate.getMaxLongitude()) && Objects.equals(coordinate.getMinLatitude(), coordinate.getMaxLatitude());
143
144 }
145
146
147
148
149
150
151
152
153 public static boolean equals(CoordinateDTO source, CoordinateDTO target) {
154
155 if (source == null && target == null) {
156
157 return true;
158 } else if (source == null ^ target == null) {
159
160 return false;
161 }
162
163 if (isPoint(source) && isPoint(target)) {
164
165 return Objects.equals(source.getMinLongitude(), target.getMinLongitude()) && Objects.equals(source.getMinLatitude(), target.getMinLatitude());
166 } else if (!isPoint(source) && !isPoint(target)) {
167
168 return Objects.equals(source.getMinLongitude(), target.getMinLongitude()) && Objects.equals(source.getMinLatitude(), target.getMinLatitude())
169 && Objects.equals(source.getMaxLongitude(), target.getMaxLongitude()) && Objects.equals(source.getMaxLatitude(), target.getMaxLatitude());
170 }
171
172
173 return false;
174
175 }
176
177
178
179
180
181
182
183
184 public static boolean equals(CoordinateDTO source, String targetPosition) {
185
186 CoordinateDTO target = getCoordinate(targetPosition);
187
188 return equals(source, target);
189 }
190
191 public static String toString(CoordinateDTO coordinate) {
192 return coordinate == null
193 ? "null"
194 : String.format(
195 "minLatitude: %s ; minLongitude: %s ; maxLatitude: %s ; maxLongitude: %s",
196 coordinate.getMinLatitude(),
197 coordinate.getMinLongitude(),
198 coordinate.getMaxLatitude(),
199 coordinate.getMaxLongitude()
200 );
201 }
202
203
204
205
206
207
208
209
210 public static Double getDistanceInMeter(CoordinateDTO coordinate) {
211
212 if (isValid(coordinate) && !isPoint(coordinate)) {
213
214 Coordinate point1 = new Coordinate(coordinate.getMinLongitude(), coordinate.getMinLatitude());
215 Coordinate point2 = new Coordinate(coordinate.getMaxLongitude(), coordinate.getMaxLatitude());
216 return getDistanceInMeter(point1, point2);
217 }
218
219 return null;
220 }
221
222
223
224
225
226
227
228
229 public static Double getDistanceInMeter(Coordinate coordinate1, Coordinate coordinate2) {
230
231 Double distance = null;
232 try {
233 distance = JTS.orthodromicDistance(coordinate1, coordinate2, DefaultGeographicCRS.WGS84);
234 } catch (TransformException e) {
235 LOG.error(String.format("Can't calculate distance between %s and %s", coordinate1, coordinate2), e);
236 }
237 return distance;
238 }
239
240 }