Search in sources :

Example 31 with Point

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

the class GeoPointType method implicitCast.

@Override
public Point implicitCast(Object value) throws IllegalArgumentException, ClassCastException {
    if (value == null) {
        return null;
    } else if (value instanceof Point) {
        return ((Point) value);
    } else if (value instanceof Double[]) {
        Double[] doubles = (Double[]) value;
        checkLengthIs2(doubles.length);
        ensurePointsInRange(doubles[0], doubles[1]);
        return new PointImpl(doubles[0], doubles[1], JtsSpatialContext.GEO);
    } else if (value instanceof Object[]) {
        Object[] values = (Object[]) value;
        checkLengthIs2(values.length);
        PointImpl point = new PointImpl(((Number) values[0]).doubleValue(), ((Number) values[1]).doubleValue(), JtsSpatialContext.GEO);
        ensurePointsInRange(point.getX(), point.getY());
        return point;
    } else if (value instanceof String) {
        return pointFromString((String) value);
    } else if (value instanceof List) {
        List<?> values = (List<?>) value;
        checkLengthIs2(values.size());
        PointImpl point = new PointImpl(((Number) values.get(0)).doubleValue(), ((Number) values.get(1)).doubleValue(), JtsSpatialContext.GEO);
        ensurePointsInRange(point.getX(), point.getY());
        return point;
    } else {
        throw new ClassCastException("Can't cast '" + value + "' to " + getName());
    }
}
Also used : List(java.util.List) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 32 with Point

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

the class MultiPointBuilder method build.

@Override
public XShapeCollection<Point> build() {
    // Could wrap JtsGeometry but probably slower due to conversions to/from JTS in relate()
    // MultiPoint geometry = FACTORY.createMultiPoint(points.toArray(new Coordinate[points.size()]));
    List<Point> shapes = new ArrayList<>(coordinates.size());
    for (Coordinate coord : coordinates) {
        shapes.add(SPATIAL_CONTEXT.makePoint(coord.x, coord.y));
    }
    XShapeCollection<Point> multiPoints = new XShapeCollection<>(shapes, SPATIAL_CONTEXT);
    multiPoints.setPointsOnly(true);
    return multiPoints;
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) XShapeCollection(org.elasticsearch.common.geo.XShapeCollection) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point)

Example 33 with Point

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

the class TransportSQLActionTest method testInsertAndSelectGeoType.

@Test
public void testInsertAndSelectGeoType() throws Exception {
    execute("create table geo_point_table (id int primary key, p geo_point) with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into geo_point_table (id, p) values (?, ?)", new Object[] { 1, new Double[] { 47.22, 12.09 } });
    execute("insert into geo_point_table (id, p) values (?, ?)", new Object[] { 2, new Double[] { 57.22, 7.12 } });
    refresh();
    execute("select p from geo_point_table order by id desc");
    assertThat(response.rowCount(), is(2L));
    Point p1 = (Point) response.rows()[0][0];
    assertThat(p1.getX(), closeTo(57.22, 0.01));
    assertThat(p1.getY(), closeTo(7.12, 0.01));
    Point p2 = (Point) response.rows()[1][0];
    assertThat(p2.getX(), closeTo(47.22, 0.01));
    assertThat(p2.getY(), closeTo(12.09, 0.01));
}
Also used : Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 34 with Point

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

the class TransportSQLActionTest method testGeoTypeQueries.

@Test
public void testGeoTypeQueries() throws Exception {
    // setup
    execute("create table t (id int primary key, i int, p geo_point) " + "clustered into 1 shards " + "with (number_of_replicas=0)");
    ensureYellow();
    execute("insert into t (id, i, p) values (1, 1, 'POINT (10 20)')");
    execute("insert into t (id, i, p) values (2, 1, 'POINT (11 21)')");
    refresh();
    // order by
    execute("select distance(p, 'POINT (11 21)') from t order by 1");
    assertThat(response.rowCount(), is(2L));
    Double result1 = (Double) response.rows()[0][0];
    Double result2 = (Double) response.rows()[1][0];
    assertThat(result1, is(0.0d));
    assertThat(result2, is(152354.32308347954));
    String stmtOrderBy = "SELECT id " + "FROM t " + "ORDER BY distance(p, 'POINT(30.0 30.0)')";
    execute(stmtOrderBy);
    assertThat(response.rowCount(), is(2L));
    String expectedOrderBy = "2\n" + "1\n";
    assertEquals(expectedOrderBy, printedTable(response.rows()));
    // aggregation (max())
    String stmtAggregate = "SELECT i, max(distance(p, 'POINT(30.0 30.0)')) " + "FROM t " + "GROUP BY i";
    execute(stmtAggregate);
    assertThat(response.rowCount(), is(1L));
    String expectedAggregate = "1| 2296582.8899438097\n";
    assertEquals(expectedAggregate, printedTable(response.rows()));
    Point point;
    // queries
    execute("select p from t where distance(p, 'POINT (11 21)') > 0.0");
    assertThat(response.rowCount(), is(1L));
    point = (Point) response.rows()[0][0];
    assertThat(point.getX(), closeTo(10.0d, 0.01));
    assertThat(point.getY(), closeTo(20.0d, 0.02));
    execute("select p from t where distance(p, 'POINT (11 21)') < 10.0");
    assertThat(response.rowCount(), is(1L));
    point = (Point) response.rows()[0][0];
    assertThat(point.getX(), closeTo(11.0d, 0.01));
    assertThat(point.getY(), closeTo(21.0d, 0.01));
    execute("select p from t where distance(p, 'POINT (11 21)') < 10.0 or distance(p, 'POINT (11 21)') > 10.0");
    assertThat(response.rowCount(), is(2L));
    execute("select p from t where distance(p, 'POINT (10 20)') >= 0.0 and distance(p, 'POINT (10 20)') <= 0.1");
    assertThat(response.rowCount(), is(1L));
    point = (Point) response.rows()[0][0];
    assertThat(point.getX(), closeTo(10.0d, 0.01));
    assertThat(point.getY(), closeTo(20.0d, 0.01));
    execute("select p from t where distance(p, 'POINT (10 20)') = 0.0");
    assertThat(response.rowCount(), is(1L));
    point = (Point) response.rows()[0][0];
    assertThat(point.getX(), closeTo(10.0d, 0.01));
    assertThat(point.getY(), closeTo(20.0d, 0.01));
    execute("select p from t where distance(p, 'POINT (10 20)') = 152354.3209044634");
    Point p = (Point) response.rows()[0][0];
    assertThat(p.getX(), closeTo(11.0, 0.01));
    assertThat(p.getY(), closeTo(21.0, 0.01));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 35 with Point

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

the class GeoPointTypeTest method testValueConversionFromList.

@Test
public void testValueConversionFromList() throws Exception {
    Point value = DataTypes.GEO_POINT.implicitCast(List.of(10.0, 20.2));
    assertThat(value.getX(), is(10.0d));
    assertThat(value.getY(), is(20.2d));
}
Also used : Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

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