|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.iceberg.geospatial; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 23 | + |
| 24 | +import java.nio.ByteBuffer; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.Arguments; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | + |
| 31 | +public class TestGeometryBoundsCollector { |
| 32 | + |
| 33 | + @ParameterizedTest(name = "{0}") |
| 34 | + @MethodSource("boundingBoxCases") |
| 35 | + public void testBoundingBox( |
| 36 | + String wkt, String hexWkb, GeospatialBound expectedLower, GeospatialBound expectedUpper) { |
| 37 | + GeometryBoundsCollector bounds = new GeometryBoundsCollector(); |
| 38 | + ByteBuffer wkb = decode(hexWkb); |
| 39 | + int position = wkb.position(); |
| 40 | + int limit = wkb.limit(); |
| 41 | + |
| 42 | + bounds.add(wkb); |
| 43 | + |
| 44 | + assertThat(wkb.position()).as(wkt).isEqualTo(position); |
| 45 | + assertThat(wkb.limit()).as(wkt).isEqualTo(limit); |
| 46 | + assertThat(bounds.lowerBound()).as(wkt).isEqualTo(expectedLower); |
| 47 | + assertThat(bounds.upperBound()).as(wkt).isEqualTo(expectedUpper); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testBoundsAcrossValuesWithMissingCoordinates() { |
| 52 | + GeometryBoundsCollector bounds = new GeometryBoundsCollector(); |
| 53 | + bounds.add(decode("0101000000000000000000f03f000000000000f87f")); |
| 54 | + bounds.add(decode("0101000000000000000000f87f0000000000000040")); |
| 55 | + |
| 56 | + assertThat(bounds.lowerBound()).isEqualTo(xy(1, 2)); |
| 57 | + assertThat(bounds.upperBound()).isEqualTo(xy(1, 2)); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest(name = "{0}") |
| 61 | + @MethodSource("invalidWkbCases") |
| 62 | + public void testInvalidWkb(String description, String hexWkb, String expectedMessage) { |
| 63 | + GeometryBoundsCollector bounds = new GeometryBoundsCollector(); |
| 64 | + assertThatThrownBy(() -> bounds.add(decode(hexWkb))) |
| 65 | + .isInstanceOf(IllegalArgumentException.class) |
| 66 | + .hasMessageContaining(expectedMessage); |
| 67 | + } |
| 68 | + |
| 69 | + private static Stream<Arguments> boundingBoxCases() { |
| 70 | + return Stream.of( |
| 71 | + Arguments.of("POINT EMPTY", "0101000000000000000000f87f000000000000f87f", null, null), |
| 72 | + Arguments.of( |
| 73 | + "POINT(1 2)", "0101000000000000000000f03f0000000000000040", xy(1, 2), xy(1, 2)), |
| 74 | + Arguments.of( |
| 75 | + "POINT(1 2) big endian", |
| 76 | + "00000000013ff00000000000004000000000000000", |
| 77 | + xy(1, 2), |
| 78 | + xy(1, 2)), |
| 79 | + Arguments.of( |
| 80 | + "LINESTRING(0 1,1 0,2 -1,-1 -2,0 1)", |
| 81 | + "0102000000050000000000000000000000000000000000f03f000000000000f03f" |
| 82 | + + "00000000000000000000000000000040000000000000f0bf000000000000f0bf" |
| 83 | + + "00000000000000c00000000000000000000000000000f03f", |
| 84 | + xy(-1, -2), |
| 85 | + xy(2, 1)), |
| 86 | + Arguments.of( |
| 87 | + "POLYGON((0 0,10 0,0 10,0 0),(1 1,1 2,2 1,1 1))", |
| 88 | + "010300000002000000040000000000000000000000000000000000000000000000" |
| 89 | + + "000024400000000000000000000000000000000000000000000024400000000000" |
| 90 | + + "000000000000000000000004000000000000000000f03f000000000000f03f0000" |
| 91 | + + "00000000f03f00000000000000400000000000000040000000000000f03f000000" |
| 92 | + + "000000f03f000000000000f03f", |
| 93 | + xy(0, 0), |
| 94 | + xy(10, 10)), |
| 95 | + Arguments.of( |
| 96 | + "MULTIPOINT((1 2),EMPTY,EMPTY,(3 4))", |
| 97 | + "0104000000040000000101000000000000000000f03f000000000000004001010000" |
| 98 | + + "00000000000000f87f000000000000f87f0101000000000000000000f87f00000000" |
| 99 | + + "0000f87f010100000000000000000008400000000000001040", |
| 100 | + xy(1, 2), |
| 101 | + xy(3, 4)), |
| 102 | + Arguments.of( |
| 103 | + "MULTILINESTRING((1 2,3 4),(5 6,7 8))", |
| 104 | + "010500000002000000010200000002000000000000000000f03f0000000000000040" |
| 105 | + + "0000000000000840000000000000104001020000000200000000000000000014400000" |
| 106 | + + "0000000018400000000000001c400000000000002040", |
| 107 | + xy(1, 2), |
| 108 | + xy(7, 8)), |
| 109 | + Arguments.of( |
| 110 | + "MULTIPOLYGON(EMPTY,((0 0,10 0,0 10,0 0),(1 1,1 2,2 1,1 1)))", |
| 111 | + "01060000000200000001030000000000000001030000000200000004000000000000000000000000000" |
| 112 | + + "00000000000000000000000244000000000000000000000000000000000000000000000244000000000" |
| 113 | + + "00000000000000000000000004000000000000000000f03f000000000000f03f000000000000f03f000" |
| 114 | + + "00000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f", |
| 115 | + xy(0, 0), |
| 116 | + xy(10, 10)), |
| 117 | + Arguments.of( |
| 118 | + "GEOMETRYCOLLECTION(POINT(1 2),LINESTRING EMPTY,POLYGON EMPTY," |
| 119 | + + "MULTIPOINT EMPTY,MULTILINESTRING EMPTY,MULTIPOLYGON EMPTY," |
| 120 | + + "GEOMETRYCOLLECTION(POINT EMPTY,LINESTRING EMPTY,POLYGON EMPTY," |
| 121 | + + "MULTIPOINT EMPTY,MULTILINESTRING EMPTY,MULTIPOLYGON EMPTY))", |
| 122 | + "0107000000070000000101000000000000000000f03f000000000000004001020000" |
| 123 | + + "00000000000103000000000000000104000000000000000105000000000000000106" |
| 124 | + + "000000000000000107000000060000000101000000000000000000f87f0000000000" |
| 125 | + + "00f87f01020000000000000001030000000000000001040000000000000001050000" |
| 126 | + + "0000000000010600000000000000", |
| 127 | + xy(1, 2), |
| 128 | + xy(1, 2))); |
| 129 | + } |
| 130 | + |
| 131 | + private static Stream<Arguments> invalidWkbCases() { |
| 132 | + return Stream.of( |
| 133 | + Arguments.of( |
| 134 | + "trailing data", "0101000000000000000000f03f000000000000004000", "trailing data"), |
| 135 | + Arguments.of( |
| 136 | + "invalid multi-point child", |
| 137 | + "010400000001000000010200000000000000", |
| 138 | + "expected geometry type"), |
| 139 | + Arguments.of( |
| 140 | + "unsupported Z geometry", |
| 141 | + "01e9030000000000000000f03f00000000000000400000000000000840", |
| 142 | + "only 2D geometries are supported"), |
| 143 | + Arguments.of( |
| 144 | + "unsupported M geometry", |
| 145 | + "01d1070000000000000000f03f00000000000000400000000000000840", |
| 146 | + "only 2D geometries are supported"), |
| 147 | + Arguments.of( |
| 148 | + "unsupported ZM geometry", |
| 149 | + "01b90b0000000000000000f03f000000000000004000000000000008400000000000001040", |
| 150 | + "only 2D geometries are supported"), |
| 151 | + Arguments.of("truncated point", "0101000000", "unexpected end of buffer")); |
| 152 | + } |
| 153 | + |
| 154 | + private static GeospatialBound xy(double x, double y) { |
| 155 | + return GeospatialBound.createXY(x, y); |
| 156 | + } |
| 157 | + |
| 158 | + private static ByteBuffer decode(String hex) { |
| 159 | + byte[] bytes = new byte[hex.length() / 2]; |
| 160 | + for (int i = 0; i < bytes.length; i += 1) { |
| 161 | + int offset = i * 2; |
| 162 | + bytes[i] = (byte) Integer.parseInt(hex.substring(offset, offset + 2), 16); |
| 163 | + } |
| 164 | + |
| 165 | + return ByteBuffer.wrap(bytes); |
| 166 | + } |
| 167 | +} |
0 commit comments