diff --git a/ocitysmap-render b/ocitysmap-render index 5405e44..f6f2bf6 100755 --- a/ocitysmap-render +++ b/ocitysmap-render @@ -25,7 +25,7 @@ def main(): action='append') parser.add_option('-t', '--title', dest='output_title', metavar='TITLE', help='Specify the title displayed in the output files', - default="City's Map") + default=None) parser.add_option('-c', '--city', dest='city_name', metavar='CITY_NAME', help='Specify the name of te city to map') parser.add_option('-C', '--config', dest='config_file', metavar='FILE', @@ -44,6 +44,24 @@ def main(): parser.print_help() return 1 + # Make sure either -b or -c is given + if not options.city_name and not options.bbox: + parser.error("One of --city or --bounding-box is mandatory") + + if options.city_name and options.bbox: + parser.error("--city or --bounding-box 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: @@ -51,18 +69,9 @@ def main(): if options.zoom_factor < 0 or options.zoom_factor > 18: parser.error("Invalid zoom factor: %s" % options.zoom_factor) - if options.no_frame: - options.output_title = None - if not options.output_format: options.output_format = ['svg'] - if not options.city_name and not options.bbox: - parser.error("One of --city or --bounding-box is mandatory") - - if options.city_name and options.bbox: - parser.error("--city or --bounding-box are exclusive") - # Parse bounding box arguments boundingbox = None if options.bbox: @@ -82,12 +91,12 @@ def main(): except KeyboardInterrupt: sys.stderr.write(' Aborting.\n') - _map = renderer.render_into_files(options.output_title, + _map = renderer.render_into_files(title, options.output_prefix, options.output_format, "zoom:%d" % options.zoom_factor) - renderer.render_index("City's Map", options.output_prefix, + renderer.render_index(title, options.output_prefix, options.output_format, _map.width, _map.height) diff --git a/ocitysmap.conf b/ocitysmap.conf index a8d0c83..13ea944 100644 --- a/ocitysmap.conf +++ b/ocitysmap.conf @@ -7,5 +7,5 @@ dbname=testdb [mapnik] map=/home/gutard/mapnik-osm/osm.xml -[maposmatic] +[ocitysmap] copyright_logo=/home/decot/downloads/git/ocitysmap/Openstreetmap_logo.png diff --git a/ocitysmap/draw_utils.py b/ocitysmap/draw_utils.py index 5c5e2b4..c36c367 100644 --- a/ocitysmap/draw_utils.py +++ b/ocitysmap/draw_utils.py @@ -75,7 +75,7 @@ def enclose_in_frame(renderer, insurf_w, insurf_h, def add_logo(ctx, paperwidth, paperheight, logo_path, - copyright_notice = u'© 2009 MapOSMatic authors. ' + copyright_notice = u'© 2009 MapOSMatic/ocitysmap authors. ' u'Map data © 2009 OpenStreetMap.org ' u'and contributors (CC-BY-SA)'): @@ -83,8 +83,9 @@ def add_logo(ctx, paperwidth, paperheight, logo_path, png = None if logo_path: try: - f = open(logo_path, 'r') + f = open(logo_path, 'rb') png = cairo.ImageSurface.create_from_png(f) + l.debug('Using copyright logo: %s' % logo_path) f.close() except Exception, ex: l.warning('Cannot open logo file: %s' % ex) diff --git a/ocitysmap/map_canvas.py b/ocitysmap/map_canvas.py index f0db3b7..d6e7fe9 100644 --- a/ocitysmap/map_canvas.py +++ b/ocitysmap/map_canvas.py @@ -330,12 +330,8 @@ class MapCanvas: surface.flush() # png rendering with cairo... - if file_type in ('png', 'png24', 'png8'): - out_file = open(output_filename, 'wb') - try: - surface.write_to_png(out_file) - finally: - out_file.close() + if file_type in ('png', 'png24'): + surface.write_to_png(output_filename) surface.finish() diff --git a/ocitysmap/street_index.py b/ocitysmap/street_index.py index 72a703b..2488d54 100644 --- a/ocitysmap/street_index.py +++ b/ocitysmap/street_index.py @@ -446,60 +446,52 @@ class OCitySMap: return sl - def _render_one_prefix(self, title, output_prefix, format, + def _render_one_prefix(self, title, output_prefix, file_type, paperwidth, paperheight): - format = format.lower() + file_type = file_type.lower() frame_width = int(max(paperheight / 20., 30)) - outfile = "%s_index.%s" % (output_prefix, format) - l.debug("rendering " + outfile + "...") + output_filename = "%s_index.%s" % (output_prefix, file_type) + l.debug("rendering " + output_filename + "...") generator = IndexPageGenerator(self.streets) - if format == 'png' or format == 'png24': - surface = cairo.ImageSurface(cairo.FORMAT_RGB24, - paperwidth + frame_width*2, - paperheight + frame_width*2) - enclose_in_frame(lambda ctx: generator.render(ctx, paperwidth, - paperheight), - paperwidth, paperheight, - title, surface, - paperwidth + frame_width*2, - paperheight + frame_width*2, frame_width) - surface.write_to_png(outfile) - surface.finish() - elif format == 'svg': - surface = cairo.SVGSurface(outfile, paperwidth + frame_width*2, - paperheight + frame_width*2) - enclose_in_frame(lambda ctx: generator.render(ctx, paperwidth, - paperheight), - paperwidth, paperheight, - title, surface, - paperwidth + frame_width*2, - paperheight + frame_width*2, frame_width) - surface.finish() - elif format == 'pdf': - surface = cairo.PDFSurface(outfile, paperwidth + frame_width*2, - paperheight + frame_width*2) - enclose_in_frame(lambda ctx: generator.render(ctx, paperwidth, - paperheight), - paperwidth, paperheight, - title, surface, - paperwidth + frame_width*2, - paperheight + frame_width*2, - frame_width) - surface.finish() - elif format == 'ps': - surface = cairo.PSSurface(outfile, paperwidth + frame_width*2, - paperheight + frame_width*2) - enclose_in_frame(lambda ctx: generator.render(ctx, paperwidth, - paperheight), - paperwidth, paperheight, - title, surface, - paperwidth + frame_width*2, - paperheight + frame_width*2, frame_width) - surface.finish() + + if file_type in ('png', 'png24'): + cairo_factory = \ + lambda w,h: cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) + + elif file_type == 'svg': + cairo_factory = lambda w,h: cairo.SVGSurface(output_filename, w, h) + + elif file_type == 'pdf': + cairo_factory = lambda w,h: cairo.PDFSurface(output_filename, w, h) + + elif file_type == 'ps': + cairo_factory = lambda w,h: cairo.PSSurface(output_filename, w, h) + else: - raise ValueError + raise ValueError('Unsupported output format: %s' % file_type) + + if title is not None: + surface = cairo_factory(paperwidth + frame_width*2, + paperheight + frame_width*2) + enclose_in_frame(lambda ctx: generator.render(ctx, paperwidth, + paperheight), + paperwidth, paperheight, + title, surface, + paperwidth + frame_width*2, + paperheight + frame_width*2, frame_width) + else: + surface = cairo_factory(paperwidth, paperheight) + ctx = cairo.Context(surface) + generator.render(ctx, paperwidth, paperheight) + + surface.flush() + + if file_type in ('png', 'png24'): + surface.write_to_png(output_filename) + + surface.finish() def render_index(self, title, output_prefix, output_format, paperwidth, paperheight): @@ -632,7 +624,7 @@ class OCitySMap: # Determine parameters try: - copyright_logo = self.parser.get('maposmatic', 'copyright_logo') + copyright_logo = self.parser.get('ocitysmap', 'copyright_logo') except Exception: copyright_logo = None