-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
plot_rasterio.py
49 lines (39 loc) · 1.31 KB
/
plot_rasterio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
.. _recipes.rasterio:
=================================
Parsing rasterio's geocoordinates
=================================
Converting a projection's cartesian coordinates into 2D longitudes and
latitudes.
These new coordinates might be handy for plotting and indexing, but it should
be kept in mind that a grid which is regular in projection coordinates will
likely be irregular in lon/lat. It is often recommended to work in the data's
original map projection (see :ref:`recipes.rasterio_rgb`).
"""
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
from pyproj import Transformer
import xarray as xr
# Read the data
url = "https://github.com/rasterio/rasterio/raw/master/tests/data/RGB.byte.tif"
da = xr.open_rasterio(url)
# Compute the lon/lat coordinates with pyproj
transformer = Transformer.from_crs(da.crs, "EPSG:4326", always_xy=True)
lon, lat = transformer.transform(*np.meshgrid(da["x"], da["y"]))
da.coords["lon"] = (("y", "x"), lon)
da.coords["lat"] = (("y", "x"), lat)
# Compute a greyscale out of the rgb image
greyscale = da.mean(dim="band")
# Plot on a map
ax = plt.subplot(projection=ccrs.PlateCarree())
greyscale.plot(
ax=ax,
x="lon",
y="lat",
transform=ccrs.PlateCarree(),
cmap="Greys_r",
add_colorbar=False,
)
ax.coastlines("10m", color="r")
plt.show()