source: subversion/applications/rendering/parking/mapnik/generate_approach_layer_xml.py @ 28739

Last change on this file since 28739 was 28123, checked in by kdrangmeister, 8 years ago

modularisation

  • Property svn:mime-type set to text/plain
File size: 3.8 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
9def transmogrify_file(sf,df):
10    dom= pxdom.getDOMImplementation('')
11    parser= dom.createLSParser(dom.MODE_SYNCHRONOUS, None)
12    parser.domConfig.setParameter('entities', 0) # 1 -> exception if attribute values is set
13    #parser.domConfig.setParameter('disallow-doctype', 1)
14    parser.domConfig.setParameter('pxdom-resolve-resources', 1) # 1 -> replace &xyz; with text
15    document = parser.parseURI(sf)
16
17#    dom_convert_to_grey(document)
18   
19    output= document.implementation.createLSOutput()
20    output.systemId= df
21    output.encoding= 'utf-8'
22    serialiser= document.implementation.createLSSerializer()
23    serialiser.write(document, output)
24
25
26def strip_doctype(f):
27    p = subprocess.Popen(['sed','-i','2,11 d',f]) # -i means 'in place'
28    p.wait()
29
30def create_approach_icons(source_symbols_dir,dest_symbols_dir):
31    create_approach_area_icons(source_symbols_dir,dest_symbols_dir)
32    create_approach_point_icons(source_symbols_dir,dest_symbols_dir)
33   
34def create_approach_area_icons(source_symbols_dir,dest_symbols_dir):
35    return
36
37def create_approach_point_icons(source_symbols_dir,dest_symbols_dir):
38    copy_files(source_symbols_dir,dest_symbols_dir,['approach-target.png'])
39
40def copy_files(src,dest,files):
41    for f in files:
42        if type(f) is tuple:
43            shutil.copy2(os.path.join(src,f[0]),os.path.join(dest,f[1]))
44        else:
45            shutil.copy2(os.path.join(src,f),os.path.join(dest,f))
46
47def colorize_icon(sf,df,color):
48    p = subprocess.Popen(['convert',sf,'-fill','#'+color,'-colorize','100',df])
49    p.wait()
50
51def hflip_icon(sf,df):
52    p = subprocess.Popen(['convert',sf,'-flip',df])
53    p.wait()
54
55def stamp_icon(sf,df,stampf):
56    p = subprocess.Popen(['convert',sf,stampf,'-compose','Darken','-composite',df])
57    p.wait()
58
59def add_license_files(dirname):
60    f = open(os.path.join(dirname,"CONTACT"), 'w')
61    f.write("This style is created by kayd@toolserver.org")
62    f.close
63    f = open(os.path.join(dirname,"LICENSE"), 'w')
64    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)")
65    f.close
66
67def main_approach(options):
68    style_name = options['stylename']
69    source_dir = options['sourcedir']
70    source_file = options['sourcefile']
71    #source_symbols_dir_mapnik = os.path.join(source_dir,"symbols")
72    source_symbols_dir_style = os.path.join(source_dir,"approach-symbols-src")
73    dest_dir = options['destdir']
74
75    dest_dir_style = os.path.join(dest_dir,style_name)
76    dest_dir_style_symbols = os.path.join(dest_dir_style,"symbols")
77    dest_file_style = 'osm-{style}.xml'.format(style=style_name)
78    style_file = os.path.join(dest_dir_style,dest_file_style)
79    if not os.path.exists(dest_dir_style_symbols):
80        os.makedirs(dest_dir_style_symbols)
81
82    create_approach_icons(source_symbols_dir_style,dest_dir_style_symbols)
83    transmogrify_file(os.path.join(source_dir,source_file),style_file)
84    strip_doctype(style_file)
85    add_license_files(dest_dir_style)
86
87if __name__ == '__main__':
88    parser = OptionParser()
89    parser.add_option("-s", "--sourcedir", dest="sourcedir", help="path to the source directory", default=".")
90    parser.add_option("-f", "--sourcefile", dest="sourcefile", help="source filename, default is 'osm.xml')", default="osm.xml")
91    parser.add_option("-d", "--destdir", dest="destdir", help="path to the destination directory, further dirs are created within. default is '/tmp'", default="/tmp")
92    (options, args) = parser.parse_args()
93    options['stylename'] = "approach"
94    print options
95    main_approach(options.__dict__)
96    sys.exit(0)
Note: See TracBrowser for help on using the repository browser.