mirror of
https://github.com/enzet/map-machine.git
synced 2025-06-23 13:07:14 +02:00
Parsing tabs; missed tags drawing.
This commit is contained in:
parent
7f74f84681
commit
efe3fcfba8
2 changed files with 121 additions and 73 deletions
161
mapper.py
161
mapper.py
|
@ -41,7 +41,7 @@ output_file = svg.SVG(open(sys.argv[2], 'w+'))
|
||||||
|
|
||||||
w, h = 2000, 2000
|
w, h = 2000, 2000
|
||||||
|
|
||||||
background_color = 'EEEEEE'
|
background_color = '000000' # 'EEEEEE'
|
||||||
grass_color = 'C8DC94'
|
grass_color = 'C8DC94'
|
||||||
sand_color = 'F0E0D0'
|
sand_color = 'F0E0D0'
|
||||||
beach_color = 'F0E0C0'
|
beach_color = 'F0E0C0'
|
||||||
|
@ -52,10 +52,13 @@ water_color = 'AACCFF'
|
||||||
water_border_color = '6688BB'
|
water_border_color = '6688BB'
|
||||||
wood_color = 'B8CC84'
|
wood_color = 'B8CC84'
|
||||||
|
|
||||||
undraw = ['operator', 'contact:facebook', 'contact:phone', 'opening_hours',
|
tags_to_write = ['operator', 'opening_hours', 'cuisine', 'network', 'website',
|
||||||
'cuisine', 'network', 'website', 'contact:website', 'phone',
|
'phone', 'branch', 'route_ref', 'brand', 'ref', 'wikipedia',
|
||||||
'branch', 'route_ref', 'addr:flats', 'brand', 'ref', 'addr:street',
|
'description', 'level', 'wikidata', 'name']
|
||||||
'wikipedia', 'description', 'layer', 'level']
|
|
||||||
|
prefix_to_write = ['addr', 'contact', 'name', 'operator', 'wikipedia']
|
||||||
|
|
||||||
|
tags_to_skip = ['note', 'layer']
|
||||||
|
|
||||||
output_file.begin(w, h)
|
output_file.begin(w, h)
|
||||||
output_file.rect(0, 0, w, h, color=background_color)
|
output_file.rect(0, 0, w, h, color=background_color)
|
||||||
|
@ -75,8 +78,12 @@ def get_d_from_file(file_name):
|
||||||
if path:
|
if path:
|
||||||
return path, x, y
|
return path, x, y
|
||||||
else:
|
else:
|
||||||
print 'No such icon: ' + file_name
|
# print 'No such icon: ' + file_name
|
||||||
|
# TODO: add to missed icons
|
||||||
return 'M 4,4 L 4,10 10,10 10,4 4,4', 0, 0
|
return 'M 4,4 L 4,10 10,10 10,4 4,4', 0, 0
|
||||||
|
|
||||||
|
# Old style.
|
||||||
|
|
||||||
if os.path.isfile('icons/' + file_name + '.svg'):
|
if os.path.isfile('icons/' + file_name + '.svg'):
|
||||||
file_name = 'icons/' + file_name + '.svg'
|
file_name = 'icons/' + file_name + '.svg'
|
||||||
size = 16
|
size = 16
|
||||||
|
@ -138,8 +145,11 @@ def draw_path(nodes, style, shift=Vector()):
|
||||||
output_file.write('" style="' + style + '" />\n')
|
output_file.write('" style="' + style + '" />\n')
|
||||||
|
|
||||||
def draw_point_shape(name, x, y, fill):
|
def draw_point_shape(name, x, y, fill):
|
||||||
shape, xx, yy = get_d_from_file(name)
|
if not isinstance(name, list):
|
||||||
draw_point(shape, x, y, fill, size=16, xx=xx, yy=yy)
|
name = [name]
|
||||||
|
for one_name in name:
|
||||||
|
shape, xx, yy = get_d_from_file(one_name)
|
||||||
|
draw_point(shape, x, y, fill, size=16, xx=xx, yy=yy)
|
||||||
|
|
||||||
def draw_point(shape, x, y, fill, size=16, xx=0, yy=0):
|
def draw_point(shape, x, y, fill, size=16, xx=0, yy=0):
|
||||||
x = int(float(x))
|
x = int(float(x))
|
||||||
|
@ -449,7 +459,12 @@ def draw_raw_nodes():
|
||||||
output_file.circle(flinged.x, flinged.y, 0.2, color='FFFFFF')
|
output_file.circle(flinged.x, flinged.y, 0.2, color='FFFFFF')
|
||||||
|
|
||||||
def no_draw(key):
|
def no_draw(key):
|
||||||
return key in undraw or 'name' in key or 'addr' in key
|
if key in tags_to_write or key in tags_to_skip:
|
||||||
|
return True
|
||||||
|
for prefix in prefix_to_write:
|
||||||
|
if key[:len(prefix) + 1] == prefix + ':':
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def draw_nodes():
|
def draw_nodes():
|
||||||
print 'Draw nodes...'
|
print 'Draw nodes...'
|
||||||
|
@ -461,102 +476,122 @@ def draw_nodes():
|
||||||
y = `flinged.y`
|
y = `flinged.y`
|
||||||
text_y = 0
|
text_y = 0
|
||||||
if 'tags' in node:
|
if 'tags' in node:
|
||||||
pairs = node['tags']
|
p = node['tags']
|
||||||
else:
|
else:
|
||||||
pairs = {}
|
p = {}
|
||||||
fill = '444444'
|
fill = '444444'
|
||||||
if 'colour' in pairs:
|
if 'colour' in p:
|
||||||
if pairs['colour'] == 'blue':
|
if p['colour'] == 'blue':
|
||||||
fill='2233AA'
|
fill='2233AA'
|
||||||
radius=3
|
radius=3
|
||||||
|
|
||||||
if pairs == {}:
|
if p == {}:
|
||||||
pass
|
pass
|
||||||
elif 'amenity' in pairs:
|
elif 'amenity' in p:
|
||||||
k = 'amenity'
|
k = 'amenity'
|
||||||
v = pairs['amenity']
|
v = p['amenity']
|
||||||
if v in ['bench', 'bicycle_parking', 'cafe', 'waste_basket', 'clinic',
|
processed = set([k])
|
||||||
'restaurant', 'pharmacy', 'drinking_water', 'toilets',
|
if v in ['bench', 'bicycle_parking', 'cafe', 'waste_basket',
|
||||||
'theatre', 'bar', 'bank', 'pub', 'post_office']:
|
'clinic', 'restaurant', 'pharmacy', 'drinking_water',
|
||||||
|
'toilets', 'theatre', 'bar', 'bank', 'pub', 'post_office']:
|
||||||
draw_point_shape(v, x, y, fill)
|
draw_point_shape(v, x, y, fill)
|
||||||
elif v == 'fast_food':
|
elif v == 'fast_food':
|
||||||
main = 'fast_food'
|
shape = 'fast_food'
|
||||||
if 'operator' in pairs:
|
if 'operator' in p:
|
||||||
if pairs['operator'] == "McDonald's":
|
if p['operator'] == "McDonald's":
|
||||||
main = 'mcdonalds'
|
shape = 'mcdonalds'
|
||||||
if 'operator:en' in pairs:
|
processed.add('operator')
|
||||||
if pairs['operator:en'] == "McDonald's":
|
if 'operator:en' in p:
|
||||||
main = 'mcdonalds'
|
if p['operator:en'] == "McDonald's":
|
||||||
draw_point_shape(main, x, y, fill)
|
shape = 'mcdonalds'
|
||||||
|
processed.add('operator:en')
|
||||||
|
draw_point_shape(shape, x, y, fill)
|
||||||
elif v == 'shop':
|
elif v == 'shop':
|
||||||
if 'shop' in pairs:
|
if 'shop' in p:
|
||||||
if pairs['shop'] in ['fishing']:
|
if p['shop'] in ['fishing']:
|
||||||
draw_point_shape('shop_' + pairs['shop'], x, y, fill)
|
draw_point_shape('shop_' + p['shop'], x, y, fill)
|
||||||
elif v == 'fountain':
|
elif v == 'fountain':
|
||||||
draw_point_shape('fountain', x, y, water_border_color)
|
draw_point_shape('fountain', x, y, water_border_color)
|
||||||
elif v == 'recycling':
|
elif v == 'recycling':
|
||||||
if not 'recycling_type' in pairs:
|
if not 'recycling_type' in p:
|
||||||
draw_point_shape('recycling', x, y, fill)
|
draw_point_shape('recycling', x, y, fill)
|
||||||
else:
|
else:
|
||||||
point(k, v, flinged.x, flinged.y, fill, text_y)
|
processed.remove(k)
|
||||||
text_y += 10
|
for k in p:
|
||||||
for k in pairs:
|
if 'karaoke' in p:
|
||||||
if 'karaoke' in pairs:
|
if p['karaoke'] == 'yes':
|
||||||
if pairs['karaoke'] == 'yes':
|
|
||||||
draw_point_shape('microphone', flinged.x + 16, y, fill)
|
draw_point_shape('microphone', flinged.x + 16, y, fill)
|
||||||
else:
|
else:
|
||||||
point(k, pairs[k], x, y, fill, text_y)
|
point(k, p[k], x, y, fill, text_y)
|
||||||
elif not no_draw(k) and k != 'amenity':
|
elif not no_draw(k) and not (k in processed):
|
||||||
point(k, pairs[k], x, y, fill, text_y)
|
point(k, p[k], x, y, fill, text_y)
|
||||||
text_y += 10
|
text_y += 10
|
||||||
elif 'natural' in pairs:
|
elif 'natural' in p:
|
||||||
k = 'natural'
|
k = 'natural'
|
||||||
v = pairs['natural']
|
v = p['natural']
|
||||||
|
processed = set([k])
|
||||||
if v == 'tree':
|
if v == 'tree':
|
||||||
main = 'tree'
|
shape = 'tree'
|
||||||
if 'leaf_type' in pairs and pairs['leaf_type'] in ['broadleaved', 'needleleaved']:
|
if 'leaf_type' in p and p['leaf_type'] in ['broadleaved', 'needleleaved']:
|
||||||
main = pairs['leaf_type']
|
shape = p['leaf_type']
|
||||||
if 'denotation' in pairs and pairs['denotation'] == 'urban':
|
processed.add(p['leaf_type'])
|
||||||
draw_point_shape([main, 'urban_tree_pot'], x, y, wood_color)
|
if 'denotation' in p and p['denotation'] == 'urban':
|
||||||
else:
|
draw_point_shape([shape, 'urban_tree_pot'], x, y, wood_color)
|
||||||
draw_point_shape(main, x, y, wood_color)
|
processed.add('denotation')
|
||||||
elif v == 'cave_entrance':
|
elif v == 'cave_entrance':
|
||||||
draw_point_shape('cave', x, y, fill)
|
draw_point_shape('cave', x, y, fill)
|
||||||
elif v == 'bush':
|
elif v == 'bush':
|
||||||
draw_point_shape('bush', x, y, wood_color)
|
draw_point_shape('bush', x, y, wood_color)
|
||||||
else:
|
else:
|
||||||
point(k, v, flinged.x, flinged.y, fill, text_y)
|
processed.remove(k)
|
||||||
text_y += 10
|
for k in p:
|
||||||
elif 'entrance' in pairs:
|
if not no_draw(k) and not (k in processed):
|
||||||
|
point(k, p[k], x, y, fill, text_y)
|
||||||
|
text_y += 10
|
||||||
|
elif 'entrance' in p:
|
||||||
k = 'entrance'
|
k = 'entrance'
|
||||||
v = pairs['entrance']
|
v = p['entrance']
|
||||||
|
processed = set([k])
|
||||||
if v == 'yes':
|
if v == 'yes':
|
||||||
draw_point_shape('entrance', x, y, fill)
|
draw_point_shape('entrance', x, y, fill)
|
||||||
elif v == 'staircase':
|
elif v == 'staircase':
|
||||||
draw_point_shape('staircase', x, y, fill)
|
draw_point_shape('staircase', x, y, fill)
|
||||||
elif 'highway' in pairs:
|
else:
|
||||||
|
processed.remove(k)
|
||||||
|
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
|
||||||
|
elif 'highway' in p:
|
||||||
k = 'highway'
|
k = 'highway'
|
||||||
v = pairs['highway']
|
v = p['highway']
|
||||||
|
processed = set([k])
|
||||||
if v == 'crossing':
|
if v == 'crossing':
|
||||||
shape = 'crossing'
|
shape = 'crossing'
|
||||||
if 'crossing' in pairs:
|
if 'crossing' in p:
|
||||||
if pairs['crossing'] == 'zebra':
|
if p['crossing'] == 'zebra':
|
||||||
shape = 'zebra'
|
shape = 'zebra'
|
||||||
elif 'crossing_ref' in pairs:
|
elif 'crossing_ref' in p:
|
||||||
if pairs['crossing_ref'] == 'zebra':
|
if p['crossing_ref'] == 'zebra':
|
||||||
shape = 'zebra'
|
shape = 'zebra'
|
||||||
draw_point_shape(shape, x, y, fill)
|
draw_point_shape(shape, x, y, fill)
|
||||||
elif v == 'street_lamp':
|
elif v == 'street_lamp':
|
||||||
draw_point_shape('street_lamp', x, y, fill)
|
draw_point_shape('street_lamp', x, y, fill)
|
||||||
|
else:
|
||||||
|
processed.remove(k)
|
||||||
|
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
|
||||||
else:
|
else:
|
||||||
for k in pairs:
|
for k in p:
|
||||||
if not no_draw(k):
|
if not no_draw(k):
|
||||||
point(k, pairs[k], x, y, fill, text_y)
|
point(k, p[k], x, y, fill, text_y)
|
||||||
text_y += 10
|
text_y += 10
|
||||||
|
|
||||||
|
|
||||||
for k in []:
|
for k in []:
|
||||||
v = pairs[k]
|
v = p[k]
|
||||||
if k == 'amenity':
|
if k == 'amenity':
|
||||||
if v in ['bench', 'bicycle_parking', 'cafe', 'waste_basket',
|
if v in ['bench', 'bicycle_parking', 'cafe', 'waste_basket',
|
||||||
'restaurant', 'pharmacy', 'drinking_water', 'toilets',
|
'restaurant', 'pharmacy', 'drinking_water', 'toilets',
|
||||||
|
@ -586,7 +621,7 @@ def draw_nodes():
|
||||||
elif v == 'traffic_signals':
|
elif v == 'traffic_signals':
|
||||||
draw_point_shape('traffic_signal', x, y, fill)
|
draw_point_shape('traffic_signal', x, y, fill)
|
||||||
elif v == 'crossing':
|
elif v == 'crossing':
|
||||||
if not ('crossing' in pairs):
|
if not ('crossing' in p):
|
||||||
draw_point_shape('crossing', x, y, fill)
|
draw_point_shape('crossing', x, y, fill)
|
||||||
else:
|
else:
|
||||||
point(k, v, flinged.x, flinged.y, fill, text_y)
|
point(k, v, flinged.x, flinged.y, fill, text_y)
|
||||||
|
@ -626,7 +661,7 @@ def draw_nodes():
|
||||||
draw_point_shape('monument', x, y, fill)
|
draw_point_shape('monument', x, y, fill)
|
||||||
elif k == 'tourism':
|
elif k == 'tourism':
|
||||||
if v == 'artwork':
|
if v == 'artwork':
|
||||||
if not ('artwork_type' in pairs):
|
if not ('artwork_type' in p):
|
||||||
draw_point_shape('monument', x, y, fill)
|
draw_point_shape('monument', x, y, fill)
|
||||||
if v == 'attraction':
|
if v == 'attraction':
|
||||||
draw_point_shape('monument', x, y, fill)
|
draw_point_shape('monument', x, y, fill)
|
||||||
|
|
|
@ -5,6 +5,7 @@ Author: Sergey Vartanov
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def parse_node_full(node_data, silent=False):
|
def parse_node_full(node_data, silent=False):
|
||||||
|
@ -93,25 +94,35 @@ def parse_osm_file(file_name, silent=False):
|
||||||
node_map = {}
|
node_map = {}
|
||||||
way_map = {}
|
way_map = {}
|
||||||
relation_map = {}
|
relation_map = {}
|
||||||
|
with open(file_name) as f:
|
||||||
|
for lines_number, l in enumerate(f):
|
||||||
|
pass
|
||||||
input_file = open(file_name)
|
input_file = open(file_name)
|
||||||
line = input_file.readline()
|
line = input_file.readline()
|
||||||
|
line_number = 0
|
||||||
while line != '':
|
while line != '':
|
||||||
|
line_number += 1
|
||||||
|
if line_number % 10000 == 0:
|
||||||
|
p = line_number / float(lines_number)
|
||||||
|
l = int(p * 100)
|
||||||
|
print 'Line ' + str(int(p * 1000) / 10) + ' %: [' + (l * '=') + ((100 - l) * ' ') + '].'
|
||||||
|
sys.stdout.write("\033[F")
|
||||||
|
|
||||||
# Node parsing.
|
# Node parsing.
|
||||||
|
|
||||||
if line[:6] == ' <node':
|
if line[:6] in [' <node', '\t<node']:
|
||||||
if line[-3] == '/':
|
if line[-3] == '/':
|
||||||
node = parse_node(line[7:-3])
|
node = parse_node(line[7:-3])
|
||||||
node_map[node['id']] = node
|
node_map[node['id']] = node
|
||||||
else:
|
else:
|
||||||
element = parse_node(line[7:-2])
|
element = parse_node(line[7:-2])
|
||||||
element['tags'] = {}
|
element['tags'] = {}
|
||||||
elif line == ' </node>\n':
|
elif line in [' </node>\n', '\t</node>\n']:
|
||||||
node_map[element['id']] = element
|
node_map[element['id']] = element
|
||||||
|
|
||||||
# Way parsing.
|
# Way parsing.
|
||||||
|
|
||||||
elif line[:5] == ' <way':
|
elif line[:5] in [' <way', '\t<way']:
|
||||||
if line[-3] == '/':
|
if line[-3] == '/':
|
||||||
way = parse_way(line[6:-3])
|
way = parse_way(line[6:-3])
|
||||||
node_map[node['id']] = node
|
node_map[node['id']] = node
|
||||||
|
@ -119,12 +130,12 @@ def parse_osm_file(file_name, silent=False):
|
||||||
element = parse_way(line[6:-2])
|
element = parse_way(line[6:-2])
|
||||||
element['tags'] = {}
|
element['tags'] = {}
|
||||||
element['nodes'] = []
|
element['nodes'] = []
|
||||||
elif line == ' </way>\n':
|
elif line == [' </way>\n', '\t</way>\n']:
|
||||||
way_map[element['id']] = element
|
way_map[element['id']] = element
|
||||||
|
|
||||||
# Relation parsing.
|
# Relation parsing.
|
||||||
|
|
||||||
elif line[:10] == ' <relation':
|
elif line[:10] in [' <relation', '\t<relation']:
|
||||||
if line[-3] == '/':
|
if line[-3] == '/':
|
||||||
relation = parse_relation(line[11:-3])
|
relation = parse_relation(line[11:-3])
|
||||||
relation_map[relation['id']] = relation
|
relation_map[relation['id']] = relation
|
||||||
|
@ -132,24 +143,26 @@ def parse_osm_file(file_name, silent=False):
|
||||||
element = parse_relation(line[11:-2])
|
element = parse_relation(line[11:-2])
|
||||||
element['tags'] = {}
|
element['tags'] = {}
|
||||||
element['members'] = []
|
element['members'] = []
|
||||||
elif line == ' </relation>\n':
|
elif line in [' </relation>\n', '\t</relation>\n']:
|
||||||
relation_map[element['id']] = element
|
relation_map[element['id']] = element
|
||||||
|
|
||||||
# Elements parsing.
|
# Elements parsing.
|
||||||
|
|
||||||
elif line[:6] == ' <tag':
|
elif line[:6] in [' <tag', '\t\t<tag']:
|
||||||
k, v = parse_tag(line[7:-3])
|
k, v = parse_tag(line[7:-3])
|
||||||
element['tags'][k] = v
|
element['tags'][k] = v
|
||||||
elif line[:5] == ' <nd':
|
elif line[:5] in [' <nd', '\t\t<nd']:
|
||||||
element['nodes'].append(int(line[11:-4]))
|
element['nodes'].append(int(line[11:-4]))
|
||||||
elif line[:5] == ' <member':
|
elif line[:5] in [' <member', '\t\t<member']:
|
||||||
member = parse_member(line[10:-3])
|
member = parse_member(line[10:-3])
|
||||||
element['members'].append(member)
|
element['members'].append(member)
|
||||||
line = input_file.readline()
|
line = input_file.readline()
|
||||||
input_file.close()
|
input_file.close()
|
||||||
if not silent:
|
if not silent:
|
||||||
print 'File readed in ' + \
|
print 'File readed in ' + \
|
||||||
str(datetime.datetime.now() - start_time) + '.'
|
str(datetime.datetime.now() - start_time) + '.'
|
||||||
|
print 'Nodes: ' + str(len(node_map)) + ', ways: ' + \
|
||||||
|
str(len(way_map)) + ', relations: ' + str(len(relation_map)) + '.'
|
||||||
return node_map, way_map, relation_map
|
return node_map, way_map, relation_map
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue