Search in sources :

Example 1 with Rectangle

use of com.facebook.presto.geospatial.Rectangle in project presto by prestodb.

the class SphericalGeoFunctions method spatialPartitions.

@ScalarFunction
@SqlNullable
@Description("Returns an array of spatial partition IDs for a geometry representing a set of points within specified distance from the input geometry")
@SqlType("array(int)")
public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice geometry, @SqlType(DOUBLE) double distance) {
    if (isNaN(distance)) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is NaN");
    }
    if (isInfinite(distance)) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is infinite");
    }
    if (distance < 0) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "distance is negative");
    }
    Envelope envelope = deserializeEnvelope(geometry);
    if (envelope.isEmpty()) {
        return null;
    }
    Rectangle expandedEnvelope2D = new Rectangle(envelope.getXMin() - distance, envelope.getYMin() - distance, envelope.getXMax() + distance, envelope.getYMax() + distance);
    return GeoFunctions.spatialPartitions((KdbTree) kdbTree, expandedEnvelope2D);
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle) PrestoException(com.facebook.presto.spi.PrestoException) EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with Rectangle

use of com.facebook.presto.geospatial.Rectangle in project presto by prestodb.

the class TestHilbertIndex method testDegenerateVerticalRectangle.

@Test
public void testDegenerateVerticalRectangle() {
    HilbertIndex hilbert = new HilbertIndex(new Rectangle(0, 0, 0, 4));
    assertEquals(hilbert.indexOf(0., 0.), 0);
    assertTrue(hilbert.indexOf(0., 1.) < hilbert.indexOf(0., 2.));
    assertEquals(hilbert.indexOf(2., 0.), Long.MAX_VALUE);
    assertEquals(hilbert.indexOf(2., 2.), Long.MAX_VALUE);
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle) Test(org.testng.annotations.Test)

Example 3 with Rectangle

use of com.facebook.presto.geospatial.Rectangle in project presto by prestodb.

the class TestHilbertIndex method testOutOfBounds.

@Test
public void testOutOfBounds() {
    HilbertIndex hilbert = new HilbertIndex(new Rectangle(0, 0, 1, 1));
    assertEquals(hilbert.indexOf(2., 2.), Long.MAX_VALUE);
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle) Test(org.testng.annotations.Test)

Example 4 with Rectangle

use of com.facebook.presto.geospatial.Rectangle in project presto by prestodb.

the class TestHilbertIndex method testOrder.

@Test
public void testOrder() {
    HilbertIndex hilbert = new HilbertIndex(new Rectangle(0, 0, 4, 4));
    long h0 = hilbert.indexOf(0., 0.);
    long h1 = hilbert.indexOf(1., 1.);
    long h2 = hilbert.indexOf(1., 3.);
    long h3 = hilbert.indexOf(3., 3.);
    long h4 = hilbert.indexOf(3., 1.);
    assertTrue(h0 < h1);
    assertTrue(h1 < h2);
    assertTrue(h2 < h3);
    assertTrue(h3 < h4);
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle) Test(org.testng.annotations.Test)

Example 5 with Rectangle

use of com.facebook.presto.geospatial.Rectangle in project presto by prestodb.

the class SpatialPartitioningInternalAggregateFunction method input.

@InputFunction
public static void input(SpatialPartitioningState state, @SqlType(GEOMETRY_TYPE_NAME) Slice slice, @SqlType(INTEGER) long partitionCount) {
    Envelope envelope = deserializeEnvelope(slice);
    if (envelope.isEmpty()) {
        return;
    }
    if (state.getCount() == 0) {
        state.setPartitionCount(toIntExact(partitionCount));
        state.setSamples(new ArrayList<>());
    }
    // use reservoir sampling
    Rectangle extent = new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax());
    List<Rectangle> samples = state.getSamples();
    if (samples.size() <= MAX_SAMPLE_COUNT) {
        samples.add(extent);
    } else {
        long sampleIndex = ThreadLocalRandom.current().nextLong(state.getCount());
        if (sampleIndex < MAX_SAMPLE_COUNT) {
            samples.set(toIntExact(sampleIndex), extent);
        }
    }
    state.setCount(state.getCount() + 1);
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle) EsriGeometrySerde.deserializeEnvelope(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope) Envelope(com.esri.core.geometry.Envelope) InputFunction(com.facebook.presto.spi.function.InputFunction)

Aggregations

Rectangle (com.facebook.presto.geospatial.Rectangle)19 Test (org.testng.annotations.Test)11 Envelope (com.esri.core.geometry.Envelope)4 Point (com.esri.core.geometry.Point)3 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)3 EsriGeometrySerde.deserializeEnvelope (com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserializeEnvelope)3 List (java.util.List)3 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)2 GeometryUtils (com.facebook.presto.geospatial.GeometryUtils)2 PrestoException (com.facebook.presto.spi.PrestoException)2 Description (com.facebook.presto.spi.function.Description)2 ImmutableList (com.google.common.collect.ImmutableList)2 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)2 ArrayList (java.util.ArrayList)2 Comparator (java.util.Comparator)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 ClassLayout (org.openjdk.jol.info.ClassLayout)2 Session (com.facebook.presto.Session)1 Page (com.facebook.presto.common.Page)1 PageBuilder (com.facebook.presto.common.PageBuilder)1