MATLAB Answers

Plotting vectors on geoplot

57 views (last 30 days)
Filip
Filip on 1 Sep 2019
Edited: david sarria on 15 Oct 2020
Dear all,
I am trying to add some vector into a geographic coordinate plot. So far I haven't been able to find a way to do this.
Here I attach a simple example demonstrating my problem. I want to have a map with two points and vectors comming out of these 2 points at a 45 degree angle. I can make 2 lines, but I would like to also have arrowheads. I tried using quiver but I cannor really understand how to solve the error it gives.
Thank you for your time, any advise is highly appreciated.
Code follows:
clc; close all; clear all;
coords_A = [39.35, 22.95];
coords_B = [44.65, 10.93];
lats = [coords_A(1); coords_B(1)];
longs = [coords_A(2); coords_B(2)];
figure;
geoscatter(lats, longs, 50, 'b', 'filled');
%Want vector in this direction: (45degrees)
u = 1;
v = 1;
%%
% This works but I really want an arrow tip.
hold on;
geoplot([coords_A(1) coords_A(1)+u],[coords_A(2) coords_A(2)+v],'LineWidth',1,'Color','k');
%%
% This doesnt work
% (Error: Adding Cartesian plot to geoaxes is not supported)
quiver(coords_A(1), coords_A(2), u, v);
%%
% This doesnt work
% (Error: Error using gcm (line 25) Not a map axes).
quiverm(coords_A(1), coords_A(2), u, v);
  3 Comments
dpb
dpb on 2 Sep 2019
"are inconsistent despite both being map axes and as a result quiverm does not work either."
No. That's the problem--they are NOT both map axes; geoscatter() creates and plots into the new "geographic axes" while quiverm() and its compadres that also end with the trailing m plot into the Mapping Toolbox map axis.
But, there is a scatterm() if you do have the TB and it is "just" a regular axes object underneath so you should not be prevented if you use it instead from using quiverm() with it.

Sign in to comment.

Answers (1)

david sarria
david sarria on 15 Oct 2020
Edited: david sarria on 15 Oct 2020
Hello,
Here is a first attempt of a function to plot an arrow on a geoplot (geographic coordinates plot) from 'begin_point' to 'end_point'. They must be 2D vectors (size 1X2 or 2X1).
begin_point = [begin_latitude begin_longitude]
end_point = [end_latitude end_longitude]
Procedure:
  • It plots a line from begin_point to end_point
  • to plot the arrow end: plot a line that is begin_point to end_point rotated around end_point by 20 degrees and reduced in length by multiplication by 0.4. Repeat with a -20 degrees angle.
By default, the three parts of the arrow will have different colors. Similar color can be forced using the 'color' option. For example for black:
plot_arrow_geoplot(begin_point,end_point,'color','k')
It can probably be well improved.
-David
function plot_arrow_geoplot(begin_point,end_point,varargin)
if ~(isvector(begin_point) && length(begin_point)==2)
error('begin_point is not a 2D vector')
end
if ~(isvector(end_point) && length(end_point)==2)
error('end_point is not a 2D vector')
end
begin_point = begin_point(:)';
end_point = end_point(:)';
pivot_x = end_point(1);
pivot_y = end_point(2);
theta = 20;
px = begin_point(1);
py = begin_point(2);
s = sind(theta);
c = cosd(theta);
px = (px - pivot_x)*0.4;
py = (py - pivot_y)*0.4;
xnew = px * c - py * s;
ynew = px * s + py * c;
px = xnew + pivot_x;
py = ynew + pivot_y;
theta = -20;
px2 = begin_point(1);
py2 = begin_point(2);
s = sind(theta);
c = cosd(theta);
px2 = (px2 - pivot_x)*0.4;
py2 = (py2 - pivot_y)*0.4;
xnew = px2 * c - py2 * s;
ynew = px2 * s + py2 * c;
px2 = xnew + pivot_x;
py2 = ynew + pivot_y;
% plot
geoplot([begin_point(1) end_point(1)],[begin_point(2) end_point(2)],... % line
[px end_point(1)],[py end_point(2)],... % arrow end first part
[px2 end_point(1)],[py2 end_point(2)],varargin{:}) % arrow end second part
end

Community Treasure Hunt

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

Start Hunting!