Search code examples
pythonmatplotlibprojection

Elevation distortion on sphere-projected image in Python


I'm trying to take two rectangular images, one of visible surface features and one representing elevation, and map them onto a 3D sphere. I know how to map features onto a sphere with Cartopy, and I know how to make relief surface maps, but I can't find a simple way to combine them to have exaggerated elevation on a spherical projection. For an example, here's it done in MATLAB: Example picture

Does anybody know if there's a simple way to do this in Python?


Solution

  • My solution does not meet all of your requirements. But it could be a good starter, to begin with.

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    from matplotlib.cbook import get_sample_data
    from matplotlib._png import read_png
    
    # Use world image with shape (360 rows, 720 columns) 
    pngfile = 'temperature_15-115.png'
    
    fn = get_sample_data(pngfile, asfileobj=False)
    img = read_png(fn)   # get array of color
    
    # Some needed functions / constant
    r = 5
    pi = np.pi
    cos = np.cos
    sin = np.sin
    sqrt = np.sqrt
    
    # Prep values to match the image shape (360 rows, 720 columns)
    phi, theta = np.mgrid[0:pi:360j, 0:2*pi:720j]
    
    # Parametric eq for a distorted globe (for demo purposes)
    x = r * sin(phi) * cos(theta)
    y = r * sin(phi) * sin(theta)
    z = r * cos(phi) + 0.5* sin(sqrt(x**2 + y**2)) * cos(2*theta)
    
    fig = plt.figure()
    fig.set_size_inches(9, 9)
    ax = fig.add_subplot(111, projection='3d', label='axes1')
    
    # Drape the image (img) on the globe's surface
    sp = ax.plot_surface(x, y, z, \
                    rstride=2, cstride=2, \
                    facecolors=img)
    
    ax.set_aspect(1)
    
    plt.show()
    

    The resulting image:

    enter image description here