Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/korp-map

crates/korp-map-wire/src/landing.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>korp-map server</title>
<style>
:root {
  --bg: #fbfbf9; --fg: #1d1d1b; --muted: #5d5d58; --line: #d9d8d2;
  --code-bg: #f0efe9; --accent: #1f5f8b; --ok: #2f6f3a; --bad: #9b2c2c;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161715; --fg: #e6e5df; --muted: #a3a29b; --line: #34352f;
    --code-bg: #20211e; --accent: #7fb4dc; --ok: #8fc79a; --bad: #e59a9a;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main { max-width: 900px; margin: 0 auto; padding: 24px 16px 64px; }
h1 { font-size: 1.7rem; margin: 0 0 4px; }
h2 { font-size: 1.2rem; margin: 36px 0 8px; padding-top: 8px; border-top: 1px solid var(--line); }
h3 { font-size: 1rem; margin: 22px 0 6px; }
p, li { max-width: 72ch; }
.muted { color: var(--muted); }
a { color: var(--accent); }
code, pre { font: 13.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
code { background: var(--code-bg); padding: 1px 4px; border-radius: 4px; }
pre { background: var(--code-bg); padding: 10px 12px; border-radius: 6px; overflow-x: auto; margin: 6px 0; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-weight: 600; }
td:first-child { white-space: nowrap; }
.wrap { overflow-x: auto; }
button {
  font: inherit; font-size: 14px; color: var(--fg); background: var(--code-bg);
  border: 1px solid var(--line); border-radius: 5px; padding: 3px 10px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
.out { white-space: pre-wrap; max-height: 260px; overflow: auto; }
.ok { color: var(--ok); } .bad { color: var(--bad); }
</style>
</head>
<body>
<main>
<h1>korp-map server</h1>
<p class="muted">{{ABOUT}}</p>
<p>This server answers map queries on OpenStreetMap region cuts. Map rows come back as an
<a href="https://arrow.apache.org/docs/format/Columnar.html#ipc-streaming-format">Arrow IPC stream</a>
of GeoParquet 1.1 native rows (<code>id</code>, <code>tags</code>, <code>min_zoom</code>,
<code>geometry</code>); address lookups come back as JSON. The same map queries are served over
Arrow Flight on the same port. The full description is in <a href="/openapi.json">/openapi.json</a>
(OpenAPI 3.1). The public server, for Sweden, is
<a href="https://karta.gunnar.rs/v1/">karta.gunnar.rs</a>.</p>
<p>Map data © OpenStreetMap contributors, under the
<a href="https://opendatacommons.org/licenses/odbl/1-0/">ODbL 1.0</a>.</p>

<h2>Regions served</h2>
<div class="wrap"><table id="regions"><tr><td class="muted">Loading <code>/v1/regions</code> …</td></tr></table></div>

<h2>Endpoints</h2>
<p>Examples use <code class="origin">this server</code> and the first region above. Each
<b>Try</b> button runs the request from this page.</p>

<h3><code>GET /v1/health</code></h3>
<p><code>ok</code> when the server is up.</p>
<pre data-ex="health"></pre>
<button data-try="health">Try</button><pre class="out" data-out="health" hidden></pre>

<h3><code>GET /v1/regions</code></h3>
<p>The regions served: name, bounding box <code>[minlon, minlat, maxlon, maxlat]</code> and
<code>data_version</code> (which cut is served).</p>
<pre data-ex="regions"></pre>
<button data-try="regions">Try</button><pre class="out" data-out="regions" hidden></pre>

<h3><code>GET /v1/map/{kind}</code></h3>
<p><code>kind</code> is <code>points</code> (tagged nodes), <code>lines</code> or <code>areas</code>.
Parameters: <code>region</code>, <code>bbox=minlon,minlat,maxlon,maxlat</code>,
<code>lod</code> (0 to 30, a web-map zoom: the coarsest stored geometry exact enough for it, and
only rows shown at that zoom), optional <code>max</code> (a row cap; the lowest
<code>min_zoom</code> comes first) and <code>compress=zstd</code> (Arrow IPC buffer compression).
The reply's schema metadata says whether it was <code>truncated</code> and which
<code>data_version</code> it came from.</p>
<pre data-ex="map"></pre>
<button data-try="map">Try</button><pre class="out" data-out="map" hidden></pre>

<h3><code>GET /v1/geocode</code></h3>
<p>An address text to ranked addresses, best first. <code>max</code> is 1 to 1000, default 10.</p>
<pre data-ex="geocode"></pre>
<button data-try="geocode">Try</button><pre class="out" data-out="geocode" hidden></pre>

<h3><code>GET /v1/revgeocode</code></h3>
<p>A point to the nearest address and its distance in metres.</p>
<pre data-ex="revgeocode"></pre>
<button data-try="revgeocode">Try</button><pre class="out" data-out="revgeocode" hidden></pre>
{{FILES_SECTION}}
<h2>Python</h2>
<p>The <code>korp-map-client</code> package reads the map as pyarrow tables or GeoDataFrames.</p>
<pre data-ex="python"></pre>

<h2>Compression and caching</h2>
<ul>
<li>Arrow and JSON replies of 1400 bytes or more are sent <code>zstd</code>, <code>br</code> or
<code>gzip</code> encoded when the request's <code>Accept-Encoding</code> allows it (browsers do
this by themselves; with curl, add <code>--compressed</code>).</li>
<li>Replies carry a strong <code>ETag</code> (the server build, the region's
<code>data_version</code> and the query) and <code>Cache-Control: public, max-age=…</code>. A request with
<code>If-None-Match</code> naming the reply gets <code>304 Not Modified</code>.</li>
<li>CORS: {{CORS}}.</li>
</ul>

<h2>Errors</h2>
<p>A refusal is JSON, <code>{"error": "…", "code": "korp-map-&lt;n&gt;"}</code>, with this HTTP
status. The Flight door sends the same JSON as the gRPC status message.</p>
<div class="wrap"><table>
<tr><th>code</th><th>status</th><th>meaning</th></tr>
{{CODES}}
</table></div>
</main>
<script>
(() => {
  const origin = location.origin;
  const $ = (s) => document.querySelectorAll(s);
  $(".origin").forEach((e) => (e.textContent = origin));
  let region = "sweden", bbox = [17.9, 59.2, 18.2, 59.4], address = "Drottninggatan 53 Stockholm",
      point = [18.07, 59.33];
  const enc = encodeURIComponent;
  const paths = () => ({
    health: "/v1/health",
    regions: "/v1/regions",
    map: `/v1/map/lines?region=${enc(region)}&bbox=${bbox.join(",")}&lod=14`,
    geocode: `/v1/geocode?region=${enc(region)}&q=${enc(address)}&max=5`,
    revgeocode: `/v1/revgeocode?region=${enc(region)}&lon=${point[0]}&lat=${point[1]}`,
  });
  const fmt = (n) => +n.toFixed(4);
  function examples() {
    const p = paths();
    for (const k of Object.keys(p)) {
      const el = document.querySelector(`[data-ex="${k}"]`);
      if (!el) continue;
      el.textContent = k === "map"
        ? `curl --compressed -o lines.arrow '${origin}${p[k]}'`
        : `curl --compressed '${origin}${p[k]}'`;
    }
    const b = `(${bbox.join(", ")})`;
    document.querySelector('[data-ex="python"]').textContent =
`pip install "korp-map-client[geo]"

import korp_map_client
c = korp_map_client.Client("${origin}")
res = c.map("lines", bbox=${b}, lod=14)
res.table                 # a pyarrow.Table
res.truncated, res.data_version
gdf = c.map_gdf("areas", bbox=${b}, lod=12)   # a GeoDataFrame
c.geocode("${address}", max=5)
c.revgeocode(${point[0]}, ${point[1]})`;
    for (const el of $("[data-ex-files]")) {
      el.textContent = el.dataset.exFiles.replaceAll("ORIGIN", origin).replaceAll("REGION", region)
        .replaceAll("MINLON", bbox[0]).replaceAll("MINLAT", bbox[1])
        .replaceAll("MAXLON", bbox[2]).replaceAll("MAXLAT", bbox[3]);
    }
  }
  function cell(t) { const td = document.createElement("td"); td.textContent = t; return td; }
  async function regions() {
    const table = document.getElementById("regions");
    try {
      const r = await fetch("/v1/regions");
      const list = await r.json();
      table.replaceChildren();
      const head = document.createElement("tr");
      for (const h of ["name", "bbox", "data_version"]) {
        const th = document.createElement("th"); th.textContent = h; head.append(th);
      }
      table.append(head);
      for (const g of list) {
        const tr = document.createElement("tr");
        tr.append(cell(g.name), cell(g.bbox.map(fmt).join(", ")), cell(g.data_version));
        table.append(tr);
      }
      if (list.length === 0) {
        const tr = document.createElement("tr"); tr.append(cell("No region is served yet.")); table.append(tr);
      } else if (!list.some((g) => g.name === "sweden")) {
        const g = list[0];
        region = g.name;
        const cx = (g.bbox[0] + g.bbox[2]) / 2, cy = (g.bbox[1] + g.bbox[3]) / 2;
        bbox = [fmt(cx - 0.15), fmt(cy - 0.1), fmt(cx + 0.15), fmt(cy + 0.1)];
        point = [fmt(cx), fmt(cy)];
        address = "Storgatan 1";
      }
    } catch (e) {
      table.replaceChildren();
      const tr = document.createElement("tr"); tr.append(cell(`/v1/regions did not answer: ${e}`)); table.append(tr);
    }
    examples();
  }
  for (const b of $("[data-try]")) {
    b.addEventListener("click", async () => {
      const k = b.dataset.try, out = document.querySelector(`[data-out="${k}"]`);
      out.hidden = false; out.className = "out"; out.textContent = "…";
      const t0 = performance.now();
      try {
        const r = await fetch(paths()[k]);
        const ms = (performance.now() - t0).toFixed(0);
        const head = `HTTP ${r.status} in ${ms} ms, ${r.headers.get("content-type") || ""}` +
          (r.headers.get("etag") ? `, ETag ${r.headers.get("etag")}` : "");
        if ((r.headers.get("content-type") || "").includes("arrow")) {
          const n = (await r.arrayBuffer()).byteLength;
          out.textContent = `${head}\n${n.toLocaleString()} bytes of Arrow IPC (decoded size; open it with pyarrow.ipc.open_stream)`;
        } else {
          const text = await r.text();
          let body = text;
          try { body = JSON.stringify(JSON.parse(text), null, 2); } catch (_) {}
          out.textContent = `${head}\n${body.length > 6000 ? body.slice(0, 6000) + "\n…" : body}`;
        }
        out.classList.add(r.ok ? "ok" : "bad");
      } catch (e) {
        out.textContent = `no answer: ${e}`; out.classList.add("bad");
      }
    });
  }
  examples();
  regions();
})();
</script>
</body>
</html>