private double getDistance(double[] xy, ISpatialExtent extent) { if (extent == null) { return Double.NaN; } /* * we're on the feature, distance is 0. Intersector does check for actual point on * actual feature. */ if (!new FeatureIntersector(xy).find().isEmpty()) { return 0; } if (extent.getShape().getGeometryType() == Type.POINT) { Point point = (Point) ((IGeometricShape) extent.getShape()).getGeometry(); return GeoNS .getDistance(new Coordinate(xy[0], xy[1]), new Coordinate(point.getX(), point.getY())) * 1000.0; } /* * complicated intersection finding between extent and extent boundary. Let JTS help. */ Coordinate[] coords = DistanceOp.nearestPoints(ShapeValue .makePoint(xy[0], xy[1]), ((IGeometricShape) extent.getShape()).getGeometry()); if (coords == null || coords.length < 2) { return Double.NaN; } return GeoNS.getDistance(coords[0], coords[1]) * 1000.0; }
/** * Linear distance in KM between the centers of any two shapes. * * @param a * @param b * @return distance */ public static double getDistance(IGeometricShape a, IGeometricShape b) { return getDistance(a.getStandardizedGeometry().getCentroid().getCoordinate(), b.getStandardizedGeometry().getCentroid().getCoordinate()); }