Skip to content

Commit

Permalink
make fill tiles respect buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Nov 13, 2024
1 parent 00b0c8b commit 58907f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,24 @@ public static int countGeometries(VectorTileProto.Tile.Feature feature) {
return result;
}

/**
* Returns the encoded geometry for a polygon that fills an entire tile plus {@code buffer} pixels as a shortcut to
* avoid needing to create an extra JTS geometry for encoding.
*/
public static VectorGeometry encodeFill(double buffer) {
int min = (int) Math.round(EXTENT * buffer / 256d);
int width = EXTENT + min + min;
return new VectorGeometry(new int[]{
CommandEncoder.commandAndLength(Command.MOVE_TO, 1),
zigZagEncode(-min), zigZagEncode(-min),
CommandEncoder.commandAndLength(Command.LINE_TO, 3),
zigZagEncode(width), 0,
0, zigZagEncode(width),
zigZagEncode(-width), 0,
CommandEncoder.commandAndLength(Command.CLOSE_PATH, 1)
}, GeometryType.POLYGON, 0);
}

/**
* Adds features in a layer to this tile.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.Polygonal;
import org.locationtech.jts.geom.impl.PackedCoordinateSequence;
import org.locationtech.jts.geom.util.AffineTransformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,14 +38,6 @@
*/
public class FeatureRenderer implements Consumer<FeatureCollector.Feature>, Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureRenderer.class);
private static final VectorTile.VectorGeometry FILL = VectorTile.encodeGeometry(GeoUtils.JTS_FACTORY
.createPolygon(GeoUtils.JTS_FACTORY.createLinearRing(new PackedCoordinateSequence.Double(new double[]{
-5, -5,
261, -5,
261, 261,
-5, 261,
-5, -5
}, 2, 0))));
private final PlanetilerConfig config;
private final Consumer<RenderedFeature> consumer;
private final Stats stats;
Expand Down Expand Up @@ -282,13 +273,13 @@ private void writeTileFeatures(int zoom, long id, FeatureCollector.Feature featu
// polygons that span multiple tiles contain detail about the outer edges separate from the filled tiles, so emit
// filled tiles now
if (feature.isPolygon()) {
emitted += emitFilledTiles(id, feature, sliced);
emitted += emitFilledTiles(zoom, id, feature, sliced);
}

stats.emittedFeatures(zoom, feature.getLayer(), emitted);
}

private int emitFilledTiles(long id, FeatureCollector.Feature feature, TiledGeometry sliced) {
private int emitFilledTiles(int zoom, long id, FeatureCollector.Feature feature, TiledGeometry sliced) {
Optional<RenderedFeature.Group> groupInfo = Optional.empty();
/*
* Optimization: large input polygons that generate many filled interior tiles (i.e. the ocean), the encoder avoids
Expand All @@ -298,7 +289,7 @@ private int emitFilledTiles(long id, FeatureCollector.Feature feature, TiledGeom
VectorTile.Feature vectorTileFeature = new VectorTile.Feature(
feature.getLayer(),
id,
FILL,
VectorTile.encodeFill(feature.getBufferPixelsAtZoom(zoom)),
feature.getAttrsAtZoom(sliced.zoomLevel())
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,18 @@ void firstCoordinateOfPolygon(double x, double y) {
}
}

@ParameterizedTest
@CsvSource({
"0, 0, 256",
"1, -1, 257",
"10, -10, 266",
})
void testFill(double buffer, double min, double max) throws GeometryException {
var geom = VectorTile.encodeFill(buffer);
assertSameGeometry(rectangle(min, max), geom.decode());
assertArrayEquals(VectorTile.encodeGeometry(rectangle(min, max)).commands(), geom.commands());
}

private static void assertArrayEquals(int[] a, int[] b) {
assertEquals(
IntStream.of(a).boxed().toList(),
Expand Down

0 comments on commit 58907f7

Please sign in to comment.