use of de.topobyte.osm4j.geometry.relation.ChainOfWays in project osm4j-geometry by topobyte.
the class RegionBuilder method build.
/**
* Build a MultiPolygon from a Set of Ways.
*
* @param ways
* the ways to use for geometry construction.
* @return the constructed MultiPolygon.
* @throws EntityNotFoundException
* when a required entity cannot be obtained.
*/
public RegionBuilderResult build(MultiSet<OsmWay> ways, OsmEntityProvider resolver, Set<OsmNode> nodes) throws EntityNotFoundException {
RegionBuilderResult result = new RegionBuilderResult();
logger.debug("Have " + ways.keySet().size() + " ways");
// Only keep ways with 2 or more nodes. Also don't keep ways with just 2
// times the same node.
List<OsmWay> invalidWays = new ArrayList<>();
for (OsmWay way : ways.keySet()) {
int numNodes = way.getNumberOfNodes();
if (numNodes == 0 || numNodes == 1) {
invalidWays.add(way);
} else if (numNodes == 2 && OsmModelUtil.isClosed(way)) {
invalidWays.add(way);
}
}
ways.removeAllOccurences(invalidWays);
logger.debug("Removed " + invalidWays.size() + " invalid ways");
CountingMultiValMap<Long, OsmWay> wayTailMap = RelationUtil.buildWayTailMap(ways);
List<ChainOfWays> chains = RelationUtil.buildRings(ways, wayTailMap);
List<ChainOfNodes> rings = new ArrayList<>();
List<ChainOfNodes> nonRings = new ArrayList<>();
RelationUtil.convertToSegmentChainsAndResolveNodeIntersections(chains, rings, nonRings);
try {
RelationUtil.checkRings(chains, resolver, missingEntitiesStrategy);
} catch (EntityNotFoundException e) {
switch(missingEntitiesStrategy) {
case BUILD_PARTIAL:
// exception with BUILD_PARTIAL
break;
case BUILD_EMPTY:
return new RegionBuilderResult();
default:
case THROW_EXCEPTION:
throw (e);
}
}
// This could be used to close non-closed chains
// RelationUtil.closeUnclosedRingWithStraightLine(rings);
List<LinearRing> linearRings = new ArrayList<>();
convert(rings, nonRings, resolver, result.getCoordinates(), result.getLineStrings(), linearRings);
Set<LinearRing> validRings = new HashSet<>();
for (LinearRing r : linearRings) {
Set<LinearRing> repaired = SelfIntersectionUtil.repair(r);
for (LinearRing ring : repaired) {
validRings.add(ring);
}
}
MultiPolygon mp = PolygonHelper.multipolygonFromRings(validRings, false);
result.setMultiPolygon(mp);
if (includePuntal) {
GeometryUtil.buildNodes(nodeBuilder, nodes, result.getCoordinates());
}
return result;
}
Aggregations