crossbind
GitHub
← Todas as bibliotecas

GeoTIFF

Metadados GeoTIFF e suporte a sistemas de coordenadas.

Instalação

Roda em navegadores; o Node.js e os ambientes de execução edge usam o mesmo pacote. Compilado para wasm32, com uma só thread e multithread.

shell
npm install @crossbind/port-geotiff-wasm@beta
crossbind.config.js
import geotiffWasm from '@crossbind/port-geotiff-wasm/crossbind.config.js';
 
export default {
dependencies: [geotiffWasm],
paths: { config: import.meta.url },
};
main.js
import { initNative, LIBGEOTIFF_STRING_VERSION } from '@crossbind/port-geotiff/geotiff.h';
 
await initNative();

O próprio crossbind vem com o seu plugin de bundler; Plugins de bundlers cobre Vite, Webpack, Rspack, Rsbuild, Rollup, esbuild, Bun e Next.js.

A libgeotiff no seu navegador: onde fica um GeoTIFF, e GeoTIFFs que a GDAL posiciona

A libgeotiff é a biblioteca GeoTIFF sobre a qual o driver GTiff da GDAL é construído. Estes apps rodam a libgeotiff 1.7.4 com a libtiff e o PROJ, compilados pelo crossbind: eles identificam o sistema de coordenadas de qualquer GeoTIFF e desenham a área de cobertura dele em um mapa, transformam uma imagem e uma caixa delimitadora em um GeoTIFF comprimido que a GDAL posiciona onde você indicou, e leem alturas e graus de um modelo de elevação. A maior parte do download da primeira execução são os dados do PROJ, 10,2 MB, principalmente o banco de dados EPSG proj.db, que a libgeotiff lê para identificar os sistemas de coordenadas. A primeira execução baixa 16,1 MB de WebAssembly uma única vez; todos os apps desta página o compartilham, e nada é enviado.

APP 01

Onde fica este GeoTIFF?

Solte uma cena de satélite, um bloco de elevação ou um mapa de drone e veja o sistema de coordenadas dele pelo nome, os cantos dele em graus e a área de cobertura dele no mapa, com o relatório completo do listgeo. O geotiff.js, o leitor de GeoTIFF em JavaScript mais usado, retorna as GeoKeys brutas; a libgeotiff dá nome ao sistema que elas descrevem pelo banco de dados EPSG do PROJ.

EXEMPLOS

Os exemplos são escritos pelo módulo: pixels em branco, posicionamentos reais. Seu arquivo fica nesta aba: ele é montado no sistema de arquivos em memória do módulo, e a libtiff lê os diretórios e as tags dele, não os pixels. Os graus estão no datum do próprio arquivo, OSGB36 para o exemplo de Londres. Para desenhar pixels de GeoTIFF em um mapa web, o geotiff.js é a opção mais leve.

Abra um exemplo ou um GeoTIFF seu para ver onde ele fica.
MOSTRAR O CÓDIGO
src/support/geotiff_app.h
// src/support/geotiff_app.h (excerpt)
Tiff tif = openTiff(path, "r"); // XTIFFOpen: libtiff with the GeoTIFF tags
Keys keys = openKeys(tif.get()); // GTIFNewEx reads the GeoKeys
GTIFDefn defn;
const bool defined = GTIFGetDefn(keys.get(), &defn) != 0; // EPSG code -> full definition
GTIFGetPCSInfo(defn.PCS, &name, nullptr, nullptr, nullptr); // its name, from proj.db
 
double x = columns[i]; // a corner, in pixels
double y = rows[i];
if (!GTIFImageToPCS(keys, &x, &y)) return "null"; // to map coordinates
return GTIFProj4ToLatLong(&defn, count, x, y) != 0; // to degrees, on the file's datum
 
GTIFPrint(keys, appendText, &text); // the report listgeo prints
GTIFPrintDefnEx(keys, defn, stream);
main.js
const m = await initNative();
const [path] = await m.autoMountFiles([file], await m.getRandomPath('/memfs')); // from <input type=file>
const info = JSON.parse(await m.GeoTiffInspector.inspect(path));
 
// For the Istanbul sample:
// info.epsg: 32635, info.name: 'WGS 84 / UTM zone 35N'
// info.corners[0].lonLat: [28.198970, 41.545594], the upper-left corner in degrees
// info.outline: 64 points around the footprint; info.listgeo: listgeo's report
APP 02

Georreferencie qualquer imagem

Dê a um mapa digitalizado, a uma captura de tela ou a uma renderização o lugar dele na Terra: uma caixa de longitude e latitude, ou um canto e um tamanho de pixel em qualquer sistema EPSG projetado. O módulo o escreve como um GeoTIFF em cinco compressões que a GDAL posiciona onde você indicou, e o lê de volta com a libgeotiff para mostrá-lo. O gravador do geotiff.js guarda os pixels sem compressão.

IMAGEM

Escrito como EPSG:4326, longitude e latitude WGS 84. A caixa é a borda externa da imagem: a GTRasterTypeGeoKey diz que cada pixel cobre uma área.

Escolha uma imagem, ou mantenha o exemplo, e depois escreva os GeoTIFFs.
MOSTRAR O CÓDIGO
src/support/geotiff_app.h
// src/support/geotiff_app.h (excerpt)
bool projected = GTIFGetPCSInfo(epsg, &name, nullptr, nullptr, nullptr) != 0; // or GTIFGetGCSInfo
TIFFSetField(tif.get(), TIFFTAG_COMPRESSION, codec);
const double tiepoint[6] = {0, 0, 0, originX, originY, 0};
const double scale[3] = {pixelWidth, pixelHeight, 0};
TIFFSetField(tif.get(), TIFFTAG_GEOTIEPOINTS, 6, tiepoint);
TIFFSetField(tif.get(), TIFFTAG_GEOPIXELSCALE, 3, scale);
 
