maposmatic-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Maposmatic-dev] [PATCH 1/2] Set grid size to 40 mm +/- 33%


From: gael . utard
Subject: [Maposmatic-dev] [PATCH 1/2] Set grid size to 40 mm +/- 33%
Date: Sat, 31 Mar 2012 18:22:02 +0200

From: Gael UTARD <address@hidden>

Signed-off-by: Gael UTARD <address@hidden>
---
 ocitysmap2/layoutlib/abstract_renderer.py   |    2 +-
 ocitysmap2/layoutlib/multi_page_renderer.py |   10 +++---
 ocitysmap2/maplib/grid.py                   |   40 +++++++++++++++++----------
 ocitysmap2/maplib/map_canvas.py             |   11 +++++++
 4 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/ocitysmap2/layoutlib/abstract_renderer.py 
b/ocitysmap2/layoutlib/abstract_renderer.py
index df146b1..5da2fde 100644
--- a/ocitysmap2/layoutlib/abstract_renderer.py
+++ b/ocitysmap2/layoutlib/abstract_renderer.py
@@ -272,7 +272,7 @@ class Renderer:
         Return a new Grid object.
         """
         # Prepare the grid SHP
-        map_grid = Grid(canvas.get_actual_bounding_box(), self.rc.i18n.isrtl())
+        map_grid = Grid(canvas.get_actual_bounding_box(), 
canvas.get_actual_scale(), self.rc.i18n.isrtl())
         grid_shape = map_grid.generate_shape_file(
             os.path.join(self.tmpdir, 'grid.shp'))
 
diff --git a/ocitysmap2/layoutlib/multi_page_renderer.py 
b/ocitysmap2/layoutlib/multi_page_renderer.py
index 7673cdd..ee35354 100644
--- a/ocitysmap2/layoutlib/multi_page_renderer.py
+++ b/ocitysmap2/layoutlib/multi_page_renderer.py
@@ -248,17 +248,17 @@ class MultiPageRenderer(Renderer):
             shade_contour.add_shade_from_wkt(shade_contour_wkt)
 
 
-            # Create the grid
-            map_grid = Grid(bb_inner, self.rc.i18n.isrtl())
-            grid_shape = map_grid.generate_shape_file(
-                os.path.join(self.tmpdir, 'grid%d.shp' % i))
-
             # Create one canvas for the current page
             map_canvas = MapCanvas(self.rc.stylesheet,
                                    bb, self._usable_area_width_pt,
                                    self._usable_area_height_pt, dpi,
                                    extend_bbox_to_ratio=False)
 
+            # Create the grid
+            map_grid = Grid(bb_inner, map_canvas.get_actual_scale(), 
self.rc.i18n.isrtl())
+            grid_shape = map_grid.generate_shape_file(
+                os.path.join(self.tmpdir, 'grid%d.shp' % i))
+
             map_canvas.add_shape_file(shade)
             map_canvas.add_shape_file(shade_contour,
                                   self.rc.stylesheet.shade_color_2,
diff --git a/ocitysmap2/maplib/grid.py b/ocitysmap2/maplib/grid.py
index b34da03..435830b 100644
--- a/ocitysmap2/maplib/grid.py
+++ b/ocitysmap2/maplib/grid.py
@@ -35,14 +35,10 @@ class Grid:
     the grid size, number and size of squares, etc.
     """
 
-    # Available and supported grid sizes, in meters.
-    AVAILABLE_GRID_SIZES_METERS = [50, 100, 250, 500]
+    # Approximative paper size of the grid squares (+/- 33%).
+    GRID_APPROX_PAPER_SIZE_MM = 40
 
-    # Number of squares under which a smaller grid size is used (until no
-    # smaller size is available).
-    GRID_COUNT_TRESHOLD = 4
-
-    def __init__(self, bounding_box, rtl=False):
+    def __init__(self, bounding_box, scale, rtl=False):
         """Creates a new grid for the given bounding box.
 
         Args:
@@ -58,14 +54,28 @@ class Grid:
         l.info('Laying out grid on %.1fx%.1fm area...' %
                (self._width_m, self._height_m))
 
-        for size in sorted(Grid.AVAILABLE_GRID_SIZES_METERS, reverse=True):
-            self.grid_size_m = size
-            self.horiz_count = self._width_m / size
-            self.vert_count = self._height_m / size
-
-            if (min(self.horiz_count, self.vert_count) >
-                Grid.GRID_COUNT_TRESHOLD):
-                break
+        # compute the terrain grid size corresponding to the targeted paper 
size
+        size = float(self.GRID_APPROX_PAPER_SIZE_MM) * scale / 1000
+        # compute the scientific notation of this size :
+        # size = significant * 10 ^ exponent with 1 <= significand < 10
+        exponent = math.log10(size)
+        significand = float(size) / 10 ** int(exponent)
+        # "round" this size to be 1, 2, 2.5 or 5 multiplied by a power of 10
+        if significand < 1.5:
+            significand = 1
+        elif significand < 2.25:
+            significand = 2
+        elif significand < 3.75:
+            significand = 2.5
+        elif significand < 7.5:
+            significand = 5
+        else:
+            significand = 10
+        size = significand * 10 ** int(exponent)
+        # use it
+        self.grid_size_m = size
+        self.horiz_count = self._width_m / size
+        self.vert_count = self._height_m / size
 
         self._horiz_angle_span = abs(self._bbox.get_top_left()[1] -
                                      self._bbox.get_bottom_right()[1])
diff --git a/ocitysmap2/maplib/map_canvas.py b/ocitysmap2/maplib/map_canvas.py
index e200a3b..87fac62 100644
--- a/ocitysmap2/maplib/map_canvas.py
+++ b/ocitysmap2/maplib/map_canvas.py
@@ -32,6 +32,7 @@ import os
 from ocitysmap2 import coords
 from layoutlib.commons import convert_pt_to_dots
 import shapes
+import math
 
 l = logging.getLogger('ocitysmap')
 
@@ -150,6 +151,16 @@ class MapCanvas:
         Mapnik."""
         return self._geo_bbox
 
+    def get_actual_scale(self):
+        # get the scale denominator computed by mapnik
+        scale = self._map.scale_denominator()
+        # the actual scale depends on the latitude
+        lat = self._geo_bbox.get_top_left()[0]
+        scale *= math.cos(math.radians(lat))
+        # by convention, the scale denominator uses 90 ppi whereas cairo uses 
72 ppi
+        scale *= float(72) / 90
+        return scale
+
     def _render_shape_file(self, shape_file, color, line_width):
         shape_file.flush()
 
-- 
1.7.5.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]