readBasemapImage: How to transform the MapCellReference coordinates into non-projected coordinates
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Max
el 17 de Jul. de 2023
Comentada: Juan Sebastian
el 1 de Nov. de 2023
-- edit removed calculation to UTM and only display WGS-84 values
Dear Matlab Community,
I am currently working on loading tiles with ReadBaseMapImage and displaying them in a figure. However, I am facing an issue where the tiles are not aligned properly, and I'm unsure of the reason behind it.
I am restricted to using only a figure() for displaying the maps. I understand that there are other methods available in Matlab for displaying maps that are not part of the figure itself. However, my current approach follows these steps:
- I have or create the corners of the map tiles in wgs84 coordinates.
- I read the image and MapCellReference using [A,R,attrib] = readBasemapImage(basemap,latlim,lonlim)
- I convert the #WorldLimits into WGS84 using [lat,lon] = projinv(proj,x,y).
- I display both images where the world limits are located.
However, when I overlay these pictures, I notice that they are not properly aligned. It seems that there might be an error in my calculations.
My question is whether someone knows the source of this offset and if there is a way to fix it so that the tiles align with each other.
I would greatly appreciate any support or insights you can provide. Thank you in advance!

PS: The code I have used is as follows:
%% Getting Coordinates from a specific location in the world (e.g., Rome)
% Defining the points for the first tile
wgs_top_left_tile_1 = [41.891352, 12.490629];
wgs_bot_right_tile_1 = [41.888996, 12.494105];
% Defining the points for the second tile
wgs_top_left_tile_2 = [41.892278, 12.491798];
wgs_bot_right_tile_2 = [41.888096, 12.49414];
% Setting up the image type
map_type = 'satellite';
%% Calculation of the coordinates in WGS for tile 1
% Convert the input to limits
wgs_lim_lat_tile_1 = sort([wgs_top_left_tile_1(1), wgs_bot_right_tile_1(1)]);
wgs_lim_lng_tile_1 = sort([wgs_top_left_tile_1(2), wgs_bot_right_tile_1(2)]);
%% Calculation of the coordinates in WGS for tile 2
% Convert the input to limits
wgs_lim_lat_tile_2 = sort([wgs_top_left_tile_2(1), wgs_bot_right_tile_2(1)]);
wgs_lim_lng_tile_2 = sort([wgs_top_left_tile_2(2), wgs_bot_right_tile_2(2)]);
%% Get the map images
% Read the image for the first map
[my_image, img_info, ~] = readBasemapImage(map_type, wgs_lim_lat_tile_1, wgs_lim_lng_tile_1);
% Read the Image for the second map
[my_image_2, img_info_2, ~] = readBasemapImage(map_type, wgs_lim_lat_tile_2, wgs_lim_lng_tile_2);
%% Display the maps
% Create a figure
figure();
% Create axes for the figure
h = axes();
% Ensure Y axes are not inverted
h.YDir = 'normal';
% Enable hold on to draw multiple maps
hold on;
% Display the first map
display_image(my_image, img_info, h);
% Display the second map
display_image(my_image_2, img_info_2, h);
function display_image(img_rgb_matrix, img_map_info, my_axes)
% Display an image with transparency on the specified axes, using WGS coordinates.
% Create the image object
x = image(img_rgb_matrix);
% Set its transparency
x.AlphaData = 0.5;
% Convert pseudo-mercator coordinates to WGS coordinates
[world_limits_x, world_limits_y] = get_wgs_from_psm(img_map_info.ProjectedCRS, img_map_info.XWorldLimits, img_map_info.YWorldLimits);
% Set the x and y data for the picture
x.XData = world_limits_y;
x.YData = world_limits_x;
% Set the x and y limits of the axes to the WGS world limits
xlim(my_axes, world_limits_y);
ylim(my_axes, world_limits_x);
% Preserve the WGS scale by making the aspect ratio of the plot equal
axis(my_axes, 'equal');
end
function [wgs_lat, wgs_lng] = get_wgs_from_psm(projection_crs, psm_x, psm_y)
% Convert pseudo-mercator coordinates to WGS coordinates using the specified projection.
% Convert projection coordinates to latitude and longitude
[wgs_lat, wgs_lng] = projinv(projection_crs, psm_x, psm_y);
end
1 comentario
Varun
el 18 de Ag. de 2023
Hello! I found that manually adjusting the initial wgs coordinates can make the images overlap better. However, I am unsure about why this is the case. I used the coordinates below to get a clearer image.
% Defining the points for the first tile
wgs_top_left_tile_1 = [41.891352, 12.490629];
wgs_bot_right_tile_1 = [41.888996, 12.494105];
% Defining the points for the second tile
wgs_top_left_tile_2 = [41.892253, 12.491798];
wgs_bot_right_tile_2 = [41.888091, 12.49414];
Can you tell me more about how you arrived at your initial points for the tile? That might help me understand the issue better!
Más respuestas (1)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

