public long getEstimatedMemorySizeInBytes() { return INSTANCE_SIZE + ogcGeometry.estimateMemorySize(); } }
private static long getGeometryMemorySize(OGCGeometry geometry) { if (geometry == null) { return 0; } // Due to the following issue: // https://github.com/Esri/geometry-api-java/issues/192 // We must check if the geometry is empty before calculating its size. Once the issue is resolved // and we bring the fix into our codebase, we can remove this check. if (geometry.isEmpty()) { return OGC_GEOMETRY_BASE_INSTANCE_SIZE; } return geometry.estimateMemorySize(); }
private static void verifySameSpatialReference(OGCGeometry leftGeometry, OGCGeometry rightGeometry) { checkArgument(Objects.equals(leftGeometry.getEsriSpatialReference(), rightGeometry.getEsriSpatialReference()), "Input geometries must have the same spatial reference"); }
@Description("Returns the cardinality of the geometry collection") @ScalarFunction("ST_NumGeometries") @SqlType(INTEGER) public static long stNumGeometries(@SqlType(GEOMETRY_TYPE_NAME) Slice input) { OGCGeometry geometry = deserialize(input); if (geometry.isEmpty()) { return 0; } GeometryType type = GeometryType.getForEsriGeometryType(geometry.geometryType()); if (!type.isMultitype()) { return 1; } return ((OGCGeometryCollection) geometry).numGeometries(); }
private static Point computeMultiPolygonCentroid(OGCMultiPolygon multiPolygon) { double xSum = 0; double ySum = 0; double weightSum = 0; for (int i = 0; i < multiPolygon.numGeometries(); i++) { Point centroid = computePolygonCentroid((Polygon) multiPolygon.geometryN(i).getEsriGeometry()); Polygon polygon = (Polygon) multiPolygon.geometryN(i).getEsriGeometry(); double weight = polygon.calculateArea2D(); weightSum += weight; xSum += centroid.getX() * weight; ySum += centroid.getY() * weight; } return new Point(xSum / weightSum, ySum / weightSum); }
private static OGCGeometry geometryFromText(Slice input) { OGCGeometry geometry; try { geometry = OGCGeometry.fromText(input.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid WKT: " + input.toStringUtf8(), e); } geometry.setSpatialReference(null); return geometry; }
@Benchmark public Object stContainsInnerPointDeserialized(BenchmarkData data) { return data.ogcGeometry.contains(data.innerOgcPoint); }
@Override public boolean hasNext() { if (geometriesDeque.isEmpty()) { return false; } while (geometriesDeque.peek() instanceof OGCConcreteGeometryCollection) { OGCGeometryCollection collection = (OGCGeometryCollection) geometriesDeque.pop(); for (int i = 0; i < collection.numGeometries(); i++) { geometriesDeque.push(collection.geometryN(i)); } } return !geometriesDeque.isEmpty(); }
private static Slice geometryFromText(String wkt) { return serialize(OGCGeometry.fromText(wkt)); }
private static void accelerateGeometry(OGCGeometry ogcGeometry, Operator relateOperator) { // Recurse into GeometryCollections GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor(); while (true) { com.esri.core.geometry.Geometry esriGeometry = cursor.next(); if (esriGeometry == null) { break; } relateOperator.accelerateGeometry(esriGeometry, null, Geometry.GeometryAccelerationDegree.enumMild); } }
@Override public Object getObjectValue(ConnectorSession session, Block block, int position) { if (block.isNull(position)) { return null; } Slice slice = block.getSlice(position, 0, block.getSliceLength(position)); return deserialize(slice).asText(); } }
private static void assertGeometryEquals(OGCGeometry actual, OGCGeometry expected) { actual.setSpatialReference(null); expected.setSpatialReference(null); ensureEnvelopeLoaded(actual); ensureEnvelopeLoaded(expected); assertEquals(actual, expected); }
/** * There is a weird bug in geometry comparison. If a geometry envelope is not loaded it may return * false for two empty line strings or multiline strings */ private static void ensureEnvelopeLoaded(OGCGeometry geometry) { geometry.envelope(); } }
private static Iterable<OGCGeometry> flattenCollection(OGCGeometry geometry) { if (geometry == null) { return ImmutableList.of(); } if (!(geometry instanceof OGCConcreteGeometryCollection)) { return ImmutableList.of(geometry); } if (((OGCConcreteGeometryCollection) geometry).numGeometries() == 0) { return ImmutableList.of(); } return () -> new GeometryCollectionIterator(geometry); }
@Benchmark public Object stContainsOuterPointInEnvelopeDeserialized(BenchmarkData data) { return data.ogcGeometry.contains(data.outerOgcPointInEnvelope); }
private static void testEsriSerialization(String wkt) { OGCGeometry expected = OGCGeometry.fromText(wkt); OGCGeometry actual = deserialize(serialize(expected)); assertGeometryEquals(actual, expected); }
public static boolean disjoint(Envelope envelope, OGCGeometry ogcGeometry) { GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor(); while (true) { Geometry geometry = cursor.next(); if (geometry == null) { return true; } if (!GeometryEngine.disjoint(geometry, envelope, null)) { return false; } } }
@Override public Object getObjectValue(ConnectorSession session, Block block, int position) { if (block.isNull(position)) { return null; } Slice slice = block.getSlice(position, 0, block.getSliceLength(position)); return deserialize(slice).asText(); } }
@Benchmark public Object stContainsOuterPointNotInEnvelopeDeserialized(BenchmarkData data) { return data.ogcGeometry.contains(data.outerOgcPointNotInEnvelope); }
public static boolean contains(OGCGeometry ogcGeometry, Envelope envelope) { GeometryCursor cursor = ogcGeometry.getEsriGeometryCursor(); while (true) { Geometry geometry = cursor.next(); if (geometry == null) { return false; } if (GeometryEngine.contains(geometry, envelope, null)) { return true; } } }