Search in sources :

Example 1 with CountryCodeProperties

use of org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties in project atlas by osmlab.

the class CountryBoundaryMap method getCountryCodeISO3.

/**
 * @param geometry
 *            The {@link Geometry} which we're assigning a country code to
 * @param fastMode
 *            In fast mode, we only return the first country hit, so if a node is right on the
 *            border, we'll only return one country code
 * @param buffer
 *            The buffer distance of geometry, please note that if the geometry is 0 dimension
 *            then a envelope expend is used instead of a rounding buffer
 * @return the resulting {@link CountryCodeProperties}
 */
public CountryCodeProperties getCountryCodeISO3(final Geometry geometry, final boolean fastMode, final double buffer) {
    StringList countryList = new StringList();
    final Geometry target;
    if (geometry.getDimension() == 0) {
        final Envelope envelope = geometry.getEnvelopeInternal();
        envelope.expandBy(buffer);
        target = geometry.getFactory().toGeometry(envelope);
    } else {
        target = geometry.buffer(buffer);
    }
    final List<Polygon> polygons = this.query(target.getEnvelopeInternal()).stream().filter(polygon -> polygon.intersects(target)).collect(Collectors.toList());
    boolean usingNearestNeighbor = false;
    if (polygons.size() == 1 || isSameCountry(polygons)) {
        countryList.add(getGeometryProperty(polygons.get(0), ISOCountryTag.KEY));
    } else {
        try {
            if (fastMode) {
                final Optional<String> match = polygons.stream().filter(polygon -> polygon.intersects(target)).map(polygon -> getGeometryProperty(polygon, ISOCountryTag.KEY)).findFirst();
                match.ifPresent(countryList::add);
            } else {
                countryList = new StringList(polygons.stream().filter(polygon -> polygon.intersects(target)).map(polygon -> getGeometryProperty(polygon, ISOCountryTag.KEY)).collect(Collectors.toList()));
            }
            if (countryList.isEmpty()) {
                // The node isn't within any country boundary - try to assign the iso_code
                // based on the nearest country
                final Geometry nearestGeometry = nearestNeighbour(target.getEnvelopeInternal(), target, new GeometryItemDistance());
                if (nearestGeometry != null) {
                    usingNearestNeighbor = true;
                    final String nearestCountryCode = getGeometryProperty(nearestGeometry, ISOCountryTag.KEY);
                    countryList.add(nearestCountryCode);
                } else {
                    countryList.add(ISOCountryTag.COUNTRY_MISSING);
                }
            }
        } catch (final Exception e) {
            logger.warn("There was exception when trying to find out country code for geometry {}, {}", geometry, e.getMessage());
            countryList.add(ISOCountryTag.COUNTRY_MISSING);
        }
    }
    return new CountryCodeProperties(this.countryListConverter.backwardConvert(countryList), usingNearestNeighbor);
}
Also used : Atlas(org.openstreetmap.atlas.geography.atlas.Atlas) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) JtsPointConverter(org.openstreetmap.atlas.geography.converters.jts.JtsPointConverter) WKTReader(org.locationtech.jts.io.WKTReader) FileDataStore(org.geotools.data.FileDataStore) ComplexBoundary(org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundary) LoggerFactory(org.slf4j.LoggerFactory) GeometryPrecisionReducer(org.locationtech.jts.precision.GeometryPrecisionReducer) AtlasEntity(org.openstreetmap.atlas.geography.atlas.items.AtlasEntity) WKTWriter(org.locationtech.jts.io.WKTWriter) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Map(java.util.Map) GeoJsonType(org.openstreetmap.atlas.geography.geojson.GeoJsonType) MultiPolygon(org.openstreetmap.atlas.geography.MultiPolygon) FeatureIterator(org.geotools.feature.FeatureIterator) Feature(org.opengis.feature.Feature) WritableResource(org.openstreetmap.atlas.streaming.resource.WritableResource) ItemBoundable(org.locationtech.jts.index.strtree.ItemBoundable) Predicate(java.util.function.Predicate) Collection(java.util.Collection) CoreException(org.openstreetmap.atlas.exception.CoreException) Location(org.openstreetmap.atlas.geography.Location) Set(java.util.Set) Collectors(java.util.stream.Collectors) CountryCodeProperties(org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Property(org.opengis.feature.Property) Objects(java.util.Objects) CountryListTwoWayStringConverter(org.openstreetmap.atlas.geography.boundary.converters.CountryListTwoWayStringConverter) List(java.util.List) AbstractNode(org.locationtech.jts.index.strtree.AbstractNode) Stream(java.util.stream.Stream) ParseException(org.locationtech.jts.io.ParseException) Polygon(org.locationtech.jts.geom.Polygon) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) Distance(org.openstreetmap.atlas.utilities.scalars.Distance) Rectangle(org.openstreetmap.atlas.geography.Rectangle) STRtree(org.locationtech.jts.index.strtree.STRtree) IntStream(java.util.stream.IntStream) ComplexBoundaryFinder(org.openstreetmap.atlas.geography.atlas.items.complex.boundaries.ComplexBoundaryFinder) CountryBoundaryMapGeoJsonConverter(org.openstreetmap.atlas.geography.boundary.converters.CountryBoundaryMapGeoJsonConverter) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) HashMap(java.util.HashMap) Longitude(org.geotools.measure.Longitude) Supplier(java.util.function.Supplier) JtsMultiPolygonToMultiPolygonConverter(org.openstreetmap.atlas.geography.converters.jts.JtsMultiPolygonToMultiPolygonConverter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) JtsPrecisionManager(org.openstreetmap.atlas.geography.converters.jts.JtsPrecisionManager) GeoJson(org.openstreetmap.atlas.geography.geojson.GeoJson) Lists(com.google.common.collect.Lists) StringTokenizer(java.util.StringTokenizer) GeometryItemDistance(org.locationtech.jts.index.strtree.GeometryItemDistance) OutputStreamWriter(java.io.OutputStreamWriter) JtsMultiPolygonConverter(org.openstreetmap.atlas.geography.converters.jts.JtsMultiPolygonConverter) MultiMap(org.openstreetmap.atlas.utilities.maps.MultiMap) Logger(org.slf4j.Logger) Resource(org.openstreetmap.atlas.streaming.resource.Resource) PolyLine(org.openstreetmap.atlas.geography.PolyLine) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) File(java.io.File) ISOCountryTag(org.openstreetmap.atlas.tags.ISOCountryTag) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) JtsPolyLineConverter(org.openstreetmap.atlas.geography.converters.jts.JtsPolyLineConverter) FileDataStoreFinder(org.geotools.data.FileDataStoreFinder) StringList(org.openstreetmap.atlas.utilities.collections.StringList) Collections(java.util.Collections) Envelope(org.locationtech.jts.geom.Envelope) ItemDistance(org.locationtech.jts.index.strtree.ItemDistance) StringList(org.openstreetmap.atlas.utilities.collections.StringList) CountryCodeProperties(org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties) Envelope(org.locationtech.jts.geom.Envelope) CoreException(org.openstreetmap.atlas.exception.CoreException) ParseException(org.locationtech.jts.io.ParseException) IOException(java.io.IOException) Geometry(org.locationtech.jts.geom.Geometry) GeometryItemDistance(org.locationtech.jts.index.strtree.GeometryItemDistance) MultiPolygon(org.openstreetmap.atlas.geography.MultiPolygon) Polygon(org.locationtech.jts.geom.Polygon)

