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:
parent
bc3e6a1436
commit
492395840d
23
qsomap.py
23
qsomap.py
|
@ -10,6 +10,7 @@ import random
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
LABEL_MIN_FONT_SIZE = 2
|
LABEL_MIN_FONT_SIZE = 2
|
||||||
|
LABEL_MAX_FONT_SIZE = 40
|
||||||
|
|
||||||
|
|
||||||
def map_azimuthal_equidistant(lat, lon, ref_lat, ref_lon, R=1):
|
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']
|
'fill': v['label_color']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
label = v['name']
|
||||||
|
|
||||||
# rotate text by 90 degrees if polygon is higher than wide
|
# rotate text by 90 degrees if polygon is higher than wide
|
||||||
if h > w:
|
if h > w:
|
||||||
font_size = h / len(v['name'])
|
font_size = h / len(label)
|
||||||
rotate = True
|
rotate = True
|
||||||
else:
|
else:
|
||||||
font_size = w / len(v['name'])
|
font_size = w / len(label)
|
||||||
rotate = False
|
rotate = False
|
||||||
|
|
||||||
if font_size < LABEL_MIN_FONT_SIZE:
|
if font_size < LABEL_MIN_FONT_SIZE:
|
||||||
print('.', end='', flush=True)
|
# try again with 2-letter country code
|
||||||
continue # too small
|
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:
|
else:
|
||||||
print('#', end='', flush=True)
|
print('#', end='', flush=True)
|
||||||
|
|
||||||
|
font_size = min(LABEL_MAX_FONT_SIZE, font_size)
|
||||||
|
|
||||||
kwargs['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)
|
**kwargs)
|
||||||
|
|
||||||
if rotate:
|
if rotate:
|
||||||
|
|
Loading…
Reference in a new issue