private static Double distancePolygonLineString(Geometry g1, Geometry g2) {
        Double distance = Double.MAX_VALUE;
        Double ringDistance;
        LineString lineString;
        Polygon polygon;
        if (g1 instanceof Polygon) {
            lineString = (LineString) g2;
            polygon = (Polygon) g1;
        } else {
            lineString = (LineString) g1;
            polygon = (Polygon) g2;
        }
        for (int i = 0; i < polygon.getExteriorRing().getNumPoints(); i++) {
            ringDistance = distancePointLine(polygon.getExteriorRing().getPointN(i), lineString);
            if (ringDistance < distance) {
                distance = ringDistance;
            }

        }
        return distance;
    }
private static Double distancePolygonLineString ( Geometry g1 , Geometry g2 ) { Double distance = Double . MAX_VALUE ; Double ringDistance ; LineString lineString ; Polygon polygon ; if ( g1 instanceof Polygon ) { lineString = ( LineString ) g2 ; polygon = ( Polygon ) g1 ; } else { lineString = ( LineString ) g1 ; polygon = ( Polygon ) g2 ; } for ( int i = 0 ; i < polygon . getExteriorRing ( ) . getNumPoints ( ) ; i ++ ) { ringDistance = distancePointLine ( polygon . getExteriorRing ( ) . getPointN ( i ) , lineString ) ; if ( ringDistance < distance ) { distance = ringDistance ; } } return distance ; }
@param g1 Geometry instance or null
@param g2 Geometry instance or null
@return minimum distance in meters between two geometries