Example 2 with CountryCodeProperties

use of org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties in project atlas by osmlab.

the class CountryBoundaryMapTest method testWithinBoundingBoxButNotWithinBoundary.

@Test
public void testWithinBoundingBoxButNotWithinBoundary() {
    final CountryBoundaryMap map = CountryBoundaryMap.fromPlainText(new InputStreamResource(() -> CountryBoundaryMapTest.class.getResourceAsStream("DMA_boundary.txt")));
    final Location location = Location.forWkt("POINT (-61.6678538 15.2957509)");
    final CountryCodeProperties countryCodeProperties = map.getCountryCodeISO3(location);
    Assert.assertTrue(countryCodeProperties.usingNearestNeighbor());
}
Also used : CountryCodeProperties(org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties) InputStreamResource(org.openstreetmap.atlas.streaming.resource.InputStreamResource) Location(org.openstreetmap.atlas.geography.Location) Test(org.junit.Test)

Example 3 with CountryCodeProperties

use of org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties in project atlas by osmlab.

the class CountryBoundaryMapTest method testGetCountryCode.

@Test
public void testGetCountryCode() {
    final CountryBoundaryMap map = CountryBoundaryMap.fromPlainText(new InputStreamResource(() -> CountryBoundaryMapTest.class.getResourceAsStream("HTI_DOM_osm_boundaries.txt.gz")).withDecompressor(Decompressor.GZIP));
    Assert.assertFalse(map.hasGridIndex());
    Point point = JTS_POINT_CONVERTER.convert(Location.forString("19.068387997775737, -71.7029007844633"));
    CountryCodeProperties countryDetails = map.getCountryCodeISO3(point);
    Assert.assertEquals("DOM", countryDetails.getIso3CountryCode());
    point = JTS_POINT_CONVERTER.convert(Location.forString("19.069172931560374, -71.70712929872246"));
    countryDetails = map.getCountryCodeISO3(point);
    Assert.assertEquals("HTI", countryDetails.getIso3CountryCode());
    point = JTS_POINT_CONVERTER.convert(Location.forString("19.0681781, -71.7075623"));
    countryDetails = map.getCountryCodeISO3(point, false);
    Assert.assertEquals("HTI,DOM", countryDetails.getIso3CountryCode());
}
Also used : CountryCodeProperties(org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties) Point(org.locationtech.jts.geom.Point) InputStreamResource(org.openstreetmap.atlas.streaming.resource.InputStreamResource) Test(org.junit.Test)

Aggregations

CountryCodeProperties (org.openstreetmap.atlas.geography.atlas.raw.slicing.CountryCodeProperties)3 Test (org.junit.Test)2 Location (org.openstreetmap.atlas.geography.Location)2 InputStreamResource (org.openstreetmap.atlas.streaming.resource.InputStreamResource)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 JsonObject (com.google.gson.JsonObject)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Serializable (java.io.Serializable)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1