use of com.esri.core.geometry.Point in project calcite by apache.
the class GeoFunctions method makeLine.
private static Geom makeLine(Geom... geoms) {
final Polyline g = new Polyline();
Point p = null;
for (Geom geom : geoms) {
if (geom.g() instanceof Point) {
final Point prev = p;
p = (Point) geom.g();
if (prev != null) {
final Line line = new Line();
line.setStart(prev);
line.setEnd(p);
g.addSegment(line, false);
}
}
}
return new SimpleGeom(g);
}
use of com.esri.core.geometry.Point in project presto by prestodb.
the class TestGeoFunctions method testSTCentroid.
@Test
public void testSTCentroid() {
assertCentroid("LINESTRING EMPTY", new Point());
assertCentroid("POINT (3 5)", new Point(3, 5));
assertCentroid("MULTIPOINT (1 2, 2 4, 3 6, 4 8)", new Point(2.5, 5));
assertCentroid("LINESTRING (1 1, 2 2, 3 3)", new Point(2, 2));
assertCentroid("MULTILINESTRING ((1 1, 5 1), (2 4, 4 4))", new Point(3, 2));
assertCentroid("POLYGON ((1 1, 1 4, 4 4, 4 1, 1 1))", new Point(2.5, 2.5));
assertCentroid("POLYGON ((1 1, 5 1, 3 4, 1 1))", new Point(3, 2));
assertCentroid("MULTIPOLYGON (((1 1, 1 3, 3 3, 3 1, 1 1)), ((2 4, 2 6, 6 6, 6 4, 2 4)))", new Point(3.3333333333333335, 4));
assertCentroid("POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))", new Point(2.5416666666666665, 2.5416666666666665));
// invalid geometry
assertApproximateCentroid("MULTIPOLYGON (((4.903234300000006 52.08474289999999, 4.903234265193165 52.084742934806826, 4.903234299999999 52.08474289999999, 4.903234300000006 52.08474289999999)))", new Point(4.9032343, 52.0847429), 1e-7);
// Numerical stability tests
assertApproximateCentroid("MULTIPOLYGON (((153.492818 -28.13729, 153.492821 -28.137291, 153.492816 -28.137289, 153.492818 -28.13729)))", new Point(153.49282, -28.13729), 1e-5);
assertApproximateCentroid("MULTIPOLYGON (((153.112475 -28.360526, 153.1124759 -28.360527, 153.1124759 -28.360526, 153.112475 -28.360526)))", new Point(153.112475, -28.360526), 1e-5);
assertApproximateCentroid("POLYGON ((4.903234300000006 52.08474289999999, 4.903234265193165 52.084742934806826, 4.903234299999999 52.08474289999999, 4.903234300000006 52.08474289999999))", new Point(4.9032343, 52.0847429), 1e-6);
assertApproximateCentroid("MULTIPOLYGON (((4.903234300000006 52.08474289999999, 4.903234265193165 52.084742934806826, 4.903234299999999 52.08474289999999, 4.903234300000006 52.08474289999999)))", new Point(4.9032343, 52.0847429), 1e-6);
assertApproximateCentroid("POLYGON ((-81.0387349 29.20822, -81.039974 29.210597, -81.0410331 29.2101579, -81.0404758 29.2090879, -81.0404618 29.2090609, -81.040433 29.209005, -81.0404269 29.208993, -81.0404161 29.2089729, -81.0398001 29.20779, -81.0387349 29.20822), (-81.0404229 29.208986, -81.04042 29.2089809, -81.0404269 29.208993, -81.0404229 29.208986))", new Point(-81.039885, 29.209191), 1e-6);
}
use of com.esri.core.geometry.Point in project presto by prestodb.
the class GeoFunctions method readPointCoordinates.
private static CoordinateSequence readPointCoordinates(Block input, String functionName, boolean forbidDuplicates) {
PackedCoordinateSequenceFactory coordinateSequenceFactory = new PackedCoordinateSequenceFactory();
double[] coordinates = new double[2 * input.getPositionCount()];
double lastX = Double.NaN;
double lastY = Double.NaN;
for (int i = 0; i < input.getPositionCount(); i++) {
if (input.isNull(i)) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: null at index %s", functionName, i + 1));
}
BasicSliceInput slice = new BasicSliceInput(GEOMETRY.getSlice(input, i));
GeometrySerializationType type = GeometrySerializationType.getForCode(slice.readByte());
if (type != GeometrySerializationType.POINT) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: geometry is not a point: %s at index %s", functionName, type.toString(), i + 1));
}
double x = slice.readDouble();
double y = slice.readDouble();
if (Double.isNaN(x) || Double.isNaN(x)) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: empty point at index %s", functionName, i + 1));
}
if (forbidDuplicates && x == lastX && y == lastY) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to %s: consecutive duplicate points at index %s", functionName, i + 1));
}
lastX = x;
lastY = y;
coordinates[2 * i] = x;
coordinates[2 * i + 1] = y;
}
return coordinateSequenceFactory.create(coordinates, 2);
}
use of com.esri.core.geometry.Point in project presto by prestodb.
the class GeoFunctions method stIntersection.
@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) {
GeometrySerializationType leftType = deserializeType(left);
GeometrySerializationType rightType = deserializeType(right);
if (leftType == GeometrySerializationType.ENVELOPE && rightType == 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() || intersection.getYMin() == intersection.getYMax()) {
if (intersection.getXMin() == intersection.getXMax() && intersection.getYMin() == intersection.getYMax()) {
return EsriGeometrySerde.serialize(createFromEsriGeometry(new Point(intersection.getXMin(), intersection.getYMin()), null));
}
return EsriGeometrySerde.serialize(createFromEsriGeometry(new Polyline(new Point(intersection.getXMin(), intersection.getYMin()), new Point(intersection.getXMax(), intersection.getYMax())), null));
}
return EsriGeometrySerde.serialize(intersection);
}
// If one side is an envelope, then if it contains the other's envelope we can just return the other geometry.
if (leftType == GeometrySerializationType.ENVELOPE && deserializeEnvelope(left).contains(deserializeEnvelope(right))) {
return right;
}
if (rightType == GeometrySerializationType.ENVELOPE && deserializeEnvelope(right).contains(deserializeEnvelope(left))) {
return left;
}
OGCGeometry leftGeometry = EsriGeometrySerde.deserialize(left);
OGCGeometry rightGeometry = EsriGeometrySerde.deserialize(right);
verifySameSpatialReference(leftGeometry, rightGeometry);
return EsriGeometrySerde.serialize(leftGeometry.intersection(rightGeometry));
}
use of com.esri.core.geometry.Point in project presto by prestodb.
the class SphericalGeoFunctions method stSphericalCentroid.
@SqlNullable
@Description("Returns the Point value that is the mathematical centroid of a Spherical Geography")
@ScalarFunction("ST_Centroid")
@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME)
public static Slice stSphericalCentroid(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input) {
OGCGeometry geometry = EsriGeometrySerde.deserialize(input);
if (geometry.isEmpty()) {
return null;
}
// TODO: add support for other types e.g. POLYGON
validateSphericalType("ST_Centroid", geometry, EnumSet.of(POINT, MULTI_POINT));
if (geometry instanceof OGCPoint) {
return input;
}
OGCGeometryCollection geometryCollection = (OGCGeometryCollection) geometry;
for (int i = 0; i < geometryCollection.numGeometries(); i++) {
OGCGeometry g = geometryCollection.geometryN(i);
validateSphericalType("ST_Centroid", g, EnumSet.of(POINT));
Point p = (Point) g.getEsriGeometry();
checkLongitude(p.getX());
checkLatitude(p.getY());
}
Point centroid;
if (geometryCollection.numGeometries() == 1) {
centroid = (Point) geometryCollection.geometryN(0).getEsriGeometry();
} else {
double x3DTotal = 0;
double y3DTotal = 0;
double z3DTotal = 0;
for (int i = 0; i < geometryCollection.numGeometries(); i++) {
CartesianPoint cp = new CartesianPoint((Point) geometryCollection.geometryN(i).getEsriGeometry());
x3DTotal += cp.getX();
y3DTotal += cp.getY();
z3DTotal += cp.getZ();
}
double centroidVectorLength = Math.sqrt(x3DTotal * x3DTotal + y3DTotal * y3DTotal + z3DTotal * z3DTotal);
if (centroidVectorLength == 0.0) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Unexpected error. Average vector length adds to zero (%f, %f, %f)", x3DTotal, y3DTotal, z3DTotal));
}
centroid = new CartesianPoint(x3DTotal / centroidVectorLength, y3DTotal / centroidVectorLength, z3DTotal / centroidVectorLength).asSphericalPoint();
}
return EsriGeometrySerde.serialize(new OGCPoint(centroid, geometryCollection.getEsriSpatialReference()));
}
Aggregations