-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_osmcsymbols.py
106 lines (80 loc) · 2.65 KB
/
generate_osmcsymbols.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from psycopg2 import *
import sys
from PIL import Image, ImageMath,ImageChops,ImageDraw
import os.path
import os
import shutil
if os.path.isdir("osmcsymbol/generated"):
shutil.rmtree("osmcsymbol/generated")
os.makedirs("osmcsymbol/generated")
COLORS = {
'white': '#ffffff',
'red':'#dd0000',
'green':'#008000',
'blue':'#0000bb',
'yellow':'#e7c500',
'orange':'#E77B00',
'black':'#273561',
'brown':'#963011',
'purple':'#5819B5',
'mtb':'#e7c500',
}
BORDER_COLOR=(0,0,0,127)
SIZE=14
def colorize(im,color):
alpha = im.split()[-1]
ret = Image.new('RGBA', (SIZE,SIZE), color)
ret.putalpha(alpha)
return ret
connection = connect("dbname='" + sys.argv[1] + "' user='klinger' password='' port=5432");
cCursor = connection.cursor()
cursor = connection.cursor()
cursor.execute('''
SELECT osm_id,osmcsymbol0,osmcsymbol1,osmcsymbol2,osmcsymbol3,osmcsymbol4,osmcsymbol5,osmcsymbol6,osmcsymbol7
FROM routes WHERE COALESCE(osmcsymbol0,osmcsymbol1,osmcsymbol2,osmcsymbol3,osmcsymbol4,osmcsymbol5,osmcsymbol6,osmcsymbol7,'') <> ''
''');
cCursor.execute("DROP TABLE IF EXISTS osmcsymbols")
cCursor.execute("CREATE TABLE osmcsymbols (osm_id INT, file TEXT)")
cCursor.close()
while True:
row_raw = cursor.fetchone()
if not row_raw:
break
row = map(lambda a: map(lambda b: b.split('_'),a.split(':')[1:]),filter(lambda a: a and 'mtb' not in a,list(set(row_raw[1:]))))
if not row:
continue
sizeY = (((len(row) - 1) / 2) + 1) * SIZE;
sizeX = min(len(row),2) * SIZE;
#print sizeX, sizeY
im = Image.new('RGBA', (sizeX,sizeY), (255, 255, 255, 0))
i = 0
for sym in row:
offsetY = SIZE * (i / 2)
offsetX = SIZE * (i % 2)
i += 1
for part in sym:
color = part[0]
if color not in COLORS:
print "not found color: %s" % color
continue
shape = part[1] if len(part) > 1 else 'full'
if not os.path.isfile('osmcsymbol/%s.png' % shape):
print "not found shape: %s" % shape
continue;
im2 = Image.open('osmcsymbol/%s.png' % shape,'r')
im2.convert('RGBA')
im2 = im2.resize((SIZE,SIZE),Image.ANTIALIAS)
im2 = colorize(im2,COLORS[color])
draw = ImageDraw.Draw(im2)
draw.line((0, 0,0,SIZE-1), fill=BORDER_COLOR)
draw.line((SIZE-1, 0,SIZE-1,SIZE-1), fill=BORDER_COLOR)
draw.line((0, 0,SIZE-1,0), fill=BORDER_COLOR)
draw.line((0, SIZE-1,SIZE-1,SIZE-1), fill=BORDER_COLOR)
im.paste(im2,(offsetX,offsetY),im2.split()[-1])
fileName = ";".join(filter(lambda a: a,row_raw[1:]));
iCursor = connection.cursor()
iCursor.execute("INSERT INTO osmcsymbols (osm_id,file) VALUES ('%s','%s')" % (row_raw[0],fileName))
iCursor.close()
im.save('osmcsymbol/generated/%s.png' % fileName);
cursor.close()
connection.commit()