GTIFKeySet(keys.get(), GTModelTypeGeoKey, TYPE_SHORT, 1, projected ? ModelTypeProjected : ModelTypeGeographic);
GTIFKeySet(keys.get(), GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea);
GTIFKeySet(keys.get(), ProjectedCSTypeGeoKey, TYPE_SHORT, 1, epsg); // GeographicTypeGeoKey for 4326
if (!GTIFWriteKeys(keys.get())) fail("libgeotiff could not write the GeoKeys");
main.js
const m = await initNative();
const dir = await m.getRandomPath('/memfs');
await m.FS.writeFile(`${dir}/picture.rgba`, rgba); // RGBA pixels, as a canvas gives them
const [west, south, east, north] = [28.6, 40.8, 29.4, 41.3];
const written = JSON.parse(await m.Georeferencer.write(
`${dir}/picture.rgba`, width, height, 4326, west, north,
(east - west) / width, (north - south) / height, 'deflate', 85, `${dir}/picture.tif`));
// written.bytes: 101434 for the 384 x 256 sample, 295368 uncompressed
const tif = await m.getFileBytes(`${dir}/picture.tif`);
APP 03

Leia alturas e graus de um modelo de elevação

Abra um GeoTIFF de elevação e passe o cursor sobre o relevo dele: a libgeotiff transforma a posição sob o cursor em longitude e latitude, ao lado da altura guardada ali. Depois salve o modelo de novo, sem perdas ou com LERC dentro de um erro que você escolher, e compare os tamanhos. Um arquivo otimizado para a nuvem é lido a partir da menor visão geral que ainda preenche a vista.

O exemplo é escrito pelo módulo: alturas geradas em 512 × 512 células de 30 m, posicionadas na zona UTM 33N. Seu arquivo fica nesta aba. A banda 1 é lida, de inteiros ou de floats, e o valor sem dados da GDAL fica de fora. Um arquivo com mais de 512 pixels de lado é amostrado para caber, pelo pixel mais próximo, e as alturas mostradas são essas amostras.

Abra o exemplo, ou um GeoTIFF de elevação seu, e depois passe o cursor sobre ele.
MOSTRAR O CÓDIGO
src/support/dem_file.h
// src/support/dem_file.h (excerpt)
// The smallest overview that still fills the grid, then band 1 block by block
TIFFGetField(tif.get(), TIFFTAG_SUBFILETYPE, &type);
if (!(type & FILETYPE_REDUCEDIMAGE) || (type & FILETYPE_MASK)) continue;
TIFFReadEncodedTile(tif, TIFFComputeTile(tif, left, top, 0, 0), buffer.data(), size);
 
// The position under the cursor, in full-resolution pixels, to degrees
if (!GTIFImageToPCS(keys.get(), &x, &y)) throw std::runtime_error("no tiepoint and pixel scale to place the pixels");
const bool degrees = toLonLat(defn, 1, &lon, &lat);
 
// Saved again as a float GeoTIFF in 256 x 256 tiles
TIFFSetField(tif.get(), TIFFTAG_COMPRESSION, codec.compression);
if (codec.compression == COMPRESSION_LERC) TIFFSetField(tif.get(), TIFFTAG_LERC_MAXZERROR, codec.maxZError);
main.js
const m = await initNative();
const dir = await m.getRandomPath('/memfs');
await m.DemProbe.writeSample(`${dir}/dem.tif`); // or a DEM mounted with autoMountFiles
const probe = await new m.DemProbe(`${dir}/dem.tif`, 512);
const info = JSON.parse(await probe.info()); // size, type, range, no-data value, EPSG
await probe.writeGrid(`${dir}/grid.f32`); // at most 512 x 512 heights, row by row
const heights = new Float32Array((await m.getFileBytes(`${dir}/grid.f32`)).slice().buffer);
const spot = JSON.parse(await probe.at(100.5, 200.5)); // the centre of pixel 100, 200
// spot.lonLat: [13.854264880, 47.073645579]; heights[200 * 512 + 100]: 734.90625
const saved = JSON.parse(await probe.encode('lerc', 0.1, `${dir}/dem-lerc.tif`));
// saved.bytes: 283298 of 1048576 raw; saved.maxError: 0.100006103515625

Exemplos

Cada um é um pequeno arquivo de cabeçalho C++ e o JavaScript que o chama, com o que ele imprimiu quando o site foi compilado. A aba Só JavaScript faz o mesmo com os próprios arquivos de cabeçalho de GeoTIFF e sem arquivo C++. O último é um programa C++ completo, compilado para WASI.

Descubra onde um GeoTIFF fica

A pergunta que a maior parte do código GeoTIFF responde: GTIFNew lê as GeoKeys, GTIFGetDefn as transforma em um sistema de coordenadas, GTIFImageToPCS coloca os pixels no mapa e GTIFProj4ToLatLong transforma coordenadas do mapa em graus.

src/native/geotiff_locator.h
#pragma once
 
#include <geo_normalize.h>
#include <geotiff.h>
#include <geovalues.h>
#include <tiffio.hxx>
#include <xtiffio.h>
 
#include <cstdint>
#include <cstdio>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
 
