source: subversion/applications/rendering/parking/mapnik/generate_parking_layer_xml.py @ 26368

Last change on this file since 26368 was 26367, checked in by kdrangmeister, 9 years ago

change deployment, moved "parking" to "parking-old"

  • Property svn:mime-type set to text/plain
File size: 16.6 KB
Line 
1# -*- coding: utf-8 -*-
2# by kay
3
4import sys,os,subprocess,shutil
5from optparse import OptionParser
6#from xml.dom.minidom import parse, parseString
7import pxdom
8
9#import colorsys
10
11condition_colors = {
12    'free': '7fff00',
13    'disc': '50a100',
14    'cust': 'b68529',
15    'resi': '785534',
16    'priv': '3f2920',
17    'fee':  '67a1eb',
18    'unkn': 'bc73e2'
19    }
20
21forbidden_colors = {
22    'nopa': 'f8b81f',
23    'nost': 'f85b1f',
24    'fire': 'f81f1f'
25    }
26
27
28"""
29def dom_convert_to_grey(document):
30    els = document.getElementsByTagName("CssParameter")
31    #print "els=",els
32    for el in els:
33        at = el.getAttribute("name")
34        if at=="stroke" or at=="fill":
35            col=el.firstChild.nodeValue
36            bw=rgb_to_css(color_to_bw(parse_color(col)))
37            print "converted {typ} from {a} to {bw}." .format(typ=at,a=col,bw=bw)
38            el.firstChild.nodeValue=bw
39
40    #<Map bgcolor="---" srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs +over" minimum_version="0.7.1">
41    els = document.getElementsByTagName("Map")
42    for el in els:
43        col = el.getAttribute("bgcolor")
44        assert(col!='')
45        assert(col!=None)
46        bw=rgb_to_css(color_to_bw(parse_color(col)))
47        print "converted {typ} from {a} to {bw}." .format(typ='bgcolor',a=col,bw=bw)
48        el.setAttribute("bgcolor",bw)
49
50    #<TextSymbolizer ... fill="#6699cc"/>
51    els = document.getElementsByTagName("TextSymbolizer")
52    for el in els:
53        col = el.getAttribute("fill")
54        assert(col!='')
55        assert(col!=None)
56        bw=rgb_to_css(color_to_bw(parse_color(col)))
57        print "converted {typ} from {a} to {bw}." .format(typ='TS-fill',a=col,bw=bw)
58        el.setAttribute("fill",bw)
59        #<TextSymbolizer halo_fill="#fed7a5"/> (optional)
60        col = el.getAttribute("halo_fill")
61        assert(col!=None)
62        if col!='':
63            bw=rgb_to_css(color_to_bw(parse_color(col)))
64            print "converted {typ} from {a} to {bw}." .format(typ='TS-halo_fill',a=col,bw=bw)
65            el.setAttribute("halo_fill",bw)
66"""
67
68def dom_strip_style_and_layer(document,stylename,layername):
69    removeElements=[]
70    # remove <Style name="points"> and <Layer name="amenity-points">
71    els = document.getElementsByTagName("Style")
72    for el in els:
73        if el.getAttribute("name")==stylename:
74            removeElements.append(el)
75    els = document.getElementsByTagName("Layer")
76    for el in els:
77        if el.getAttribute("name")==layername:
78            removeElements.append(el)
79    print "removing the following elements:"
80    print removeElements
81    for el in removeElements:
82        parent = el.parentNode
83        parent.removeChild(el)
84
85def dom_strip_icons(document):
86    dom_strip_style_and_layer(document,"points","amenity-points")
87    dom_strip_style_and_layer(document,"power_line","power_line")
88    dom_strip_style_and_layer(document,"power_minorline","power_minorline")
89    dom_strip_style_and_layer(document,"power_towers","power_towers")
90    dom_strip_style_and_layer(document,"power_poles","power_poles")
91
92def transmogrify_file(sf,dfgrey,dfnoicons):
93    dom= pxdom.getDOMImplementation('')
94    parser= dom.createLSParser(dom.MODE_SYNCHRONOUS, None)
95    parser.domConfig.setParameter('entities', 0) # 1 -> exception if attribute values is set
96    #parser.domConfig.setParameter('disallow-doctype', 1)
97    parser.domConfig.setParameter('pxdom-resolve-resources', 1) # 1 -> replace &xyz; with text
98    document = parser.parseURI(sf)
99
100#    dom_convert_to_grey(document)
101   
102    output= document.implementation.createLSOutput()
103    output.systemId= dfgrey
104    output.encoding= 'utf-8'
105    serialiser= document.implementation.createLSSerializer()
106    serialiser.write(document, output)
107
108"""
109    dom_strip_icons(document)
110   
111    output= document.implementation.createLSOutput()
112    output.systemId= dfnoicons
113    output.encoding= 'utf-8'
114    serialiser= document.implementation.createLSSerializer()
115    serialiser.write(document, output)
116"""
117
118def strip_doctype(f):
119    p = subprocess.Popen(['sed','-i','2,10 d',f]) # -i means 'in place'
120    p.wait()
121
122def create_parking_icons(source_symbols_dir,dest_symbols_dir):
123    create_parking_lane_icons(source_symbols_dir,dest_symbols_dir)
124    create_parking_area_icons(source_symbols_dir,dest_symbols_dir)
125    create_parking_point_icons(source_symbols_dir,dest_symbols_dir)
126   
127def create_parking_lane_icons(source_symbols_dir,dest_symbols_dir):
128    # first create mirror images (from left to right)
129    image_files = os.listdir(source_symbols_dir)
130    image_files = [f for f in image_files if f.startswith('park-l') and f.endswith('png')]
131    for f in image_files:
132        sf = os.path.join(source_symbols_dir,f)
133        df = os.path.join(source_symbols_dir,f) # this is changed so that we write in the source dir
134        hflip_icon(sf,df.replace('-l','-r'))
135
136    # then, create the colors
137    image_files = os.listdir(source_symbols_dir)
138    image_files = [f for f in image_files if f.endswith('source.png')]
139    for f in image_files:
140        # convert ./original-mapnik/symbols/*.png -fx '0.25*r + 0.62*g + 0.13*b' ./bw-mapnik/symbols/*.png
141        sf = os.path.join(source_symbols_dir,f)
142        df = os.path.join(dest_symbols_dir,f)
143        if 'n-' in f:      # then it's a non-parking thing
144            for c in forbidden_colors.iterkeys():
145                colorize_icon(sf,df.replace('source',c),forbidden_colors.get(c))
146        else:
147            for c in condition_colors.iterkeys():
148                colorize_icon(sf,df.replace('source',c),condition_colors.get(c))
149
150def create_parking_area_icons(source_symbols_dir,dest_symbols_dir):
151    df = os.path.join(dest_symbols_dir,"parking_area_source.png")
152    stampf = os.path.join(source_symbols_dir,"parking_area_stamp.png")
153    for c in condition_colors.iterkeys():
154        p = subprocess.Popen(['convert','-size','16x16','xc:#'+condition_colors.get(c),stampf,'-compose','Darken','-composite',df.replace('source',c)])
155        p.wait()
156
157def create_parking_point_icons(source_symbols_dir,dest_symbols_dir):
158    # for now there's only the parking-vending icon
159    copy_files(source_symbols_dir,dest_symbols_dir,['parking-vending.png'])
160
161def copy_files(src,dest,files):
162    for f in files:
163        if type(f) is tuple:
164            shutil.copy2(os.path.join(src,f[0]),os.path.join(dest,f[1]))
165        else:
166            shutil.copy2(os.path.join(src,f),os.path.join(dest,f))
167
168def colorize_icon(sf,df,color):
169    p = subprocess.Popen(['convert',sf,'-fill','#'+color,'-colorize','100',df])
170    p.wait()
171
172def hflip_icon(sf,df):
173    p = subprocess.Popen(['convert',sf,'-flip',df])
174    p.wait()
175
176def stamp_icon(sf,df,stampf):
177    p = subprocess.Popen(['convert',sf,stampf,'-compose','Darken','-composite',df])
178    p.wait()
179
180def add_license_files(dirname):
181    f = open(os.path.join(dirname,"CONTACT"), 'w')
182    f.write("This style is created by kayd@toolserver.org")
183    f.close
184    f = open(os.path.join(dirname,"LICENSE"), 'w')
185    f.write("This derived work is published by the author, Kay Drangmeister, under the same license as the original OSM mapnik style sheet (found here: http://svn.openstreetmap.org/applications/rendering/mapnik)")
186    f.close
187
188def main_parktrans(options):
189    style_name = options['stylename']
190    source_dir = options['sourcedir']
191    source_file = options['sourcefile']
192    #source_symbols_dir_mapnik = os.path.join(source_dir,"symbols")
193    source_symbols_dir_style = os.path.join(source_dir,"parking-symbols-src")
194    dest_dir = options['destdir']
195
196    # parktrans - transparent layer to be put on top of mapnik or bw-noicons base layer
197   
198    dest_dir_style = os.path.join(dest_dir,style_name)
199    dest_dir_style_symbols = os.path.join(dest_dir_style,"symbols")
200    dest_file_style = 'osm-{style}.xml'.format(style=style_name)
201    style_file = os.path.join(dest_dir_style,dest_file_style)
202    if not os.path.exists(dest_dir_style_symbols):
203        os.makedirs(dest_dir_style_symbols)
204
205    create_parking_icons(source_symbols_dir_style,dest_dir_style_symbols)
206    transmogrify_file(os.path.join(source_dir,source_file),style_file,"")
207    strip_doctype(style_file)
208    add_license_files(dest_dir_style)
209
210def main_parking(options):
211    style_name = options['stylename']
212    source_dir = options['sourcedir']
213    source_file = options['sourcefile']
214    source_symbols_dir_style = os.path.join(source_dir,"parking-symbols-src")
215    dest_dir = options['destdir']
216
217    # parking - bw-noicons background ans parking information on top - single layer containing all
218
219    dest_dir_style = os.path.join(dest_dir,style_name)
220    dest_dir_style_symbols = os.path.join(dest_dir_style,"symbols")
221    dest_file_style = 'osm-{style}.xml'.format(style=style_name)
222    style_file = os.path.join(dest_dir_style,dest_file_style)
223    if not os.path.exists(dest_dir_style_symbols):
224        os.makedirs(dest_dir_style_symbols)
225
226    create_parking_icons(source_symbols_dir_style,dest_dir_style_symbols)
227    transmogrify_file(os.path.join(source_dir,source_file),style_file,"")
228    strip_doctype(style_file)
229    add_license_files(dest_dir_style)
230
231def main_parking_neu(options):
232    style_name = options['stylename']
233    source_bwn_dir = options['sourcebwndir']
234    source_bwn_file = options['sourcebwnfile']
235    source_p_dir = options['sourcepdir']
236    source_p_file = options['sourcepfile']
237    source_symbols_dir_style = os.path.join(source_p_dir,"parking-symbols-src")
238    dest_dir = options['destdir']
239
240    dest_dir_style = os.path.join(dest_dir,style_name)
241    dest_dir_style_symbols = os.path.join(dest_dir_style,"symbols")
242    dest_file_style = 'osm-parking-neu.xml'
243    style_file = os.path.join(dest_dir_style,dest_file_style)
244    if not os.path.exists(dest_dir_style_symbols):
245        os.makedirs(dest_dir_style_symbols)
246
247    create_parking_icons(source_symbols_dir_style,dest_dir_style_symbols)
248    merge_bw_noicons_and_parktrans_style(os.path.join(source_bwn_dir,source_bwn_file),os.path.join(source_p_dir,source_p_file),style_file)
249    #strip_doctype(style_file)
250    add_license_files(dest_dir_style)
251
252def merge_bw_noicons_and_parktrans_style(bwnoicons_style_file,parktrans_style_file,dest_parking_style_file):
253    dom= pxdom.getDOMImplementation('')
254    parser= dom.createLSParser(dom.MODE_SYNCHRONOUS, None)
255    parser.domConfig.setParameter('entities', 0) # 1 -> exception if attribute values is set
256    #parser.domConfig.setParameter('disallow-doctype', 1)
257    parser.domConfig.setParameter('pxdom-resolve-resources', 1) # 1 -> replace &xyz; with text
258    dest_parking_style_document = parser.parseURI(bwnoicons_style_file)
259    parktrans_style_document = parser.parseURI(parktrans_style_file)
260
261#    dom_convert_to_grey(document)
262    parking_area_style = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parking-area'))
263    parking_area_layer = dest_parking_style_document.adoptNode(parking_dom_cut_layer(parktrans_style_document,'parking-area'))
264    #parking_dom_insert_things_before_layer(dest_parking_style_document,parking_area_style,'planet roads text osm low zoom')
265    #parking_dom_insert_things_before_layer(dest_parking_style_document,parking_area_layer,'planet roads text osm low zoom')
266    things=[parking_area_style,parking_area_layer]
267    parking_dom_insert_things_before_layer(dest_parking_style_document,things,'planet roads text osm low zoom')
268
269    pllno = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-left-no'))
270    plrno = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-right-no'))
271    pllin = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-left-inline'))
272    plrin = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-right-inline'))
273    plldi = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-left-diagonal'))
274    plrdi = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-right-diagonal'))
275    pllor = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-left-orthogonal'))
276    plror = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parkinglane-right-orthogonal'))
277    pll = dest_parking_style_document.adoptNode(parking_dom_cut_layer(parktrans_style_document,'parkinglane-left'))
278    plr = dest_parking_style_document.adoptNode(parking_dom_cut_layer(parktrans_style_document,'parkinglane-right'))
279
280    things=[pllno,plrno,pllin,plrin,plldi,plrdi,pllor,plror,pll,plr]
281    parking_dom_insert_things_before_layer(dest_parking_style_document,things,'direction_pre_bridges')
282
283    parking_points_style = dest_parking_style_document.adoptNode(parking_dom_cut_style(parktrans_style_document,'parking-points'))
284    parking_points_layer = dest_parking_style_document.adoptNode(parking_dom_cut_layer(parktrans_style_document,'parking-points'))
285    #parking_dom_insert_things_before_layer(dest_parking_style_document,parking_area_style,'planet roads text osm low zoom')
286    #parking_dom_insert_things_before_layer(dest_parking_style_document,parking_area_layer,'planet roads text osm low zoom')
287    things=[parking_points_style,parking_points_layer]
288    parking_dom_insert_things_before_layer(dest_parking_style_document,things,'direction_pre_bridges')
289
290    output= dest_parking_style_document.implementation.createLSOutput()
291    output.systemId= dest_parking_style_file
292    output.encoding= 'utf-8'
293    serialiser= dest_parking_style_document.implementation.createLSSerializer()
294    serialiser.write(dest_parking_style_document, output)
295
296    ''' write a "rest" file to check if everything has been cut out
297    output= parktrans_style_document.implementation.createLSOutput()
298    output.systemId= 'rest.xml'
299    output.encoding= 'utf-8'
300    serialiser= parktrans_style_document.implementation.createLSSerializer()
301    serialiser.write(parktrans_style_document, output)
302    '''
303
304def parking_dom_insert_things_before_layer(document,things,here):
305    # insert things after the "leisure" layer
306    els = document.getElementsByTagName("Layer")
307    #print "els="
308    #print els
309    for el in els:
310        #print "layername={ln}".format(ln=el.getAttribute("name"))
311        if el.getAttribute("name")==here:
312            #print "found it"
313            if type(things) is list:
314                for s in things:
315                    el.parentNode.insertBefore(s,el)
316            else:
317                el.parentNode.insertBefore(things,el)
318            return
319    raise 'Layer name not found'
320
321def parking_dom_cut_layer(document,what):
322    els = document.getElementsByTagName("Layer")
323    for el in els:
324        #print "layername={ln}".format(ln=el.getAttribute("name"))
325        if el.getAttribute("name")==what:
326            #print "found it"
327            el.parentNode.removeChild(el)
328            return el
329    raise 'Layer name not found'
330
331def parking_dom_cut_style(document,what):
332    els = document.getElementsByTagName("Style")
333    for el in els:
334        #print "layername={ln}".format(ln=el.getAttribute("name"))
335        if el.getAttribute("name")==what:
336            #print "found it"
337            el.parentNode.removeChild(el)
338            return el
339    raise 'Style name not found'
340
341"""
342./generate_xml.py osm-parking-src.xml    osm-parking.xml    --accept-none --host sql-mapnik --dbname osm_mapnik --prefix planet --inc ./parking-inc --symbols ./parking-symbols/ --world_boundaries /home/project/o/s/m/osm/data/world_boundaries/ --password ''
343./generate_xml.py osm-parking-bw-src.xml osm-parking-bw.xml --accept-none --host sql-mapnik --dbname osm_mapnik --prefix planet --inc ./parking-inc --symbols ./parking-symbols/ --world_boundaries /home/project/o/s/m/osm/data/world_boundaries/ --password ''
344./generate_xml.py osm-parkerr-src.xml    osm-parkerr.xml    --accept-none --host sql-mapnik --dbname osm_mapnik --prefix planet --inc ./parking-inc --symbols ./parking-symbols/ --world_boundaries /home/project/o/s/m/osm/data/world_boundaries/ --password ''
345"""
346
347if __name__ == '__main__':
348    parser = OptionParser()
349    parser.add_option("-s", "--sourcedir", dest="sourcedir", help="path to the source directory", default=".")
350    parser.add_option("-f", "--sourcefile", dest="sourcefile", help="source filename, default is 'osm.xml')", default="osm.xml")
351    parser.add_option("-d", "--destdir", dest="destdir", help="path to the destination directory, further dirs are created within. default is '/tmp'", default="/tmp")
352    (options, args) = parser.parse_args()
353    options['stylename'] = "parktrans"
354    print options
355    main_parktrans(options.__dict__)
356    options['sourcefile'] = "osm-parking-src.xml"
357    options['stylename'] = "parking"
358    main_parking(options.__dict__)
359    sys.exit(0)
Note: See TracBrowser for help on using the repository browser.