crossbind
GitHub
← Toutes les bibliothèques

GeoTIFF

Métadonnées GeoTIFF et systèmes de coordonnées.

Installation

Fonctionne dans les navigateurs ; Node.js et les environnements edge utilisent le même paquet. Compilé pour wasm32, monothread et 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();

crossbind lui-même est fourni avec votre plugin de bundler ; la page Plugins de bundler couvre Vite, Webpack, Rspack, Rsbuild, Rollup, esbuild, Bun et Next.js.

libgeotiff dans votre navigateur : où se trouve un GeoTIFF, et des GeoTIFF que GDAL sait placer

libgeotiff est la bibliothèque GeoTIFF sur laquelle repose le pilote GTiff de GDAL. Ces apps exécutent libgeotiff 1.7.4 avec libtiff et PROJ, compilés par crossbind : elles nomment le système de coordonnées de n’importe quel GeoTIFF et en dessinent l’emprise sur une carte, transforment une image et un rectangle englobant en un GeoTIFF compressé que GDAL place là où vous l’avez indiqué, et lisent altitudes et degrés sur un modèle d’élévation. Les données de PROJ forment l’essentiel du téléchargement de la première exécution : 10,2 MB, surtout la base de données EPSG proj.db, que libgeotiff lit pour nommer les systèmes de coordonnées. La première exécution télécharge 16,1 MB de WebAssembly, une seule fois ; toutes les apps de cette page le partagent, et rien n’est envoyé.

APP 01

Où se trouve ce GeoTIFF ?

Déposez une scène satellite, une tuile d’altitude ou une carte de drone et voyez son système de coordonnées par son nom, ses coins en degrés et son emprise sur la carte, avec le rapport complet de listgeo. geotiff.js, le lecteur GeoTIFF JavaScript le plus utilisé, renvoie les GeoKeys brutes ; libgeotiff nomme le système qu’elles décrivent grâce à la base EPSG de PROJ.

EXEMPLES

Les exemples sont écrits par le module : pixels vides, placements réels. Votre fichier reste dans cet onglet : il est monté dans le système de fichiers en mémoire du module, et libtiff lit ses répertoires et ses tags, pas ses pixels. Les degrés sont exprimés dans le datum du fichier, OSGB36 pour l’exemple de Londres. Pour dessiner des pixels GeoTIFF sur une carte web, geotiff.js est le choix le plus léger.

Ouvrez un exemple ou votre propre GeoTIFF pour voir où il se trouve.
VOIR LE CODE
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

Géoréférencer n’importe quelle image

Donnez à une carte scannée, une capture d’écran ou un rendu sa place sur la Terre : un rectangle en longitude et latitude, ou un coin et une taille de pixel dans n’importe quel système EPSG projeté. Le module l’écrit en GeoTIFF dans cinq compressions, que GDAL place là où vous l’avez indiqué, et le relit avec libgeotiff pour l’afficher. Le writer de geotiff.js stocke les pixels sans compression.

IMAGE

Écrit en EPSG:4326, longitude et latitude WGS 84. Le rectangle est le bord extérieur de l’image : GTRasterTypeGeoKey indique que chaque pixel couvre une surface.

Choisissez une image, ou gardez l’exemple, puis écrivez les GeoTIFF.
VOIR LE CODE
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

Lire altitudes et degrés sur un modèle d’élévation

Ouvrez un GeoTIFF d’altitude et survolez son relief : libgeotiff convertit la position sous le curseur en longitude et latitude, à côté de l’altitude qui y est stockée. Enregistrez ensuite à nouveau le modèle, sans perte ou avec LERC dans la marge d’erreur de votre choix, et comparez les tailles. Un fichier optimisé pour le cloud est lu depuis le plus petit aperçu (overview) qui remplit encore la vue.

L’exemple est écrit par le module : des altitudes générées sur 512 × 512 cellules de 30 m, placées dans la zone UTM 33N. Votre fichier reste dans cet onglet. La bande 1 est lue, en entiers ou en flottants, et la valeur NoData de GDAL est écartée. Un fichier de plus de 512 pixels de côté est échantillonné pour tenir, au pixel le plus proche, et les altitudes affichées sont ces échantillons.

Ouvrez l’exemple, ou votre propre GeoTIFF d’altitude, puis survolez-le.
VOIR LE CODE
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

Exemples

Chacun se compose d’un petit fichier d’en-tête C++ et du JavaScript qui l’appelle, avec ce qu’il a affiché lors du build du site. L’onglet JavaScript uniquement fait de même avec les fichiers d’en-tête fournis par la bibliothèque GeoTIFF, sans fichier C++. Le dernier est un programme C++ complet, compilé pour WASI.

Trouver où se situe un GeoTIFF

La question à laquelle répond la plupart du code GeoTIFF : GTIFNew lit les GeoKeys, GTIFGetDefn les transforme en système de coordonnées, GTIFImageToPCS place les pixels sur la carte et GTIFProj4ToLatLong transforme les coordonnées de la carte en degrés.

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)}`);
AFFICHEla première exécution télécharge 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

Écrire un GeoTIFF compressé

Le géoréférencement tient en deux tags TIFF et quelques GeoKeys : TIFFTAG_GEOTIEPOINTS et TIFFTAG_GEOPIXELSCALE placent les pixels, GTIFKeySet et GTIFWriteKeys nomment le système de coordonnées. GTIFPrint affiche ensuite le résultat comme le fait listgeo.

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());
AFFICHEla première exécution télécharge 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.

Lire les GeoKeys, le point de calage et l’échelle de pixel

GTIFDirectoryInfo compte les clés, GTIFKeyInfo et GTIFKeyGet lisent chacune avec son type, et GTIFValueNameEx nomme sa valeur. Le point de calage et l’échelle de pixel sont des tags TIFF, lus avec 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]}`);
AFFICHEla première exécution télécharge 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

Développer un code EPSG en une définition complète

GTIFGetDefn normalise les GeoKeys à l’aide de la base EPSG de PROJ : à partir de la seule clé ProjectedCSTypeGeoKey, il retrouve la méthode et les paramètres de projection, le datum, l’ellipsoïde et l’unité. GTIFGetProj4Defn l’écrit sous forme de chaîne PROJ, avec le facteur d’échelle arrondi à six décimales.

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());
AFFICHEla première exécution télécharge 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

Convertir entre pixels et longitude, latitude

GTIFProj4FromLatLong et GTIFPCSToImage trouvent le pixel situé sous une longitude et une latitude ; GTIFImageToPCS et GTIFProj4ToLatLong font le chemin inverse depuis un pixel. La classe garde le fichier ouvert entre les questions.

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(', ')}`);
AFFICHEla première exécution télécharge 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

Un outil GeoTIFF en ligne de commande

WASI n’a pas de bindings JavaScript : le programme est un main() lié à libgeotiff, libtiff et PROJ, compilé en un seul .wasm et exécuté avec wasmtime. Il écrit le fichier du premier exemple d’utilisation, octet pour octet, puis indique où se situe ce GeoTIFF. GTIFGetDefn recherche les codes EPSG dans le proj.db de PROJ, que le build copie dans dist/data ; la dernière commande monte ce dossier et définit 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
Tapez pour chercher dans toutes les pages et sections du guide.
↑↓ naviguer↵ ouvriresc fermer