// Where is a GeoTIFF? Reads the coordinate system from its GeoKeys, puts two corners in map
// coordinates and turns them into longitude and latitude. Files travel as bytes, one per character,
// and libtiff's C++ stream API reads and writes them in memory.
class GeoTiffLocator {
public:
static std::string version() { return LIBGEOTIFF_STRING_VERSION; }
 
// A blank 8-bit image in the projected system `epsg`, its upper-left corner at (x, y), with
// square pixels `pixelSize` map units wide.
static std::u16string write(int epsg, int width, int height, double x, double y, double pixelSize) {
std::ostringstream out;
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("sample.tif", &out), XTIFFClose);
if (!tif) throw std::runtime_error("libtiff could not start a file");
TIFFSetField(tif.get(), TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif.get(), TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif.get(), TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif.get(), TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif.get(), TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif.get(), TIFFTAG_ROWSPERSTRIP, height);
const double tiepoint[6] = {0, 0, 0, x, y, 0}; // pixel (0, 0) sits at map (x, y)
const double scale[3] = {pixelSize, pixelSize, 0};
TIFFSetField(tif.get(), TIFFTAG_GEOTIEPOINTS, 6, tiepoint);
TIFFSetField(tif.get(), TIFFTAG_GEOPIXELSCALE, 3, scale);
 
Keys keys(GTIFNew(tif.get()), GTIFFree);
GTIFKeySet(keys.get(), GTModelTypeGeoKey, TYPE_SHORT, 1, ModelTypeProjected);
GTIFKeySet(keys.get(), GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea);
GTIFKeySet(keys.get(), ProjectedCSTypeGeoKey, TYPE_SHORT, 1, epsg);
GTIFWriteKeys(keys.get());
keys.reset();
 
std::string row(width, '\0');
for (int y = 0; y < height; y += 1) {
if (TIFFWriteScanline(tif.get(), row.data(), y, 0) < 0) throw std::runtime_error("libtiff could not write a row");
}
tif.reset();
const std::string bytes = out.str();
return std::u16string(bytes.begin(), bytes.end());
}
 
// JSON: the EPSG code and name, the size in pixels, and the upper-left and lower-right corners
// in map units and in degrees.
static std::string locate(const std::u16string& bytes) {
std::istringstream in(std::string(bytes.begin(), bytes.end()));
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("input.tif", &in), XTIFFClose);
if (!tif) throw std::runtime_error("not a TIFF file");
Keys keys(GTIFNew(tif.get()), GTIFFree);
GTIFDefn defn;
if (!keys || !GTIFGetDefn(keys.get(), &defn)) throw std::runtime_error("no coordinate system in the GeoKeys");
 
uint32_t width = 0;
uint32_t height = 0;
TIFFGetField(tif.get(), TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif.get(), TIFFTAG_IMAGELENGTH, &height);
double x[2] = {0, static_cast<double>(width)};
double y[2] = {0, static_cast<double>(height)};
for (int i = 0; i < 2; i += 1) {
if (!GTIFImageToPCS(keys.get(), &x[i], &y[i])) throw std::runtime_error("no tiepoint and pixel scale to place the pixels");
}
double lon[2] = {x[0], x[1]};
double lat[2] = {y[0], y[1]};
if (defn.Model == ModelTypeProjected && !GTIFProj4ToLatLong(&defn, 2, lon, lat)) throw std::runtime_error("PROJ could not unproject the corners");
 
const bool projected = defn.Model == ModelTypeProjected;
char* name = nullptr;
if (projected) GTIFGetPCSInfo(defn.PCS, &name, nullptr, nullptr, nullptr);
else GTIFGetGCSInfo(defn.GCS, &name, nullptr, nullptr, nullptr);
const std::string crs = name ? name : "unnamed";
GTIFFreeMemory(name);
 
char json[640];
std::snprintf(json, sizeof json,
"{\"epsg\":%d,\"name\":\"%s\",\"width\":%u,\"height\":%u,\"upperLeft\":[%.17g,%.17g],\"lowerRight\":[%.17g,%.17g],"
"\"upperLeftLonLat\":[%.17g,%.17g],\"lowerRightLonLat\":[%.17g,%.17g]}",
projected ? defn.PCS : defn.GCS, crs.c_str(), width, height, x[0], y[0], x[1], y[1], lon[0], lat[0], lon[1], lat[1]);
return json;
}
 
private:
using Tiff = std::unique_ptr<TIFF, void (*)(TIFF*)>;
using Keys = std::unique_ptr<GTIF, void (*)(GTIF*)>;
};
main.js
import { initNative, GeoTiffLocator } from './native/geotiff_locator.h';
 
await initNative();
// 100 x 100 pixels of 30 m in WGS 84 / UTM zone 33N, the upper-left corner at 500000, 4650000
const tiff = await GeoTiffLocator.write(32633, 100, 100, 500000, 4650000, 30);
const where = JSON.parse(await GeoTiffLocator.locate(tiff));
console.log(`libgeotiff ${await GeoTiffLocator.version()}, ${tiff.length} B: EPSG:${where.epsg} ${where.name}, ${where.width} x ${where.height} pixels`);
const degrees = ([lon, lat]) => `${lon.toFixed(6)} E, ${lat.toFixed(6)} N`;
console.log(`upper left ${where.upperLeft.join(', ')} = ${degrees(where.upperLeftLonLat)}`);
console.log(`lower right ${where.lowerRight.join(', ')} = ${degrees(where.lowerRightLonLat)}`);
IMPRIMEa primeira execução baixa 16,1 MB
libgeotiff 1.7.4, 10262 B: EPSG:32633 WGS 84 / UTM zone 33N, 100 x 100 pixels
upper left 500000, 4650000 = 15.000000 E, 42.002015 N
lower right 503000, 4647000 = 15.036210 E, 41.974990 N

Escreva um GeoTIFF comprimido

O georreferenciamento são duas tags TIFF e algumas GeoKeys: TIFFTAG_GEOTIEPOINTS e TIFFTAG_GEOPIXELSCALE posicionam os pixels, GTIFKeySet e GTIFWriteKeys nomeiam o sistema de coordenadas. GTIFPrint então imprime o resultado do jeito que o listgeo faz.

src/native/geotiff_writer.h
#pragma once
 
#include <geotiff.h>
#include <geovalues.h>
#include <tiffio.hxx>
#include <xtiffio.h>
 
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
 
