Skip to content
This repository
Newer
Older
100644 100 lines (76 sloc) 4.007 kb
ae97fbad » dane.springmeyer
2010-04-08 put docs and command line scripts in sync - closes #33
1 #!/usr/bin/env python
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
2
3 import os
85ced475 » migurski
2008-11-26 Split compile.py into one that's meant to be executed with __main__, …
4 import sys
339638c3 » migurski
2011-12-03 Bumped to 1.1.2 with switch from os.rename() to shutil.move() in casc…
5 import shutil
713d66e4 » migurski
2008-09-03 oop
6 import optparse
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
7 import tempfile
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
8 from os.path import realpath, dirname
9
0bd5c8be » Michal Migurski
2012-05-01 Centralized mapnik import reduce confusion of mapnik2/mapnik versions
10 import cascadenik
11 from cascadenik import mapnik
12
429a1afc » migurski
2010-09-24 Fixed issues with various pretty-print options
13 try:
14 import xml.etree.ElementTree as ElementTree
15 from xml.etree.ElementTree import Element
16 except ImportError:
17 try:
18 import lxml.etree as ElementTree
19 from lxml.etree import Element
20 except ImportError:
21 import elementtree.ElementTree as ElementTree
22 from elementtree.ElementTree import Element
713d66e4 » migurski
2008-09-03 oop
23
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
24 def main(src_file, dest_file, **kwargs):
713d66e4 » migurski
2008-09-03 oop
25 """ Given an input layers file and a directory, print the compiled
26 XML file to stdout and save any encountered external image files
27 to the named directory.
28 """
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
29 mmap = mapnik.Map(1, 1)
30 # allow [zoom] filters to work
31 mmap.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'
d13c1a37 » migurski
2012-11-17 Bumped to version 2.6.0 with addition of --style option to cascadenik…
32 load_kwargs = dict([(k, v) for (k, v) in kwargs.items() if k in ('cache_dir', 'scale', 'verbose', 'datasources_cfg', 'user_styles')])
87fc9413 » ninowalker
2010-09-26 Merged latest with Migurski, again.
33 cascadenik.load_map(mmap, src_file, dirname(realpath(dest_file)), **load_kwargs)
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
34
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
35 (handle, tmp_file) = tempfile.mkstemp(suffix='.xml', prefix='cascadenik-mapnik-')
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
36 os.close(handle)
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
37 mapnik.save_map(mmap, tmp_file)
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
38
39 if kwargs.get('pretty'):
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
40 doc = ElementTree.fromstring(open(tmp_file, 'rb').read())
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
41 cascadenik._compile.indent(doc)
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
42 f = open(tmp_file, 'wb')
9b2e87e4 » migurski
2010-09-25 Fixed xml writing for stdlib etree
43 ElementTree.ElementTree(doc).write(f)
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
44 f.close()
45
b6e2d16f » springmeyer
2010-11-02 allow cascadenik-compile to overwrite outputs on windows
46 # manually unlinking seems to be required on windows
8cb235ee » springmeyer
2010-11-13 forgot to merge in this bugfix
47 if os.path.exists(dest_file):
48 os.unlink(dest_file)
e9d2c7cf » Michal Migurski
2012-05-01 Setting liberal permissions on output file
49
50 os.chmod(tmp_file, 0666^os.umask(0))
339638c3 » migurski
2011-12-03 Bumped to 1.1.2 with switch from os.rename() to shutil.move() in casc…
51 shutil.move(tmp_file, dest_file)
713d66e4 » migurski
2008-09-03 oop
52 return 0
53
88be2bf4 » migurski
2012-06-01 Bumped to 2.4.1 with fix to a bad ref in cascadenik-compile
54 parser = optparse.OptionParser(usage="""%prog [options] <mml> <xml>""", version='%prog ' + cascadenik.__version__)
713d66e4 » migurski
2008-09-03 oop
55
d13c1a37 » migurski
2012-11-17 Bumped to version 2.6.0 with addition of --style option to cascadenik…
56 parser.set_defaults(cache_dir=None, pretty=True, verbose=False, scale=1, user_styles=[], datasources_cfg=None)
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
57
4ae2f711 » migurski
2010-09-26 Added --cache-dir option and slightly modified --datasources-config o…
58 # the actual default for cache_dir is handled in load_map(),
59 # to ensure that the mkdir behavior is correct.
60 parser.add_option('-c', '--cache-dir', dest='cache_dir',
61 help='Cache file-based resources (symbols, shapefiles, etc) to this directory. (default: %s)' % cascadenik.CACHE_DIR)
62
e13ba4fc » migurski
2010-09-26 Shortened datasources_cfg name just a bit
63 parser.add_option('-d' , '--datasources-config', dest='datasources_cfg',
4ae2f711 » migurski
2010-09-26 Added --cache-dir option and slightly modified --datasources-config o…
64 help='Use the specified .cfg file to provide local overrides to datasources and variables.',
65 type="string")
07990e25 » dane.springmeyer
2010-07-27 Big additions to cascadenik.compile and wrapper script, see compile d…
66
67 parser.add_option('--srs', dest='srs',
4ae2f711 » migurski
2010-09-26 Added --cache-dir option and slightly modified --datasources-config o…
68 help='Target srs for the compiled stylesheet. If provided, overrides default map srs in the mml. (default: None)')
07990e25 » dane.springmeyer
2010-07-27 Big additions to cascadenik.compile and wrapper script, see compile d…
69
a4bb4740 » Michal Migurski
2012-05-22 Added non-working --2x argument to cascadenik-compile.py
70 parser.add_option('--2x', dest='scale', action='store_const', const=2,
71 help='Optionally scale all values (lengths and scale denominators) in output xml by two, suitable for display on high-resolution (e.g. iPhone) screens.')
72
d13c1a37 » migurski
2012-11-17 Bumped to version 2.6.0 with addition of --style option to cascadenik…
73 parser.add_option('--style', dest='user_styles', action='append',
74 help='Look for additional styles in the named file, which will override anything provided in the MML. Any number of these can be provided.')
75
07990e25 » dane.springmeyer
2010-07-27 Big additions to cascadenik.compile and wrapper script, see compile d…
76 parser.add_option('-p', '--pretty', dest='pretty',
4ae2f711 » migurski
2010-09-26 Added --cache-dir option and slightly modified --datasources-config o…
77 help='Pretty print the xml output. (default: True)',
07990e25 » dane.springmeyer
2010-07-27 Big additions to cascadenik.compile and wrapper script, see compile d…
78 action='store_true')
79
80 parser.add_option('-v' , '--verbose', dest='verbose',
4ae2f711 » migurski
2010-09-26 Added --cache-dir option and slightly modified --datasources-config o…
81 help='Make a bunch of noise. (default: False)',
07990e25 » dane.springmeyer
2010-07-27 Big additions to cascadenik.compile and wrapper script, see compile d…
82 action='store_true')
83
713d66e4 » migurski
2008-09-03 oop
84 if __name__ == '__main__':
85 (options, args) = parser.parse_args()
b7c9c34a » ninowalker
2010-09-26 Handle local overrides from a config file for datasources.
86
5400f290 » springmeyer
2010-10-05 check args length to cascadenik-compile and make sure to always prefe…
87 if not len(args) == 2:
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
88 parser.error('Please specify .mml and .xml files')
89
90 layersfile, outputfile = args[0:2]
91
92 print >> sys.stderr, 'output file:', outputfile, dirname(realpath(outputfile))
93
94 if not layersfile.endswith('.mml'):
95 parser.error('Input must be an .mml file')
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
96
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
97 if not outputfile.endswith('.xml'):
98 parser.error('Output must be an .xml file')
677db026 » robert.coup
2010-09-25 Merge -xmlbad branch r643:1060 to trunk.
99
d8dedccc » migurski
2010-09-25 Made output XML file compulsory and removed targget_dir option as a r…
100 sys.exit(main(layersfile, outputfile, **options.__dict__))
Something went wrong with that request. Please try again.