@Test public void testMultiPolygon() throws ParseException { WKTReader reader = new WKTReader(); GeometryBuilder gb = new GeometryBuilder(); MultiPolygon p = (MultiPolygon) reader.read( "MultiPolygon (((0 5, 5 10, 10 5, 5 0, 0 5)),((11.74609375 1.6357421875, 11.7255859375 3.24609375, 13.9306640625 3.3076171875, 13.951171875 1.73828125, 11.74609375 1.6357421875)))"); Point point = (Point) PolyLabeller.getPolylabel(p, 1); Point expected = gb.point(5, 5); assertEquals(expected, point); }
static Geometry getPolylabel(Geometry polygon, double precision) { MultiPolygon multiPolygon; if (polygon instanceof Polygon) { multiPolygon = GB.multiPolygon((Polygon) polygon); } else if (polygon instanceof MultiPolygon) { multiPolygon = (MultiPolygon) polygon; } else { throw new IllegalStateException("Input polygon must be a Polygon or MultiPolygon"); } if (polygon.isEmpty() || polygon.getArea() <= 0.0) { throw new IllegalStateException("Can not label empty geometries"); } double minX, minY, maxX, maxY; Envelope env = multiPolygon.getEnvelopeInternal(); minX = env.getMinX(); maxX = env.getMaxX(); minY = env.getMinY(); maxY = env.getMaxY(); double width = env.getWidth(); double height = env.getHeight(); double cellSize = Math.min(width, height); double h = cellSize / 2.0; PriorityQueue<Cell> cellQueue = new PriorityQueue<>(); for (double x = minX; x < maxX; x += cellSize) { for (double y = minY; y < maxY; y += cellSize) { cellQueue.add(new Cell(x + h, y + h, h, multiPolygon)); } } Cell bestCell = getCentroidCell(multiPolygon); int numProbes = cellQueue.size(); while (!cellQueue.isEmpty()) { Cell cell = cellQueue.remove(); if (cell.getD() > bestCell.getD()) { bestCell = cell; if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer( "found best " + (Math.round(1e4 * cell.getD()) / 1e4) + " after " + numProbes + " probes"); } } if (cell.getMax() - bestCell.getD() <= precision) continue; h = cell.getH() / 2; cellQueue.add(new Cell(cell.getX() - h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() - h, cell.getY() + h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() + h, h, multiPolygon)); numProbes += 4; } if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("num probes: " + numProbes); LOGGER.finer("best distance: " + bestCell.getD()); } return bestCell.getPoint(); }
PolyLabeller { static Geometry getPolylabel(Geometry polygon, double precision) { MultiPolygon multiPolygon; if (polygon instanceof Polygon) { multiPolygon = GB.multiPolygon((Polygon) polygon); } else if (polygon instanceof MultiPolygon) { multiPolygon = (MultiPolygon) polygon; } else { throw new IllegalStateException("Input polygon must be a Polygon or MultiPolygon"); } if (polygon.isEmpty() || polygon.getArea() <= 0.0) { throw new IllegalStateException("Can not label empty geometries"); } double minX, minY, maxX, maxY; Envelope env = multiPolygon.getEnvelopeInternal(); minX = env.getMinX(); maxX = env.getMaxX(); minY = env.getMinY(); maxY = env.getMaxY(); double width = env.getWidth(); double height = env.getHeight(); double cellSize = Math.min(width, height); double h = cellSize / 2.0; PriorityQueue<Cell> cellQueue = new PriorityQueue<>(); for (double x = minX; x < maxX; x += cellSize) { for (double y = minY; y < maxY; y += cellSize) { cellQueue.add(new Cell(x + h, y + h, h, multiPolygon)); } } Cell bestCell = getCentroidCell(multiPolygon); int numProbes = cellQueue.size(); while (!cellQueue.isEmpty()) { Cell cell = cellQueue.remove(); if (cell.getD() > bestCell.getD()) { bestCell = cell; if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer( "found best " + (Math.round(1e4 * cell.getD()) / 1e4) + " after " + numProbes + " probes"); } } if (cell.getMax() - bestCell.getD() <= precision) continue; h = cell.getH() / 2; cellQueue.add(new Cell(cell.getX() - h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() - h, cell.getY() + h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() + h, h, multiPolygon)); numProbes += 4; } if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("num probes: " + numProbes); LOGGER.finer("best distance: " + bestCell.getD()); } return bestCell.getPoint(); } }
PolyLabeller { static Geometry getPolylabel(Geometry polygon, double precision) { MultiPolygon multiPolygon; if (polygon instanceof Polygon) { multiPolygon = GB.multiPolygon((Polygon) polygon); } else if (polygon instanceof MultiPolygon) { multiPolygon = (MultiPolygon) polygon; } else { throw new IllegalStateException("Input polygon must be a Polygon or MultiPolygon"); } if (polygon.isEmpty() || polygon.getArea() <= 0.0) { throw new IllegalStateException("Can not label empty geometries"); } double minX, minY, maxX, maxY; Envelope env = multiPolygon.getEnvelopeInternal(); minX = env.getMinX(); maxX = env.getMaxX(); minY = env.getMinY(); maxY = env.getMaxY(); double width = env.getWidth(); double height = env.getHeight(); double cellSize = Math.min(width, height); double h = cellSize / 2.0; PriorityQueue<Cell> cellQueue = new PriorityQueue<>(); for (double x = minX; x < maxX; x += cellSize) { for (double y = minY; y < maxY; y += cellSize) { cellQueue.add(new Cell(x + h, y + h, h, multiPolygon)); } } Cell bestCell = getCentroidCell(multiPolygon); int numProbes = cellQueue.size(); while (!cellQueue.isEmpty()) { Cell cell = cellQueue.remove(); if (cell.getD() > bestCell.getD()) { bestCell = cell; if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer( "found best " + (Math.round(1e4 * cell.getD()) / 1e4) + " after " + numProbes + " probes"); } } if (cell.getMax() - bestCell.getD() <= precision) continue; h = cell.getH() / 2; cellQueue.add(new Cell(cell.getX() - h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() - h, cell.getY() + h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() + h, h, multiPolygon)); numProbes += 4; } if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("num probes: " + numProbes); LOGGER.finer("best distance: " + bestCell.getD()); } return bestCell.getPoint(); }  }
PolyLabeller { static Geometry getPolylabel(Geometry polygon, double precision) { MultiPolygon multiPolygon; if (polygon instanceof Polygon) { multiPolygon = GB.multiPolygon((Polygon) polygon); } else if (polygon instanceof MultiPolygon) { multiPolygon = (MultiPolygon) polygon; } else { throw new IllegalStateException("Input polygon must be a Polygon or MultiPolygon"); } if (polygon.isEmpty() || polygon.getArea() <= 0.0) { throw new IllegalStateException("Can not label empty geometries"); } double minX, minY, maxX, maxY; Envelope env = multiPolygon.getEnvelopeInternal(); minX = env.getMinX(); maxX = env.getMaxX(); minY = env.getMinY(); maxY = env.getMaxY(); double width = env.getWidth(); double height = env.getHeight(); double cellSize = Math.min(width, height); double h = cellSize / 2.0; PriorityQueue<Cell> cellQueue = new PriorityQueue<>(); for (double x = minX; x < maxX; x += cellSize) { for (double y = minY; y < maxY; y += cellSize) { cellQueue.add(new Cell(x + h, y + h, h, multiPolygon)); } } Cell bestCell = getCentroidCell(multiPolygon); int numProbes = cellQueue.size(); while (!cellQueue.isEmpty()) { Cell cell = cellQueue.remove(); if (cell.getD() > bestCell.getD()) { bestCell = cell; if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer( "found best " + (Math.round(1e4 * cell.getD()) / 1e4) + " after " + numProbes + " probes"); } } if (cell.getMax() - bestCell.getD() <= precision) continue; h = cell.getH() / 2; cellQueue.add(new Cell(cell.getX() - h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() - h, cell.getY() + h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() + h, h, multiPolygon)); numProbes += 4; } if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("num probes: " + numProbes); LOGGER.finer("best distance: " + bestCell.getD()); } return bestCell.getPoint(); }   }
PolyLabeller { static Geometry getPolylabel(Geometry polygon, double precision) { MultiPolygon multiPolygon; if (polygon instanceof Polygon) { multiPolygon = GB.multiPolygon((Polygon) polygon); } else if (polygon instanceof MultiPolygon) { multiPolygon = (MultiPolygon) polygon; } else { throw new IllegalStateException("Input polygon must be a Polygon or MultiPolygon"); } if (polygon.isEmpty() || polygon.getArea() <= 0.0) { throw new IllegalStateException("Can not label empty geometries"); } double minX, minY, maxX, maxY; Envelope env = multiPolygon.getEnvelopeInternal(); minX = env.getMinX(); maxX = env.getMaxX(); minY = env.getMinY(); maxY = env.getMaxY(); double width = env.getWidth(); double height = env.getHeight(); double cellSize = Math.min(width, height); double h = cellSize / 2.0; PriorityQueue<Cell> cellQueue = new PriorityQueue<>(); for (double x = minX; x < maxX; x += cellSize) { for (double y = minY; y < maxY; y += cellSize) { cellQueue.add(new Cell(x + h, y + h, h, multiPolygon)); } } Cell bestCell = getCentroidCell(multiPolygon); int numProbes = cellQueue.size(); while (!cellQueue.isEmpty()) { Cell cell = cellQueue.remove(); if (cell.getD() > bestCell.getD()) { bestCell = cell; if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer( "found best " + (Math.round(1e4 * cell.getD()) / 1e4) + " after " + numProbes + " probes"); } } if (cell.getMax() - bestCell.getD() <= precision) continue; h = cell.getH() / 2; cellQueue.add(new Cell(cell.getX() - h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() - h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() - h, cell.getY() + h, h, multiPolygon)); cellQueue.add(new Cell(cell.getX() + h, cell.getY() + h, h, multiPolygon)); numProbes += 4; } if (LOGGER.isLoggable(Level.FINER)) { LOGGER.finer("num probes: " + numProbes); LOGGER.finer("best distance: " + bestCell.getD()); } return bestCell.getPoint(); }    }