Search in sources :

Example 16 with Rectangle

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

the class TestSpatialPartitioningInternalAggregation method getSpatialPartitioning.

private String getSpatialPartitioning(List<OGCGeometry> geometries, int partitionCount) {
    ImmutableList.Builder<Rectangle> rectangles = ImmutableList.builder();
    for (OGCGeometry geometry : geometries) {
        Envelope envelope = new Envelope();
        geometry.getEsriGeometry().queryEnvelope(envelope);
        rectangles.add(new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax()));
    }
    return KdbTreeUtils.toJson(buildKdbTree(roundToInt(geometries.size() * 1.0 / partitionCount, CEILING), rectangles.build()));
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) ImmutableList(com.google.common.collect.ImmutableList) Rectangle(com.facebook.presto.geospatial.Rectangle) Envelope(com.esri.core.geometry.Envelope)

Example 17 with Rectangle

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

the class GeoFunctions 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(GEOMETRY_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 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 18 with Rectangle

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

the class PagesRTreeIndex method testReferencePoint.

private boolean testReferencePoint(Rectangle probeEnvelope, Rectangle buildEnvelope, int partition) {
    Rectangle intersection = buildEnvelope.intersection(probeEnvelope);
    if (intersection == null) {
        return false;
    }
    Rectangle extent = partitions.get(partition);
    double x = intersection.getXMin();
    double y = intersection.getYMin();
    return x >= extent.getXMin() && x < extent.getXMax() && y >= extent.getYMin() && y < extent.getYMax();
}
Also used : Rectangle(com.facebook.presto.geospatial.Rectangle)

Example 19 with Rectangle

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

the class PagesRTreeIndex method findJoinPositions.

/**
 * Returns an array of addresses from {@link PagesIndex#getValueAddresses()} corresponding
 * to rows with matching geometries.
 * <p>
 * The caller is responsible for calling {@link #isJoinPositionEligible(int, int, Page)}
 * for each of these addresses to apply additional join filters.
 */
@Override
public int[] findJoinPositions(int probePosition, Page probe, int probeGeometryChannel, Optional<Integer> probePartitionChannel) {
    Block probeGeometryBlock = probe.getBlock(probeGeometryChannel);
    if (probeGeometryBlock.isNull(probePosition)) {
        return EMPTY_ADDRESSES;
    }
    int probePartition = probePartitionChannel.map(channel -> toIntExact(INTEGER.getLong(probe.getBlock(channel), probePosition))).orElse(-1);
    Slice slice = probeGeometryBlock.getSlice(probePosition, 0, probeGeometryBlock.getSliceLength(probePosition));
    OGCGeometry probeGeometry = deserialize(slice);
    verify(probeGeometry != null);
    if (probeGeometry.isEmpty()) {
        return EMPTY_ADDRESSES;
    }
    IntArrayList matchingPositions = new IntArrayList();
    Rectangle queryRectangle = getExtent(probeGeometry);
    boolean probeIsPoint = queryRectangle.isPointlike();
    rtree.findIntersections(queryRectangle, geometryWithPosition -> {
        OGCGeometry buildGeometry = geometryWithPosition.getGeometry();
        Rectangle buildEnvelope = geometryWithPosition.getExtent();
        if (partitions.isEmpty() || (probePartition == geometryWithPosition.getPartition() && (probeIsPoint || buildEnvelope.isPointlike() || testReferencePoint(queryRectangle, buildEnvelope, probePartition)))) {
            OptionalDouble radius = radiusChannel == -1 ? OptionalDouble.empty() : OptionalDouble.of(getRadius(geometryWithPosition.getPosition()));
            if (spatialRelationshipTest.apply(buildGeometry, probeGeometry, radius)) {
                matchingPositions.add(geometryWithPosition.getPosition());
            }
        }
    });
    return matchingPositions.toIntArray();
}
Also used : Page(com.facebook.presto.common.Page) Slice(io.airlift.slice.Slice) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) OptionalDouble(java.util.OptionalDouble) PageBuilder(com.facebook.presto.common.PageBuilder) HasExtent(com.facebook.presto.geospatial.rtree.HasExtent) Verify.verify(com.google.common.base.Verify.verify) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) AdaptiveLongBigArray(com.facebook.presto.common.array.AdaptiveLongBigArray) GeometryUtils(com.facebook.presto.geospatial.GeometryUtils) JoinUtils.channelsToPages(com.facebook.presto.operator.JoinUtils.channelsToPages) Math.toIntExact(java.lang.Math.toIntExact) SyntheticAddress.decodePosition(com.facebook.presto.operator.SyntheticAddress.decodePosition) Type(com.facebook.presto.common.type.Type) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Session(com.facebook.presto.Session) GeometryUtils.getExtent(com.facebook.presto.geospatial.GeometryUtils.getExtent) Flatbush(com.facebook.presto.geospatial.rtree.Flatbush) Rectangle(com.facebook.presto.geospatial.Rectangle) List(java.util.List) ClassLayout(org.openjdk.jol.info.ClassLayout) EsriGeometrySerde.deserialize(com.facebook.presto.geospatial.serde.EsriGeometrySerde.deserialize) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) SyntheticAddress.decodeSliceIndex(com.facebook.presto.operator.SyntheticAddress.decodeSliceIndex) Optional(java.util.Optional) Block(com.facebook.presto.common.block.Block) SpatialPredicate(com.facebook.presto.operator.SpatialIndexBuilderOperator.SpatialPredicate) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) JoinFilterFunctionFactory(com.facebook.presto.sql.gen.JoinFilterFunctionCompiler.JoinFilterFunctionFactory) OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) Slice(io.airlift.slice.Slice) Rectangle(com.facebook.presto.geospatial.Rectangle) Block(com.facebook.presto.common.block.Block) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) OptionalDouble(java.util.OptionalDouble)

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