Skip to content

Instantly share code, notes, and snippets.

@megies
Created November 14, 2019 12:01
Show Gist options
  • Save megies/b1d03ec98cb54656644d95a8f7abffb8 to your computer and use it in GitHub Desktop.
Save megies/b1d03ec98cb54656644d95a8f7abffb8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# conda create -n georasters 'georasters=0.5.15' ipython
import numpy as np
import georasters as gr
# from osgeo import gdalnumeric, gdal
import matplotlib.pyplot as plt
# link is not a typo, it's really that weird
# https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/raster/HYP_50M_SR_W.zip
filename = '/tmp/HYP_50M_SR_W.tif'
# just hardcode extent for now and not extract a subset..
extent = (-180, 180, -90, 90)
data = gr.from_file(filename)
# plot RGB 3-band file
# we need to move the first axis (of length 3, which is the three bands for
# Red, Green, Blue) to the end
print(data.raster.shape)
data.raster = np.moveaxis(data.raster, 0, -1)
data.shape = data.raster.shape
print(data.raster.shape)
fig, ax = plt.subplots()
ax.imshow(data.raster, extent=extent)
ax.grid()
# # just use one band from the 3-band RGB geotiff file
# data.raster = data.raster[0]
# # self.shape needs to be manually updated if array is tampered with
# data.shape = data.raster.shape
# builtin plot actually works if it's a 3-band RGB file and axes are rearranged
data.plot()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment