@SqlNullable @Description("Returns X minima of a bounding box of a Geometry") @ScalarFunction("ST_XMin") @SqlType(DOUBLE) public static Double stXMin(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { Envelope envelope = deserializeEnvelope(input); if (envelope == null) { return null; } return envelope.getXMin(); }
private static Envelope getEnvelope(OGCGeometry ogcGeometry) { com.esri.core.geometry.Envelope env = new com.esri.core.geometry.Envelope(); ogcGeometry.getEsriGeometry().queryEnvelope(env); return new Envelope(env.getXMin(), env.getXMax(), env.getYMin(), env.getYMax()); }
@InputFunction public static void input(SpatialPartitioningState state, @SqlType(GEOMETRY_TYPE_NAME) Slice slice, @SqlType(INTEGER) long partitionCount) { Envelope envelope = deserializeEnvelope(slice); if (envelope == null) { return; } Rectangle extent = new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax()); if (state.getCount() == 0) { state.setPartitionCount(toIntExact(partitionCount)); state.setExtent(extent); state.setSamples(new ArrayList<>()); } else { state.setExtent(state.getExtent().merge(extent)); } // use reservoir sampling 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); }
private static Envelope getEnvelope(OGCGeometry ogcGeometry, double radius) { com.esri.core.geometry.Envelope envelope = new com.esri.core.geometry.Envelope(); ogcGeometry.getEsriGeometry().queryEnvelope(envelope); return new Envelope(envelope.getXMin() - radius, envelope.getXMax() + radius, envelope.getYMin() - radius, envelope.getYMax() + radius); }
public void eval() { com.esri.core.geometry.ogc.OGCGeometry geom1; geom1 = com.esri.core.geometry.ogc.OGCGeometry .fromBinary(geom1Param.buffer.nioBuffer(geom1Param.start, geom1Param.end - geom1Param.start)); if (geom1.geometryType().equals("Point")) { out.value = ((com.esri.core.geometry.ogc.OGCPoint) geom1).X(); } else { com.esri.core.geometry.Envelope envelope = new com.esri.core.geometry.Envelope(); geom1.getEsriGeometry().queryEnvelope(envelope); out.value = envelope.getXMin(); } } }
public static Slice serialize(Envelope envelope) { requireNonNull(envelope, "envelope is null"); verify(!envelope.isEmpty()); DynamicSliceOutput output = new DynamicSliceOutput(100); output.appendByte(GeometrySerializationType.ENVELOPE.code()); output.appendDouble(envelope.getXMin()); output.appendDouble(envelope.getYMin()); output.appendDouble(envelope.getXMax()); output.appendDouble(envelope.getYMax()); return output.slice(); }
private String getSpatialPartitioning(Rectangle extent, 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), extent, rectangles.build())); } }
@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 == null) { return null; } Rectangle expandedEnvelope2D = new Rectangle(envelope.getXMin() - distance, envelope.getYMin() - distance, envelope.getXMax() + distance, envelope.getYMax() + distance); return spatialPartitions((KdbTree) kdbTree, expandedEnvelope2D); }
@ScalarFunction @SqlNullable @Description("Returns an array of spatial partition IDs for a given geometry") @SqlType("array(int)") public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(GEOMETRY_TYPE_NAME) Slice geometry) { Envelope envelope = deserializeEnvelope(geometry); if (envelope == null) { // Empty geometry return null; } return spatialPartitions((KdbTree) kdbTree, new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax())); }
@Description("Returns the Geometry value that represents the point set intersection of two Geometries") @ScalarFunction("ST_Intersection") @SqlType(GEOMETRY_TYPE_NAME) public static Slice stIntersection(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right) { if (deserializeType(left) == GeometrySerializationType.ENVELOPE && deserializeType(right) == GeometrySerializationType.ENVELOPE) { Envelope leftEnvelope = deserializeEnvelope(left); Envelope rightEnvelope = deserializeEnvelope(right); // Envelope#intersect updates leftEnvelope to the intersection of the two envelopes if (!leftEnvelope.intersect(rightEnvelope)) { return EMPTY_POLYGON; } Envelope intersection = leftEnvelope; if (intersection.getXMin() == intersection.getXMax()) { if (intersection.getYMin() == intersection.getYMax()) { return serialize(createFromEsriGeometry(new Point(intersection.getXMin(), intersection.getXMax()), null)); } return serialize(createFromEsriGeometry(new Polyline(new Point(intersection.getXMin(), intersection.getYMin()), new Point(intersection.getXMin(), intersection.getYMax())), null)); } if (intersection.getYMin() == intersection.getYMax()) { return serialize(createFromEsriGeometry(new Polyline(new Point(intersection.getXMin(), intersection.getYMin()), new Point(intersection.getXMax(), intersection.getYMin())), null)); } return serialize(intersection); } OGCGeometry leftGeometry = deserialize(left); OGCGeometry rightGeometry = deserialize(right); verifySameSpatialReference(leftGeometry, rightGeometry); return serialize(leftGeometry.intersection(rightGeometry)); }
@Description("Converts a Geometry object to a SphericalGeography object") @ScalarFunction("to_spherical_geography") @SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) public static Slice toSphericalGeography(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { // "every point in input is in range" <=> "the envelope of input is in range" Envelope envelope = deserializeEnvelope(input); if (envelope != null) { checkLatitude(envelope.getYMin()); checkLatitude(envelope.getYMax()); checkLongitude(envelope.getXMin()); checkLongitude(envelope.getXMax()); } OGCGeometry geometry = deserialize(input); if (geometry.is3D()) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert 3D geometry to a spherical geography"); } GeometryCursor cursor = geometry.getEsriGeometryCursor(); while (true) { com.esri.core.geometry.Geometry subGeometry = cursor.next(); if (subGeometry == null) { break; } if (!GEOMETRY_TYPES_FOR_SPHERICAL_GEOGRAPHY.contains(subGeometry.getType())) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot convert geometry of this type to spherical geography: " + subGeometry.getType()); } } return input; }
@SqlNullable @Description("Returns the lower left and upper right corners of bounding rectangular polygon of a Geometry") @ScalarFunction("ST_EnvelopeAsPts") @SqlType("array(" + GEOMETRY_TYPE_NAME + ")") public static Block stEnvelopeAsPts(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { Envelope envelope = deserializeEnvelope(input); if (envelope == null) { return null; } BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2); Point lowerLeftCorner = new Point(envelope.getXMin(), envelope.getYMin()); Point upperRightCorner = new Point(envelope.getXMax(), envelope.getYMax()); GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(lowerLeftCorner, null, false))); GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(upperRightCorner, null, false))); return blockBuilder.build(); }
checkLongitude(envelope.getXMin(), LONGITUDE_SPAN_OUT_OF_RANGE); checkLongitude(envelope.getXMax(), LONGITUDE_SPAN_OUT_OF_RANGE); BingTile leftUpperTile = latitudeLongitudeToTile(envelope.getYMax(), envelope.getXMin(), zoomLevel); BingTile rightLowerTile = getTileCoveringLowerRightCorner(envelope, zoomLevel);
@SqlNullable @Description("Returns X minima of a bounding box of a Geometry") @ScalarFunction("ST_XMin") @SqlType(DOUBLE) public static Double stXMin(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { Envelope envelope = deserializeEnvelope(input); if (envelope == null) { return null; } return envelope.getXMin(); }
private static Envelope getEnvelope(OGCGeometry ogcGeometry, double radius) { com.esri.core.geometry.Envelope envelope = new com.esri.core.geometry.Envelope(); ogcGeometry.getEsriGeometry().queryEnvelope(envelope); return new Envelope(envelope.getXMin() - radius, envelope.getXMax() + radius, envelope.getYMin() - radius, envelope.getYMax() + radius); }
private static Envelope getEnvelope(OGCGeometry ogcGeometry) { com.esri.core.geometry.Envelope env = new com.esri.core.geometry.Envelope(); ogcGeometry.getEsriGeometry().queryEnvelope(env); return new Envelope(env.getXMin(), env.getXMax(), env.getYMin(), env.getYMax()); }
private static Envelope getEnvelope(OGCGeometry ogcGeometry, double radius) { com.esri.core.geometry.Envelope envelope = new com.esri.core.geometry.Envelope(); ogcGeometry.getEsriGeometry().queryEnvelope(envelope); return new Envelope(envelope.getXMin() - radius, envelope.getXMax() + radius, envelope.getYMin() - radius, envelope.getYMax() + radius); }
public static Slice serialize(Envelope envelope) { requireNonNull(envelope, "envelope is null"); verify(!envelope.isEmpty()); DynamicSliceOutput output = new DynamicSliceOutput(100); output.appendByte(GeometrySerializationType.ENVELOPE.code()); output.appendDouble(envelope.getXMin()); output.appendDouble(envelope.getYMin()); output.appendDouble(envelope.getXMax()); output.appendDouble(envelope.getYMax()); return output.slice(); }
@ScalarFunction @SqlNullable @Description("Returns an array of spatial partition IDs for a given geometry") @SqlType("array(int)") public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(GEOMETRY_TYPE_NAME) Slice geometry) { Envelope envelope = deserializeEnvelope(geometry); if (envelope == null) { // Empty geometry return null; } return spatialPartitions((KdbTree) kdbTree, new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax())); }