Asked by charliewalker1
on 25 Jul 2017

I have a function that displays the countries of the world on a global plot and I need to know how to plot lines of lat and lon in even increments of 10 degrees onto this global plot without using the geoshow command found in the mapping toolbox.

Answer by Chad Greene
on 2 Aug 2017

Accepted Answer

You can do this easily with the standard matlab function sph2cart. Treat the lines of latitude and longitude separately like this:

R = 6371; % earth radius in km

latspacing = 10;

lonspacing = 20;

% lines of longitude:

[lon1,lat1] = meshgrid(-180:lonspacing:180,linspace(-90,90,300));

[x1,y1,z1] = sph2cart(lon1*pi/180,lat1*pi/180,R);

plot3(x1,y1,z1,'-','color',0.5*[1 1 1])

hold on

% lines of latitude:

[lat2,lon2] = meshgrid(-90:latspacing:90,linspace(-180,180,300));

[x2,y2,z2] = sph2cart(lon2*pi/180,lat2*pi/180,R);

plot3(x2,y2,z2,'-','color',0.5*[1 1 1])

axis equal tight off

And since you can see all the way through the globe, perhaps you want to put an opaque sphere inside the globe. Do that like this. I'm making the sphere 0.99 times the size of the Earth just to make sure it doesn't overlap the lines of lat and lon:

[X,Y,Z] = sphere(100);

surf(X*R*.99,Y*R*.99,Z*R*.99,'facecolor','w','edgecolor','none')

For a little context, I'll also convert national borders to cartesian coordinates in the same way. You'll need the data from my borders function for this part:

C = load('borderdata.mat');

for k = 1:246

[xtmp,ytmp,ztmp] = sph2cart(deg2rad(C.lon{k}),deg2rad(C.lat{k}),R);

plot3(xtmp,ytmp,ztmp,'k')

end

Margareta
on 10 Sep 2018

Hi Chad,

that's a very neat solution - thank you very much!

I have a related question - what if I want to "hide" the latitude and longitude of the hemisphere facing the viewer, and only plot the opposite hemisphere - with the borders of countries? I'm sort of trying to obtain a view of countries and continents that someone sitting at the center of the Earth, facing the same direction as the viewer looking at the figure, would see if the planet suddenly became transparent :-)

Looking forward to ideas and tips from all you Matlab gurus out there! /Maggie

Chad Greene
on 10 Sep 2018

Yikes, I can't figure out a great way to show it, but here's something. Basically NaN out whichever half the globe you don't want to see, then set the viewing angle.

R = 6371; % earth radius in km

latspacing = 10;

lonspacing = 20;

% lines of longitude:

[lon1,lat1] = meshgrid(-180:lonspacing:180,linspace(-90,90,300));

[x1,y1,z1] = sph2cart(lon1*pi/180,lat1*pi/180,R);

z1(lon1<0) = nan;

plot3(x1,y1,z1,'-','color',0.5*[1 1 1])

hold on

% lines of latitude:

[lat2,lon2] = meshgrid(-90:latspacing:90,linspace(-180,180,300));

[x2,y2,z2] = sph2cart(lon2*pi/180,lat2*pi/180,R);

z2(lon2<0) = nan;

plot3(x2,y2,z2,'-','color',0.5*[1 1 1])

% an extra line at 0 longitude:

[lon3,lat3] = meshgrid([0 180],linspace(-90,90,300));

[x3,y3,z3] = sph2cart(lon3*pi/180,lat3*pi/180,R);

plot3(x3,y3,z3,'k-')

axis equal tight off

load topo

[Lon,Lat] = meshgrid(0.5:359.5,-89.5:89.5);

topo(topo<=0) = nan;

topo(Lon>180) = nan;

[X,Y,Z] = sph2cart(deg2rad(Lon),deg2rad(Lat),R+topo/10); % topo/10 produces 100x exaggeration

surf(X,Y,Z,topo);

shading interp

view(43,39)

camlight

material dull

Marisabel Gonzalez
on 15 Feb 2019

How could you plot a lat/lot point and a declination and inclination point?

Sign in to comment.

Answer by Chad Greene
on 8 Mar 2019

Margareta
on 8 Mar 2019

Dear Chad,

thank you so much for sharing the "half Earth" code example earlier (last September), and for also going to the trouble of providing this really nice and nifty set of functions as part of the CD toolbox!

Cheers from Sweden!

Maggie

Chad Greene
on 8 Mar 2019

Thanks so much for saying that, Maggie!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## KSSV (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/350195-how-can-you-plot-lines-of-latitude-and-longitude-on-a-globe-without-using-the-mapping-toolbox#comment_471816

## Rik (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/350195-how-can-you-plot-lines-of-latitude-and-longitude-on-a-globe-without-using-the-mapping-toolbox#comment_471852

## per isakson (view profile)

Direct link to this comment:https://ch.mathworks.com/matlabcentral/answers/350195-how-can-you-plot-lines-of-latitude-and-longitude-on-a-globe-without-using-the-mapping-toolbox#comment_471949

Sign in to comment.