MATLAB Answers

Is there a way to embed a colorbar in a webmap, or am I going about this the wrong way?

11 views (last 30 days)
Anna Paulson
Anna Paulson on 5 Sep 2018
Edited: Anna Paulson on 17 Sep 2018
I am trying to generate a heatmap for parameters we have collected vs. latitude/longitude on a USGS webmap. Here's my script so far...
nBins = 20;
lat = source.VSA_Results_Table.Latitude;
lon = source.VSA_Results_Table.Longitude;
pathloss = source.VSA_Results_Table.Path_Loss_dB;
binSpacing = (max(pathloss) - min(pathloss))/nBins;
binRanges = min(pathloss):binSpacing:max(pathloss)-binSpacing;
latlim = [min(lat), max(lat)];
lonlim = [min(lon), max(lon)];
[latlim, lonlim] = bufgeoquad(latlim, lonlim, .01, .01);
% Add an inf to binRanges to enclose the values above the last bin.
binRanges(end+1) = inf;
% histcdetermines which bin each speed value falls into.
[~, PathLossBins] = histc(pathloss, binRanges);
% Create a geographical shape vector, which stores the line segments as
% features.
s = geoshape();
lat = lat';
lon = lon';
PathLossBins = PathLossBins';
for k = 1:nBins
% Keep only the lat/lon values which match the current bin. Leave the
% rest as NaN, which are interpreted as breaks in the line segments.
latValid = nan(1, length(lat));
latValid(PathLossBins==k) = lat(PathLossBins==k);
lonValid = nan(1, length(lon));
lonValid(PathLossBins==k) = lon(PathLossBins==k);
% To make the path continuous despite being segmented into different
% colors, the lat/lon values that occur after transitioning from the
% current speed bin to another speed bin will need to be kept.
transitions = [diff(PathLossBins) 0];
insertionInd = find(PathLossBins==k & transitions~=0) + 1;
% Preallocate space for and insert the extra lat/lon values.
latSeg = zeros(1, length(latValid) + length(insertionInd));
latSeg(insertionInd + (0:length(insertionInd)-1)) = lat(insertionInd);
latSeg(~latSeg) = latValid;
lonSeg = zeros(1, length(lonValid) + length(insertionInd));
lonSeg(insertionInd + (0:length(insertionInd)-1)) = lon(insertionInd);
lonSeg(~lonSeg) = lonValid;
% Add the lat/lon segments to the geographic shape vector.
s(k) = geoshape(latSeg, lonSeg);
%generate the web map and route overlay
wm = webmap('USGS Imagery');
colors = hot(nBins);
wmlimits(latlim, lonlim)
%wmcenter(wm, 39.98, 105.26)
wmzoom(wm, 17)
wmline(s, 'Color', colors, 'Width', 7);
And the script works really well for generating the heatmap and underlying map. Unfortunately, if I just try adding a colorbar, using something like...
colors = hot(nBins);
c = colorbar
c.Limits = [round(binRanges(1), 2),round(binRanges(20), 2)]
c.Label.String = 'Bit Error Measurement (BER)'
wm = webmap('USGS Imagery');
wmlimits(latlim, lonlim)
%wmcenter(wm, 39.98, 105.26)
wmzoom(wm, 17)
wmline(s, 'Color', colors, 'Width', 7);
it'll generate a colorbar based on my specifications (mostly), but it's on a separate figure from my webmap. Is there a way to add a colorbar on the webmap image using soemthing like set(gcf,...)?
Looking at the mapdemos examples, it seems like webmaps might not be the best way forward. If it isn't, could someone show me where to start with putting my drive route lat/lon coordinates onto a USGS...shapefile(?) of the area, or pointing me to an example?


Sign in to comment.

Accepted Answer

Anna Paulson
Anna Paulson on 17 Sep 2018
Edited: Anna Paulson on 17 Sep 2018
There were a number of factors that made this a hard problem for me to solve on my own - for one, I was not very familiar with the mapping toolbox and making my own GeoTIFF files; second, the heat map solution I adapted ( from here) kept me trying to use a webmap instead of trying to do this in a figure, which is probably where I should have started.
Here's the solution Mathworks support and I came up with...
%decide which GeoTIFF file to use
if ~contains(filename, 'Res')
[DTroute, R] = geotiffread('/myfilepath/DT_route.tif');
city_mapDT = geoshow(DTroute(:,:,1:3), R);
[ResRoute, R] = geotiffread('/myfilepath/ResRoute.tif');
city_mapRes = geoshow(ResRoute(:,:,1:3), R);
%generate the web map and route overlay
xmtr_loc = geoshow(xmtrLat, xmtrLon, 'DisplayType', 'Point','Marker','x','MarkerEdgeColor','Yellow', 'MarkerSize', 12)
colors = hot(nBins);
cmap = colormap(colors);
c = colorbar;
c.LimitsMode = 'auto';
c.Limits = [0 230];
c.TicksMode = 'manual';
c.Ticks = [0, 230];
c.TickLabels = [round(binRanges(1), 2),round(binRanges(20), 2)];
c.Label.String = 'Bit Error Rate (BER) Measurement';
c.Location = 'southoutside';
c.FontSize = 11;
c.FontWeight = 'bold';
hold on
axis image off
%draw the parameter data on the GeoTIFF as a heat map
for i=1:length(cmap)
fig = geoshow(s(i),'Color',cmap(i,:),'LineWidth', 3);
A lot of the code is just setting up the colorbar. The real work comes with choosing the correct GeoTIFF to start with (see first if/else statement) and then drawing the heatmap in the for loop at the end.
The output (see attached file) is exactly what I was looking for, i.e. it's a solution we can apply with minimal refactoring to each heatmap we'd like to generate, and it draws the colorbar with the numerical limits (max/min) for whichever data set we choose to use; the base GeoTIFFs, the parameters to display as a heatmap, the colorbar/map, transmitter location, and max/min values are easily changeable. Hopefully, someone else will find this to be useful!


Sign in to comment.

More Answers (1)

Akshay Khadse
Akshay Khadse on 14 Sep 2018
As per my knowledge, 'webmap' opens a new browser window whereas 'colormap' cannot plot in the browser. Therefore, you are getting the color bar in a separate figure window.
However, currently there is no documented functionality to insert the 'webmap' into a 'panel' or a figure window. You could still dock the 'webmap'. The 'webmap' remembers the dock settings and will be docked the next time it is opened.
Please note that you cannot download the data and show it in a MATLAB Figure window using 'geoshow'.

  1 Comment

Anna Paulson
Anna Paulson on 17 Sep 2018
After working with Mathworks support for the past week, I would agree that there is no way to put a colormap into a webmap. We arrived at that conclusion fairly quickly and then spent the remainder of the time working on a solution, which was the second, and, arguably, more important aspect of the request. However, given the way my question was worded, this is an acceptable answer, although not the answer I was looking for when I posed the question. I'll post my solution in a couple of minutes.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!