Select to view content in your preferred language

Not able to correctly infer GPS points from geotiff in Javascript

394
1
01-06-2024 03:24 AM
Labels (3)
Kimmart
New Contributor
 

I am trying to create a list of <lat, long, elevation> for each pixel in a geotiff.

The script below seems to be getting values, but when I check the lat/longs on the same geotiff imported into Arcgis Pro I get different values.

import { fromFile } from 'geotiff';

//Script assumes EPSG:4326

async function readDemToXyz(filename) {
  const tiff = await fromFile(filename);
  const image = await tiff.getImage();
  const width = image.getWidth();
  const height = image.getHeight();
  const bbox = image.getBoundingBox();
  const elevationData = await image.readRasters({ interleave: true });

  const deltaX = (bbox[2] - bbox[0]) / width;
  const deltaY = (bbox[3] - bbox[1]) / height;

  let xyzData = [];

  for (let i = 0; i < height; i++) {
    for (let j = 0; j < width; j++) {
      const x = bbox[0] + j * deltaX;
      const y = bbox[1] + i * deltaY;
      const z = elevationData[i * width + j];
      console.log(`Coordinates: (${x.toFixed(9)}, ${y.toFixed(9)}), Elevation: ${z}`);

      xyzData.push([x, y, z]);
    }
  }

  return xyzData;
}

// Usage
const filename = 'example.tif';
readDemToXyz(filename).then(xyzData => {
  //console.log(xyzData.slice(0, 100));  // Print the first 100 [x, y, MSL] values
  //console.log("Printing all values")
  //console.log(xyzData)
});

example console.log Coordinates: (-83.298493884, 34.970485560), Elevation: 1079.0888671875

example lat/long being checking Arcgis Pro

3.png

image showing that the map crs

4.png

image showing export of dem geotiff crs

I was expecting that <lat, long, elevation> values would be the same in the console.log as they were in Arcgis Pro on the same Geotiff.

Instead they are different and I'm not sure why.

Tags (1)
0 Kudos
1 Reply
Zikang
by
Occasional Contributor

i am not very sure about the geotiff lib that you used here. could there be a problem on the point of origin?

another suggestion: could you convert your geotiff into a cloud-optimized geotiff? in this way, you will be able to use arcgis javascript api and create an imagery tile layer out of it. imagery tile layer will support get pixel value at a specific location. you may use this workflow as cross check.

if i were to debug the problem, i would use arcgis pro as the baseline. it is the most reliable in most cases...

0 Kudos