public Point getCentroid() { return polygon.getCentroid(); }
} else { Point center = polygon.getCentroid(); LatLong centroid = new LatLong(center.getY(), center.getX()); double disOld = centroid.sphericalDistance(new LatLong(adminArea.getKey().lat, adminArea.getKey().lon));
/** * Finds a centroid for a polygon catching any exceptions resulting from generalization or other * polygon irregularities. * * @param geom The polygon. * @return The polygon centroid, or null if it can't be found. */ public static Point getPolygonCentroid(Polygon geom) { Point centroid; try { centroid = geom.getCentroid(); } catch (Exception e) { // generalized polygons causes problems - this // tries to hide them. try { centroid = geom.getExteriorRing().getCentroid(); } catch (Exception ee) { try { centroid = geom.getFactory().createPoint(geom.getCoordinate()); } catch (Exception eee) { return null; // we're hooped } } } return centroid; }
private static Point convertToPoint(final Polygon pt){ return pt.getCentroid(); }
Point center = polygon.getCentroid(); if (center == null) { return;
public void testSampleForCentralPoint() throws Exception { // create a "C" shape polygon that won't contain it's centroid Polygon g = (Polygon) new WKTReader() .read( "POLYGON ((-112.534433451864 43.8706532611928,-112.499157652296 44.7878240499628,-99.6587666095152 44.7878240499628,-99.7242788087131 43.2155312692142,-111.085391877449 43.099601544023,-110.744593363875 36.1862602686501,-98.6760836215473 35.9436771582516,-98.7415958207452 33.5197257879307,-111.77852346112 33.9783111823157,-111.758573671673 34.6566040234952,-113.088767445077 34.7644575726901,-113.023255245879 43.8706532611928,-112.534433451864 43.8706532611928))"); Point p = g.getCentroid(); assertFalse(g.contains(p)); // test with few samples, we shouldn't hit the inside assertNull(RendererUtilities.sampleForInternalPoint(g, p, null, null, -1, 2)); // up the samples, we should hit the inside now assertNotNull(RendererUtilities.sampleForInternalPoint(g, p, null, null, -1, 10)); }
return GeometryUtils.GEOMETRY_FACTORY.createPolygon(orderedCoords).getCentroid();