Published
Edited
Feb 19, 2022
1 star
Insert cell
# Clip and Reproject a GeoTIFF with GeoWarp

### GeoTIFF from https://geo.fas.usda.gov/gadas
### Sri Lanka Boundaries from https://geojson-maps.ash.ms/
Insert cell
GeoTIFF = require("geotiff")
Insert cell
tif = GeoTIFF.fromUrl("https://raw.githubusercontent.com/GeoTIFF/test-data/main/files/gadas.tif")
Insert cell
rasters = tif.readRasters()
Insert cell
toCanvas = require("to-canvas@0.1.0")
Insert cell
toCanvas({ data: rasters })
Insert cell
image = tif.getImage()
Insert cell
geotiff_width = image.getWidth()
Insert cell
geotiff_height = image.getHeight()
Insert cell
bbox_of_geotiff = image.getBoundingBox()
Insert cell
sri_lanka_geojson = ({"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[81.7879590188914,7.523055324733164],[81.63732221876059,6.481775214051921],[81.21801964714433,6.197141424988288],[80.34835696810441,5.968369859232155],[79.87246870312853,6.76346344647493],[79.69516686393513,8.200843410673386],[80.14780073437964,9.824077663609557],[80.83881798698656,9.268426825391188],[81.30431928907177,8.56420624433369],[81.7879590188914,7.523055324733164]]]}})
Insert cell
geowarp = import("https://cdn.skypack.dev/geowarp@1.3.1").then(module => module.default)
Insert cell
proj4 = import("https://cdn.skypack.dev/proj4-fully-loaded").then(module => module.default)
Insert cell
out_srs = "EPSG:5234" // Kandawala / Sri Lanka Grid
Insert cell
result = geowarp({
in_bbox: bbox_of_geotiff,
in_data: rasters,
in_height: geotiff_height,
in_layout: "[band][row,column]",
in_srs: "EPSG:3857",
in_width: geotiff_width,
out_height: geotiff_height,
out_layout: "[row,column,band]",
out_srs,
out_width: geotiff_width,
forward: proj4("EPSG:3857", out_srs).forward,
inverse: proj4("EPSG:3857", out_srs).inverse,
cutline: sri_lanka_geojson,
cutline_srs: 4326,
cutline_forward: proj4("EPSG:4326", out_srs).forward,
method: "near"
});
Insert cell
toCanvas({ data: result.data, height: geotiff_height, width: geotiff_width })
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more