public static Geometry minimumCircle(Geometry g) { if (g == null) return null; MinimumBoundingCircle circle = new MinimumBoundingCircle(g); return circle.getCircle(); }
private void doMinimumBoundingCircleTest(String wkt, String expectedWKT, Coordinate expectedCentre, double expectedRadius) throws ParseException { MinimumBoundingCircle mbc = new MinimumBoundingCircle(reader.read(wkt)); Coordinate[] exPts = mbc.getExtremalPoints(); Geometry actual = geometryFactory.createMultiPoint(exPts); double actualRadius = mbc.getRadius(); Coordinate actualCentre = mbc.getCentre(); //System.out.println( // " Centre = " + actualCentre // + " Radius = " + actualRadius); Geometry expected = reader.read(expectedWKT); boolean isEqual = actual.equals(expected); // need this hack because apparently equals does not work for MULTIPOINT EMPTY if (actual.isEmpty() && expected.isEmpty()) isEqual = true; if (!isEqual) { System.out.println("Actual = " + actual + ", Expected = " + expected); } assertTrue(isEqual); if (expectedCentre != null) { assertTrue(expectedCentre.distance(actualCentre) < TOLERANCE); } if (expectedRadius >= 0) { assertTrue(Math.abs(expectedRadius - actualRadius) < TOLERANCE); } }
void run(int nPts) { Coordinate[] randPts = createRandomPoints(nPts); Geometry mp = geomFact.createMultiPoint(randPts); MinimumBoundingCircle mbc = new MinimumBoundingCircle(mp); Coordinate centre = mbc.getCentre(); double radius = mbc.getRadius(); System.out.println("Testing " + nPts + " random points. Radius = " + radius); checkWithinCircle(randPts, centre, radius, 0.0001); }
public static double maximumDiameterLength(Geometry g) { return 2 * (new MinimumBoundingCircle(g)).getRadius(); }
public static Geometry farthestPoints(Geometry g) { return ((new MinimumBoundingCircle(g)).getFarthestPoints()); } public static double maximumDiameterLength(Geometry g) { return 2 * (new MinimumBoundingCircle(g)).getRadius(); }
public static Geometry maximumDiameter(Geometry g) { return g.getFactory().createLineString((new MinimumBoundingCircle(g)).getExtremalPoints()); } public static Geometry farthestPoints(Geometry g) {
/** * Compute the minimum bounding circle center of a geometry * @param geometry Any geometry * @return Minimum bounding circle center point */ public static Point getCircumCenter(Geometry geometry) { if(geometry == null || geometry.getNumPoints() == 0) { return null; } return geometry.getFactory().createPoint(new MinimumBoundingCircle(geometry).getCentre()); } }
/** * Gets the centre point of the computed Minimum Bounding Circle. * * @return the centre point of the Minimum Bounding Circle * @return null if the input is empty */ public Coordinate getCentre() { compute(); return centre; }
/** * Gets the radius of the computed Minimum Bounding Circle. * * @return the radius of the Minimum Bounding Circle */ public double getRadius() { compute(); return radius; }
/** * Computes the bounding circle * * @param geometry * @return */ public static Geometry computeBoundingCircle(Geometry geometry) { if (geometry == null) { return null; } return new MinimumBoundingCircle(geometry).getCircle(); } }
/** * Gets the extremal points which define the computed Minimum Bounding Circle. * There may be zero, one, two or three of these points, * depending on the number of points in the input * and the geometry of those points. * * @return the points defining the Minimum Bounding Circle */ public Coordinate[] getExtremalPoints() { compute(); return extremalPts; }
public static Geometry minimumBoundingCircle(Geometry g) { return (new MinimumBoundingCircle(g)).getCircle(); } public static Geometry maximumDiameter(Geometry g) { return
/** * Gets a geometry which represents the Minimum Bounding Circle. * If the input is degenerate (empty or a single unique point), * this method will return an empty geometry or a single Point geometry. * Otherwise, a Polygon will be returned which approximates the * Minimum Bounding Circle. * (Note that because the computed polygon is only an approximation, * it may not precisely contain all the input points.) * * @return a Geometry representing the Minimum Bounding Circle. */ public Geometry getCircle() { //TODO: ensure the output circle contains the extermal points. //TODO: or maybe even ensure that the returned geometry contains ALL the input points? compute(); if (centre == null) return input.getFactory().createPolygon(); Point centrePoint = input.getFactory().createPoint(centre); if (radius == 0.0) return centrePoint; return centrePoint.buffer(radius); }
/** Test of evaluate method, of class FilterFunction_minimumCircle. */ @Test public void testEvaluate() throws Exception { SimpleFeatureCollection featureCollection = FunctionTestFixture.polygons(); // Test the Function FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); Function exp = ff.function("mincircle", ff.property("geom")); SimpleFeatureIterator iter = featureCollection.features(); while (iter.hasNext()) { SimpleFeature feature = iter.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); Geometry circle = new MinimumBoundingCircle(geom).getCircle(); Object value = exp.evaluate(feature); assertTrue(value instanceof Polygon); assertTrue(circle.equals((Geometry) value)); } iter.close(); // Check for null safeness assertNull(exp.evaluate(null)); } }
/** * Gets a geometry representing a line between the two farthest points * in the input. * These points will be two of the extremal points of the Minimum Bounding Circle. * They also lie on the convex hull of the input. * * @return a LineString between the two farthest points of the input * @return a empty LineString if the input is empty * @return a Point if the input is a point */ public Geometry getFarthestPoints() { compute(); switch (extremalPts.length) { case 0: return input.getFactory().createLineString(); case 1: return input.getFactory().createPoint(centre); } Coordinate p0 = extremalPts[0]; Coordinate p1 = extremalPts[extremalPts.length - 1]; return input.getFactory().createLineString(new Coordinate[] { p0, p1 }); }
/** * Gets a geometry representing the diameter of the computed Minimum Bounding * Circle. * * @return the diameter LineString of the Minimum Bounding Circle * @return a empty LineString if the input is empty * @return a Point if the input is a point */ public Geometry getDiameter() { compute(); switch (extremalPts.length) { case 0: return input.getFactory().createLineString(); case 1: return input.getFactory().createPoint(centre); } // TODO: handle case of 3 extremal points, by computing a line from one of // them through the centre point with len = 2*radius Coordinate p0 = extremalPts[0]; Coordinate p1 = extremalPts[1]; return input.getFactory().createLineString(new Coordinate[] { p0, p1 }); }