]> git.street.me.uk Git - andy/viking.git/blobdiff - src/vikmapniklayer.c
Only call gps_close() after a successful gps_open().
[andy/viking.git] / src / vikmapniklayer.c
index 43802facf90d43e4b4437410f7394c699a3266e3..38587d14ec674df3038abab29a0e9bbce482bfe5 100644 (file)
 
 #include "vikmapslayer.h"
 
-#if !GLIB_CHECK_VERSION(2,26,0)
-typedef struct stat GStatBuf;
-#endif
-
 struct _VikMapnikLayerClass
 {
        VikLayerClass object_class;
@@ -163,6 +159,8 @@ VikLayerInterface vik_mapnik_layer_interface = {
        (VikLayerFuncDraw)                    mapnik_layer_draw,
        (VikLayerFuncChangeCoordMode)         NULL,
 
+       (VikLayerFuncGetTimestamp)            NULL,
+
        (VikLayerFuncSetMenuItemsSelection)   NULL,
        (VikLayerFuncGetMenuItemsSelection)   NULL,
 
@@ -229,8 +227,10 @@ static VikLayerParamData plugins_default ( void )
 #else
        if ( g_file_test ( "/usr/lib/mapnik/input", G_FILE_TEST_EXISTS ) )
                data.s = g_strdup ( "/usr/lib/mapnik/input" );
+               // Current Debian locations
+       else if ( g_file_test ( "/usr/lib/mapnik/3.0/input", G_FILE_TEST_EXISTS ) )
+               data.s = g_strdup ( "/usr/lib/mapnik/3.0/input" );
        else if ( g_file_test ( "/usr/lib/mapnik/2.2/input", G_FILE_TEST_EXISTS ) )
-               // Current Debian location
                data.s = g_strdup ( "/usr/lib/mapnik/2.2/input" );
        else
                data.s = g_strdup ( "" );
@@ -270,7 +270,7 @@ static GHashTable *requests = NULL;
 /**
  * vik_mapnik_layer_init:
  *
- * Mostly to initialize preferences
+ * Just initialize preferences
  */
 void vik_mapnik_layer_init (void)
 {
@@ -291,7 +291,15 @@ void vik_mapnik_layer_init (void)
 
        tmp.s = "carto";
        a_preferences_register(&prefs[i++], tmp, MAPNIK_PREFS_GROUP_KEY);
+}
 
+/**
+ * vik_mapnik_layer_post_init:
+ *
+ * Initialize data structures - now that reading preferences is OK to perform
+ */
+void vik_mapnik_layer_post_init (void)
+{
        tp_mutex = vik_mutex_new();
 
        // Just storing keys only
@@ -323,9 +331,14 @@ void vik_mapnik_layer_uninit ()
 // NB Only performed once per program run
 static void mapnik_layer_class_init ( VikMapnikLayerClass *klass )
 {
-       mapnik_interface_initialize ( a_preferences_get (MAPNIK_PREFS_NAMESPACE"plugins_directory")->s,
-                                     a_preferences_get (MAPNIK_PREFS_NAMESPACE"fonts_directory")->s,
-                                     a_preferences_get (MAPNIK_PREFS_NAMESPACE"recurse_fonts_directory")->b );
+       VikLayerParamData *pd = a_preferences_get (MAPNIK_PREFS_NAMESPACE"plugins_directory");
+       VikLayerParamData *fd = a_preferences_get (MAPNIK_PREFS_NAMESPACE"fonts_directory");
+       VikLayerParamData *rfd = a_preferences_get (MAPNIK_PREFS_NAMESPACE"recurse_fonts_directory");
+
+       if ( pd && fd && rfd )
+               mapnik_interface_initialize ( pd->s, fd->s, rfd->b );
+       else
+               g_critical ( "Unable to initialize mapnik interface from preferences" );
 }
 
 GType vik_mapnik_layer_get_type ()
@@ -526,6 +539,7 @@ gboolean carto_load ( VikMapnikLayer *vml, VikViewport *vvp )
                                if ( !g_strcmp0 ( vml->filename_xml, vml->filename_css ) ) {
                                        vml->filename_xml = g_strconcat ( vml->filename_css, ".xml", NULL );
                                }
+                               g_regex_unref ( regex );
                        }
                        if ( !g_file_set_contents (vml->filename_xml, mystdout, -1, &error)  ) {
                                g_warning ("%s: %s", __FUNCTION__, error->message );
@@ -620,7 +634,8 @@ static void possibly_save_pixbuf ( VikMapnikLayer *vml, GdkPixbuf *pixbuf, MapCo
 
                        gchar *dir = g_path_get_dirname ( filename );
                        if ( !g_file_test ( filename, G_FILE_TEST_EXISTS ) )
-                               g_mkdir_with_parents ( dir , 0777 );
+                               if ( g_mkdir_with_parents ( dir , 0777 ) != 0 )
+                                       g_warning ("%s: Failed to mkdir %s", __FUNCTION__, dir );
                        g_free ( dir );
 
                        if ( !gdk_pixbuf_save (pixbuf, filename, "png", &error, NULL ) ) {
@@ -661,8 +676,9 @@ static void render ( VikMapnikLayer *vml, VikCoord *ul, VikCoord *br, MapCoord *
 
        // NB Mapnik can apply alpha, but use our own function for now
        if ( vml->alpha < 255 )
-               pixbuf = ui_pixbuf_set_alpha ( pixbuf, vml->alpha );
+               pixbuf = ui_pixbuf_scale_alpha ( pixbuf, vml->alpha );
        a_mapcache_add ( pixbuf, (mapcache_extra_t){ tt }, ulm->x, ulm->y, ulm->z, MAP_ID_MAPNIK_RENDER, ulm->scale, vml->alpha, 0.0, 0.0, vml->filename_xml );
+       g_object_unref(pixbuf);
 }
 
 static void render_info_free ( RenderInfo *data )
@@ -699,7 +715,7 @@ static void render_cancel_cleanup (RenderInfo *data)
 /**
  * Thread
  */
-void thread_add (VikMapnikLayer *vml, MapCoord *mul, VikCoord *ul, VikCoord *br, gint x, gint y, gint z, gint zoom, const gchar* name )
+static void thread_add (VikMapnikLayer *vml, MapCoord *mul, VikCoord *ul, VikCoord *br, gint x, gint y, gint z, gint zoom, const gchar* name )
 {
        // Create request
        guint nn = name ? g_str_hash ( name ) : 0;
@@ -743,6 +759,9 @@ void thread_add (VikMapnikLayer *vml, MapCoord *mul, VikCoord *ul, VikCoord *br,
 
 /**
  * load_pixbuf:
+ *
+ * If function returns GdkPixbuf properly, reference counter to this
+ * buffer has to be decreased, when buffer is no longer needed.
  */
 static GdkPixbuf *load_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *brm, gboolean *rerender )
 {
@@ -775,7 +794,8 @@ static GdkPixbuf *load_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *br
 }
 
 /**
- *
+ * Caller has to decrease reference counter of returned
+ * GdkPixbuf, when buffer is no longer needed.
  */
 static GdkPixbuf *get_pixbuf ( VikMapnikLayer *vml, MapCoord *ulm, MapCoord *brm )
 {
@@ -819,6 +839,13 @@ static void mapnik_layer_draw ( VikMapnikLayer *vml, VikViewport *vvp )
                return;
        }
 
+       if ( vml->mi ) {
+               gchar *copyright = mapnik_interface_get_copyright ( vml->mi );
+               if ( copyright ) {
+                       vik_viewport_add_copyright ( vvp, copyright );
+               }
+       }
+
        VikCoord ul, br;
        ul.mode = VIK_COORD_LATLON;
        br.mode = VIK_COORD_LATLON;
@@ -855,6 +882,7 @@ static void mapnik_layer_draw ( VikMapnikLayer *vml, VikViewport *vvp )
                                        map_utils_iTMS_to_vikcoord ( &ulm, &coord );
                                        vik_viewport_coord_to_screen ( vvp, &coord, &xx, &yy );
                                        vik_viewport_draw_pixbuf ( vvp, pixbuf, 0, 0, xx, yy, vml->tile_size_x, vml->tile_size_x );
+                                       g_object_unref(pixbuf);
                                }
                        }
                }
@@ -954,6 +982,24 @@ static void mapnik_layer_carto ( menu_array_values values )
                mapnik_layer_draw ( vml, vvp );
 }
 
+/**
+ * Show Mapnik configuration parameters
+ */
+static void mapnik_layer_information ( menu_array_values values )
+{
+       VikMapnikLayer *vml = values[MA_VML];
+       if ( !vml->mi )
+               return;
+       GArray *array = mapnik_interface_get_parameters( vml->mi );
+       if ( array->len ) {
+               a_dialog_list (  VIK_GTK_WINDOW_FROM_LAYER(vml), _("Mapnik Information"), array, 1 );
+               // Free the copied strings
+               for ( int i = 0; i < array->len; i++ )
+                       g_free ( g_array_index(array,gchar*,i) );
+       }
+       g_array_free ( array, FALSE );
+}
+
 /**
  *
  */
@@ -1000,6 +1046,11 @@ static void mapnik_layer_add_menu_items ( VikMapnikLayer *vml, GtkMenu *menu, gp
                gtk_widget_show ( item );
        }
 
+       item = gtk_image_menu_item_new_from_stock ( GTK_STOCK_INFO, NULL );
+       g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(mapnik_layer_information), values );
+       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+       gtk_widget_show ( item );
+
        item = gtk_image_menu_item_new_from_stock ( GTK_STOCK_ABOUT, NULL );
        g_signal_connect_swapped ( G_OBJECT(item), "activate", G_CALLBACK(mapnik_layer_about), values );
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
@@ -1042,7 +1093,7 @@ static void mapnik_layer_tile_info ( VikMapnikLayer *vml )
        if ( g_file_test ( filename, G_FILE_TEST_EXISTS ) ) {
                filemsg = g_strconcat ( "Tile File: ", filename, NULL );
                // Get some timestamp information of the tile
-               struct stat stat_buf;
+               GStatBuf stat_buf;
                if ( g_stat ( filename, &stat_buf ) == 0 ) {
                        gchar time_buf[64];
                        strftime ( time_buf, sizeof(time_buf), "%c", gmtime((const time_t *)&stat_buf.st_mtime) );