1 package fr.ifremer.reefdb.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.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
36
37
38
39 public class Geometries extends fr.ifremer.quadrige3.core.dao.technical.Geometries {
40
41
42
43
44
45
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
69
70
71
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
85
86
87
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
101
102
103
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
117
118
119
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
130
131
132
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
140 return true;
141 }
142
143
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
151
152
153
154
155
156 public static boolean equals(CoordinateDTO source, CoordinateDTO target) {
157
158 if (source == null && target == null) {
159
160 return true;
161 } else if (source == null ^ target == null) {
162
163 return false;
164 }
165
166 if (isPoint(source) && isPoint(target)) {
167
168 return Objects.equals(source.getMinLongitude(), target.getMinLongitude()) && Objects.equals(source.getMinLatitude(), target.getMinLatitude());
169 } else if (!isPoint(source) && !isPoint(target)) {
170
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
176 return false;
177
178 }
179
180
181
182
183
184
185
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 }