Skip to content

Commit

Permalink
Add support for grid_height/grid_width as separate values
Browse files Browse the repository at this point in the history
  • Loading branch information
iandees committed Dec 7, 2021
1 parent 23786bf commit d8b7dbe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
30 changes: 28 additions & 2 deletions queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,36 @@ post_process:
params:
source_layer: places
start_zoom: 8
end_zoom: 12
end_zoom: 9
items_matching: { kind: locality }
max_items: 1
grid_width: 3
sorting_keys:
- { sort_key: 'min_zoom', reverse: False }
- { sort_key: 'collision_rank', reverse: False }
- { sort_key: 'population', reverse: True }
- { sort_key: 'id', reverse: True }
- fn: vectordatasource.transform.keep_n_features_gridded
params:
source_layer: places
start_zoom: 9
end_zoom: 11
items_matching: { kind: locality }
max_items: 1
grid_width: 6
sorting_keys:
- { sort_key: 'min_zoom', reverse: False }
- { sort_key: 'collision_rank', reverse: False }
- { sort_key: 'population', reverse: True }
- { sort_key: 'id', reverse: True }
- fn: vectordatasource.transform.keep_n_features_gridded
params:
source_layer: places
start_zoom: 11
end_zoom: 13
items_matching: { kind: locality }
max_items: 1
grid_size: 16
grid_width: 12
sorting_keys:
- { sort_key: 'min_zoom', reverse: False }
- { sort_key: 'collision_rank', reverse: False }
Expand Down
14 changes: 7 additions & 7 deletions test/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def test_not_points(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=1,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "foo"},
],
Expand Down Expand Up @@ -461,7 +461,7 @@ def test_points_keep_1(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=1,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "foo"},
],
Expand Down Expand Up @@ -499,7 +499,7 @@ def test_points_keep_1_multisort_second(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=1,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "min_zoom"},
{"sort_key": "population", "reverse": True},
Expand Down Expand Up @@ -538,7 +538,7 @@ def test_points_keep_1_multisort_minzoom(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=1,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "min_zoom"},
{"sort_key": "population", "reverse": True},
Expand Down Expand Up @@ -581,7 +581,7 @@ def test_points_keep_1_different_buckets(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=1,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "population", "reverse": True},
],
Expand Down Expand Up @@ -625,7 +625,7 @@ def test_points_keep_more_than_in_one_bucket(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=5,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "min_zoom", "reverse": True},
{"sort_key": "population"},
Expand Down Expand Up @@ -667,7 +667,7 @@ def test_fail_on_non_integer_reverse_sort_key(self):
source_layer="test_layer",
items_matching=dict(foo="bar"),
max_items=5,
grid_size=2,
grid_width=2,
sorting_keys=[
{"sort_key": "population", "reverse": True},
],
Expand Down
17 changes: 10 additions & 7 deletions vectordatasource/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3120,9 +3120,10 @@ def keep_n_features_gridded(ctx):
pairs in `items_matching` into a grid, then keep the
first `max_items` features in each grid cell.
The grid is created by dividing the width and height
of the tile into `grid_size` buckets (so you end up
with grid_size*grid_size buckets total).
The grid is created by dividing the tile into buckets.
You can specify the `grid_width` and `grid_height` to
get grid_width*grid_height buckets or just `grid_width`
to get grid_width*grid_width buckets.
NOTE: This only works with point features and will
pass through non-point features untouched.
Expand All @@ -3139,15 +3140,17 @@ def keep_n_features_gridded(ctx):
end_zoom = ctx.params.get('end_zoom')
items_matching = ctx.params.get('items_matching')
max_items = ctx.params.get('max_items')
grid_size = ctx.params.get('grid_size')
grid_width = ctx.params.get('grid_width')
# if grid_height is not specified, use grid_width for grid_height
grid_height = ctx.params.get('grid_height') or grid_width
sorting_keys = ctx.params.get('sorting_keys')

# leaving items_matching, grid_size, or max_items as None (or zero)
# would mean that this filter would do nothing, so assume
# that this is really a configuration error.
assert items_matching, 'keep_n_features_gridded: missing or empty item match dict'
assert max_items, 'keep_n_features_gridded: missing or zero max number of items'
assert grid_size, 'keep_n_features_gridded: missing or zero grid size'
assert grid_width, 'keep_n_features_gridded: missing or zero grid width'
assert sorting_keys, 'keep_n_features_gridded: missing sorting keys'
assert isinstance(sorting_keys, list), 'keep_n_features_gridded: sorting keys should be a list'

Expand All @@ -3166,8 +3169,8 @@ def keep_n_features_gridded(ctx):
return None

minx, miny, maxx, maxy = ctx.unpadded_bounds
bucket_width = (maxx - minx) / grid_size
bucket_height = (maxy - miny) / grid_size
bucket_width = (maxx - minx) / grid_width
bucket_height = (maxy - miny) / grid_height

# Sort the features into buckets
buckets = defaultdict(list)
Expand Down

0 comments on commit d8b7dbe

Please sign in to comment.