// Writes 8-bit grey pixels as a Deflate-compressed GeoTIFF in longitude and latitude (WGS 84), and
// prints any GeoTIFF's GeoKeys and georeferencing tags the way the listgeo tool does.
class GeoTiffWriter {
public:
// `pixels` holds width x height bytes, row by row from the top. The image covers west to east
// and south to north, in degrees.
static std::u16string write(const std::u16string& pixels, int width, int height, double west, double south, double east, double north,
const std::string& citation) {
if (width < 1 || height < 1 || pixels.size() != static_cast<size_t>(width) * height) throw std::invalid_argument("pixels must hold width * height bytes");
std::ostringstream out;
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("grey.tif", &out), XTIFFClose);
if (!tif) throw std::runtime_error("libtiff could not start a file");
TIFFSetField(tif.get(), TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif.get(), TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif.get(), TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif.get(), TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif.get(), TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif.get(), TIFFTAG_COMPRESSION, COMPRESSION_ADOBE_DEFLATE);
TIFFSetField(tif.get(), TIFFTAG_PREDICTOR, PREDICTOR_HORIZONTAL);
TIFFSetField(tif.get(), TIFFTAG_ROWSPERSTRIP, 16);
const double tiepoint[6] = {0, 0, 0, west, north, 0};
const double scale[3] = {(east - west) / width, (north - south) / height, 0};
TIFFSetField(tif.get(), TIFFTAG_GEOTIEPOINTS, 6, tiepoint);
TIFFSetField(tif.get(), TIFFTAG_GEOPIXELSCALE, 3, scale);
 
Keys keys(GTIFNew(tif.get()), GTIFFree);
GTIFKeySet(keys.get(), GTModelTypeGeoKey, TYPE_SHORT, 1, ModelTypeGeographic);
GTIFKeySet(keys.get(), GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea);
GTIFKeySet(keys.get(), GTCitationGeoKey, TYPE_ASCII, 0, citation.c_str());
GTIFKeySet(keys.get(), GeographicTypeGeoKey, TYPE_SHORT, 1, GCS_WGS_84);
GTIFKeySet(keys.get(), GeogAngularUnitsGeoKey, TYPE_SHORT, 1, Angular_Degree);
GTIFWriteKeys(keys.get());
keys.reset();
 
std::string row(width, '\0');
for (int y = 0; y < height; y += 1) {
for (int x = 0; x < width; x += 1) row[x] = static_cast<char>(pixels[static_cast<size_t>(y) * width + x]);
if (TIFFWriteScanline(tif.get(), row.data(), y, 0) < 0) throw std::runtime_error("libtiff could not write a row");
}
tif.reset();
const std::string bytes = out.str();
return std::u16string(bytes.begin(), bytes.end());
}
 
// GTIFPrint: the key and tag dump listgeo starts with, collected into a string.
static std::string print(const std::u16string& tiff) {
std::istringstream in(std::string(tiff.begin(), tiff.end()));
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("input.tif", &in), XTIFFClose);
if (!tif) throw std::runtime_error("not a TIFF file");
Keys keys(GTIFNew(tif.get()), GTIFFree);
if (!keys) throw std::runtime_error("libgeotiff could not read the GeoKeys");
std::string text;
GTIFPrint(keys.get(), append, &text);
return text;
}
 
private:
using Tiff = std::unique_ptr<TIFF, void (*)(TIFF*)>;
using Keys = std::unique_ptr<GTIF, void (*)(GTIF*)>;
 
static int append(char* message, void* text) {
static_cast<std::string*>(text)->append(message);
return 1;
}
};
main.js
import { initNative, GeoTiffWriter } from './native/geotiff_writer.h';
 
await initNative();
const [width, height] = [256, 128];
let seed = 11;
const noise = () => (seed = (seed * 48271) % 2147483647) % 24;
const relief = (x, y) => 60 + 2 * Math.abs((x % 64) - 32) + 2 * Math.abs((y % 48) - 24) + noise(); // ridges and noise
const pixels = Array.from({ length: width * height }, (_, i) => String.fromCharCode(relief(i % width, Math.floor(i / width)))).join('');
 
// 10 W to 30 E and 35 N to 60 N, in WGS 84 degrees
const tiff = await GeoTiffWriter.write(pixels, width, height, -10, 35, 30, 60, 'Europe, synthetic relief');
console.log(`${width} x ${height} pixels: ${pixels.length} B, ${tiff.length} B as a Deflate GeoTIFF`);
for (const line of (await GeoTiffWriter.print(tiff)).trimEnd().split('\n')) console.log(line.trimEnd());
IMPRIMEa primeira execução baixa 16,1 MB
256 x 128 pixels: 32768 B, 23246 B as a Deflate GeoTIFF
Geotiff_Information:
   Version: 1
   Key_Revision: 1.0
   Tagged_Information:
      ModelTiepointTag (2,3):
         0                 0                 0
         -10               60                0
      ModelPixelScaleTag (1,3):
         0.15625           0.1953125         0
      End_Of_Tags.
   Keyed_Information:
      GTModelTypeGeoKey (Short,1): ModelTypeGeographic
      GTRasterTypeGeoKey (Short,1): RasterPixelIsArea
      GTCitationGeoKey (Ascii,25): "Europe, synthetic relief"
      GeographicTypeGeoKey (Short,1): GCS_WGS_84
      GeogAngularUnitsGeoKey (Short,1): Angular_Degree
      End_Of_Keys.
   End_Of_Geotiff.

Leia as GeoKeys, o tiepoint e a escala do pixel

GTIFDirectoryInfo conta as chaves, GTIFKeyInfo e GTIFKeyGet leem cada uma com o tipo dela, e GTIFValueNameEx nomeia o valor dela. O tiepoint e a escala do pixel são tags TIFF, lidas com TIFFGetField.

src/native/geokey_reader.h
#pragma once
 
#include <geotiff.h>
#include <tiffio.hxx>
#include <xtiffio.h>
 
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
 
