Skip to content

Commit

Permalink
add modified server script
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Nov 8, 2024
1 parent 2dd1174 commit 35ba3be
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 20 deletions.
117 changes: 117 additions & 0 deletions node_mbtiles/poc/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const http = require('http');

Check failure on line 1 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Replace `'http'` with `"http"`

Check failure on line 1 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Strings must use doublequote
const sqlite3 = require("sqlite3");

Check failure on line 2 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'sqlite3'
const url = require("url");

const morgan = require('morgan');

Check failure on line 5 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Expected 1 empty line after require statement not followed by another require

Check failure on line 5 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Replace `'morgan'` with `"morgan"`

Check failure on line 5 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module 'morgan'

Check failure on line 5 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Strings must use doublequote
const logger = morgan('combined');

Check failure on line 6 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Replace `'combined'` with `"combined"`

Check failure on line 6 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Strings must use doublequote

const sourceConfigs = require('source-configs');

Check failure on line 8 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Replace `'source-configs'` with `"source-configs"`
const schema = require('./configs-schema');
const configs = sourceConfigs(schema);

try {
configs.databases = parseDatabases(configs.databases);
} catch (err) {
console.error(err);
process.exit();
}

const databases = configs.databases;

const mode = configs.mode;
const port = configs.port;

let databaseAlias = Object.keys(databases);
databaseAlias.forEach(element => {
databases[element].db = new sqlite3.Database(databases[element].filePath, sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.log("Failed to load: " + databases[element].filePath);
throw err;
}
});
});

const requestListener = function (req, res) {

Check warning on line 34 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Unexpected unnamed function
logger(req, res, function(err) {

Check warning on line 35 in node_mbtiles/poc/main.js

View workflow job for this annotation

GitHub Actions / test

Unexpected unnamed function
if (err) {
return done(err)
}
let dbAlias;
let z;
let x;
let y;
//Argument mode - Slower, but compatible with non pretty PHP urls
if (mode == "argument") {
const queryObject = url.parse(req.url, true).query;
z = (queryObject.z ? parseInt(queryObject.z) : undefined);
x = (queryObject.x ? parseInt(queryObject.x) : undefined);
y = (queryObject.y ? parseInt(queryObject.y) : undefined);
dbAlias = (queryObject.db ? queryObject.db.split(".")[0] : undefined);
}
//End argument mode

//Pretty mode - Faster and better to use if it is a new initiation.
else if (mode == "pretty") {
args = parseUrl(req.url);
dbAlias = args[1];
z = (args[2] ? parseInt(args[2]) : undefined);
x = (args[3] ? parseInt(args[3]) : undefined);
y = (args[4] ? parseInt(args[4]) : undefined);
}
//End prettymode

if (databaseAlias.includes(dbAlias) && typeof z == "number" && typeof x == "number" && typeof y == "number") {

databases[dbAlias].db.get(`SELECT * FROM tiles WHERE zoom_level = ${z} AND tile_column = ${x} AND tile_row = ${y}`, (err, row) => {
if (err || row == undefined) {
res.writeHead(404, {
"Access-Control-Allow-Origin": "*"
});
res.end();
} else {
res.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-Encoding": "gzip",
"Access-Control-Allow-Origin": "*"
});
res.end(row.tile_data);
}
});
} else {
res.writeHead(400);
res.end();
}
})
}

const server = http.createServer(requestListener);
server.listen(port);
server.on("error", err => console.log(err));
console.log("Listening for connections on port: " + port);

function parseUrl(url) {
return url.split("/");
}

function parseDatabases(databasesConfig) {
if (!databasesConfig) {
throw new Error("No databases defined");
}
let databases = {};

databasesSplit = databasesConfig.split(',');

console.log("databases: " + JSON.stringify(databasesSplit))

databasesSplit.forEach(database => {
const databaseSplit = database.split(';');
console.log("parsing " + database);
if (databaseSplit.length != 2) {
throw new Error("Unable to parse Database string. Possible use of wrong delimiter");
}
databases[databaseSplit[0]] = {
filePath: databaseSplit[1]
}
})
return databases;
}
45 changes: 25 additions & 20 deletions scripts/add_tippecanoe_layer_names.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,30 @@
const path = require("path");
const fs = require("fs");

const dir = `${__dirname}/../public/dengue/may24/resources/geojson/admin2/`;
const inputFile = path.normalize(`${dir}gadm41_BRA_2_100pc.json`);
const outputFile = path.normalize(`${dir}gadm41_BRA_2_100pc_for_tippecanoe.json`);
console.log(`Transforming geojson at ${inputFile}`);
const transformFile = (relativeDir, fileName, layerIdProp) => {
const dir = `${__dirname}${relativeDir}`;
const inputFile = path.normalize(`${dir}/${fileName}`);
const outputFile = path.normalize(`${dir}/tippecanoe_${fileName}`);
console.log(`Transforming geojson at ${inputFile}`);

const input = require(inputFile);
const features = input.features;
console.log(`found ${features.length} features`);
features.forEach((feature) => {
const id = feature.properties.GID_2;
feature.tippecanoe = {
layer: id
};
});
const input = require(inputFile);
const features = input.features;
console.log(`found ${features.length} features`);
features.forEach((feature) => {
const id = feature.properties.GID_2;
feature.tippecanoe = {
layer: id
};
});

const output = JSON.stringify(input);
fs.writeFile(outputFile, output, (err) => {
if (err) {
throw err;
}
console.log(`Saved to ${outputFile}`);
});
const output = JSON.stringify(input);
fs.writeFile(outputFile, output, (err) => {
if (err) {
throw err;
}
console.log(`Saved to ${outputFile}`);
});
}

transformFile("/../public/dengue/may24/resources/geojson/admin2/", "gadm41_BRA_2_100pc.json", "GID_2");
transformFile("/../public/dengue/may24/resources/geojson/admin1/", "all_adm1_100pc.json", "GID_1");

0 comments on commit 35ba3be

Please sign in to comment.