12
12
import gov .nasa .podaac .inventory .model .Dataset ;
13
13
import gov .nasa .podaac .inventory .model .DatasetCitation ;
14
14
import org .apache .commons .lang3 .StringUtils ;
15
+ import org .apache .commons .lang3 .SerializationUtils ;
15
16
import org .apache .commons .logging .Log ;
16
17
import org .apache .commons .logging .LogFactory ;
17
18
@@ -222,6 +223,34 @@ public static boolean isGlobalBoundingBox(ArrayList<Coordinate> coordinates) {
222
223
}
223
224
}
224
225
226
+
227
+ /**
228
+ * Shift the polygon, if necessary, to move it into Cartesian space when there
229
+ * is an antimeridian crossing.
230
+ * @param coordinates the polygon coordinates
231
+ * @return the shifted polygon if there is an antimeridian cross, else a copy
232
+ * of the original polygon
233
+ */
234
+ public static Coordinate [] shiftPolygon (final Coordinate [] coordinates ) {
235
+ Coordinate [] shiftedCoordinates = SerializationUtils .clone (coordinates );
236
+
237
+ for (int i = 1 ; i < shiftedCoordinates .length ; ++i ) {
238
+ Coordinate coordinate = shiftedCoordinates [i ];
239
+ Coordinate previousCoordinate = shiftedCoordinates [i - 1 ];
240
+
241
+ double deltaLongitude = coordinate .x - previousCoordinate .x ;
242
+
243
+ if (Math .abs (deltaLongitude ) > 180 ) {
244
+ double direction = deltaLongitude / Math .abs (deltaLongitude );
245
+ double crossingShift = direction * 360 ;
246
+ coordinate .x = coordinate .x - crossingShift ;
247
+ }
248
+ }
249
+
250
+ return shiftedCoordinates ;
251
+ }
252
+
253
+
225
254
/**
226
255
* This function is to take a counterclockwise or clockwise sequence indicator and translate the input coordination
227
256
* array based on the desired orientation.
@@ -234,17 +263,19 @@ public static Coordinate[] ensureOrientation(
234
263
if (coord .length == 0 ) {
235
264
return coord ;
236
265
}
237
- final int orientation = CGAlgorithms .isCCW (coord ) ? CGAlgorithms .COUNTERCLOCKWISE
266
+
267
+ Coordinate [] shiftedPolygon = shiftPolygon (coord );
268
+ final int orientation = CGAlgorithms .isCCW (shiftedPolygon ) ? CGAlgorithms .COUNTERCLOCKWISE
238
269
: CGAlgorithms .CLOCKWISE ;
239
270
240
271
if (orientation != desiredOrientation ) {
241
- final Coordinate [] reverse = coord .clone ();
242
- reverse (reverse );
243
-
244
- return reverse ;
272
+ reverse (coord );
245
273
}
274
+
246
275
return coord ;
247
276
}
277
+
278
+
248
279
public static ArrayList <Coordinate > eliminateDuplicates (ArrayList <Coordinate > inputCoordinates ) {
249
280
int size = inputCoordinates .size ();
250
281
ArrayList <Coordinate > removeCandidateCoordinates = new ArrayList <>();
0 commit comments