X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/a7023a1b43a8052fccc4ded7fbfc3a7e1132002f..e16e573b57db05c051a165bbdb5c56af3cd088d7:/src/vikgeoreflayer.c diff --git a/src/vikgeoreflayer.c b/src/vikgeoreflayer.c index de14c54e..caef8bac 100644 --- a/src/vikgeoreflayer.c +++ b/src/vikgeoreflayer.c @@ -24,6 +24,7 @@ #endif #include "viking.h" +#include "vikutils.h" #include #include #include @@ -33,48 +34,20 @@ #include "icons/icons.h" /* -static struct UTM default_location_UTM ( void ) -{ - struct LatLon ll; - ll.lat = a_vik_get_default_lat (); - ll.lon = a_vik_get_default_long (); - struct UTM u; - a_coords_latlon_to_utm (&ll, &u); - return u; -} - -static VikLayerParamData easting_default ( void ) -{ - struct UTM u = default_location_UTM(); - VikLayerParamData data; - data.d = u.easting; - return data; -} - -static VikLayerParamData northing_default ( void ) -{ - struct UTM u = default_location_UTM(); - VikLayerParamData data; - data.d = u.northing; - return data; -} - static VikLayerParamData image_default ( void ) { VikLayerParamData data; data.s = g_strdup (""); return data; } - -static VikLayerParamData mpp_default ( void ) { return VIK_LPD_DOUBLE ( 4.0 ); } */ VikLayerParam georef_layer_params[] = { - { VIK_LAYER_GEOREF, "image", VIK_LAYER_PARAM_STRING, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL }, - { VIK_LAYER_GEOREF, "corner_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL }, - { VIK_LAYER_GEOREF, "corner_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL }, - { VIK_LAYER_GEOREF, "mpp_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL }, - { VIK_LAYER_GEOREF, "mpp_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL }, + { VIK_LAYER_GEOREF, "image", VIK_LAYER_PARAM_STRING, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL }, + { VIK_LAYER_GEOREF, "corner_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL }, + { VIK_LAYER_GEOREF, "corner_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL }, + { VIK_LAYER_GEOREF, "mpp_easting", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL }, + { VIK_LAYER_GEOREF, "mpp_northing", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_NOT_IN_PROPERTIES, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL }, }; enum { PARAM_IMAGE = 0, PARAM_CE, PARAM_CN, PARAM_ME, PARAM_MN, NUM_PARAMS }; @@ -85,14 +58,14 @@ static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewp static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLayerParamData data, VikViewport *vp, gboolean is_file_operation ); static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 id, gboolean is_file_operation ); static VikGeorefLayer *georef_layer_new ( VikViewport *vvp ); -VikGeorefLayer *georef_layer_create ( VikViewport *vp ); +static VikGeorefLayer *georef_layer_create ( VikViewport *vp ); static void georef_layer_free ( VikGeorefLayer *vgl ); -gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp ); -static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ); +static gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp ); +static void georef_layer_draw ( VikGeorefLayer *vgl, VikViewport *vp ); static void georef_layer_add_menu_items ( VikGeorefLayer *vgl, GtkMenu *menu, gpointer vlp ); static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image ); static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWindow *w ); -static void georef_layer_load_image ( VikGeorefLayer *vgl ); +static void georef_layer_load_image ( VikGeorefLayer *vgl, VikViewport *vp, gboolean from_file ); /* tools */ static gpointer georef_layer_move_create ( VikWindow *vw, VikViewport *vvp); @@ -108,14 +81,14 @@ static VikToolInterface georef_tools[] = { (VikToolMouseFunc) georef_layer_move_press, NULL, (VikToolMouseFunc) georef_layer_move_release, (VikToolKeyFunc) NULL, FALSE, - GDK_CURSOR_IS_PIXMAP, &cursor_geomove_pixbuf }, + GDK_CURSOR_IS_PIXMAP, &cursor_geomove_pixbuf, NULL }, { { "GeorefZoomTool", "vik-icon-Georef Zoom Tool", N_("Georef Z_oom Tool"), NULL, N_("Georef Zoom Tool"), 0 }, (VikToolConstructorFunc) georef_layer_zoom_create, NULL, NULL, NULL, (VikToolMouseFunc) georef_layer_zoom_press, NULL, NULL, (VikToolKeyFunc) NULL, FALSE, - GDK_CURSOR_IS_PIXMAP, &cursor_geozoom_pixbuf }, + GDK_CURSOR_IS_PIXMAP, &cursor_geozoom_pixbuf, NULL }, }; VikLayerInterface vik_georef_layer_interface = { @@ -160,6 +133,7 @@ VikLayerInterface vik_georef_layer_interface = { (VikLayerFuncSetParam) georef_layer_set_param, (VikLayerFuncGetParam) georef_layer_get_param, + (VikLayerFuncChangeParam) NULL, (VikLayerFuncReadFileData) NULL, (VikLayerFuncWriteFileData) NULL, @@ -232,7 +206,7 @@ static VikGeorefLayer *georef_layer_unmarshall( guint8 *data, gint len, VikViewp VikGeorefLayer *rv = georef_layer_new ( vvp ); vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp ); if (rv->image) { - georef_layer_load_image ( rv ); + georef_layer_load_image ( rv, vvp, TRUE ); } return rv; } @@ -246,6 +220,7 @@ static gboolean georef_layer_set_param ( VikGeorefLayer *vgl, guint16 id, VikLay case PARAM_CE: vgl->corner.easting = data.d; break; case PARAM_MN: vgl->mpp_northing = data.d; break; case PARAM_ME: vgl->mpp_easting = data.d; break; + default: break; } return TRUE; } @@ -255,11 +230,27 @@ static VikLayerParamData georef_layer_get_param ( VikGeorefLayer *vgl, guint16 i VikLayerParamData rv; switch ( id ) { - case PARAM_IMAGE: rv.s = vgl->image ? vgl->image : ""; break; + case PARAM_IMAGE: { + gboolean set = FALSE; + if ( is_file_operation ) { + if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) { + gchar *cwd = g_get_current_dir(); + if ( cwd ) { + rv.s = file_GetRelativeFilename ( cwd, vgl->image ); + if ( !rv.s ) rv.s = ""; + set = TRUE; + } + } + } + if ( !set ) + rv.s = vgl->image ? vgl->image : ""; + break; + } case PARAM_CN: rv.d = vgl->corner.northing; break; case PARAM_CE: rv.d = vgl->corner.easting; break; case PARAM_MN: rv.d = vgl->mpp_northing; break; case PARAM_ME: rv.d = vgl->mpp_easting; break; + default: break; } return rv; } @@ -273,6 +264,11 @@ static VikGeorefLayer *georef_layer_new ( VikViewport *vvp ) // initializing this way won't do anything yet.. vik_layer_set_defaults ( VIK_LAYER(vgl), vvp ); + // Make these defaults based on the current view + vgl->mpp_northing = vik_viewport_get_ympp ( vvp ); + vgl->mpp_easting = vik_viewport_get_xmpp ( vvp ); + vik_coord_to_utm ( vik_viewport_get_center ( vvp ), &(vgl->corner) ); + vgl->image = NULL; vgl->pixbuf = NULL; vgl->click_x = -1; @@ -283,12 +279,13 @@ static VikGeorefLayer *georef_layer_new ( VikViewport *vvp ) return vgl; } -static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ) +static void georef_layer_draw ( VikGeorefLayer *vgl, VikViewport *vp ) { -/* bla, bla */ + if ( vik_viewport_get_drawmode(vp) != VIK_VIEWPORT_DRAWMODE_UTM ) + return; + if ( vgl->pixbuf ) { - VikViewport *vp = VIK_VIEWPORT(data); struct UTM utm_middle; gdouble xmpp = vik_viewport_get_xmpp(vp), ympp = vik_viewport_get_ympp(vp); GdkPixbuf *pixbuf = vgl->pixbuf; @@ -297,33 +294,6 @@ static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ) vik_coord_to_utm ( vik_viewport_get_center ( vp ), &utm_middle ); - /* scale the pixbuf if it doesn't match our dimensions */ - if ( xmpp != vgl->mpp_easting || ympp != vgl->mpp_northing ) - { - layer_width = round(vgl->width * vgl->mpp_easting / xmpp); - layer_height = round(vgl->height * vgl->mpp_northing / ympp); - - /* rescale if necessary */ - if (layer_width == vgl->scaled_width && layer_height == vgl->scaled_height && vgl->scaled != NULL) - pixbuf = vgl->scaled; - else - { - pixbuf = gdk_pixbuf_scale_simple( - vgl->pixbuf, - layer_width, - layer_height, - GDK_INTERP_BILINEAR - ); - - if (vgl->scaled != NULL) - g_object_unref(vgl->scaled); - - vgl->scaled = pixbuf; - vgl->scaled_width = layer_width; - vgl->scaled_height = layer_height; - } - } - guint width = vik_viewport_get_width(vp), height = vik_viewport_get_height(vp); gint32 x, y; vgl->corner.zone = utm_middle.zone; @@ -331,8 +301,43 @@ static void georef_layer_draw ( VikGeorefLayer *vgl, gpointer data ) VikCoord corner_coord; vik_coord_load_from_utm ( &corner_coord, vik_viewport_get_coord_mode(vp), &(vgl->corner) ); vik_viewport_coord_to_screen ( vp, &corner_coord, &x, &y ); - if ( (x < 0 || x < width) && (y < 0 || y < height) && x+layer_width > 0 && y+layer_height > 0 ) + + /* mark to scale the pixbuf if it doesn't match our dimensions */ + gboolean scale = FALSE; + if ( xmpp != vgl->mpp_easting || ympp != vgl->mpp_northing ) + { + scale = TRUE; + layer_width = round(vgl->width * vgl->mpp_easting / xmpp); + layer_height = round(vgl->height * vgl->mpp_northing / ympp); + } + + // If image not in viewport bounds - no need to draw it (or bother with any scaling) + if ( (x < 0 || x < width) && (y < 0 || y < height) && x+layer_width > 0 && y+layer_height > 0 ) { + + if ( scale ) + { + /* rescale if necessary */ + if (layer_width == vgl->scaled_width && layer_height == vgl->scaled_height && vgl->scaled != NULL) + pixbuf = vgl->scaled; + else + { + pixbuf = gdk_pixbuf_scale_simple( + vgl->pixbuf, + layer_width, + layer_height, + GDK_INTERP_BILINEAR + ); + + if (vgl->scaled != NULL) + g_object_unref(vgl->scaled); + + vgl->scaled = pixbuf; + vgl->scaled_width = layer_width; + vgl->scaled_height = layer_height; + } + } vik_viewport_draw_pixbuf ( vp, pixbuf, 0, 0, x, y, layer_width, layer_height ); /* todo: draw only what we need to. */ + } } } @@ -344,17 +349,17 @@ static void georef_layer_free ( VikGeorefLayer *vgl ) g_object_unref ( vgl->scaled ); } -VikGeorefLayer *georef_layer_create ( VikViewport *vp ) +static VikGeorefLayer *georef_layer_create ( VikViewport *vp ) { return georef_layer_new ( vp ); } -gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp ) +static gboolean georef_layer_properties ( VikGeorefLayer *vgl, gpointer vp ) { return georef_layer_dialog ( &vgl, vp, VIK_GTK_WINDOW_FROM_WIDGET(vp) ); } -static void georef_layer_load_image ( VikGeorefLayer *vgl ) +static void georef_layer_load_image ( VikGeorefLayer *vgl, VikViewport *vp, gboolean from_file ) { GError *gx = NULL; if ( vgl->image == NULL ) @@ -372,7 +377,8 @@ static void georef_layer_load_image ( VikGeorefLayer *vgl ) if (gx) { - g_warning ( _("Couldn't open image file: %s"), gx->message ); + if ( !from_file ) + a_dialog_error_msg_extra ( VIK_GTK_WINDOW_FROM_WIDGET(vp), _("Couldn't open image file: %s"), gx->message ); g_error_free ( gx ); } else @@ -381,6 +387,14 @@ static void georef_layer_load_image ( VikGeorefLayer *vgl ) vgl->height = gdk_pixbuf_get_height ( vgl->pixbuf ); } + if ( !from_file ) + { + if ( vik_viewport_get_drawmode(vp) != VIK_VIEWPORT_DRAWMODE_UTM ) + { + a_dialog_warning_msg ( VIK_GTK_WINDOW_FROM_WIDGET(vp), + _("GeoRef map cannot be displayed in the current drawmode.\nSelect \"UTM Mode\" from View menu to view it.") ); + } + } /* should find length and width here too */ } @@ -395,7 +409,11 @@ static void georef_layer_set_image ( VikGeorefLayer *vgl, const gchar *image ) } if ( image == NULL ) vgl->image = NULL; - vgl->image = g_strdup ( image ); + + if ( g_strcmp0 (image, "") != 0 ) + vgl->image = vu_get_canonical_filename ( VIK_LAYER(vgl), image ); + else + vgl->image = g_strdup (image); } static gboolean world_file_read_line ( gchar *buffer, gint size, FILE *f, GtkWidget *widget, gboolean use_value ) @@ -512,7 +530,7 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind GtkWidget *pass_along[4]; table = gtk_table_new ( 6, 2, FALSE ); - gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 0 ); + gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, TRUE, TRUE, 0 ); wfp_hbox = gtk_hbox_new ( FALSE, 0 ); wfp_label = gtk_label_new ( _("World File Parameters:") ); @@ -523,11 +541,11 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind ce_label = gtk_label_new ( _("Corner pixel easting:") ); ce_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 1500000.0, 1, 5, 0 ), 1, 4 ); - gtk_widget_set_tooltip_text ( GTK_WIDGET(ce_spin), _("the UTM \"easting\" value of the upper-right corner pixel of the map") ); + gtk_widget_set_tooltip_text ( GTK_WIDGET(ce_spin), _("the UTM \"easting\" value of the upper-left corner pixel of the map") ); cn_label = gtk_label_new ( _("Corner pixel northing:") ); cn_spin = gtk_spin_button_new ( (GtkAdjustment *) gtk_adjustment_new ( 4, 0.0, 9000000.0, 1, 5, 0 ), 1, 4 ); - gtk_widget_set_tooltip_text ( GTK_WIDGET(cn_spin), _("the UTM \"northing\" value of the upper-right corner pixel of the map") ); + gtk_widget_set_tooltip_text ( GTK_WIDGET(cn_spin), _("the UTM \"northing\" value of the upper-left corner pixel of the map") ); xlabel = gtk_label_new ( _("X (easting) scale (mpp): ")); ylabel = gtk_label_new ( _("Y (northing) scale (mpp): ")); @@ -538,7 +556,7 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind gtk_widget_set_tooltip_text ( GTK_WIDGET(yspin), _("the scale of the map in the Y direction (meters per pixel)") ); imagelabel = gtk_label_new ( _("Map Image:") ); - imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN); + imageentry = vik_file_entry_new (GTK_FILE_CHOOSER_ACTION_OPEN, VF_FILTER_IMAGE); if (*vgl) { @@ -598,7 +616,7 @@ static gboolean georef_layer_dialog ( VikGeorefLayer **vgl, gpointer vp, GtkWind if ( (!(*vgl)->image) || strcmp( (*vgl)->image, vik_file_entry_get_filename(VIK_FILE_ENTRY(imageentry)) ) != 0 ) { georef_layer_set_image ( *vgl, vik_file_entry_get_filename(VIK_FILE_ENTRY(imageentry)) ); - georef_layer_load_image ( *vgl ); + georef_layer_load_image ( *vgl, VIK_VIEWPORT(vp), FALSE ); } gtk_widget_destroy ( GTK_WIDGET(dialog) ); @@ -628,7 +646,7 @@ static void georef_layer_goto_center ( gpointer vgl_vlp[2] ) utm.northing = vgl->corner.northing - (vgl->height * vgl->mpp_northing / 2); vik_coord_load_from_utm ( &coord, vik_viewport_get_coord_mode ( vp ), &utm ); - vik_viewport_set_center_coord ( vp, &coord ); + vik_viewport_set_center_coord ( vp, &coord, TRUE ); vik_layers_panel_emit_update ( VIK_LAYERS_PANEL(vgl_vlp[1]) ); }