Skip to content

Commit

Permalink
Use empty Envelope instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and martint committed Feb 7, 2020
1 parent ce02652 commit 8d2e230
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
Expand Up @@ -69,10 +69,7 @@ public static Slice serialize(Envelope envelope)
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());
writeEnvelopeCoordinates(output, envelope);
return output.slice();
}

Expand Down Expand Up @@ -307,7 +304,7 @@ private static Envelope getEnvelope(BasicSliceInput input, GeometrySerialization

private static Envelope getGeometryCollectionOverallEnvelope(BasicSliceInput input)
{
Envelope overallEnvelope = null;
Envelope overallEnvelope = new Envelope();
while (input.available() > 0) {
int length = input.readInt() - 1;
GeometrySerializationType type = GeometrySerializationType.getForCode(input.readByte());
Expand All @@ -322,28 +319,20 @@ private static Envelope getSimpleGeometryEnvelope(BasicSliceInput input, int len
// skip type injected by esri
input.readInt();

double xMin = input.readDouble();
double yMin = input.readDouble();
double xMax = input.readDouble();
double yMax = input.readDouble();
Envelope envelope = readEnvelope(input);

int skipLength = length - (4 * Double.BYTES) - Integer.BYTES;
verify(input.skip(skipLength) == skipLength);

if (isEsriNaN(xMin) || isEsriNaN(yMin) || isEsriNaN(xMax) || isEsriNaN(yMax)) {
// TODO: isn't it better to return empty envelope instead?
return null;
}
return new Envelope(xMin, yMin, xMax, yMax);
return envelope;
}

private static Envelope getPointEnvelope(BasicSliceInput input)
{
double x = input.readDouble();
double y = input.readDouble();
if (isNaN(x) || isNaN(y)) {
// TODO: isn't it better to return empty envelope instead?
return null;
return new Envelope();
}
return new Envelope(x, y, x, y);
}
Expand All @@ -355,9 +344,28 @@ private static Envelope readEnvelope(SliceInput input)
double yMin = input.readDouble();
double xMax = input.readDouble();
double yMax = input.readDouble();
if (isEsriNaN(xMin) || isEsriNaN(yMin) || isEsriNaN(xMax) || isEsriNaN(yMax)) {
return new Envelope();
}
return new Envelope(xMin, yMin, xMax, yMax);
}

private static void writeEnvelopeCoordinates(DynamicSliceOutput output, Envelope envelope)
{
if (envelope.isEmpty()) {
output.appendDouble(NaN);
output.appendDouble(NaN);
output.appendDouble(NaN);
output.appendDouble(NaN);
}
else {
output.appendDouble(envelope.getXMin());
output.appendDouble(envelope.getYMin());
output.appendDouble(envelope.getXMax());
output.appendDouble(envelope.getYMax());
}
}

@Nullable
private static Envelope merge(@Nullable Envelope left, @Nullable Envelope right)
{
Expand Down
Expand Up @@ -154,9 +154,9 @@ public void testDeserializeEnvelope()
assertDeserializeEnvelope("POLYGON ((0 0, 0 4, 4 0))", new Envelope(0, 0, 4, 4));
assertDeserializeEnvelope("MULTIPOLYGON (((0 0 , 0 2, 2 2, 2 0)), ((2 2, 2 4, 4 4, 4 2)))", new Envelope(0, 0, 4, 4));
assertDeserializeEnvelope("GEOMETRYCOLLECTION (POINT (3 7), LINESTRING (4 6, 7 10))", new Envelope(3, 6, 7, 10));
assertDeserializeEnvelope("POLYGON EMPTY", null);
assertDeserializeEnvelope("POLYGON EMPTY", new Envelope());
assertDeserializeEnvelope("POINT (1 2)", new Envelope(1, 2, 1, 2));
assertDeserializeEnvelope("POINT EMPTY", null);
assertDeserializeEnvelope("POINT EMPTY", new Envelope());
assertDeserializeEnvelope("GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (POINT (2 7), LINESTRING (4 6, 7 10)), POINT (3 7), LINESTRING (4 6, 7 10))", new Envelope(2, 6, 7, 10));
}

Expand Down
Expand Up @@ -334,7 +334,7 @@ public static Slice toSphericalGeography(@SqlType(GEOMETRY_TYPE_NAME) Slice inpu
{
// "every point in input is in range" <=> "the envelope of input is in range"
Envelope envelope = deserializeEnvelope(input);
if (envelope != null) {
if (!envelope.isEmpty()) {
checkLatitude(envelope.getYMin());
checkLatitude(envelope.getYMax());
checkLongitude(envelope.getXMin());
Expand Down Expand Up @@ -505,8 +505,7 @@ public static Boolean stIsClosed(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
@SqlType(BOOLEAN)
public static Boolean stIsEmpty(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
return envelope == null || envelope.isEmpty();
return deserializeEnvelope(input).isEmpty();
}

@Description("Returns TRUE if this Geometry has no anomalous geometric points, such as self intersection or self tangency")
Expand Down Expand Up @@ -738,7 +737,7 @@ private static List<Point> interpolatePoints(OGCGeometry geometry, double fracti
public static Double stXMax(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
if (envelope.isEmpty()) {
return null;
}
return envelope.getXMax();
Expand All @@ -751,7 +750,7 @@ public static Double stXMax(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
public static Double stYMax(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
if (envelope.isEmpty()) {
return null;
}
return envelope.getYMax();
Expand All @@ -764,7 +763,7 @@ public static Double stYMax(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
public static Double stXMin(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
if (envelope.isEmpty()) {
return null;
}
return envelope.getXMin();
Expand All @@ -777,7 +776,7 @@ public static Double stXMin(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
public static Double stYMin(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
if (envelope.isEmpty()) {
return null;
}
return envelope.getYMin();
Expand Down Expand Up @@ -1138,7 +1137,7 @@ public static Slice stBoundary(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
public static Slice stEnvelope(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
if (envelope.isEmpty()) {
return EMPTY_POLYGON;
}
return serialize(envelope);
Expand All @@ -1151,7 +1150,7 @@ public static Slice stEnvelope(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
public static Block stEnvelopeAsPts(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
Envelope envelope = deserializeEnvelope(input);
if (envelope == null) {
if (envelope.isEmpty()) {
return null;
}
BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2);
Expand Down Expand Up @@ -1389,7 +1388,7 @@ public static Slice stGeometryType(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree, @SqlType(GEOMETRY_TYPE_NAME) Slice geometry)
{
Envelope envelope = deserializeEnvelope(geometry);
if (envelope == null) {
if (envelope.isEmpty()) {
// Empty geometry
return null;
}
Expand All @@ -1416,7 +1415,7 @@ public static Block spatialPartitions(@SqlType(KdbTreeType.NAME) Object kdbTree,
}

Envelope envelope = deserializeEnvelope(geometry);
if (envelope == null) {
if (envelope.isEmpty()) {
return null;
}

Expand Down Expand Up @@ -1683,7 +1682,7 @@ private static boolean envelopes(Slice left, Slice right, EnvelopesPredicate pre
{
Envelope leftEnvelope = deserializeEnvelope(left);
Envelope rightEnvelope = deserializeEnvelope(right);
if (leftEnvelope == null || rightEnvelope == null) {
if (leftEnvelope.isEmpty() || rightEnvelope.isEmpty()) {
return false;
}
return predicate.apply(leftEnvelope, rightEnvelope);
Expand Down
Expand Up @@ -48,7 +48,7 @@ private SpatialPartitioningInternalAggregateFunction() {}
public static void input(SpatialPartitioningState state, @SqlType(GEOMETRY_TYPE_NAME) Slice slice, @SqlType(INTEGER) long partitionCount)
{
Envelope envelope = deserializeEnvelope(slice);
if (envelope == null) {
if (envelope.isEmpty()) {
return;
}

Expand Down

0 comments on commit 8d2e230

Please sign in to comment.