Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oceans as GeoJSON to avoid "blue" zones over lands. #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ services:
- "traefik.enable=true"
- "traefik.port=8080"
- "traefik.http.routers.maps-server.entrypoints=web"
- "traefik.http.routers.maps-server.rule=PathPrefix(`/tiles`)"
- "traefik.http.routers.maps-server.rule=PathPrefix(`/tiles`) || PathPrefix(`/oceans.geojson`)"
#ports:
#- "8083:80"

Expand All @@ -49,4 +49,4 @@ volumes:
maps-importer:
maps-postgis:
maps-server:

10 changes: 0 additions & 10 deletions importer/lib/imposm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ async function importOceansData(data) {
async function importOsmFile() {
const mappingFile = path.join(path.dirname(module.filename), 'mapping.yml');

// Data extracted from Natural Earth.
const oceansFile = path.join(path.dirname(module.filename), 'oceans.json');

logger.info(`Reading ${CONFIG.osmFilePath}...`);

await spawnImposm([
Expand All @@ -99,13 +96,6 @@ async function importOsmFile() {
'-connection', CONFIG.postgisConnection,
], fs.createWriteStream(CONFIG.imposmLogFilePath, { flags: 'a' }));

logger.info('Import oceans...');

const oceanFileData = JSON.parse(fs.readFileSync(oceansFile));
const oceansGeoJson = oceanFileData.features[0].geometry;
await importOceansData(oceansGeoJson);


logger.info('Deploy production tables...');

await spawnImposm([
Expand Down
1 change: 0 additions & 1 deletion importer/lib/imposm/oceans.json

This file was deleted.

165 changes: 99 additions & 66 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,82 +108,115 @@ function genQuery(type, x, y, z) {
`;
}

http.createServer(async (request, response) => {
try {
const res = /\/tiles\/(?<type>[a-z0-9_]+)\/(?<z>[0-9]+)\/(?<x>[0-9]+)\/(?<y>[0-9]+).pbf/.exec(request.url);
async function serverTile(res, request, response) {
const pgClient = await pgPool.connect();

if (res) {
const pgClient = await pgPool.connect();
// ---- Check mtime from database.
const pgMtime = await getPgMtime(pgClient);

// ---- Check mtime from database.
const pgMtime = await getPgMtime(pgClient);
let done = false;

let done = false;
if (request.headers['if-modified-since']) {
const remoteMTime = new Date(request.headers['if-modified-since']).getTime();

if (request.headers['if-modified-since']) {
const remoteMTime = new Date(request.headers['if-modified-since']).getTime();
if (remoteMTime === pgMtime) {
response.writeHead(304, {
'Access-Control-Allow-Origin': '*',
'Last-Modified': new Date(pgMtime).toUTCString(),
});

if (remoteMTime === pgMtime) {
response.writeHead(304, {
'Access-Control-Allow-Origin': '*',
'Last-Modified': new Date(pgMtime).toUTCString(),
});
response.end();

response.end();
done = true;
}
}

done = true;
}
}
if (!done) {
const {
type, x, y, z,
} = res.groups;

if (!done) {
const {
type, x, y, z,
} = res.groups;

// ----- Check if tile is already in the cache.
const tilePath = path.join(CONFIG.cacheDir, `${type}/${z}/${x}/${y}.pbf`);

if (fs.existsSync(tilePath)) {
// ----- Send cached tile to to client.
const data = fs.readFileSync(tilePath);

response.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': data.length,
'Access-Control-Allow-Origin': '*',
'From-Cache' : true,
'Last-Modified': fs.statSync(tilePath).mtime.toUTCString(),
});

response.end(data, 'binary');
} else {
// ----- Get tile data from database.
const pgRes = await pgClient.query(genQuery(type, x, y, z));
const data = pgRes.rows[0].st_asmvt;

// ----- Send answer to client.
response.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': data.length,
'Access-Control-Allow-Origin': '*',
'From-Cache' : false,
'Last-Modified': new Date(pgMtime).toUTCString(),
});

response.end(data, 'binary');

// ----- Save generated tile in cache.
// Temporary name in case we have concurrent connection asking for the same tile.
const tmpTilePath = `${tilePath}.tmp_${new Date().getTime()}`;

mkdirp.sync(path.dirname(tilePath));
fs.writeFileSync(tmpTilePath, data, { encoding: 'binary' });
fs.renameSync(tmpTilePath, tilePath);
}
}
// ----- Check if tile is already in the cache.
const tilePath = path.join(CONFIG.cacheDir, `${type}/${z}/${x}/${y}.pbf`);

await pgClient.release();
if (fs.existsSync(tilePath)) {
// ----- Send cached tile to to client.
const data = fs.readFileSync(tilePath);

response.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': data.length,
'Access-Control-Allow-Origin': '*',
'From-Cache': true,
'Last-Modified': fs.statSync(tilePath).mtime.toUTCString(),
});

response.end(data, 'binary');
} else {
// ----- Get tile data from database.
const pgRes = await pgClient.query(genQuery(type, x, y, z));
const data = pgRes.rows[0].st_asmvt;

// ----- Send answer to client.
response.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': data.length,
'Access-Control-Allow-Origin': '*',
'From-Cache': false,
'Last-Modified': new Date(pgMtime).toUTCString(),
});

response.end(data, 'binary');

// ----- Save generated tile in cache.
// Temporary name in case we have concurrent connection asking for the same tile.
const tmpTilePath = `${tilePath}.tmp_${new Date().getTime()}`;

mkdirp.sync(path.dirname(tilePath));
fs.writeFileSync(tmpTilePath, data, { encoding: 'binary' });
fs.renameSync(tmpTilePath, tilePath);
}
}

await pgClient.release();
}

async function serveOceans(res, request, response) {
// Data based on Natural Earth Data.
const filePath = './oceans.geojson';

const { size, mtime } = fs.statSync(filePath);

// ----- Send answer to client.
response.writeHead(200, {
'Content-Type': 'application/geo+json',
'Content-Length': size,
'Access-Control-Allow-Origin': '*',
'Last-Modified': mtime,
});

fs.createReadStream(filePath).pipe(response);
}

const controllers = [
[/\/tiles\/(?<type>[a-z0-9_]+)\/(?<z>[0-9]+)\/(?<x>[0-9]+)\/(?<y>[0-9]+).pbf/, serverTile],
[new RegExp('^/oceans.geojson$'), serveOceans],
];

http.createServer(async (request, response) => {
try {
let done = false;

for (const controller of controllers) {
const res = controller[0].exec(request.url);
if (res) {
await controller[1](res, request, response);
done = true;
break;
}
}

if (!done) {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end(`404 ${http.STATUS_CODES[404]}`);
}
Expand Down
8 changes: 8 additions & 0 deletions server/oceans.geojson

Large diffs are not rendered by default.