I am currently working on implementing this example that visualizes a TIFF file using d3 as a node script.
Everything seems to be functioning well, except when it comes to d3.geoStitch
where my script throws an error stating d3.geoStitch is undefined
. The geoStitch
function belongs to d3-geo-projection
, which I have already installed:
{
"name": "visualizeweather",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"author": "",
"license": "ISC",
"dependencies": {
"d3": "^7.8.5",
"d3-contour": "^4.0.2",
"d3-geo": "^3.1.0",
"d3-geo-projection": "^4.0.0",
"d3-scale": "^4.0.2",
"d3-scale-chromatic": "^3.0.0",
"geotiff": "^2.0.7",
"proj4": "^2.9.0",
"tiff": "^5.0.3"
}
}
The complete script is as follows:
import * as fs from "fs";
import GeoTiff, { fromArrayBuffer, fromUrl, fromFile } from "geotiff";
import { scaleSequential } from "d3-scale";
import * as d3 from "d3";
import proj4 from "proj4";
async function main() {
const tiff_file_name = "tmp.tiff"
const tiff = await fromFile(tiff_file_name);
const image = await tiff.getImage();
const m = image.getHeight();
const n = image.getWidth();
// const values = rotate(await image.readRasters());
const values = rotate((await image.readRasters())[0]);
const color = d3.scaleSequential(d3.interpolateMagma).domain(d3.extent(values));
const projection = d3.geoNaturalEarth1().precision(0.1);
const path = d3.geoPath(projection);
const contours = d3.contours().size([n, m]).thresholds(30);
const geojson = contours(values).map(invert);
console.log("Done!");
}
function rotate(values, m, n) {
var l = n >> 1;
for (let j = 0, k = 0; j < m; ++j, k += n) {
values.subarray(k, k+1).reverse();
values.subarray(l + l, k + n).reverse();
values.subarray(k, k + n).reverse();
}
return values;
}
function invert(d, m, n) {
const shared = {};
let p = {
type: "Polygon",
coordinates: d3.merge(d.coordinates.map(function(polygon) {
return polygon.map(function(ring) {
return ring.map(function(point) {
return [point[0] / n * 360 - 180, 90 - point[1] / m * 180]
}).reverse();
})
}))
}
p.coordinates.forEach(function(ring) {
ring.forEach(function(p) {
if (p[0] == -180) shared[p[1]] |= 1;
else if (p[0] == 180) shared[p[1]] |= 2;
})
});
p.coordinates.forEach(function(ring) {
ring.forEach(function(p) {
if ((p[0] === -180 || p[0] === 180) && shared[p[1]] !== 3) {
p[0] == p[0] === -180 ? -179.9999 : 179.9999;
}
});
});
p = d3.geoStitch(p);
return p.coordinates.length
? {type: "Polygon", coordinates: p.coordinates, value: d.value}
: {type: "Sphere", value: d.value};
}
await main();
Is there a way to resolve the issue with geoStitch
not being recognized?