Improve the country labeling

- Set a maximum font size
- Try to label polygons that are too small for the full name with the
  2-letter contry code
This commit is contained in:
Thomas Kolb 2021-06-22 22:40:21 +02:00
parent bc3e6a1436
commit 492395840d

View file

@ -10,6 +10,7 @@ import random
import argparse
LABEL_MIN_FONT_SIZE = 2
LABEL_MAX_FONT_SIZE = 40
def map_azimuthal_equidistant(lat, lon, ref_lat, ref_lon, R=1):
@ -347,23 +348,35 @@ def svg_add_country_names(doc, simplegeodata, map_radius):
'fill': v['label_color']
}
label = v['name']
# rotate text by 90 degrees if polygon is higher than wide
if h > w:
font_size = h / len(v['name'])
font_size = h / len(label)
rotate = True
else:
font_size = w / len(v['name'])
font_size = w / len(label)
rotate = False
if font_size < LABEL_MIN_FONT_SIZE:
print('.', end='', flush=True)
continue # too small
# try again with 2-letter country code
label = k
font_size = min(LABEL_MIN_FONT_SIZE*2,
(h if rotate else w) / len(label))
if font_size < LABEL_MIN_FONT_SIZE:
print('.', end='', flush=True)
continue # too small
else:
print('!', end='', flush=True)
else:
print('#', end='', flush=True)
font_size = min(LABEL_MAX_FONT_SIZE, font_size)
kwargs['font-size'] = font_size
txt = doc.text(v['name'], (center_x, center_y),
txt = doc.text(label, (center_x, center_y),
**kwargs)
if rotate: