maposmatic-dev
[Top][All Lists]
Advanced

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

[Maposmatic-dev] [PATCH] ocitysmap2: show OSM dat a last update timestam


From: Thomas Petazzoni
Subject: [Maposmatic-dev] [PATCH] ocitysmap2: show OSM dat a last update timestamp on rendered maps
Date: Sun, 25 Mar 2012 21:12:40 +0200

We use the maposmatic_admin table that is now part of the GIS database
and updated by the planet-update.sh script to show the timestamp of
the OSM data on rendered maps. If this table does not exist, we
gracefully fallback to an "unknown" timestamp, so that the rendering
doesn't fail.

Signed-off-by: Thomas Petazzoni <address@hidden>
---
 ocitysmap2/__init__.py                        |   21 ++++++++++++++++++---
 ocitysmap2/layoutlib/single_page_renderers.py |   20 ++++++++++++++------
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/ocitysmap2/__init__.py b/ocitysmap2/__init__.py
index 3d4eb37..026e67e 100644
--- a/ocitysmap2/__init__.py
+++ b/ocitysmap2/__init__.py
@@ -362,6 +362,18 @@ SELECT ST_AsText(ST_LongestLine(
         result = polygon_geom.union(line_geom)
         return (result.envelope.wkt, result.wkt)
 
+    def get_osm_database_last_update(self):
+        cursor = self._db.cursor()
+        query = "select last_update from maposmatic_admin;"
+        try:
+            cursor.execute(query)
+        except psycopg2.ProgrammingError:
+            self._db.rollback()
+            return None
+        # Extract datetime object. It is located as the first element
+        # of a tuple, itself the first element of an array.
+        return cursor.fetchall()[0][0]
+
     def get_all_style_configurations(self):
         """Returns the list of all available stylesheet configurations (list of
         Stylesheet objects)."""
@@ -435,6 +447,8 @@ SELECT ST_AsText(ST_LongestLine(
             LOG.warning("Designated area leads to an empty index")
             street_index = None
 
+        osm_date = self.get_osm_database_last_update()
+
         # Create a temporary directory for all our shape files
         tmpdir = tempfile.mkdtemp(prefix='ocitysmap')
         try:
@@ -453,7 +467,8 @@ SELECT ST_AsText(ST_LongestLine(
             for output_format in output_formats:
                 output_filename = '%s.%s' % (file_prefix, output_format)
                 try:
-                    self._render_one(renderer, output_format, output_filename)
+                    self._render_one(renderer, 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"
@@ -465,7 +480,7 @@ SELECT ST_AsText(ST_LongestLine(
         finally:
             self._cleanup_tempdir(tmpdir)
 
-    def _render_one(self, renderer, output_format, output_filename):
+    def _render_one(self, renderer, output_format, output_filename, osm_date):
         LOG.info('Rendering to %s format...' % output_format.upper())
 
         factory = None
@@ -511,7 +526,7 @@ SELECT ST_AsText(ST_LongestLine(
                 'Unsupported output format: %s!' % output_format.upper()
 
         surface = factory(renderer.paper_width_pt, renderer.paper_height_pt)
-        renderer.render(surface, dpi)
+        renderer.render(surface, dpi, osm_date)
 
         LOG.debug('Writing %s...' % output_filename)
         if output_format == 'png':
diff --git a/ocitysmap2/layoutlib/single_page_renderers.py 
b/ocitysmap2/layoutlib/single_page_renderers.py
index e06e6b2..5ae2f9a 100644
--- a/ocitysmap2/layoutlib/single_page_renderers.py
+++ b/ocitysmap2/layoutlib/single_page_renderers.py
@@ -254,7 +254,8 @@ class SinglePageRenderer(Renderer):
         ctx.restore()
 
 
-    def _draw_copyright_notice(self, ctx, w_dots, h_dots, notice=None):
+    def _draw_copyright_notice(self, ctx, w_dots, h_dots, notice=None,
+                               osm_date=None):
         """
         Draw a copyright notice at current location and within the
         given w_dots*h_dots rectangle.
@@ -271,8 +272,8 @@ class SinglePageRenderer(Renderer):
             _(u'Copyright © %(year)d MapOSMatic/OCitySMap developers. '
               u'Map data © %(year)d OpenStreetMap.org '
               u'and contributors (cc-by-sa).\n'
-              u'This map has been rendered on %(date)s and may be '
-              u'incomplete or innacurate. '
+              u'Map rendered on: %(date)s. OSM data updated on: %(osmdate)s. '
+              u'The map may be incomplete or inaccurate. '
               u'You can contribute to improve this map. '
               u'See http://wiki.openstreetmap.org')
 
@@ -280,8 +281,14 @@ class SinglePageRenderer(Renderer):
         prev_locale = locale.getlocale(locale.LC_TIME)
         locale.setlocale(locale.LC_TIME, self.rc.i18n.language_code())
         try:
+            if osm_date is None:
+                osm_date_str = _(u'unknown')
+            else:
+                osm_date_str = osm_date.strftime("%d %B %Y %H:%M")
+
             notice = notice % {'year': today.year,
-                               'date': today.strftime("%d %B %Y")}
+                               'date': today.strftime("%d %B %Y"),
+                               'osmdate': osm_date_str}
         finally:
             locale.setlocale(locale.LC_TIME, prev_locale)
 
@@ -297,7 +304,7 @@ class SinglePageRenderer(Renderer):
         ctx.restore()
 
 
-    def render(self, cairo_surface, dpi):
+    def render(self, cairo_surface, dpi, osm_date):
         """Renders the map, the index and all other visual map features on the
         given Cairo surface.
 
@@ -410,7 +417,8 @@ class SinglePageRenderer(Renderer):
 
         # Draw the copyright notice
         self._draw_copyright_notice(ctx, usable_area_width_dots,
-                                    copyright_margin_dots)
+                                    copyright_margin_dots,
+                                    osm_date=osm_date)
         ctx.restore()
 
         # TODO: map scale
-- 
1.7.4.1




reply via email to

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