Published
Edited
Nov 4, 2021
Insert cell
# Resizing Images with GeoWarp
Insert cell
geowarp = import("https://cdn.skypack.dev/geowarp").then(module => module.default)
Insert cell
url = "https://storage.googleapis.com/pdd-stac/disasters/hurricane-harvey/0831/20170831_172754_101c_3b_Visual.tif"
Insert cell
GeoTIFF = require("geotiff")
Insert cell
fromUrl = GeoTIFF.fromUrl
Insert cell
geotiff = fromUrl(url)
Insert cell
image = geotiff.getImage()
Insert cell
read_height = 300
Insert cell
read_width = 800
Insert cell
rasters = geotiff.readRasters({ height: read_height, width: read_width })
Insert cell
toImageData = import("https://cdn.skypack.dev/to-image-data").then(module => module.default)
Insert cell
image_data = toImageData(rasters)
Insert cell
function image_data_to_canvas(image_data) {
const canvas = document.createElement("CANVAS");
canvas.height = image_data.height;
canvas.width = image_data.width;
const context = canvas.getContext("2d");
context.putImageData(image_data, 0, 0);
return canvas;
}
Insert cell
image_data_to_canvas(image_data)
Insert cell
# now try to resize
Insert cell
function resize({ data, debug_level=0, in_height, in_width, in_layout, out_height, out_width, out_layout, method = "median", round = false, theoretical_min, theoretical_max }) {
const bbox = [0, 0, in_width, in_height];
return geowarp({
debug_level,
in_data: data,
in_bbox: bbox,
in_layout,
in_width,
in_height,
out_bbox: bbox,
out_layout,
out_height,
out_width,
method: method || "median",
round,
theoretical_min,
theoretical_max
}).data;
}

Insert cell
resized_height = 150
Insert cell
resized_width = 300
Insert cell
resized_rasters = resize({ data: rasters, in_height: read_height, in_width: read_width, in_layout: "[band][row,column]", out_layout: "[band][row][column]", out_height: resized_height, out_width: resized_width, method: "median"})
Insert cell
resized_image_data = toImageData({ data: resized_rasters, height: resized_height, width: resized_width })
Insert cell
image_data_to_canvas(resized_image_data)
Insert cell
xdim = import("https://cdn.skypack.dev/xdim")
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