Search in sources :

Example 36 with Point

use of org.locationtech.spatial4j.shape.Point in project crate by crate.

the class GeoPointTypeTest method testConversionFromObjectArrayOfIntegers.

@Test
public void testConversionFromObjectArrayOfIntegers() throws Exception {
    Point value = DataTypes.GEO_POINT.implicitCast(new Object[] { 1, 2 });
    assertThat(value.getX(), is(1.0));
    assertThat(value.getY(), is(2.0));
}
Also used : Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 37 with Point

use of org.locationtech.spatial4j.shape.Point in project ddf by codice.

the class GeoNamesWebService method getCountryCode.

@Override
public Optional<String> getCountryCode(String locationWkt, int radius) {
    notNull(locationWkt, "argument locationWkt may not be null");
    Point wktCenterPoint = createPointFromWkt(locationWkt);
    String urlStr = String.format("%s://%s/countryCode?lat=%f&lng=%f&radius=%d&type=JSON&username=%s", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), radius, USERNAME);
    Object result = webQuery(urlStr);
    if (result instanceof JSONObject) {
        JSONObject jsonResult = (JSONObject) result;
        Object countryCode = jsonResult.get(GEONAMES_COUNTRYCODE);
        if (countryCode != null) {
            String alpha2CountryCode = (String) countryCode;
            if (StringUtils.isNotEmpty(alpha2CountryCode)) {
                try {
                    String alpha3CountryCode = new Locale(Locale.ENGLISH.getLanguage(), alpha2CountryCode).getISO3Country();
                    return Optional.of(alpha3CountryCode);
                } catch (MissingResourceException e) {
                    LOGGER.debug("Failed to convert country code {} to alpha-3 format. Returning " + "empty value", alpha2CountryCode);
                }
            }
        }
    }
    return Optional.empty();
}
Also used : Locale(java.util.Locale) JSONObject(net.minidev.json.JSONObject) MissingResourceException(java.util.MissingResourceException) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point)

Example 38 with Point

use of org.locationtech.spatial4j.shape.Point in project ddf by codice.

the class NearbyLocationImplTest method testNearbyLocationImpl.

@Test
public void testNearbyLocationImpl() {
    final Point source = new PointImpl(50, 50, SpatialContext.GEO);
    final Point nearby = new PointImpl(50.5, 50.5, SpatialContext.GEO);
    final NearbyLocation nearbyLocation = new NearbyLocationImpl(source, nearby, "Nearby");
    assertThat(nearbyLocation.getCardinalDirection(), is("SW"));
    // This distance value was obtained from http://www.movable-type.co.uk/scripts/latlong.html
    String expected = NumberFormat.getNumberInstance(Locale.getDefault()).format(65.99);
    assertThat(String.format("%.2f", nearbyLocation.getDistance()), is(expected));
    assertThat(nearbyLocation.getName(), is("Nearby"));
}
Also used : NearbyLocation(org.codice.ddf.spatial.geocoding.context.NearbyLocation) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) Test(org.junit.Test)

Example 39 with Point

use of org.locationtech.spatial4j.shape.Point in project elasticsearch by elastic.

the class GeoDistanceQueryBuilderTests method doCreateTestQueryBuilder.

@Override
protected GeoDistanceQueryBuilder doCreateTestQueryBuilder() {
    GeoDistanceQueryBuilder qb = new GeoDistanceQueryBuilder(GEO_POINT_FIELD_NAME);
    String distance = "" + randomDouble();
    if (randomBoolean()) {
        DistanceUnit unit = randomFrom(DistanceUnit.values());
        distance = distance + unit.toString();
    }
    int selector = randomIntBetween(0, 2);
    switch(selector) {
        case 0:
            qb.distance(randomDouble(), randomFrom(DistanceUnit.values()));
            break;
        case 1:
            qb.distance(distance, randomFrom(DistanceUnit.values()));
            break;
        case 2:
            qb.distance(distance);
            break;
    }
    Point p = RandomShapeGenerator.xRandomPoint(random());
    qb.point(new GeoPoint(p.getY(), p.getX()));
    if (randomBoolean()) {
        qb.setValidationMethod(randomFrom(GeoValidationMethod.values()));
    }
    if (randomBoolean()) {
        qb.geoDistance(randomFrom(GeoDistance.values()));
    }
    if (randomBoolean()) {
        qb.ignoreUnmapped(randomBoolean());
    }
    return qb;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LatLonPoint(org.apache.lucene.document.LatLonPoint) Point(org.locationtech.spatial4j.shape.Point) GeoPoint(org.elasticsearch.common.geo.GeoPoint) DistanceUnit(org.elasticsearch.common.unit.DistanceUnit) LatLonPoint(org.apache.lucene.document.LatLonPoint) Point(org.locationtech.spatial4j.shape.Point) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 40 with Point

use of org.locationtech.spatial4j.shape.Point in project elasticsearch by elastic.

the class ShapeBuilderTests method testNewPoint.

public void testNewPoint() {
    Point point = ShapeBuilders.newPoint(-100, 45).build();
    assertEquals(-100D, point.getX(), 0.0d);
    assertEquals(45D, point.getY(), 0.0d);
}
Also used : Point(org.locationtech.spatial4j.shape.Point)

Aggregations

Point (org.locationtech.spatial4j.shape.Point)71 Test (org.junit.Test)21 Shape (org.locationtech.spatial4j.shape.Shape)15 Query (org.apache.lucene.search.Query)9 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)9 Rectangle (org.locationtech.spatial4j.shape.Rectangle)9 ArrayList (java.util.ArrayList)7 Field (org.apache.lucene.document.Field)6 PointImpl (org.locationtech.spatial4j.shape.impl.PointImpl)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 TopDocs (org.apache.lucene.search.TopDocs)5 IOException (java.io.IOException)4 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)4 Document (org.apache.lucene.document.Document)3 StoredField (org.apache.lucene.document.StoredField)3 IndexReader (org.apache.lucene.index.IndexReader)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 Cell (org.apache.lucene.spatial.prefix.tree.Cell)3 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)3