Skip to content

Tutorials

Kohei Otsuka edited this page Jul 24, 2026 · 1 revision

Tutorials

Step-by-step guides for using MaplatTin in Node.js and the browser.

Note: For the latest quick start (install commands, CDN URLs, version numbers), see the README. This page covers release-independent tutorial material.

Table of Contents


1. Node.js usage

import Tin from '@maplat/tin';

const tin = new Tin({
  wh: [500, 500],
  yaxisMode: Tin.YAXIS_FOLLOW,
});

tin.setPoints([
  [[100, 100], [200, 200]],
  [[200, 200], [400, 400]],
  [[150, 150], [320, 350]],
  [[200, 100], [420, 220]],
]);

tin.updateTin();

if (tin.strict_status === Tin.STATUS_STRICT) {
  console.log('Topology OK: roundtrip transform is guaranteed');
} else if (tin.strict_status === Tin.STATUS_LOOSE) {
  console.log('Topology warning: roundtrip transform is not guaranteed');
}

// Forward transform
const ord = tin.transform([160, 160], false);
console.log(ord);

// Backward transform
const rev = tin.transform(ord, true);
console.log(rev);

Note on method name: The historical wiki documented updateTinAsync() returning a Promise. The current API exposes updateTin() (synchronous after the internal computation completes). See docs/api/ for the authoritative signature.

2. Browser usage

UMD

<script src="https://cdn.jsdelivr.net/npm/@maplat/tin@0.14.2/dist/maplat_tin.umd.js"></script>
<script>
  const tin = new maplatTin.default({ wh: [500, 500] });
  tin.setPoints([
    [[100, 100], [200, 200]],
    [[200, 200], [400, 400]],
    [[150, 150], [320, 350]],
    [[200, 100], [420, 220]],
  ]);
  tin.updateTin();
  console.log(tin.transform([160, 160], false));
</script>

ES Modules

<script type="module">
  import Tin from 'https://cdn.jsdelivr.net/npm/@maplat/tin@0.14.2/dist/maplat_tin.js';
  const tin = new Tin({ wh: [500, 500] });
  // ...
</script>

The legacy script maplat_tin.js (note the underscore) referenced in the old wiki is the same UMD bundle; the current package name is @maplat/tin (scoped). Use the scoped name in all new code.

3. Saving and restoring the solved TIN

Solving the TIN network from many control points is expensive. MaplatTin supports object serialization so the solved state can be reused.

// After updateTin()
const compiled = tin.getCompiled();

// Later, in another process
const tin2 = new Tin();
tin2.setCompiled(compiled);
// No updateTin() call is needed — the triangulation is already loaded.

console.log(tin2.transform([160, 160], false));

4. Error handling

try {
  tin.updateTin();
} catch (e) {
  if (e === 'TOO LINEAR1' || e === 'TOO LINEAR2') {
    console.log('Given GCP points are too linear; cannot build TIN.');
  } else if (e === 'SOME POINTS OUTSIDE') {
    console.log('Some points are outside the boundary.');
  } else {
    throw e;
  }
}

For the complete list of error cases, see docs/api/ Error handling section.


日本語版はこちら / Read this page in Japanese

See Also

MaplatTin

Language / 言語

Pages / ページ

English

日本語

External / 外部

Clone this wiki locally