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);
}
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());
}
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());
}
Aggregations