in baremaps-openstreetmap/src/main/java/org/apache/baremaps/openstreetmap/function/WayGeometryBuilder.java [55:96]
public void accept(Entity entity) {
if (entity instanceof Way way) {
try {
// Build the coordinate list and remove duplicates.
List<Coordinate> list = new ArrayList<>();
Coordinate previous = null;
for (Long id : way.getNodes()) {
Coordinate coordinate = coordinateMap.get(id);
if (coordinate != null && !coordinate.equals(previous)) {
list.add(coordinate);
previous = coordinate;
}
}
Coordinate[] array = list.toArray(new Coordinate[0]);
LineString line = geometryFactory.createLineString(array);
if (!line.isEmpty()) {
// Ways can be open or closed depending on the geometry or the tags:
// https://wiki.openstreetmap.org/wiki/Way
if (!line.isClosed()
|| way.getTags().containsKey("railway")
|| way.getTags().containsKey("highway")
|| way.getTags().containsKey("barrier")) {
way.setGeometry(line);
} else {
Polygon polygon = geometryFactory.createPolygon(line.getCoordinates());
if (polygon.isValid()) {
way.setGeometry(polygon);
} else {
var geometryFixer = new GeometryFixer(polygon);
var fixedGeometry = geometryFixer.getResult();
way.setGeometry(fixedGeometry);
}
}
}
} catch (Exception e) {
logger.debug("Unable to build the geometry for way #" + way.getId(), e);
way.setGeometry(geometryFactory.createEmpty(0));
}
}
}