|
| 1 | +package com.protomaps.basemap.postprocess; |
| 2 | + |
| 3 | +import static com.onthegomap.planetiler.TestUtils.newLineString; |
| 4 | +import static com.onthegomap.planetiler.TestUtils.newPolygon; |
| 5 | +import static org.junit.jupiter.api.Assertions.*; |
| 6 | + |
| 7 | +import com.onthegomap.planetiler.VectorTile; |
| 8 | +import com.onthegomap.planetiler.geo.GeometryException; |
| 9 | +import com.onthegomap.planetiler.geo.TileCoord; |
| 10 | +import com.onthegomap.planetiler.reader.FileFormatException; |
| 11 | +import com.onthegomap.planetiler.stats.Stats; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +class ClipTest { |
| 19 | + private final Stats stats = Stats.inMemory(); |
| 20 | + |
| 21 | + @Test |
| 22 | + void testLoadGeoJSON() { |
| 23 | + Path cwd = Path.of("").toAbsolutePath(); |
| 24 | + Path pathFromRoot = Path.of("tiles", "src", "test", "resources", "clip.geojson"); |
| 25 | + var clip = Clip.fromGeoJSONFile(stats, 0, 0, cwd.resolveSibling(pathFromRoot)); |
| 26 | + assertNotNull(clip); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void testLoadNonJSON() { |
| 31 | + Path cwd = Path.of("").toAbsolutePath(); |
| 32 | + Path pathFromRoot = Path.of("tiles", "src", "test", "resources", "empty.geojson"); |
| 33 | + assertThrows(FileFormatException.class, () -> { |
| 34 | + Clip.fromGeoJSONFile(stats, 0, 0, cwd.resolveSibling(pathFromRoot)); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testClipLine() throws GeometryException { |
| 40 | + List<VectorTile.Feature> unclipped = new ArrayList<>(); |
| 41 | + unclipped.add(new VectorTile.Feature("layer", 1, |
| 42 | + // a horizontal line in the across the middle of the 0,0,0 tile. |
| 43 | + VectorTile.encodeGeometry(newLineString(0, 128, 256, 128)), |
| 44 | + Map.of("foo", "bar") |
| 45 | + )); |
| 46 | + |
| 47 | + // a rectangle that is 50% of the earths width, centered at null island. |
| 48 | + var n = new Clip(stats, 0, 0, newPolygon(0.25, 0.25, 0.75, 0.25, 0.75, 0.75, 0.25, 0.75, 0.25, 0.25)); |
| 49 | + var clipped = n.postProcessTile(TileCoord.ofXYZ(0, 0, 0), Map.of("layer", unclipped)); |
| 50 | + |
| 51 | + assertEquals(1, clipped.size()); |
| 52 | + assertEquals(1, clipped.get("layer").size()); |
| 53 | + assertEquals(newLineString(64, 128, 192, 128), clipped.get("layer").getFirst().geometry().decode()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testClipLineMulti() throws GeometryException { |
| 58 | + List<VectorTile.Feature> unclipped = new ArrayList<>(); |
| 59 | + unclipped.add(new VectorTile.Feature("layer", 1, |
| 60 | + // a V shape that enters and leaves the clipping square |
| 61 | + VectorTile.encodeGeometry(newLineString(32, 128, 128, 224, 224, 128)), |
| 62 | + Map.of("foo", "bar") |
| 63 | + )); |
| 64 | + |
| 65 | + // a rectangle that is 50% of the earths width, centered at null island. |
| 66 | + var n = new Clip(stats, 0, 0, newPolygon(0.25, 0.25, 0.75, 0.25, 0.75, 0.75, 0.25, 0.75, 0.25, 0.25)); |
| 67 | + var clipped = n.postProcessTile(TileCoord.ofXYZ(0, 0, 0), Map.of("layer", unclipped)); |
| 68 | + |
| 69 | + assertEquals(1, clipped.size()); |
| 70 | + assertEquals(2, clipped.get("layer").size()); |
| 71 | + assertEquals(newLineString(64, 160, 96, 192), clipped.get("layer").get(0).geometry().decode()); |
| 72 | + assertEquals(newLineString(160, 192, 192, 160), clipped.get("layer").get(1).geometry().decode()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testClipPolygon() throws GeometryException { |
| 77 | + List<VectorTile.Feature> unclipped = new ArrayList<>(); |
| 78 | + unclipped.add(new VectorTile.Feature("layer", 1, |
| 79 | + // a V shape that enters and leaves the clipping square |
| 80 | + VectorTile.encodeGeometry(newPolygon(32, 160, 96, 160, 96, 224, 32, 224, 32, 160)), |
| 81 | + Map.of("foo", "bar") |
| 82 | + )); |
| 83 | + |
| 84 | + // a rectangle that is 50% of the earths width, centered at null island. |
| 85 | + var n = new Clip(stats, 0, 0, newPolygon(0.25, 0.25, 0.75, 0.25, 0.75, 0.75, 0.25, 0.75, 0.25, 0.25)); |
| 86 | + var clipped = n.postProcessTile(TileCoord.ofXYZ(0, 0, 0), Map.of("layer", unclipped)); |
| 87 | + |
| 88 | + assertEquals(1, clipped.size()); |
| 89 | + assertEquals(1, clipped.get("layer").size()); |
| 90 | + assertEquals(newPolygon(64, 160, 96, 160, 96, 192, 64, 192, 64, 160), |
| 91 | + clipped.get("layer").getFirst().geometry().decode()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testClipBelowMinZoom() throws GeometryException { |
| 96 | + List<VectorTile.Feature> unclipped = new ArrayList<>(); |
| 97 | + unclipped.add(new VectorTile.Feature("layer", 1, |
| 98 | + VectorTile.encodeGeometry(newLineString(0, 128, 256, 128)), |
| 99 | + Map.of("foo", "bar") |
| 100 | + )); |
| 101 | + |
| 102 | + var n = new Clip(stats, 1, 1, newPolygon(0.25, 0.25, 0.75, 0.25, 0.75, 0.75, 0.25, 0.75, 0.25, 0.25)); |
| 103 | + var clipped = n.postProcessTile(TileCoord.ofXYZ(0, 0, 0), Map.of("layer", unclipped)); |
| 104 | + assertEquals(0, clipped.size()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testClipWhollyOutside() throws GeometryException { |
| 109 | + List<VectorTile.Feature> unclipped = new ArrayList<>(); |
| 110 | + unclipped.add(new VectorTile.Feature("layer", 1, |
| 111 | + VectorTile.encodeGeometry(newLineString(0, 1, 5, 1)), |
| 112 | + Map.of("foo", "bar") |
| 113 | + )); |
| 114 | + |
| 115 | + var n = new Clip(stats, 0, 0, newPolygon(0.25, 0.25, 0.75, 0.25, 0.75, 0.75, 0.25, 0.75, 0.25, 0.25)); |
| 116 | + var clipped = n.postProcessTile(TileCoord.ofXYZ(0, 0, 0), Map.of("layer", unclipped)); |
| 117 | + assertEquals(0, clipped.size()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testClipInInterior() throws GeometryException { |
| 122 | + List<VectorTile.Feature> unclipped = new ArrayList<>(); |
| 123 | + unclipped.add(new VectorTile.Feature("layer", 1, |
| 124 | + VectorTile.encodeGeometry(newLineString(0, 1, 5, 1)), |
| 125 | + Map.of("foo", "bar") |
| 126 | + )); |
| 127 | + |
| 128 | + var n = new Clip(stats, 0, 3, newPolygon(0.25, 0.25, 0.75, 0.25, 0.75, 0.75, 0.25, 0.75, 0.25, 0.25)); |
| 129 | + var clipped = n.postProcessTile(TileCoord.ofXYZ(3, 3, 3), Map.of("layer", unclipped)); |
| 130 | + assertEquals(1, clipped.size()); |
| 131 | + assertEquals(1, clipped.get("layer").size()); |
| 132 | + } |
| 133 | +} |
0 commit comments