3

I have a map that I want to project onto a sphere, I use cartopy as suggested in another thread. I program in Jupyter.

My code is:

import os
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

os.chdir(os.getenv("HOME"))
os.chdir("Downloads/")
img = plt.imread("europamap1.jpg")

plt.figure(figsize=(3, 3))

ax = plt.axes(projection=ccrs.Orthographic(-10, 45))
ax.gridlines(color="black", linestyle="dotted")
ax.imshow(img, origin="upper", extent=(-180, 180, -90, 90),
          transform=ccrs.PlateCarree())  # Important

plt.show()

What I get is a white filled circle enter image description here

I don't know why I cannot see any gridlines nor the image! I tried to plot.show(img) and the image is loaded! I basically just copied and pasted from this thread

Franndy Abreu
  • 186
  • 2
  • 12
Romero Azzalini
  • 184
  • 1
  • 1
  • 12

1 Answers1

1

The image file you are loading with plt.imreadis not a geospatial raster format, which are commonly GeoTIFF files that end with the .tiff extension.

The problem is that your image lacks a coordinate reference system (CRS) to align with cartopy. For example print(img.crs) most likely does not print a CRS.

To project your data you will need to find a GeoTIFF file and align the CRS with the projection that cartopy uses - ccrs.PlateCarree(), for example.

Some test satellite imagery can be found here: https://gisgeography.com/usgs-earth-explorer-download-free-landsat-imagery/.

For reading and writing raster data, see rasterio: https://rasterio.readthedocs.io/en/latest/.

Here is an example using a GeoTIFF from the source linked in the comments. https://photojournal.jpl.nasa.gov/tiff/PIA03526.tif:.

import matplotlib.pyplot as plt
import rasterio
from rasterio import plot

src = rasterio.open(r"../tests/data/PIA03526.tif")
plot.show(src)
Sam Comber
  • 1,237
  • 1
  • 15
  • 35
  • oh wow, I did not realize that! would you know how to project such maps onto a globe? https://photojournal.jpl.nasa.gov/catalog/PIA03526 – Romero Azzalini Oct 30 '18 at 23:08
  • @RomeroAzzalini Yes, use the `rasterio` library! See my answer edits. Also, this notebook should get you started: https://github.com/mapbox/rasterio/blob/master/examples/Data%20visualization.ipynb. – Sam Comber Oct 30 '18 at 23:15
  • it seems to work, but it's not what I want. I would to wrap the picture on a spherical surface (sphere radius=1) and would love to do some 3D trajetory plotting in the same picture aswell – Romero Azzalini Oct 31 '18 at 00:52
  • @SamComber FYI, that notebook has been deleted from the rasterio sources. The maintainer said it was deprecated. Do you have another more current example somewhere? – Translunar Jul 14 '19 at 21:53