Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Polygon increase label grid limit(Beta version) #996

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private void renderGeometry(Geometry geom, FeatureCollector.Feature feature) {
}

private void renderPoint(FeatureCollector.Feature feature, Coordinate... origCoords) {
boolean hasLabelGrid = feature.hasLabelGrid();
Coordinate[] coords = new Coordinate[origCoords.length];
for (int i = 0; i < origCoords.length; i++) {
coords[i] = origCoords[i].copy();
Expand All @@ -114,14 +113,7 @@ private void renderPoint(FeatureCollector.Feature feature, Coordinate... origCoo

// for "label grid" point density limiting, compute the grid square that this point sits in
// only valid if not a multipoint
RenderedFeature.Group groupInfo = null;
if (hasLabelGrid && coords.length == 1) {
double labelGridTileSize = feature.getPointLabelGridPixelSizeAtZoom(zoom) / 256d;
groupInfo = labelGridTileSize < 1d / 4096d ? null : new RenderedFeature.Group(
GeoUtils.labelGridId(tilesAtZoom, labelGridTileSize, coords[0]),
feature.getPointLabelGridLimitAtZoom(zoom)
);
}
RenderedFeature.Group groupInfo = coords.length != 1 ? null : computeLabelGridGroup(feature, coords[0], zoom);

// compute the tile coordinate of every tile these points should show up in at the given buffer size
TileExtents.ForZoom extents = config.bounds().tileExtents().getForZoom(zoom);
Expand All @@ -140,6 +132,21 @@ private void renderPoint(FeatureCollector.Feature feature, Coordinate... origCoo
stats.processedElement("point", feature.getLayer());
}

private static RenderedFeature.Group computeLabelGridGroup(FeatureCollector.Feature feature, Coordinate coord,
int zoom) {
if (feature.hasLabelGrid()) {
double labelGridTileSize = feature.getPointLabelGridPixelSizeAtZoom(zoom) / 256d;
if (labelGridTileSize < 1d / 4096d) {
return null;
}
return new RenderedFeature.Group(
GeoUtils.labelGridId(1 << zoom, labelGridTileSize, coord),
feature.getPointLabelGridLimitAtZoom(zoom)
);
}
return null;
}

private void encodeAndEmitFeature(FeatureCollector.Feature feature, long id, Map<String, Object> attrs,
TileCoord tile, Geometry geom, RenderedFeature.Group groupInfo, int scale) {
consumer.accept(new RenderedFeature(
Expand Down Expand Up @@ -231,11 +238,11 @@ private void renderLineOrPolygonGeometry(FeatureCollector.Feature feature, Geome
attrs = new HashMap<>(attrs);
attrs.put(numPointsAttr, geom.getNumPoints());
}
writeTileFeatures(z, feature.getId(), feature, sliced, attrs);
writeTileFeatures(z, feature.getId(), feature, sliced, attrs, geom.getCoordinate());
}

private void writeTileFeatures(int zoom, long id, FeatureCollector.Feature feature, TiledGeometry sliced,
Map<String, Object> attrs) {
Map<String, Object> attrs, Coordinate coordinate) {
int emitted = 0;
for (var entry : sliced.getTileData().entrySet()) {
TileCoord tile = entry.getKey();
Expand Down Expand Up @@ -271,7 +278,8 @@ private void writeTileFeatures(int zoom, long id, FeatureCollector.Feature featu
}

if (!geom.isEmpty()) {
encodeAndEmitFeature(feature, id, attrs, tile, geom, null, scale);
RenderedFeature.Group groupInfo = computeLabelGridGroup(feature, coordinate, zoom);
encodeAndEmitFeature(feature, id, attrs, tile, geom, groupInfo, scale);
emitted++;
}
} catch (GeometryException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,87 @@ void testLabelGridLimit() throws Exception {
), results.tiles);
}

@Test
void testLabelGridLimitLine() throws Exception {
double y = 0.5 + Z14_WIDTH / 4;
double lat = GeoUtils.getWorldLat(y);

double x1 = 0.5 + Z14_WIDTH / 4;
double lng1 = GeoUtils.getWorldLon(x1);
double lng2 = GeoUtils.getWorldLon(x1 + Z14_WIDTH * 10d / 256);
double lng3 = GeoUtils.getWorldLon(x1 + Z14_WIDTH * 20d / 256);
double lng4 = GeoUtils.getWorldLon(x1 + Z14_WIDTH * 30d / 256);

var results = runWithReaderFeatures(
Map.of("threads", "1"),
List.of(
newReaderFeature(newLineString(lng1, lat, lng2, lat), Map.of("rank", "1")),
newReaderFeature(newLineString(lng2, lat, lng3, lat), Map.of("rank", "2")),
newReaderFeature(newLineString(lng3, lat, lng4, lat), Map.of("rank", "3"))
),
(in, features) -> features.line("layer")
.setZoomRange(13, 14)
.inheritAttrFromSource("rank")
.setSortKey(Integer.parseInt(in.getTag("rank").toString()))
.setPointLabelGridSizeAndLimit(13, 128, 2)
.setBufferPixels(128)
);

assertSubmap(Map.of(
TileCoord.ofXYZ(Z14_TILES / 2, Z14_TILES / 2, 14), List.of(
feature(newLineString(64, 64, 74, 64), Map.of("rank", "1")),
feature(newLineString(74, 64, 84, 64), Map.of("rank", "2")),
feature(newLineString(84, 64, 94, 64), Map.of("rank", "3"))
),
TileCoord.ofXYZ(Z13_TILES / 2, Z13_TILES / 2, 13), List.of(
// omit rank=3 due to label grid size
feature(newLineString(32, 32, 37, 32), Map.of("rank", "1")),
feature(newLineString(37, 32, 42, 32), Map.of("rank", "2"))
)
), results.tiles);
}

@Test
void testLabelGridLimitLPolygon() throws Exception {
double y = 0.5 + Z14_WIDTH / 2;
double lat = GeoUtils.getWorldLat(y);
double lat2 = GeoUtils.getWorldLat(y - Z14_WIDTH * 10d / 256);

double x1 = 0.5 + Z14_WIDTH / 4;
double lng1 = GeoUtils.getWorldLon(x1);
double lng2 = GeoUtils.getWorldLon(x1 + Z14_WIDTH * 10d / 256);
double lng3 = GeoUtils.getWorldLon(x1 + Z14_WIDTH * 20d / 256);
double lng4 = GeoUtils.getWorldLon(x1 + Z14_WIDTH * 30d / 256);

var results = runWithReaderFeatures(
Map.of("threads", "1"),
List.of(
newReaderFeature(rectangle(lng1, lat, lng2, lat2), Map.of("rank", "1")),
newReaderFeature(rectangle(lng2, lat, lng3, lat2), Map.of("rank", "2")),
newReaderFeature(rectangle(lng3, lat, lng4, lat2), Map.of("rank", "3"))
),
(in, features) -> features.polygon("layer")
.setZoomRange(13, 14)
.inheritAttrFromSource("rank")
.setSortKey(Integer.parseInt(in.getTag("rank").toString()))
.setPointLabelGridSizeAndLimit(13, 128, 2)
.setBufferPixels(128)
);

assertSubmap(Map.of(
TileCoord.ofXYZ(Z14_TILES / 2, Z14_TILES / 2, 14), List.of(
feature(rectangle(64, 128, 74, 118), Map.of("rank", "1")),
feature(rectangle(74, 128, 84, 118), Map.of("rank", "2")),
feature(rectangle(84, 128, 94, 118), Map.of("rank", "3"))
),
TileCoord.ofXYZ(Z13_TILES / 2, Z13_TILES / 2, 13), List.of(
// omit rank=3 due to label grid size
feature(rectangle(32, 64, 37, 59), Map.of("rank", "1")),
feature(rectangle(37, 64, 42, 59), Map.of("rank", "2"))
)
), results.tiles);
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testLineString(boolean anyGeom) throws Exception {
Expand Down
Loading