Skip to content

Commit

Permalink
Merge pull request HSLdevcom#127 from HSLdevcom/fix-proper-multipolygons
Browse files Browse the repository at this point in the history
Break multipolygons into multiple areas
  • Loading branch information
vesameskanen authored Mar 10, 2017
2 parents 45caca6 + f16c8c8 commit 84d137b
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ the License, or (at your option) any later version.
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

public class OSMDatabase implements OpenStreetMapContentHandler {

Expand Down Expand Up @@ -661,10 +662,29 @@ private void processMultipolygonRelations() {
}
}
processedAreas.add(relation);
try {
newArea(new Area(relation, outerWays, innerWays, nodesById));
} catch (Area.AreaConstructionException|Ring.RingConstructionException e) {
continue;

Map<Ring, OSMWay> wayForRing = new HashMap<>();
List<List<Ring>> groups = new ArrayList<>();;
for (OSMWay way : outerWays) {
Ring ring = new Ring(way.getNodeRefs(), nodesById);
wayForRing.put(ring, way);
for (List<Ring> group : groups) {
if (ring.toJtsPolygon().intersects(group.get(0).toJtsPolygon())) {
group.add(ring);
break;
}
}
groups.add(new ArrayList<>(Collections.singletonList(ring)));
}

for (List<Ring> group : groups) {
try {
List<OSMWay> outerRingWays = group.stream().map(wayForRing::get)
.collect(Collectors.toList());
newArea(new Area(relation, outerRingWays, innerWays, nodesById));
} catch (Area.AreaConstructionException|Ring.RingConstructionException e) {
continue;
}
}

for (OSMRelationMember member : relation.getMembers()) {
Expand Down

0 comments on commit 84d137b

Please sign in to comment.