// Reads a GeoTIFF's GeoKeys one at a time, as GDAL and most readers do: GTIFKeyInfo says whether a
// key is set and what type it has, GTIFKeyGet copies its value out. The tiepoint and the pixel
// scale are plain TIFF tags next to the keys.
class GeoKeyReader {
public:
// JSON: the key directory's version, every common key the file sets, and the two tags.
static std::string read(const std::u16string& tiff) {
std::istringstream in(std::string(tiff.begin(), tiff.end()));
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("input.tif", &in), XTIFFClose);
if (!tif) throw std::runtime_error("not a TIFF file");
Keys gtif(GTIFNew(tif.get()), GTIFFree);
if (!gtif) throw std::runtime_error("libgeotiff could not read the GeoKeys");
 
int versions[3] = {0, 0, 0}; // directory version, key revision, minor revision
int count = 0;
GTIFDirectoryInfo(gtif.get(), versions, &count);
char head[96];
std::snprintf(head, sizeof head, "{\"version\":%d,\"revision\":\"%d.%d\",\"count\":%d,\"keys\":[", versions[0], versions[1], versions[2], count);
std::string json = head;
const geokey_t common[] = {GTModelTypeGeoKey, GTRasterTypeGeoKey, GTCitationGeoKey, GeographicTypeGeoKey,
GeogCitationGeoKey, GeogAngularUnitsGeoKey, ProjectedCSTypeGeoKey, PCSCitationGeoKey,
ProjLinearUnitsGeoKey, VerticalCSTypeGeoKey, VerticalUnitsGeoKey};
for (const geokey_t key : common) {
const std::string entry = describe(gtif.get(), key);
if (entry.empty()) continue;
if (json.back() != '[') json += ',';
json += entry;
}
json += "],\"tiepoint\":" + doubles(tif.get(), TIFFTAG_GEOTIEPOINTS) + ",\"pixelScale\":" + doubles(tif.get(), TIFFTAG_GEOPIXELSCALE) + "}";
return json;
}
 
private:
using Tiff = std::unique_ptr<TIFF, void (*)(TIFF*)>;
using Keys = std::unique_ptr<GTIF, void (*)(GTIF*)>;
 
// One key as JSON, or an empty string when the file does not set it. A SHORT key also gets the
// name of its value, from libgeotiff's tables or PROJ's database.
static std::string describe(GTIF* gtif, geokey_t key) {
int size = 0;
tagtype_t type = TYPE_UNKNOWN;
const int count = GTIFKeyInfo(gtif, key, &size, &type);
if (count == 0) return "";
std::string entry = "{\"key\":\"" + std::string(GTIFKeyName(key)) + "\",\"type\":\"" + GTIFTypeName(type) + "\",\"value\":";
if (type == TYPE_ASCII) {
std::string text(count + 1, '\0');
GTIFKeyGetASCII(gtif, key, text.data(), count + 1);
text.resize(std::strlen(text.c_str()));
entry += quoted(text);
} else if (type == TYPE_SHORT) {
unsigned short value = 0;
GTIFKeyGetSHORT(gtif, key, &value, 0, 1);
entry += std::to_string(value) + ",\"name\":" + quoted(GTIFValueNameEx(gtif, key, value));
} else {
double value = 0;
GTIFKeyGetDOUBLE(gtif, key, &value, 0, 1);
entry += number(value);
}
return entry + "}";
}
 
// A TIFF tag of doubles as a JSON array; libgeotiff registers the GeoTIFF tags with a count.
static std::string doubles(TIFF* tif, uint32_t tag) {
uint16_t count = 0;
double* values = nullptr;
if (!TIFFGetField(tif, tag, &count, &values)) return "null";
std::string json = "[";
for (uint16_t i = 0; i < count; i += 1) json += (i ? "," : "") + number(values[i]);
return json + "]";
}
 
static std::string number(double value) {
char text[32];
std::snprintf(text, sizeof text, "%.17g", value);
return text;
}
 
static std::string quoted(const std::string& text) {
std::string out = "\"";
for (const char c : text) {
if (c == '"' || c == '\\') out += '\\';
out += static_cast<unsigned char>(c) < 0x20 ? ' ' : c;
}
return out + "\"";
}
};
main.js
import { initNative, GeoKeyReader } from './native/geokey_reader.h';
import { GeoTiffWriter } from './native/geotiff_writer.h';
 
await initNative();
const pixels = String.fromCharCode(...Array.from({ length: 64 * 32 }, (_, i) => (i * 7) % 256));
const tiff = await GeoTiffWriter.write(pixels, 64, 32, -10, 35, 30, 60, 'Europe'); // the writer from the previous example
 
const read = JSON.parse(await GeoKeyReader.read(tiff));
console.log(`GeoTIFF ${read.version}, key revision ${read.revision}, ${read.count} keys`);
for (const key of read.keys) console.log(`${key.key} (${key.type}): ${JSON.stringify(key.value)}${key.name ? ` = ${key.name}` : ''}`);
const [column, row, , x, y] = read.tiepoint;
console.log(`tiepoint: pixel ${column}, ${row} is at ${x}, ${y}; pixel scale: ${read.pixelScale[0]} x ${read.pixelScale[1]}`);
IMPRIMEa primeira execução baixa 16,1 MB
GeoTIFF 1, key revision 1.0, 5 keys
GTModelTypeGeoKey (Short): 2 = ModelTypeGeographic
GTRasterTypeGeoKey (Short): 1 = RasterPixelIsArea
GTCitationGeoKey (Ascii): "Europe"
GeographicTypeGeoKey (Short): 4326 = GCS_WGS_84
GeogAngularUnitsGeoKey (Short): 9102 = Angular_Degree
tiepoint: pixel 0, 0 is at -10, 60; pixel scale: 0.625 x 0.78125

Expanda um código EPSG em uma definição completa

GTIFGetDefn normaliza as GeoKeys pelo banco de dados EPSG do PROJ: só a partir de ProjectedCSTypeGeoKey, ele recupera o método e os parâmetros da projeção, o datum, o elipsoide e a unidade. GTIFGetProj4Defn escreve a definição como uma string PROJ, com o fator de escala arredondado para seis casas decimais.

src/native/crs_definition.h
#pragma once
 
#include <geo_normalize.h>
#include <geotiff.h>
#include <geovalues.h>
#include <tiffio.hxx>
#include <xtiffio.h>
 
#include <cstdio>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
 
