Search in sources :

Example 61 with Point

use of org.locationtech.spatial4j.shape.Point in project janusgraph by JanusGraph.

the class GeoshapeHelper method getPoint.

@SuppressWarnings("unchecked")
public Geoshape.Point getPoint(Geoshape geoshape, int position) {
    final Shape shape = geoshape.getShape();
    final Point p;
    if (position < 0 || position >= size(shape))
        throw new ArrayIndexOutOfBoundsException("Invalid position: " + position);
    switch(getType(shape)) {
        case POINT:
        case CIRCLE:
            return geoshape.getPoint();
        case BOX:
            if (position == 0)
                return new Geoshape.Point(shape.getBoundingBox().getMinY(), shape.getBoundingBox().getMinX());
            else
                return new Geoshape.Point(shape.getBoundingBox().getMaxY(), shape.getBoundingBox().getMaxX());
        case LINE:
            p = ((BufferedLineString) shape).getPoints().get(position);
            break;
        case MULTIPOINT:
            p = ((ShapeCollection<Point>) shape).getShapes().get(position);
            break;
        case MULTILINESTRING:
            p = ((ShapeCollection<BufferedLineString>) shape).getShapes().stream().flatMap(line -> line.getPoints().stream()).skip(position).findFirst().orElse(null);
            break;
        case GEOMETRYCOLLECTION:
            return ((ShapeCollection<Shape>) shape).getShapes().stream().flatMap(internShape -> IntStream.range(0, size(internShape)).mapToObj(i -> new AbstractMap.SimpleImmutableEntry<>(internShape, i))).skip(position).findFirst().map(entry -> getPoint(new Geoshape(entry.getKey()), entry.getValue())).orElse(null);
        default:
            throw new IllegalStateException("getPoint(int) not supported for type: " + getType(shape));
    }
    if (p == null)
        throw new ArrayIndexOutOfBoundsException("Invalid position: " + position);
    return new Geoshape.Point(p.getY(), p.getX());
}
Also used : WKTWriter(org.locationtech.spatial4j.io.WKTWriter) IntStream(java.util.stream.IntStream) DataInputStream(java.io.DataInputStream) Circle(org.locationtech.spatial4j.shape.Circle) GeoJSONReader(org.locationtech.spatial4j.io.GeoJSONReader) Set(java.util.Set) IOException(java.io.IOException) BinaryCodec(org.locationtech.spatial4j.io.BinaryCodec) Shape(org.locationtech.spatial4j.shape.Shape) Collectors(java.util.stream.Collectors) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) Point(org.locationtech.spatial4j.shape.Point) AbstractMap(java.util.AbstractMap) List(java.util.List) Rectangle(org.locationtech.spatial4j.shape.Rectangle) DataOutputStream(java.io.DataOutputStream) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) WKTReader(org.locationtech.spatial4j.io.WKTReader) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) BufferedLineString(org.locationtech.spatial4j.shape.impl.BufferedLineString) ShapeCollection(org.locationtech.spatial4j.shape.ShapeCollection) Shape(org.locationtech.spatial4j.shape.Shape) Point(org.locationtech.spatial4j.shape.Point) BufferedLineString(org.locationtech.spatial4j.shape.impl.BufferedLineString)

Example 62 with Point

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

the class GeoPointTypeTest method testStreaming.

@Test
public void testStreaming() throws Throwable {
    Point p1 = new PointImpl(41.2, -37.4, JtsSpatialContext.GEO);
    BytesStreamOutput out = new BytesStreamOutput();
    DataTypes.GEO_POINT.writeValueTo(out, p1);
    StreamInput in = out.bytes().streamInput();
    Point p2 = DataTypes.GEO_POINT.readValueFrom(in);
    assertThat(p1, equalTo(p2));
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 63 with Point

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

the class GeoPointTypeTest method test_sanitize_list_of_doubles_value.

@Test
public void test_sanitize_list_of_doubles_value() {
    Point value = DataTypes.GEO_POINT.sanitizeValue(List.of(1d, 2d));
    assertThat(value.getX(), is(1.0d));
    assertThat(value.getY(), is(2.0d));
}
Also used : Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 64 with Point

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

the class GeoPointTypeTest method testWktToGeoPointValue.

@Test
public void testWktToGeoPointValue() throws Exception {
    Point value = DataTypes.GEO_POINT.implicitCast("POINT(1 2)");
    assertThat(value.getX(), is(1.0d));
    assertThat(value.getY(), is(2.0d));
}
Also used : Point(org.locationtech.spatial4j.shape.Point) Test(org.junit.Test)

Example 65 with Point

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

the class GeoPointTypeTest method testConversionFromIntegerArray.

@Test
public void testConversionFromIntegerArray() throws Exception {
    Point value = DataTypes.GEO_POINT.implicitCast(new Integer[] { 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)

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