From: Gael UTARD <[email protected]> There are 2 kinds of problems with scaling: - the rendering has some bugs (position of arrows, text halo) - the rendered sizes (font size, road width) do not match the ones specified in the stylesheet
Signed-off-by: Gael UTARD <[email protected]> --- ocitysmap2/__init__.py | 26 +++++++++++++----------- ocitysmap2/layoutlib/abstract_renderer.py | 4 +- ocitysmap2/layoutlib/single_page_renderers.py | 15 +++++-------- ocitysmap2/maplib/map_canvas.py | 8 ++++-- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/ocitysmap2/__init__.py b/ocitysmap2/__init__.py index 026e67e..ca2f856 100644 --- a/ocitysmap2/__init__.py +++ b/ocitysmap2/__init__.py @@ -61,7 +61,7 @@ The layout name is the renderer's key name. You can get the list of all supported renderers with ocitysmap.get_all_renderers(). The output_formats is a list of output formats. For now, the following formats are supported: - * PNG at 300dpi + * PNG at 72dpi * PDF * SVG * SVGZ (gzipped-SVG) @@ -193,8 +193,7 @@ class OCitySMap: DEFAULT_REQUEST_TIMEOUT_MIN = 15 - DEFAULT_RESOLUTION_KM_IN_MM = 150 - DEFAULT_RENDERING_PNG_DPI = 300 + DEFAULT_RENDERING_PNG_DPI = 72 STYLESHEET_REGISTRY = [] @@ -456,19 +455,13 @@ SELECT ST_AsText(ST_LongestLine( # Prepare the generic renderer renderer_cls = renderers.get_renderer_class_by_name(renderer_name) - renderer = renderer_cls(config, tmpdir, street_index) - - # Update the street_index to reflect the grid's actual position - if renderer.grid and street_index: - street_index.apply_grid(renderer.grid) - street_index.group_identical_grid_locations() # Perform the actual rendering to the Cairo devices for output_format in output_formats: output_filename = '%s.%s' % (file_prefix, output_format) try: - self._render_one(renderer, output_format, output_filename, - osm_date) + self._render_one(config, tmpdir, renderer_cls, street_index, + output_format, output_filename, osm_date) except IndexDoesNotFitError: LOG.exception("The actual font metrics probably don't " "match those pre-computed by the renderer's" @@ -480,7 +473,9 @@ SELECT ST_AsText(ST_LongestLine( finally: self._cleanup_tempdir(tmpdir) - def _render_one(self, renderer, output_format, output_filename, osm_date): + def _render_one(self, config, tmpdir, renderer_cls, street_index, + output_format, output_filename, osm_date): + LOG.info('Rendering to %s format...' % output_format.upper()) factory = None @@ -525,6 +520,13 @@ SELECT ST_AsText(ST_LongestLine( raise ValueError, \ 'Unsupported output format: %s!' % output_format.upper() + renderer = renderer_cls(config, tmpdir, dpi, street_index) + + # Update the street_index to reflect the grid's actual position + if renderer.grid and street_index: + street_index.apply_grid(renderer.grid) + street_index.group_identical_grid_locations() + surface = factory(renderer.paper_width_pt, renderer.paper_height_pt) renderer.render(surface, dpi, osm_date) diff --git a/ocitysmap2/layoutlib/abstract_renderer.py b/ocitysmap2/layoutlib/abstract_renderer.py index 17f23b0..f2bc858 100644 --- a/ocitysmap2/layoutlib/abstract_renderer.py +++ b/ocitysmap2/layoutlib/abstract_renderer.py @@ -212,7 +212,7 @@ class Renderer: ctx.restore() - def _create_map_canvas(self, graphical_ratio, + def _create_map_canvas(self, width, height, dpi, draw_contour_shade = True): """ Create a new MapCanvas object. @@ -228,7 +228,7 @@ class Renderer: # Prepare the map canvas canvas = MapCanvas(self.rc.stylesheet, self.rc.bounding_box, - graphical_ratio) + width, height, dpi) if draw_contour_shade: # Area to keep visible diff --git a/ocitysmap2/layoutlib/single_page_renderers.py b/ocitysmap2/layoutlib/single_page_renderers.py index 5ae2f9a..0fec650 100644 --- a/ocitysmap2/layoutlib/single_page_renderers.py +++ b/ocitysmap2/layoutlib/single_page_renderers.py @@ -55,7 +55,7 @@ class SinglePageRenderer(Renderer): MAX_INDEX_OCCUPATION_RATIO = 1/3. - def __init__(self, rc, tmpdir, + def __init__(self, rc, tmpdir, dpi, street_index = None, index_position = 'side'): """ Create the renderer. @@ -130,8 +130,9 @@ class SinglePageRenderer(Renderer): # Prepare the map self._map_canvas = self._create_map_canvas( - float(self._map_coords[2]) / # W - float(self._map_coords[3]) ) # H + float(self._map_coords[2]), # W + float(self._map_coords[3]), # H + dpi ) # Prepare the grid self.grid = self._create_grid(self._map_canvas) @@ -376,10 +377,6 @@ class SinglePageRenderer(Renderer): # Draw the rescaled Map ctx.save() rendered_map = self._map_canvas.get_rendered_map() - ctx.scale(map_coords_dots[2] - / rendered_map.width, - map_coords_dots[3] - / rendered_map.height) mapnik.render(rendered_map, ctx) ctx.restore() @@ -491,7 +488,7 @@ class SinglePageRendererNoIndex(SinglePageRenderer): name = 'plain' description = 'Full-page layout without index.' - def __init__(self, rc, tmpdir, street_index): + def __init__(self, rc, tmpdir, dpi, street_index): """ Create the renderer. @@ -500,7 +497,7 @@ class SinglePageRendererNoIndex(SinglePageRenderer): tmpdir (os.path): Path to a temp dir that can hold temp files. street_index (StreetIndex): None or the street index object. """ - SinglePageRenderer.__init__(self, rc, tmpdir, None, None) + SinglePageRenderer.__init__(self, rc, tmpdir, dpi, None, None) @staticmethod diff --git a/ocitysmap2/maplib/map_canvas.py b/ocitysmap2/maplib/map_canvas.py index 392a1c3..3ba0cd9 100644 --- a/ocitysmap2/maplib/map_canvas.py +++ b/ocitysmap2/maplib/map_canvas.py @@ -30,6 +30,7 @@ except ImportError: import os from ocitysmap2 import coords +from layoutlib.commons import convert_pt_to_dots import shapes l = logging.getLogger('ocitysmap') @@ -46,7 +47,7 @@ class MapCanvas: their respective alpha levels. """ - def __init__(self, stylesheet, bounding_box, graphical_ratio): + def __init__(self, stylesheet, bounding_box, _width, _height, dpi): """Initialize the map canvas for rendering. Args: @@ -62,6 +63,7 @@ class MapCanvas: # is adjusted (extended) to fill the destination zone. See # _fix_bbox_ratio for more details on how this is done. orig_envelope = self._project_envelope(bounding_box) + graphical_ratio = _width / _height off_x, off_y, width, height = self._fix_bbox_ratio( orig_envelope.minx, orig_envelope.miny, @@ -71,8 +73,8 @@ class MapCanvas: envelope = mapnik.Box2d(off_x, off_y, off_x+width, off_y+height) self._geo_bbox = self._inverse_envelope(envelope) - g_height, g_width = self._geo_bbox.get_pixel_size_for_zoom_factor( - stylesheet.zoom_level) + g_width = int(convert_pt_to_dots(_width, dpi)) + g_height = int(convert_pt_to_dots(_height, dpi)) l.debug('Corrected bounding box from %s to %s, ratio: %.2f.' % (bounding_box, self._geo_bbox, graphical_ratio)) -- 1.7.5.4
