Skip to content

Commit

Permalink
Merge pull request #111 from Kanahiro/feat/cog
Browse files Browse the repository at this point in the history
feat: support `cog://` protocol
  • Loading branch information
Kanahiro authored Sep 16, 2024
2 parents f02d24f + 8d6a005 commit 84d400a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules/
dist/
.cache/
coverage/
coverage/
package-lock.json
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ node dist/main.js tile-server -c s3 -s3b chiitiler -s3r ap-northeast-1
"s3://tiles/{z}/{x}/{y}.pbf"
],
"maxzoom": 6
},
"cog": {
"type": "raster",
"tiles": [
"cog://https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/54/T/WN/2024/9/S2A_54TWN_20240908_0_L2A/TCI.tif/{z}/{x}/{y}"
],
"tileSize": 256
}
},
"layers": [
Expand Down Expand Up @@ -233,6 +240,14 @@ node dist/main.js tile-server -c s3 -s3b chiitiler -s3r ap-northeast-1
"circle-radius": 3,
"circle-color": "green"
}
},
{
"id": "cog",
"source": "cog",
"type": "raster",
"paint": {
"raster-opacity": 0.5
}
}
]
}
Expand Down
30 changes: 29 additions & 1 deletion localdata/style.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"version": "8",
"version": 8,
"sources": {
"osm": {
"type": "raster",
"tiles": [
"https://tile.openstreetmap.org/{z}/{x}/{y}.png"
],
"tileSize": 256
},
"dir": {
"type": "vector",
"tiles": [
Expand Down Expand Up @@ -35,9 +42,22 @@
"pmtiles://s3://tiles/school.pmtiles/{z}/{x}/{y}"
],
"maxzoom": 10
},
"cog": {
"type": "raster",
"tiles": [
"cog://https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/54/T/WN/2024/9/S2A_54TWN_20240908_0_L2A/TCI.tif/{z}/{x}/{y}"
],
"tileSize": 256
}
},
"layers": [
{
"id": "osm",
"source": "osm",
"type": "raster",
"paint": {}
},
{
"id": "dir",
"source": "dir",
Expand Down Expand Up @@ -87,6 +107,14 @@
"circle-radius": 3,
"circle-color": "purple"
}
},
{
"id": "cog",
"source": "cog",
"type": "raster",
"paint": {
"raster-opacity": 0.8
}
}
]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "chiitiler",
"version": "1.13.0",
"version": "1.14.0",
"description": "Tiny map rendering server for MapLibre Style Spec",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -43,10 +43,11 @@
"better-sqlite3": "^9.6.0",
"commander": "^11.0.0",
"file-system-cache": "^2.4.4",
"higuruma": "^0.1.6",
"hono": "^4.5.4",
"lightning-pool": "^4.2.2",
"lru-cache": "^11.0.0",
"pmtiles": "^3.0.5",
"sharp": "^0.32.5"
}
}
}
2 changes: 2 additions & 0 deletions src/render/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const TRANSPARENT_BUFFER: Record<string, Buffer> = {
};

function handleFileExt(uri: string) {
if (uri.startsWith('cog://')) return 'png'; // cog:// always returns as png

// extract extension only, take into account query string or hash
const basename = path.basename(uri).split(/[?#]/)[0];
const ext = basename.split('.').pop();
Expand Down
21 changes: 21 additions & 0 deletions src/source/cog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference lib="dom" />
// for using native fetch in TypeScript

import { renderTile } from 'higuruma';

async function getCogSource(uri: string): Promise<Buffer | null> {
const cogPath = uri.replace('cog://', '').replace(/\/\d+\/\d+\/\d+$/, '');
const [z, x, y] = uri.replace(`cog://${cogPath}/`, '').split('/');

try {
const tile = await renderTile(cogPath, Number(z), Number(x), Number(y));
const buf = Buffer.from(tile);
if (buf.byteLength === 129) return null; // empty tile
return buf;
} catch (e) {
console.error(`[ERROR] ${e}`);
return null;
}
}

export { getCogSource };
7 changes: 7 additions & 0 deletions src/source/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe('getSource', () => {
expect(data).not.toBeNull();
});

it('cog://', async () => {
const uri =
'cog://https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/54/T/WN/2024/9/S2A_54TWN_20240908_0_L2A/TCI.tif/6/32/24';
const data = await getSource(uri);
expect(data).not.toBeNull();
});

it('invalid uri', async () => {
const uri = 'invalid://localdata/style.json';
const data = await getSource(uri);
Expand Down
3 changes: 3 additions & 0 deletions src/source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getHttpSource } from './http.js';
import { getPmtilesSoruce } from './pmtiles.js';
import { getMbtilesSource } from './mbtiles.js';
import { getS3Source } from './s3.js';
import { getCogSource } from './cog.js';
import { Cache, noneCache } from '../cache/index.js';

/**
Expand All @@ -24,6 +25,8 @@ async function getSource(
else if (uri.startsWith('mbtiles://')) data = await getMbtilesSource(uri);
else if (uri.startsWith('pmtiles://'))
data = await getPmtilesSoruce(uri, cache);
else if (uri.startsWith('cog://')) data = await getCogSource(uri);
else return null;

return data;
}
Expand Down

0 comments on commit 84d400a

Please sign in to comment.