Search in sources :

Example 1 with ComplexBoundary

use of org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundary in project atlas by osmlab.

the class CountryBoundaryMap method resolveOverlappingBorders.

/**
 * Filters the given iterable of {@link ComplexBoundary}s to only allow a single country to
 * claim an outer boundary polygon.
 *
 * @param complexBoundaries
 *            The {@link ComplexBoundary}s to filter duplicate polygons from
 * @return An iterable of {@link ComplexBoundary} with no duplicate outer polygons
 */
public Iterable<ComplexBoundary> resolveOverlappingBorders(final Iterable<ComplexBoundary> complexBoundaries) {
    final List<ComplexBoundary> deduplicatedComplexBoundaries = new ArrayList<>();
    final Set<org.openstreetmap.atlas.geography.Polygon> processedOuters = new HashSet<>();
    final List<ComplexBoundary> complexBoundaryList = Lists.newArrayList(complexBoundaries);
    Collections.sort(complexBoundaryList, (firstBoundary, secondBoundary) -> {
        if (firstBoundary.getIdentifier() > secondBoundary.getIdentifier()) {
            return 1;
        } else if (secondBoundary.getIdentifier() > firstBoundary.getIdentifier()) {
            return -1;
        }
        return 0;
    });
    for (final ComplexBoundary currentComplexBoundary : complexBoundaryList) {
        final MultiPolygon outline = currentComplexBoundary.getOutline();
        if (outline != null) {
            for (final org.openstreetmap.atlas.geography.Polygon outer : outline.outers()) {
                if (processedOuters.contains(outer)) {
                    currentComplexBoundary.removeOuter(outer);
                } else // ensures that the outer is only claimed once it has been assigned to a country
                if (Lists.newArrayList(currentComplexBoundary.getCountries()).size() > 0) {
                    processedOuters.add(outer);
                }
            }
            deduplicatedComplexBoundaries.add(currentComplexBoundary);
        }
    }
    return deduplicatedComplexBoundaries;
}
Also used : MultiPolygon(org.openstreetmap.atlas.geography.MultiPolygon) ArrayList(java.util.ArrayList) MultiPolygon(org.openstreetmap.atlas.geography.MultiPolygon) Polygon(org.locationtech.jts.geom.Polygon) ComplexBoundary(org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundary) HashSet(java.util.HashSet)

Example 2 with ComplexBoundary

use of org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundary in project atlas by osmlab.

the class CountryBoundaryMap method readFromAtlas.

/**
 * Read a {@link CountryBoundaryMap} from the {@link ComplexBoundary}(ies) inside an
 * {@link Atlas}
 *
 * @param atlas
 *            The {@link Atlas} to read from.
 */
public void readFromAtlas(final Atlas atlas) {
    final Iterable<ComplexBoundary> deduplicatedComplexBoundaries = resolveOverlappingBorders(new ComplexBoundaryFinder().find(atlas));
    for (final ComplexBoundary complexBoundary : deduplicatedComplexBoundaries) {
        if (complexBoundary.hasCountryCode()) {
            final List<String> countryCodes = new ArrayList<>();
            try {
                final MultiPolygon outline = complexBoundary.getOutline();
                final org.locationtech.jts.geom.MultiPolygon multiPolygon = JTS_MULTI_POLYGON_TO_MULTI_POLYGON_CONVERTER.backwardConvert(outline);
                complexBoundary.getCountries().forEach(isoCountry -> this.addCountry(isoCountry.getIso3CountryCode(), multiPolygon));
            } catch (final IllegalArgumentException e) {
                throw new CoreException("Unable to read country boundary for country codes {}", countryCodes, e);
            }
        }
    }
}
Also used : CoreException(org.openstreetmap.atlas.exception.CoreException) MultiPolygon(org.openstreetmap.atlas.geography.MultiPolygon) ArrayList(java.util.ArrayList) ComplexBoundaryFinder(org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundaryFinder) ComplexBoundary(org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundary)

Aggregations

ArrayList (java.util.ArrayList)2 MultiPolygon (org.openstreetmap.atlas.geography.MultiPolygon)2 ComplexBoundary (org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundary)2 HashSet (java.util.HashSet)1 Polygon (org.locationtech.jts.geom.Polygon)1 CoreException (org.openstreetmap.atlas.exception.CoreException)1 ComplexBoundaryFinder (org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundaryFinder)1