ocitysmap/ocitysmap-render

158 wiersze
6.0 KiB
Python
Executable File

#!/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:
renderer = OCitySMap(options.config_file, 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())