geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
845 stars 175 forks source link

Generating a geotiff from an image and a list of ground control points. #421

Open DevkumarPatel opened 5 months ago

DevkumarPatel commented 5 months ago

I am not sure if a functionality exists in the geotiff.js library, but the issue is that I have a set of Ground control points (GCPS) which have the image points (x,y,0) and (lat, lng, 0). Now I want to write a geotiff using a raw image object or an image url, and the set of ground control points. I have tried to use tags such as the ModelTiePoint and ModelPixelScale, however, they do not do georeferning. The ground control point is an array in this example [ x, y, 0, lng, lat, 0].

const metadata = {
  width: rawImageDocument.width,
  height: rawImageDocument.height,
  ModelTiepoint: controlPoints.flat(),
  ModelPixelScale: [pixelWidth, pixelHeight, 0.0], // ModelPixelScale
  ExtraSamples: 0,
  SamplesPerPixel: 3, // 3=RGB, 4=RGBA Set the SamplesPerPixel tag for RGBA image
  ProjectedCSTypeGeoKey: 4326, // (EPSG code for WGS 84)
  GeoAsciiParams: 'WGS 84',
          };

// Generate the GeoTIFF synchronously
const tiffData = GeoTIFF.writeArrayBuffer(rgbArray, metadata);

Screenshot 2024-02-23 at 4 01 57 pm The following ground control points, if I have georeferenced marker:

// [x, y, 0, lng, lat, 0] 
[ [0,0,0,121.53644574764846,-28.488382393735773,0],
[1258,0,0,121.6301344525469,-28.488382393735773,0],
[1258,1761,0,121.6301344525469,-28.603588342154737,0],
[0,1761,0,121.53644574764846,-28.603588342154737,0],
[544.90,712.057,0,121.57085806131366,-28.58478369021423,0]]

Here is the georeferenced image: Image 23-2-2024 at 4 10 pm

Here is a generated geo tiff file result in QGIS for the above image: Screenshot 2024-02-23 at 4 14 50 pm It will be a great idea to add functions like set_gcps, provided in gdal, to geotiff if possible, only if the functionality is not available currently.