// A file often carries a single EPSG code. GTIFGetDefn expands it through PROJ's database into the
// whole definition: projection method and parameters, datum, ellipsoid, prime meridian and unit.
class CrsDefinition {
public:
// JSON: the normalised definition, with EPSG names, and the PROJ string libgeotiff builds from it.
static std::string describe(const std::u16string& tiff) {
std::istringstream in(std::string(tiff.begin(), tiff.end()));
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("input.tif", &in), XTIFFClose);
if (!tif) throw std::runtime_error("not a TIFF file");
Keys gtif(GTIFNew(tif.get()), GTIFFree);
GTIFDefn defn;
if (!gtif || !GTIFGetDefn(gtif.get(), &defn)) throw std::runtime_error("no coordinate system in the GeoKeys");
 
std::string json = "{\"model\":" + quoted(GTIFValueNameEx(gtif.get(), GTModelTypeGeoKey, defn.Model));
if (defn.Model == ModelTypeProjected) {
json += ",\"pcs\":" + code(defn.PCS, pcsName(defn.PCS));
json += ",\"projection\":" + code(defn.ProjCode, projectionName(defn.ProjCode));
json += ",\"method\":" + quoted(GTIFValueNameEx(gtif.get(), ProjCoordTransGeoKey, defn.CTProjection));
json += ",\"parameters\":[";
for (int i = 0; i < defn.nParms; i += 1) {
if (defn.ProjParmId[i] == 0) continue; // a slot the method does not use
if (json.back() != '[') json += ',';
json += "[" + quoted(GTIFKeyName(static_cast<geokey_t>(defn.ProjParmId[i]))) + "," + number(defn.ProjParm[i]) + "]";
}
json += "]";
}
char* name = nullptr;
GTIFGetGCSInfo(defn.GCS, &name, nullptr, nullptr, nullptr);
json += ",\"gcs\":" + code(defn.GCS, taken(name));
GTIFGetDatumInfo(defn.Datum, &name, nullptr);
json += ",\"datum\":" + code(defn.Datum, taken(name));
GTIFGetEllipsoidInfo(defn.Ellipsoid, &name, nullptr, nullptr);
json += ",\"ellipsoid\":" + code(defn.Ellipsoid, taken(name)) + ",\"axes\":[" + number(defn.SemiMajor) + "," + number(defn.SemiMinor) + "]";
GTIFGetPMInfo(defn.PM, &name, nullptr);
json += ",\"primeMeridian\":" + code(defn.PM, taken(name));
GTIFGetUOMLengthInfo(defn.UOMLength, &name, nullptr);
json += ",\"unit\":" + code(defn.UOMLength, taken(name)) + ",\"metres\":" + number(defn.UOMLengthInMeters);
char* proj = GTIFGetProj4Defn(&defn);
json += ",\"proj\":" + quoted(taken(proj)) + "}";
return json;
}
 
private:
using Tiff = std::unique_ptr<TIFF, void (*)(TIFF*)>;
using Keys = std::unique_ptr<GTIF, void (*)(GTIF*)>;
 
static std::string pcsName(int pcs) {
char* name = nullptr;
GTIFGetPCSInfo(pcs, &name, nullptr, nullptr, nullptr);
return taken(name);
}
 
static std::string projectionName(int projection) {
char* name = nullptr;
GTIFGetProjTRFInfo(projection, &name, nullptr, nullptr);
return taken(name);
}
 
// The GTIFGet...Info functions hand over strings the caller frees.
static std::string taken(char*& text) {
const std::string value = text ? text : "";
GTIFFreeMemory(text);
text = nullptr;
return value;
}
 
static std::string code(int value, const std::string& name) { return "[" + std::to_string(value) + "," + quoted(name) + "]"; }
 
static std::string number(double value) {
char text[32];
std::snprintf(text, sizeof text, "%.17g", value);
return text;
}
 
static std::string quoted(const std::string& text) {
std::string out = "\"";
for (const char c : text) {
if (c == '"' || c == '\\') out += '\\';
out += static_cast<unsigned char>(c) < 0x20 ? ' ' : c;
}
return out + "\"";
}
};
main.js
import { initNative, CrsDefinition } from './native/crs_definition.h';
import { GeoTiffLocator } from './native/geotiff_locator.h';
 
await initNative();
// A file that only says ProjectedCSTypeGeoKey = 27700, written by the first example's class
const tiff = await GeoTiffLocator.write(27700, 100, 100, 529000, 181000, 10);
const crs = JSON.parse(await CrsDefinition.describe(tiff));
console.log(`${crs.model} EPSG:${crs.pcs[0]} ${crs.pcs[1]}`);
console.log(`projection ${crs.projection[0]} ${crs.projection[1]}, ${crs.method}`);
for (const [name, value] of crs.parameters) console.log(` ${name} ${value}`);
console.log(`geographic ${crs.gcs.join(' ')}, datum ${crs.datum.join(' ')}`);
console.log(`ellipsoid ${crs.ellipsoid.join(' ')}: ${crs.axes.map((axis) => axis.toFixed(3)).join(' m, ')} m`);
console.log(`prime meridian ${crs.primeMeridian.join(' ')}, unit ${crs.unit.join(' ')} (${crs.metres} m)`);
console.log(crs.proj.trim());
IMPRIMEa primeira execução baixa 16,1 MB
ModelTypeProjected EPSG:27700 OSGB36 / British National Grid
projection 19916 British National Grid, CT_TransverseMercator
  ProjNatOriginLatGeoKey 49
  ProjNatOriginLongGeoKey -2
  ProjScaleAtNatOriginGeoKey 0.9996012717
  ProjFalseEastingGeoKey 400000
  ProjFalseNorthingGeoKey -100000
geographic 4277 OSGB36, datum 6277 Ordnance Survey of Great Britain 1936
ellipsoid 7001 Airy 1830: 6377563.396 m, 6356256.909 m
prime meridian 8901 Greenwich, unit 9001 metre (1 m)
+proj=tmerc +lat_0=49.000000000 +lon_0=-2.000000000 +k=0.999601 +x_0=400000.000 +y_0=-100000.000 +a=6377563.396 +b=6356256.909 +units=m

