Skip to content

Commit dba56b9

Browse files
authored
Merge pull request #2040 from tilezen/dees/tweak-thinning
Make locality thinning grids smaller
2 parents f7994f6 + 2eca280 commit dba56b9

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

integration-test/1999-keep-n-gridded.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ def test_thinned_tokyo(self):
2525
# We should thin out most of the data at zoom 8
2626
self.assert_n_matching_features(
2727
8, 227, 100, 'places',
28-
{'kind': 'locality'}, 134)
28+
{'kind': 'locality'}, 9)
2929

3030
# Zoom 11 should have Tokyo
3131
self.assert_has_feature(
3232
11, 1819, 806, 'places',
3333
{'kind': 'locality', 'id': 4})
34-
# .. and SHOULD have the nearby small locality Chiyoda
34+
self.assert_n_matching_features(
35+
11, 1819, 806, 'places',
36+
{'kind': 'locality'}, 5)
37+
# .. and SHOULD have the nearby smaller locality
3538
self.assert_has_feature(
3639
11, 1819, 806, 'places',
37-
{'kind': 'locality', 'id': 143})
40+
{'kind': 'locality', 'id': 66})

queries.yaml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ post_process:
10521052
end_zoom: 9
10531053
items_matching: { kind: locality }
10541054
max_items: 1
1055-
grid_width: 3
1055+
grid_width_meters: 52181.0113
10561056
sorting_keys:
10571057
- { sort_key: 'min_zoom', reverse: False }
10581058
- { sort_key: 'collision_rank', reverse: False }
@@ -1062,10 +1062,23 @@ post_process:
10621062
params:
10631063
source_layer: places
10641064
start_zoom: 9
1065+
end_zoom: 10
1066+
items_matching: { kind: locality }
1067+
max_items: 1
1068+
grid_width_meters: 26090.5056
1069+
sorting_keys:
1070+
- { sort_key: 'min_zoom', reverse: False }
1071+
- { sort_key: 'collision_rank', reverse: False }
1072+
- { sort_key: 'population', reverse: True }
1073+
- { sort_key: 'id', reverse: True }
1074+
- fn: vectordatasource.transform.keep_n_features_gridded
1075+
params:
1076+
source_layer: places
1077+
start_zoom: 10
10651078
end_zoom: 11
10661079
items_matching: { kind: locality }
10671080
max_items: 1
1068-
grid_width: 6
1081+
grid_width_meters: 13045.2528
10691082
sorting_keys:
10701083
- { sort_key: 'min_zoom', reverse: False }
10711084
- { sort_key: 'collision_rank', reverse: False }
@@ -1075,10 +1088,23 @@ post_process:
10751088
params:
10761089
source_layer: places
10771090
start_zoom: 11
1091+
end_zoom: 12
1092+
items_matching: { kind: locality }
1093+
max_items: 1
1094+
grid_width_meters: 6522.1264
1095+
sorting_keys:
1096+
- { sort_key: 'min_zoom', reverse: False }
1097+
- { sort_key: 'collision_rank', reverse: False }
1098+
- { sort_key: 'population', reverse: True }
1099+
- { sort_key: 'id', reverse: True }
1100+
- fn: vectordatasource.transform.keep_n_features_gridded
1101+
params:
1102+
source_layer: places
1103+
start_zoom: 12
10781104
end_zoom: 13
10791105
items_matching: { kind: locality }
10801106
max_items: 1
1081-
grid_width: 12
1107+
grid_width_meters: 3261.0632
10821108
sorting_keys:
10831109
- { sort_key: 'min_zoom', reverse: False }
10841110
- { sort_key: 'collision_rank', reverse: False }

vectordatasource/transform.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,13 +3122,10 @@ def keep_n_features_gridded(ctx):
31223122
pairs in `items_matching` into a grid, then keep the
31233123
first `max_items` features in each grid cell.
31243124
3125-
The grid is created by dividing the tile into buckets.
3126-
You can specify the `grid_width` and `grid_height` to
3127-
get grid_width*grid_height buckets or just `grid_width`
3128-
to get grid_width*grid_width buckets.
3129-
3130-
This may impact "256" and "512" sized tiles differently,
3131-
so it might be worth checking both sizes.
3125+
The grid is created by dividing the bounds into cells.
3126+
The `grid_width_meters` and `grid_height_meters` params
3127+
specify the width and height (in mercator meters) of
3128+
each grid cell.
31323129
31333130
NOTE: This only works with point features and will
31343131
pass through non-point features untouched.
@@ -3145,9 +3142,10 @@ def keep_n_features_gridded(ctx):
31453142
end_zoom = ctx.params.get('end_zoom')
31463143
items_matching = ctx.params.get('items_matching')
31473144
max_items = ctx.params.get('max_items')
3148-
grid_width = ctx.params.get('grid_width')
3149-
# if grid_height is not specified, use grid_width for grid_height
3150-
grid_height = ctx.params.get('grid_height') or grid_width
3145+
grid_width = ctx.params.get('grid_width_meters')
3146+
# if grid_height_meters is not specified, use grid_width_meters
3147+
# for grid_height_meters
3148+
grid_height = ctx.params.get('grid_height_meters') or grid_width
31513149
sorting_keys = ctx.params.get('sorting_keys')
31523150

31533151
# leaving items_matching, grid_size, or max_items as None (or zero)
@@ -3174,8 +3172,6 @@ def keep_n_features_gridded(ctx):
31743172
return None
31753173

31763174
minx, miny, maxx, maxy = ctx.unpadded_bounds
3177-
bucket_width = (maxx - minx) / grid_width
3178-
bucket_height = (maxy - miny) / grid_height
31793175

31803176
# Sort the features into buckets
31813177
buckets = defaultdict(list)
@@ -3189,8 +3185,8 @@ def keep_n_features_gridded(ctx):
31893185
# Calculate the bucket to put this feature in.
31903186
# Note that this purposefully allows for buckets outside the unpadded bounds
31913187
# so we can bucketize the padding area, too.
3192-
bucket_x = int((shape.x - minx) / bucket_width)
3193-
bucket_y = int((shape.y - miny) / bucket_height)
3188+
bucket_x = int((shape.x - minx) / grid_width)
3189+
bucket_y = int((shape.y - miny) / grid_height)
31943190
bucket_id = (bucket_x, bucket_y)
31953191

31963192
buckets[bucket_id].append((shape, props, fid))

0 commit comments

Comments
 (0)