Drawing icons for buildings; colors parsing.

This commit is contained in:
Sergey Vartanov 2015-08-03 02:35:24 +03:00
parent 6338497f6b
commit aa5221cc1b
2 changed files with 79 additions and 52 deletions

163
mapper.py
View file

@ -375,6 +375,8 @@ def draw_ways():
#floors = float(way['tags']['building:levels'])
draw_path(way['nodes'], 'fill:#D0D0C0;stroke:#AAAAAA;opacity:1.0;')
c = line_center(way['nodes'])
shapes, fill, processed = get_icon(way['tags'], scheme, '444444')
draw_shapes(shapes, True, points, c.x, c.y, fill, True, way['tags'], processed)
for tag in way['tags']:
v = way['tags'][tag]
if tag == 'building':
@ -471,8 +473,22 @@ def to_write(key):
return True
return False
def get_color(color, scheme):
if color in scheme['colors']:
return scheme['colors'][color]
else:
m = re.match('^(\\#)?(?P<color1>[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])' + \
'(?P<color2>[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])?$', color)
if m:
if 'color2' in m.groups():
return m.group('color1') + m.group('color2')
else:
return ''.join(map(lambda x: x + x, m.group('color1')))
return '444444'
def get_icon(tags, scheme, fill='444444'):
tags_hash = ','.join(tags.keys()) + ':' + ','.join(tags.values())
tags_hash = ','.join(tags.keys()) + ':' + \
','.join(map(lambda x: str(x), tags.values()))
if tags_hash in scheme['cache']:
return scheme['cache'][tags_hash]
main_icon = None
@ -510,18 +526,13 @@ def get_icon(tags, scheme, fill='444444'):
fill = scheme['colors'][element['color']]
for key in element['tags'].keys():
processed.add(key)
if 'color' in tags:
if tags['color'] in scheme['colors']:
fill = scheme['colors'][tags['color']]
processed.add('color')
for color_name in ['color', 'colour', 'building:colour']:
if color_name in tags:
fill = get_color(tags[color_name], scheme)
if fill != '444444':
processed.add(color_name)
else:
print 'No color ' + tags['color'] + '.'
if 'colour' in tags:
if tags['colour'] in scheme['colors']:
fill = scheme['colors'][tags['colour']]
processed.add('colour')
else:
print 'No color ' + tags['colour'] + '.'
print 'No color ' + tags[color_name] + '.'
if main_icon:
returned = [main_icon] + extra_icons, fill, processed
else:
@ -529,66 +540,8 @@ def get_icon(tags, scheme, fill='444444'):
scheme['cache'][tags_hash] = returned
return returned
def draw_nodes(show_missed_tags=False, overlap=14, draw=True):
print 'Draw nodes...'
start_time = datetime.datetime.now()
scheme = yaml.load(open('tags.yml'))
scheme['cache'] = {}
if overlap != 0:
points = []
node_number = 0
processed_tags = 0
skipped_tags = 0
s = sorted(node_map.keys(), key=lambda x: -node_map[x]['lat'])
for node_id in s:
node_number += 1
ui.write_line(node_number, len(node_map))
node = node_map[node_id]
flinged = flinger.fling(Geo(node['lat'], node['lon']))
x = flinged.x
y = flinged.y
def draw_shapes(shapes, overlap, points, x, y, fill, show_missed_tags, tags, processed):
text_y = 0
if 'tags' in node:
p = node['tags']
else:
p = {}
shapes, fill, processed = get_icon(p, scheme)
for k in p:
if k in processed or no_draw(k):
processed_tags += 1
else:
skipped_tags += 1
for k in []: # p:
if to_write(k):
draw_text(k + ': ' + p[k], x, y + 18 + text_y, '444444')
text_y += 10
if show_missed_tags:
for k in p:
v = p[k]
if not no_draw(k) and not k in processed:
if ('node ' + k + ': ' + v) in missed_tags:
missed_tags['node ' + k + ': ' + v] += 1
else:
missed_tags['node ' + k + ': ' + v] = 1
if not draw:
continue
if show_missed_tags:
for k in p:
if not no_draw(k) and not k in processed:
point(k, p[k], x, y, fill, text_y)
text_y += 10
xxx = -(len(shapes) - 1) * 8
if overlap != 0:
@ -607,6 +560,63 @@ def draw_nodes(show_missed_tags=False, overlap=14, draw=True):
for shape in shapes:
draw_point_shape(shape, x + xxx, y, fill)
xxx += 16
if show_missed_tags:
for k in tags:
if not no_draw(k) and not k in processed:
point(k, tags[k], x, y, fill, text_y)
text_y += 10
def draw_nodes(show_missed_tags=False, overlap=14, draw=True):
print 'Draw nodes...'
start_time = datetime.datetime.now()
node_number = 0
processed_tags = 0
skipped_tags = 0
s = sorted(node_map.keys(), key=lambda x: -node_map[x]['lat'])
for node_id in s:
node_number += 1
ui.write_line(node_number, len(node_map))
node = node_map[node_id]
flinged = flinger.fling(Geo(node['lat'], node['lon']))
x = flinged.x
y = flinged.y
if 'tags' in node:
tags = node['tags']
else:
tags = {}
shapes, fill, processed = get_icon(tags, scheme)
for k in tags:
if k in processed or no_draw(k):
processed_tags += 1
else:
skipped_tags += 1
for k in []: # tags:
if to_write(k):
draw_text(k + ': ' + tags[k], x, y + 18 + text_y, '444444')
text_y += 10
if show_missed_tags:
for k in tags:
v = tags[k]
if not no_draw(k) and not k in processed:
if ('node ' + k + ': ' + v) in missed_tags:
missed_tags['node ' + k + ': ' + v] += 1
else:
missed_tags['node ' + k + ': ' + v] = 1
if not draw:
continue
draw_shapes(shapes, overlap, points, x, y, fill, show_missed_tags, tags, processed)
ui.write_line(-1, len(node_map))
print 'Nodes drawed in ' + str(datetime.datetime.now() - start_time) + '.'
print 'Tags processed: ' + str(processed_tags) + ', tags skipped: ' + \
@ -649,16 +659,23 @@ else:
flinger = GeoFlinger(minimum, maximum, Vector(25, 25), Vector(975, 975))
print 'Done.'
#layers = construct_layers()
layers = construct_layers()
#draw_raw_nodes()
#draw_raw_ways()
icons = extract_icon.IconExtractor('icons.svg')
points = []
#sys.exit(0)
#draw_ways()
scheme = yaml.load(open('tags.yml'))
scheme['cache'] = {}
w3c_colors = yaml.load(open('colors.yml'))
for color_name in w3c_colors:
scheme['colors'][color_name] = w3c_colors[color_name]
draw_ways()
draw_nodes(show_missed_tags=True, overlap=12, draw=True)
#draw_ways()

View file

@ -84,6 +84,8 @@ tags:
icon: [fast_food]
- tags: {amenity: shop, shop: fishing}
icon: [fishing]
- tags: {shop: convenience}
icon: [shop_convenience]
- tags: {amenity: shop, shop: convenience}
icon: [shop_convenience]
- tags: {amenity: shop, shop: kiosk}
@ -95,6 +97,14 @@ tags:
color: water_border
- tags: {amenity: '*', karaoke: 'yes'}
add_icon: [microphone]
- tags: {amenity: place_of_warship, religion: chiristian}
icon: [christian]
- tags: {amenity: place_of_warship, religion: muslim}
icon: [muslim]
- tags: {amenity: place_of_warship, religion: buddhist}
icon: [buddhist]
- tags: {amenity: place_of_warship, religion: hindu}
icon: [hindu]
- tags: {building: '*', 'roof:material': metal}
icon: [metal_roof]