Converta entre pixels e longitude, latitude

GTIFProj4FromLatLong e GTIFPCSToImage encontram o pixel sob uma longitude e uma latitude; GTIFImageToPCS e GTIFProj4ToLatLong fazem o caminho de volta a partir de um pixel. A classe mantém o arquivo aberto entre as perguntas.

src/native/pixel_position.h
#pragma once
 
#include <geo_normalize.h>
#include <geotiff.h>
#include <geovalues.h>
#include <tiffio.hxx>
#include <xtiffio.h>
 
#include <cstdio>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
 
// Opens a GeoTIFF once and converts positions both ways: pixel to map coordinates to longitude and
// latitude, and back. Pixel (0, 0) is the upper-left corner of the first pixel, so the centre of
// pixel (column, row) is (column + 0.5, row + 0.5).
class PixelPosition {
public:
explicit PixelPosition(const std::u16string& tiff) : in(std::string(tiff.begin(), tiff.end())), tif(open(in)), keys(GTIFNew(tif.get()), GTIFFree) {
if (!keys || !GTIFGetDefn(keys.get(), &defn)) throw std::runtime_error("no coordinate system in the GeoKeys");
}
 
// JSON {"map": [x, y], "lonLat": [lon, lat]} for a position in pixels.
std::string lonLat(double column, double row) {
double x = column;
double y = row;
if (!GTIFImageToPCS(keys.get(), &x, &y)) throw std::runtime_error("no tiepoint and pixel scale to place the pixels");
double lon = x;
double lat = y;
if (defn.Model == ModelTypeProjected && !GTIFProj4ToLatLong(&defn, 1, &lon, &lat)) throw std::runtime_error("PROJ could not unproject the point");
return json("lonLat", x, y, lon, lat);
}
 
// JSON {"map": [x, y], "pixel": [column, row]} for a longitude and latitude.
std::string pixel(double lon, double lat) {
double x = lon;
double y = lat;
if (defn.Model == ModelTypeProjected && !GTIFProj4FromLatLong(&defn, 1, &x, &y)) throw std::runtime_error("PROJ could not project the point");
double column = x;
double row = y;
if (!GTIFPCSToImage(keys.get(), &column, &row)) throw std::runtime_error("no tiepoint and pixel scale to place the pixels");
return json("pixel", x, y, column, row);
}
 
private:
using Tiff = std::unique_ptr<TIFF, void (*)(TIFF*)>;
using Keys = std::unique_ptr<GTIF, void (*)(GTIF*)>;
 
static Tiff open(std::istringstream& in) {
XTIFFInitialize();
Tiff tif(TIFFStreamOpen("input.tif", &in), XTIFFClose);
if (!tif) throw std::runtime_error("not a TIFF file");
return tif;
}
 
static std::string json(const char* name, double x, double y, double a, double b) {
char text[160];
std::snprintf(text, sizeof text, "{\"map\":[%.17g,%.17g],\"%s\":[%.17g,%.17g]}", x, y, name, a, b);
return text;
}
 
std::istringstream in; // libtiff reads from it for as long as the file is open
Tiff tif;
Keys keys;
GTIFDefn defn;
};
main.js
import { initNative, PixelPosition } from './native/pixel_position.h';
import { GeoTiffLocator } from './native/geotiff_locator.h';
 
await initNative();
// 300 x 300 pixels of 100 m over Istanbul in WGS 84 / UTM zone 35N, written by the first example's class
const tiff = await GeoTiffLocator.write(32635, 300, 300, 650000, 4560000, 100);
const position = await new PixelPosition(tiff);
 
const tower = JSON.parse(await position.pixel(28.974167, 41.025833)); // Galata Tower
console.log(`Galata Tower: ${tower.map.map((v) => v.toFixed(2)).join(', ')} m, pixel ${tower.pixel.map((v) => v.toFixed(3)).join(', ')}`);
const [column, row] = tower.pixel.map(Math.floor);
const centre = JSON.parse(await position.lonLat(column + 0.5, row + 0.5));
console.log(`centre of pixel ${column}, ${row}: ${centre.map.join(', ')} m, ${centre.lonLat.map((v) => v.toFixed(6)).join(', ')}`);
IMPRIMEa primeira execução baixa 16,1 MB
Galata Tower: 665970.23, 4543502.03 m, pixel 159.702, 164.980
centre of pixel 159, 164: 665950, 4543550 m, 28.973939, 41.026269

Uma ferramenta de linha de comando do GeoTIFF

O WASI não tem bindings JavaScript: o programa é um main() que vincula a libgeotiff, a libtiff e o PROJ, compilado em um único .wasm e executado com o wasmtime. Ele escreve o arquivo do primeiro exemplo de uso, byte a byte, e depois diz onde ele fica. GTIFGetDefn busca os códigos EPSG no proj.db do PROJ, que a compilação copia para dist/data; o último comando monta essa pasta e define PROJ_DATA.

crossbind.config.js
import geotiffWasi from '@crossbind/port-geotiff-wasi/crossbind.config.js';
 
export default {
general: { name: 'geotiff-tool' },
dependencies: [geotiffWasi],
// PROJ's data folder is copied from the build directory to dist/data, so the two must differ.
paths: { config: import.meta.url, output: 'dist' },
};
src/native/main.cpp
// geotiff-tool: writes a small GeoTIFF, and says where any GeoTIFF is: its size, its coordinate
// system and its corners in map units and in degrees. GTIFGetDefn reads PROJ's proj.db, so the
// commands mount the data folder the build puts next to the .wasm and point PROJ_DATA at it.
#include <geo_normalize.h>
#include <geotiff.h>
#include <geovalues.h>
#include <xtiffio.h>
 
#include <sys/stat.h>
 
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string>
#include <vector>
 
using Tiff = std::unique_ptr<TIFF, void (*)(TIFF*)>;
using Keys = std::unique_ptr<GTIF, void (*)(GTIF*)>;
 
