CoordinatePair { public static final CoordinatePair findNearestPair(GeometryWrapper sourceGeometry, GeometryWrapper targetGeometry) throws SrsException { SRSInfo sourceSRSInfo = sourceGeometry.getSrsInfo(); SRSInfo targetSRSInfo = targetGeometry.getSrsInfo(); if (!(sourceSRSInfo.isGeographic() && targetSRSInfo.isGeographic()) || !(sourceSRSInfo.getSrsURI().equals(targetSRSInfo.getSrsURI()))) { throw new SrsException("Expected same Geographic SRS for GeometryWrappers. " + sourceGeometry + " : " + targetGeometry); } Point point1 = null; Point point2 = null; Geometry sourceXYGeometry = sourceGeometry.getXYGeometry(); Geometry targetXYGeometry = targetGeometry.getXYGeometry(); if (sourceXYGeometry instanceof Point) { point1 = (Point) sourceXYGeometry; } if (targetXYGeometry instanceof Point) { point2 = (Point) targetXYGeometry; } if (point1 != null && point2 != null) { return new CoordinatePair(point1.getCoordinate(), point2.getCoordinate()); } Envelope sourceEnvelope = sourceGeometry.getEnvelope(); Envelope targetEnvelope = targetGeometry.getEnvelope(); double domainRange = sourceSRSInfo.getDomainRangeX(); double halfRange = domainRange / 2; double diff = targetEnvelope.getMaxX() - sourceEnvelope.getMinX(); Geometry adjustedSource; Geometry adjustedTarget; if (diff > halfRange) { adjustedSource = sourceGeometry.translateXYGeometry(); adjustedTarget = targetXYGeometry; } else if (diff < -halfRange) { adjustedSource = sourceXYGeometry; adjustedTarget = targetGeometry.translateXYGeometry(); } else { adjustedSource = sourceXYGeometry; adjustedTarget = targetXYGeometry; } DistanceOp distanceOp = new DistanceOp(adjustedSource, adjustedTarget); Coordinate[] nearest = distanceOp.nearestPoints(); return new CoordinatePair(nearest[0], nearest[1]); } CoordinatePair(Coordinate coord1, Coordinate coord2); Coordinate getCoord1(); Coordinate getCoord2(); boolean isEqual(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); static final CoordinatePair findNearestPair(GeometryWrapper sourceGeometry, GeometryWrapper targetGeometry);  }
@Test public void testFindNearestPair_boundary() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(180, 20), new Coordinate(179, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }
@Test public void testFindNearestPair_boundary2() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(179, 20), new Coordinate(180, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }
@Test public void testFindNearestPair_near() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(1, 20), new Coordinate(-1, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }
@Test public void testFindNearestPair_near_postive() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(179, 20), new Coordinate(178, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }
@Test public void testFindNearestPair_near_negative() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(-179, 20), new Coordinate(-178, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }
@Test public void testFindNearestPair_same_positive_half() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(179, 20), new Coordinate(1, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }
@Test public void testFindNearestPair_same_negative_half() { GeometryWrapper sourceGeometry = GeometryWrapper.extract("<http: GeometryWrapper targetGeometry = GeometryWrapper.extract("<http: CoordinatePair expResult = new CoordinatePair(new Coordinate(-179, 20), new Coordinate(-1, 20)); CoordinatePair result = CoordinatePair.findNearestPair(sourceGeometry, targetGeometry); assertEquals(expResult, result); }