Skip to content

Commit

Permalink
Use more descriptive names for variables and configuration parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerebubuth committed Nov 6, 2018
1 parent fb5565c commit a75fd59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
5 changes: 4 additions & 1 deletion queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,10 @@ post_process:
source_layer: boundaries
start_zoom: 8
end_zoom: 11
factor: 11
# pixels (256px nominal) to require per letter in the name. if lines are shorter than this,
# the name gets dropped. name pairs (e.g: :left and :right) get dropped if either are longer
# than pixels_per_letter. larger values mean fewer lines are eligible for labelling.
pixels_per_letter: 11

# drop names on boundary lines (country, region, macroregion) except zoom 7 from Natural Earth
- fn: vectordatasource.transform.drop_properties
Expand Down
32 changes: 20 additions & 12 deletions vectordatasource/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1967,10 +1967,10 @@ def _unicode_len(s):
return None


def _delete_labels_longer_than(max_label, props):
def _delete_labels_longer_than(max_label_chars, props):
"""
Delete entries in the props dict where the key starts with 'name' and the
unicode length of the value is greater than max_length.
unicode length of the value is greater than max_label_chars.
If one half of a left/right pair is too long, then the opposite in the pair
is also deleted.
Expand All @@ -1982,12 +1982,12 @@ def _delete_labels_longer_than(max_label, props):
if not k.startswith('name'):
continue

length = _unicode_len(v)
if length is None:
length_chars = _unicode_len(v)
if length_chars is None:
# huh? name isn't a string?
continue

if length <= max_label:
if length_chars <= max_label_chars:
continue

to_delete.add(k)
Expand All @@ -2013,7 +2013,8 @@ def drop_names_on_short_boundaries(ctx):
layer_name = params.required('source_layer')
start_zoom = params.optional('start_zoom', typ=int, default=0)
end_zoom = params.optional('end_zoom', typ=int)
factor = params.optional('factor', typ=(int, float), default=10.0)
pixels_per_letter = params.optional('pixels_per_letter', typ=(int, float),
default=10.0)

layer = _find_layer(ctx.feature_layers, layer_name)
zoom = ctx.nominal_zoom
Expand All @@ -2022,26 +2023,33 @@ def drop_names_on_short_boundaries(ctx):
(end_zoom is not None and zoom >= end_zoom):
return None

tolerance = factor * tolerance_for_zoom(zoom)
# tolerance for zoom gives us a value in meters for a pixel, so it's
# meters per pixel
meters_per_letter = pixels_per_letter * tolerance_for_zoom(zoom)

for shape, props, fid in layer['features']:
geom_type = shape.geom_type

if geom_type in ('LineString', 'MultiLineString'):
label_shape = shape.simplify(tolerance)
# simplify to one letter size. this gets close to what might
# practically be renderable, and means we're not counting any
# sub-letter scale fractal crinklyness towards the length of
# the line.
label_shape = shape.simplify(meters_per_letter)

if geom_type == 'LineString':
shape_length = label_shape.length
shape_length_meters = label_shape.length
else:
# get the longest section to see if that's labellable - if
# not, then none of the sections could have a label and we
# can drop the names.
shape_length = max(part.length for part in label_shape)
shape_length_meters = max(part.length for part in label_shape)

# maximum number of characters we'll be able to print at this
# zoom.
max_label = int(shape_length / tolerance)
max_label_chars = int(shape_length_meters / meters_per_letter)

_delete_labels_longer_than(max_label, props)
_delete_labels_longer_than(max_label_chars, props)

return None

Expand Down

0 comments on commit a75fd59

Please sign in to comment.