static std::string taken(char* text) {
const std::string value = text ? text : "unknown";
GTIFFreeMemory(text);
return value;
}
 
// A blank 100 x 100 8-bit image in the projected system `epsg`, upper-left corner at (x, y).
static int write(const char* path, int epsg, double x, double y, double pixel) {
Tiff tif(XTIFFOpen(path, "w"), XTIFFClose);
if (!tif) return std::fprintf(stderr, "cannot create %s\n", path), 1;
const int side = 100;
TIFFSetField(tif.get(), TIFFTAG_IMAGEWIDTH, side);
TIFFSetField(tif.get(), TIFFTAG_IMAGELENGTH, side);
TIFFSetField(tif.get(), TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif.get(), TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif.get(), TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif.get(), TIFFTAG_ROWSPERSTRIP, side);
const double tiepoint[6] = {0, 0, 0, x, y, 0};
const double scale[3] = {pixel, pixel, 0};
TIFFSetField(tif.get(), TIFFTAG_GEOTIEPOINTS, 6, tiepoint);
TIFFSetField(tif.get(), TIFFTAG_GEOPIXELSCALE, 3, scale);
Keys keys(GTIFNew(tif.get()), GTIFFree);
GTIFKeySet(keys.get(), GTModelTypeGeoKey, TYPE_SHORT, 1, ModelTypeProjected);
GTIFKeySet(keys.get(), GTRasterTypeGeoKey, TYPE_SHORT, 1, RasterPixelIsArea);
GTIFKeySet(keys.get(), ProjectedCSTypeGeoKey, TYPE_SHORT, 1, epsg);
GTIFWriteKeys(keys.get());
keys.reset();
std::vector<unsigned char> row(side, 0);
for (int line = 0; line < side; line += 1) {
if (TIFFWriteScanline(tif.get(), row.data(), line, 0) < 0) return std::fprintf(stderr, "cannot write %s\n", path), 1;
}
tif.reset();
struct stat info;
stat(path, &info);
std::printf("libgeotiff %s write: %s, %d x %d pixels in EPSG:%d, %lld B\n", LIBGEOTIFF_STRING_VERSION, path, side, side, epsg, static_cast<long long>(info.st_size));
return 0;
}
 
static int describe(const char* path) {
Tiff tif(XTIFFOpen(path, "r"), XTIFFClose);
if (!tif) return std::fprintf(stderr, "%s is not a TIFF file\n", path), 1;
Keys keys(GTIFNew(tif.get()), GTIFFree);
GTIFDefn defn;
if (!keys || !GTIFGetDefn(keys.get(), &defn)) return std::fprintf(stderr, "%s has no GeoTIFF coordinate system\n", path), 1;
uint32_t width = 0;
uint32_t height = 0;
uint16_t bits = 8;
uint16_t bands = 1;
TIFFGetField(tif.get(), TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif.get(), TIFFTAG_IMAGELENGTH, &height);
TIFFGetFieldDefaulted(tif.get(), TIFFTAG_BITSPERSAMPLE, &bits);
TIFFGetFieldDefaulted(tif.get(), TIFFTAG_SAMPLESPERPIXEL, &bands);
std::printf("%s: %u x %u pixels, %u band%s, %u bits per sample\n", path, width, height, bands, bands == 1 ? "" : "s", bits);
 
const bool projected = defn.Model == ModelTypeProjected;
char* name = nullptr;
if (projected) GTIFGetPCSInfo(defn.PCS, &name, nullptr, nullptr, nullptr);
else GTIFGetGCSInfo(defn.GCS, &name, nullptr, nullptr, nullptr);
const std::string crs = taken(name);
GTIFGetDatumInfo(defn.Datum, &name, nullptr);
const std::string datum = taken(name);
std::printf("EPSG:%d %s, datum %s\n", projected ? defn.PCS : defn.GCS, crs.c_str(), datum.c_str());
 
const char* labels[2] = {"upper left", "lower right"};
for (int corner = 0; corner < 2; corner += 1) {
double x = corner ? width : 0;
double y = corner ? height : 0;
if (!GTIFImageToPCS(keys.get(), &x, &y)) return std::fprintf(stderr, "%s has no tiepoint and pixel scale\n", path), 1;
double lon = x;
double lat = y;
if (projected && !GTIFProj4ToLatLong(&defn, 1, &lon, &lat)) return std::fprintf(stderr, "PROJ could not unproject %s\n", labels[corner]), 1;
std::printf("%s %.3f %.3f = lon %.6f, lat %.6f\n", labels[corner], x, y, lon, lat);
}
return 0;
}
 
int main(int argc, char** argv) {
const std::string command = argc > 1 ? argv[1] : "";
if (command == "write" && argc == 7) return write(argv[2], std::atoi(argv[3]), std::atof(argv[4]), std::atof(argv[5]), std::atof(argv[6]));
if (command == "info" && argc == 3) return describe(argv[2]);
std::fprintf(stderr, "usage: geotiff-tool write <file.tif> <epsg> <x> <y> <pixel size>\n geotiff-tool info <file.tif>\n");
return 2;
}
shell
npx crossbind build -p wasi -e wasi -b release
wasmtime run --dir=. dist/geotiff-tool-wasi-wasm32-st-release.wasm write utm33.tif 32633 500000 4650000 30
wasmtime run --dir=. --dir=dist/data::/data --env PROJ_DATA=/data/proj dist/geotiff-tool-wasi-wasm32-st-release.wasm info utm33.tif
output
libgeotiff 1.7.4 write: utm33.tif, 100 x 100 pixels in EPSG:32633, 10262 B
utm33.tif: 100 x 100 pixels, 1 band, 8 bits per sample
EPSG:32633 WGS 84 / UTM zone 33N, datum World Geodetic System 1984
upper left 500000.000 4650000.000 = lon 15.000000, lat 42.002015
lower right 503000.000 4647000.000 = lon 15.036210, lat 41.974990
Digite para pesquisar em todas as páginas e seções do guia.
↑↓ navegar↵ abriresc fechar