Skip to content

Instantly share code, notes, and snippets.

@GerardoLopez
Created March 26, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerardoLopez/5bec2dc0309a6bb2d0c5788d23b5b0e8 to your computer and use it in GitHub Desktop.
Save GerardoLopez/5bec2dc0309a6bb2d0c5788d23b5b0e8 to your computer and use it in GitHub Desktop.
Lazy in-memory reprojection to a user-specified Coordinate Reference System (CRS)
import xarray as xr
import rasterio
from rasterio.vrt import WarpedVRT
fname = '/data/peatlands_test_data/MOD11A1/LST_Day_1km/h17v03/MOD11A1_LST_Day_1km_h17v03_2019-08.tif'
# Lazy in-memory warping from Sinusoidal to WGS84 lat/lon
with rasterio.open(fname) as src:
# print(src.profile)
with WarpedVRT(src, crs='epsg:4326') as vrt:
# print(vrt.profile)
with xr.open_rasterio(vrt) as dataset_wgs84:
print(dataset_wgs84)
# Lazy in-memory warping from Sinusoidal to BNG
# BNG definition
# from https://spatialreference.org/ref/epsg/osgb-1936-british-national-grid/
# User should pass those parameters?
bng_crs = (f' +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 '
f' +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 '
f' +units=m +no_defs ')
with rasterio.open(fname) as src:
# print(src.profile)
with WarpedVRT(src, crs=bng_crs) as vrt:
# print(vrt.profile)
with xr.open_rasterio(vrt) as dataset_bng:
print(dataset_bng)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment