// ensure both this and the given geometry are either points or
// linesegments so we can use the CGAlrogrithm calculations.
List<LineSegment> lines1 = null;
List<LineSegment> lines2 = null;
PointImpl point1 = null;
PointImpl point2 = null;
// convert this geom
if (this instanceof PointImpl) {
point1 = (PointImpl) this;
}
else if (this instanceof CurveImpl) {
lines1 = ((CurveImpl)this).asLineSegments();
}
else if (this instanceof RingImplUnsafe) {
lines1 = ((RingImplUnsafe)this).asLineString().asLineSegments();
}
else if (this instanceof RingImpl) {
lines1 = ((RingImpl)this).asLineString().asLineSegments();
}
else if (this instanceof SurfaceImpl) {
lines1 = ((RingImplUnsafe)((SurfaceImpl)this).getBoundary().getExterior()).asLineString().asLineSegments();
}
// convert given geom
if (geometry instanceof PointImpl) {
point2 = (PointImpl) geometry;
}
else if (geometry instanceof CurveImpl) {
lines2 = ((CurveImpl)geometry).asLineSegments();
}
else if (geometry instanceof RingImplUnsafe) {
lines2 = ((RingImplUnsafe)geometry).asLineString().asLineSegments();
}
else if (geometry instanceof RingImpl) {
lines2 = ((RingImpl)geometry).asLineString().asLineSegments();
}
else if (geometry instanceof SurfaceImpl) {
lines2 = ((RingImplUnsafe)((SurfaceImpl)geometry).getBoundary().getExterior()).asLineString().asLineSegments();
}
// now determine which algorithm to use for finding the shortest
// distance between the two geometries
if (point1 != null && point2 != null) {
// use directposition.distance()
return point1.getPosition().distance(point2.getPosition());
}
else if (lines1 != null) {
if (point2 != null) {
// loop through each linesegment and check for the min distance
double minDistance = Double.POSITIVE_INFINITY;
for (int i=0; i<lines1.size(); i++) {
Coordinate c1 = new Coordinate(point2.getRepresentativePoint().getCoordinate());
Coordinate cA = new Coordinate(lines1.get(i).getStartPoint().getCoordinate());
Coordinate cB = new Coordinate(lines1.get(i).getEndPoint().getCoordinate());
double d = CGAlgorithms.distancePointLine(c1, cA, cB);
if ( d < minDistance) {
minDistance = d;