Skip to main content

plotting - How to make a 3D topographic globe?


I've made some attempt with this dataset of elevation.


But I have some trouble with ListSurfacePlot3D, which can not show the globe correctly: my attempt



And I've checked that the data is not the problem, since the same data drawn by ListPointPlot3D shows a globe:


another attempt


By the way, my goal is to make a topographic globe which shows 3D mountains and sea basins like this:


a globe


Here is my code:


(*elev data input*)
elev1d = BinaryReadList["D:\\topo\\ETOPO5.DAT", {"Integer16"}, ByteOrdering -> +1];
elev2d = ArrayReshape[elev1d, {2160, 4320}];
lati = Flatten @
Transpose @ Table[Rest @ Table[i, {i, 90, -90, -1/12}], {4320}];

long = Flatten @ Table[Rest @ Table[i, {i, 0, 360, 1/12}], {2160}];
(* make a {lat, lon, altitude} matrix*)
elevlatlon = Transpose @ {lati, long, Flatten @ elev1d};
(*select part of the huge amount of data, add mean earth radius to altitude*)
elevlatlonInUse = (elevlatlon[[;; ;; 12, All]] /.
{m_, n_, o_} -> {m, n, o/200 + 6721}) /. {x_, y /; y > 180, z_} -> {x, y - 360, z};
coordsToXYZ[list_] := Transpose[{Cos[#[[1]]*Pi/180.]*Cos[#[[2]]*Pi/180.]*#[[3]],
Cos[#[[1]]*Pi/180.]*Sin[#[[2]]*Pi/180.]*#[[3]],
Sin[#[[1]]*Pi/180.]*#[[3]]} & @ Transpose[list]]
xyz = First[coordsToXYZ /@ {elevlatlonInUse}];

ListPointPlot3D[xyz, BoxRatios -> {1, 1, 1}]
ListSurfacePlot3D[xyz, BoxRatios -> {1, 1, 1}]

It's a little different from How to make a 3D globe?. That's a globe with a 2D texture covering it, but this is a real 3D globe with elevations shown in 3D as well.


P.S. Someone reminded me that, compared with the radius of the Earth (about $6371 \text{ km}$), even Mt. Everest ($8.8\text{ km}$) and the Marianas Trench ($-11\text{ km}$) can be ignored. That's true, I know, but to draw a globe with bumps, we can just scale the elevation. A visualized topographic globe is just for presentation, and not for calculation.



Answer



This answer is intended to demonstrate a neat method I'd recently learned for constructing interpolating functions over the sphere.


A persistent problem dogging a lot of interpolation methods on the sphere has been the subject of what to do at the poles. A recently studied method, dubbed the "double Fourier sphere method" in this paper (based on earlier work by Merilees) copes remarkably well. This is based on constructing a periodic extension/reflection of the data over at the poles, and then subjecting the resulting matrix to a low-rank approximation. The first reference gives a sophisticated method based on structured Gaussian elimination; in this answer, to keep things simple (at the expense of some slowness), I will use SVD instead.


As I noted in this Wolfram Community post, one can conveniently obtain elevation data for the Earth through GeoElevationData[]. Here is some elevation data with modest resolution (those with sufficient computing power might consider increasing the GeoZoomLevel setting):


gdm = Reverse[QuantityMagnitude[GeoElevationData["World", "Geodetic",

GeoZoomLevel -> 2, UnitSystem -> "Metric"]]];

The DFS trick is remarkably simple:


gdmdfst = Join[gdm, Reverse[RotateLeft[gdm, {0, Length[gdm]}]]];

This yields a $1024\times 1024$ matrix. We now take its SVD:


{uv, s, vv} = SingularValueDecomposition[gdmdfst];

To construct the required low-rank approximations, we treat the left and right singular vectors (uv and vv) as interpolation data. Here is a routine for trigonometric fitting (code originally from here, but made slightly more convenient):


trigFit[data_?VectorQ, n : (_Integer?Positive | Automatic) : Automatic,

{x_, x0_: 0, x1_}] :=
Module[{c0, clist, cof, k, l, m, t},
l = Quotient[Length[data] - 1, 2]; m = If[n === Automatic, l, Min[n, l]];
cof = If[! VectorQ[data, InexactNumberQ], N[data], data];
clist = Rest[cof]/2;
cof = Prepend[{1, I}.{{1, 1}, {1, -1}}.{clist, Reverse[clist]}, First[cof]];
cof = Fourier[cof, FourierParameters -> {-1, 1}];
c0 = Chop[First[cof]]; clist = Rest[cof];
cof = Chop[Take[{{1, 1}, {-1, 1}}.{clist, Reverse[clist]}, 2, m]];
t = Rescale[x, {x0, x1}, {0, 2 π}];

c0 + Total[MapThread[Dot, {cof, Transpose[Table[{Cos[k t], Sin[k t]},
{k, m}]]}]]]

Now, convert the singular vectors into trigonometric interpolants (and extract the singular values as well):


vals = Diagonal[s];
usc = trigFit[#, {φ, 2 π}] & /@ Transpose[uv];
vsc = trigFit[#, {θ, 2 π}] & /@ Transpose[vv];

Now, build the spherical interpolant, taking as many singular values and vectors as seen fit (I arbitrarily chose $\ell=768$, corresponding to $3/4$ of the singular values), and construct it as a compiled function for added efficiency:


l = 768; (* increase or decrease as needed *)

earthFun = With[{fun = Total[Take[vals, l] Take[usc, l] Take[vsc, l]]},
Compile[{{θ, _Real}, {φ, _Real}}, fun,
Parallelization -> True, RuntimeAttributes -> {Listable},
RuntimeOptions -> "Speed"]];

Now, for the plots. Here is an appropriate color gradient:


myGradient1 = Blend[{{-8000, RGBColor["#000000"]}, {-7000, RGBColor["#141E35"]},
{-6000, RGBColor["#263C6A"]}, {-5000, RGBColor["#2E5085"]},
{-4000, RGBColor["#3563A0"]}, {-3000, RGBColor["#4897D3"]},
{-2000, RGBColor["#5AB9E9"]}, {-1000, RGBColor["#8DD2EF"]},

{0, RGBColor["#F5FFFF"]}, {0, RGBColor["#699885"]},
{50, RGBColor["#76A992"]}, {200, RGBColor["#83B59B"]},
{600, RGBColor["#A5C0A7"]}, {1000, RGBColor["#D3C9B3"]},
{2000, RGBColor["#D4B8A4"]}, {3000, RGBColor["#DCDCDC"]},
{5000, RGBColor["#EEEEEE"]}, {6000, RGBColor["#F6F7F6"]},
{7000, RGBColor["#FAFAFA"]}, {8000, RGBColor["#FFFFFF"]}}, #] &;

Let's start with a density plot:


DensityPlot[earthFun[θ, φ], {θ, 0, 2 π}, {φ, 0, π},
AspectRatio -> Automatic, ColorFunction -> myGradient1,

ColorFunctionScaling -> False, Frame -> False, PlotPoints -> 185,
PlotRange -> All]

Earth map constructed from DFS trigonometric interpolant


Due to the large amount of terms, the plotting is a bit slow, even with the compilation. One might consider using e.g. the Goertzel-Reinsch algorithm for added efficiency, which I leave to the interested reader to try out.


For comparison, here are plots constructed from approximations of even lower rank ($\ell=128,256,512$), compared with a ListDensityPlot[] of the raw data (bottom right):


low rank approximations of Earth


Finally, we can look at an actual globe:


With[{s = 2*^5},
ParametricPlot3D[(1 + earthFun[θ, φ]/s)

{Sin[φ] Cos[θ], Sin[φ] Sin[θ], -Cos[φ]} // Evaluate,
{θ, 0, 2 π}, {φ, 0, π}, Axes -> None, Boxed -> False,
ColorFunction -> (With[{r = Norm[{#1, #2, #3}]},
myGradient1[s r - s]] &),
ColorFunctionScaling -> False, MaxRecursion -> 1,
Mesh -> False, PlotPoints -> {500, 250}]] // Quiet

DFS globe


(I had chosen the scaling factor s to make the depressions and elevations slightly more prominent, just like in my Community post.)


Of course, using all the singular values and vectors will result in an interpolation of the data (tho it is even more expensive to evaluate). It is remarkable, however, that even the low-rank DFS approximations already do pretty well.



Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...