-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocitysmap-render
executable file
·158 lines (133 loc) · 6.04 KB
/
ocitysmap-render
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# -*- coding: utf-8; mode: Python -*-
# ocitysmap, city map and street index generator from OpenStreetMap data
# Copyright (C) 2009 David Decotigny
# Copyright (C) 2009 Frédéric Lehobey
# Copyright (C) 2009 David Mentré
# Copyright (C) 2009 Maxime Petazzoni
# Copyright (C) 2009 Thomas Petazzoni
# Copyright (C) 2009 Gaël Utard
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = '0.1'
import logging
import optparse
import sys, os
from ocitysmap.street_index import OCitySMap, BaseOCitySMapError
from ocitysmap.coords import BoundingBox
def main():
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
usage = '%prog [options] [-c <cityname>|-b <lat1,long1 lat2,long2>|--polygon-osmid <osmid>]'
parser = optparse.OptionParser(usage=usage,
version='%%prog %s' % __version__)
parser.add_option('-p', '--prefix', dest='output_prefix', metavar='PREFIX',
help='Specify the prefix of generated files. '
'Defaults to "citymap"',
default='citymap')
parser.add_option('-f', '--format', dest='output_format', metavar='FMT',
help='Specify the output formats. Supported file '
'formats: svg, svgz, pdf, ps, png, and csv for '
'the index, xml for the map. Defaults to '
'SVGZ. May be specified multiple times.',
action='append')
parser.add_option('-t', '--title', dest='output_title', metavar='TITLE',
help='Specify the title displayed in the output files',
default=None)
parser.add_option('-c', '--city', dest='city_name', metavar='CITY_NAME',
help='Specify the name of te city to map',
default=None)
parser.add_option('-C', '--config', dest='config_file', metavar='FILE',
help='Specify the location of the config file')
parser.add_option('--no-frame', dest='no_frame', action='store_true',
default=False,
help="Don't insert the map and index inside a frame")
parser.add_option('-z', '--zoom-factor',
metavar='[0-18]', help='Zoom factor for the'
'rendering (default=16)', type='int', default =16)
parser.add_option('-b', '--bounding-box', dest='bbox', nargs=2,
metavar='LAT1,LON1 LAT2,LON2', help='Bounding box')
parser.add_option('', '--polygon-osmid', dest='osmid', metavar='OSMID',
help='OSM id representing the polygon of the city to render'),
parser.add_option('-l', '--language', dest='language',
metavar='LANGUAGE_CODE',
help='Language to use when generating the index'
' (default=fr_FR.UTF-8)',
default='fr_FR.UTF-8')
(options, args) = parser.parse_args()
if len(args):
parser.print_help()
return 1
# Make sure either -b or -c is given
optcnt = 0
for var in options.city_name, options.bbox, options.osmid:
if var:
optcnt += 1
if optcnt == 0:
parser.error("One of --city or --bounding-box or --osmid is mandatory")
if optcnt > 1:
parser.error("--city or --bounding-box or --osmid are exclusive")
# Determine title
if options.no_frame:
title = None
elif options.output_title is not None:
title = options.output_title
elif options.city_name is not None:
title = options.city_name
else:
title = "City's Map"
# Parse zoom factor
try:
options.zoom_factor = int(options.zoom_factor)
except ValueError:
parser.error("Invalid zoom factor: %s" % options.zoom_factor)
if options.zoom_factor < 0 or options.zoom_factor > 18:
parser.error("Invalid zoom factor: %s" % options.zoom_factor)
if not options.output_format:
options.output_format = ['svgz']
options.output_format = set(options.output_format)
# Parse bounding box arguments
boundingbox = None
if options.bbox:
try:
boundingbox = BoundingBox.parse(options.bbox)
except ValueError:
sys.stderr.write('ERROR: Invalid city bounding box!\n')
return 1
if options.city_name:
city_name = unicode(options.city_name.decode('utf-8'))
else:
city_name = None
osmid = None
if options.osmid:
try:
osmid = int(options.osmid)
except ValueError:
sys.stderr.write('ERROR: Invalid polygon OSM id!\n')
return 1
try:
prefix = 'ocitysmap_render_%d' % os.getpid()
renderer = OCitySMap(options.config_file, prefix, city_name,
boundingbox, osmid, options.language)
except BaseOCitySMapError, e:
sys.stderr.write('ERROR: %s\n' % e)
return 1
except KeyboardInterrupt:
sys.stderr.write(' Aborting.\n')
_map = renderer.render_map_into_files(title,
options.output_prefix,
options.output_format,
"zoom:%d" % options.zoom_factor)
renderer.render_index(title, options.output_prefix,
options.output_format,
_map.width, _map.height)
return 0
if __name__ == '__main__':
sys.exit(main())