how to add geoplot scatter plot custom icon overlays?

조회 수: 15 (최근 30일)
tybo1mos
tybo1mos 2024년 12월 27일
답변: Adam Danz 2025년 1월 15일
I'm trying to create a geoplot with custom icons for each scatter point.
Here is what I have been able to get so far that overlays my custom Icon, but the Icons end up outside/off the actual GEO plot. Looking for any suggestions on how to correctly implement this:
lat = [37.7749, 34.0522, 40.7128]; % San Francisco, Los Angeles, New York
lon = [-122.4194, -118.2437, -74.0060];
% Create a geoaxes and plot points for reference
figure;
gx = geoaxes; % Create a geoaxes object
geoscatter(lat, lon, 100, 'blue', 'filled'); % Plot placeholder points
hold on;
% Path to the custom icon (must be a PNG with transparency)
iconPath = 'http://maps.google.com/mapfiles/kml/shapes/airports.png'; % Replace with the actual file path to your icon
icon = imread(iconPath); % Load the custom icon image
% Define the size of the icons as a fraction of the map area
iconSize = 0.05; % Fraction of map area to represent icon size
% Get geoaxes limits
latlim = gx.LatitudeLimits;
lonlim = gx.LongitudeLimits;
% Loop through each location to overlay icons
for i = 1:length(lat)
% Convert geographic coordinates to normalized coordinates
x = (lon(i) - lonlim(1)) / (lonlim(2) - lonlim(1)); % Normalize longitude
y = (lat(i) - latlim(1)) / (latlim(2) - latlim(1)); % Normalize latitude
% Overlay custom icons using an additional axes for each icon
axIcon = axes('Position', [x - iconSize/2, y - iconSize/2, iconSize, iconSize], ...
'Units', 'normalized');
imshow(icon, 'Parent', axIcon); % Display the custom icon
set(axIcon, 'Color', 'none', 'XColor', 'none', 'YColor', 'none'); % Hide axes borders
end
% Hold off after finishing
hold off;
% (Optional) Adjust figure appearance
gx.Basemap = 'satellite'; % Example basemap

답변 (2개)

Adam Danz
Adam Danz 2025년 1월 15일
If you're using MATLAB R2024b or later and have the Mapping Toolbox, use the new geoiconchart.
lat = [37.7749, 34.0522, 40.7128]; % San Francisco, Los Angeles, New York
lon = [-122.4194, -118.2437, -74.0060];
fig = figure();
gx = geoaxes(fig);
% Path to the custom icon
iconPath = 'http://maps.google.com/mapfiles/kml/shapes/airports.png';
gc = geoiconchart(gx,lat,lon,iconPath);
gx.Basemap = 'satellite';

dpb
dpb 2024년 12월 29일
편집: dpb 2024년 12월 29일
lat = [37.7749, 34.0522, 40.7128]; % San Francisco, Los Angeles, New York
lon = [-122.4194, -118.2437, -74.0060];
% Create a geoaxes and plot points for reference
figure;
gx = geoaxes; % Create a geoaxes object
hGS=geoscatter(lat, lon, 100, 'blue', 'filled'); % Plot placeholder points
%hGS
hold on;
% Path to the custom icon (must be a PNG with transparency)
iconPath = 'http://maps.google.com/mapfiles/kml/shapes/airports.png'; % Replace with the actual file path to your icon
icon = imread(iconPath); % Load the custom icon image
% Define the size of the icons as a fraction of the map area
iconSize = 0.05; % Fraction of map area to represent icon size
offset=iconSize/2;
% Get geoaxes limits
latlim = gx.LatitudeLimits;
lonlim = gx.LongitudeLimits;
dx=(lonlim(2) - lonlim(1)); % ranges X,Y
dy=(latlim(2) - latlim(1));
x=(lon-lonlim(1))/dx; % Normalize longitude use MATLAB vectorized features
y=(lat-latlim(1))/dy;
pos=gx.Position; % get the axes position
X=x+pos(1); % add the left origin to relative x position
X = 1×3
0.1538 0.2360 1.1062
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Y=y+pos(2); % and the bottom origin to relative y position
[x.' X.' y.' Y.']
ans = 3×4
0.0238 0.1538 0.5618 0.6718 0.1060 0.2360 0.4440 0.5540 0.9762 1.1062 0.6547 0.7647
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i = 1:length(lat)
axIcon=axes('Position',[X(i)-offset,Y(i)-offset,iconSize,iconSize],'Units','normalized');
imshow(icon, 'Parent', axIcon); % Display the custom icon
set(axIcon, 'Color', 'none', 'XColor', 'none', 'YColor', 'none'); % Hide axes borders
end
% Hold off after finishing
hold off;
% (Optional) Adjust figure appearance
%gx.Basemap = 'satellite'; % Example basemap
Left off satellite to make easier to see...
There are two issues; the first is corrected above to add the axes opening position to the normalized data coordinates to get the positions in the axes relative the origin.
This corrects the longitude issue of them being to the left off the axes so that the two CA cities are almost right in the X direction, but there's a bias heading east that shows up with LA being somewhat more offset that SanF and then NY still being off in the Atlantic somewhere.
The second issue I didn't work on is that the offset and size of the icon as a fraction of the image also has to be converted to the relative size in the two orientations; the scaling difference of the axes between latitude and longitude shows up as the Y offset is much larger than the X offset of the position as to where they're needed to be.
I have neither the Mapping nor the Image toolboxes so can't do much other than here which is a very painful place to debug; particularly when not very familiar with the toolboxes so I'll leave that exercise to you, but you need to convert the size to the image range in data units for each axis direction and then that magnitude into normalized coordinated distances to center the icon at the desired point.
  댓글 수: 1
dpb
dpb 2024년 12월 29일
편집: dpb 2024년 12월 29일
As noted above, I don't have the Image Processing TB so am not familiar with its content (nor am I an image processing guru; nothing I've done much of at all) but I'd think there would be tools to simply overlay/insert the image into the displayed one rather than having to add additional axes onto the figure.
That would still take scaling the icon to the range of the actual image in the two directions as well as locating it within the image, of course, but might be less hassle than the addtional axes.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by