Skip to content

Instantly share code, notes, and snippets.

@nrenner
Created February 5, 2021 14:50
Show Gist options
  • Save nrenner/e0b3a98f04e655fee55858b1996a9e43 to your computer and use it in GitHub Desktop.
Save nrenner/e0b3a98f04e655fee55858b1996a9e43 to your computer and use it in GitHub Desktop.
BRouter rd5 grid
var fs = require("fs"),
topojson = require("topojson"),
filesize = require('file-size');
// https://brouter.de/brouter/segments4/
var path = 'Index of _brouter_segments4_.html',
gridSize = 5; // degrees
var entryList = [];
function readHtmlDirList(path) {
var lines = fs.readFileSync(path).toString().split('\n');
var i, matches, entry,
//<a href="E0_N10.rd5">E0_N10.rd5</a> 04-Feb-2021 01:03 7620723
regex = /\s*<a href=\"(.*)\.rd5\".*<\/a>\s+(\S+)\s(\S+)\s+(\S+)\s*/;
for (i = 0; i < lines.length; i++) {
matches = lines[i].match(regex);
if (matches) {
entry = {
name: matches[1],
date: matches[2],
//time: matches[3],
size: filesize(+matches[4], { fixed: 0 }).human()
};
entryList.push(entry);
}
}
}
function toGeoJson() {
var properties, matches, lat, lon
features = [];
function toPolygon(lat, lon) {
var n = lat + gridSize,
w = lon,
s = lat,
e = lon + gridSize;
return [
[ [w, n], [e, n], [e, s], [w, s], [w, n] ]
];
}
for (i = 0; i < entryList.length; i++) {
properties = entryList[i];
// W180_S85
matches = properties.name.match(/(E|W)(\d*)_(N|S)(\d*)/);
lon = parseInt(matches[2]) * ( matches[1] === 'E' ? 1 : -1 );
lat = parseInt(matches[4]) * ( matches[3] === 'N' ? 1 : -1 );
features.push({
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": toPolygon(lat, lon)
},
"properties": properties
});
}
var geoJson = {
"type": "FeatureCollection",
"features": features
};
return geoJson;
}
function toTopoJson(geoJson) {
return topojson.topology({
collection: geoJson
}, {
'verbose': true,
'quantization': false,
'stitch-poles': false,
'property-transform': function (feature) {
return feature.properties;
}
});
}
function writeJson(path, obj) {
fs.writeFile(path, JSON.stringify(obj), function(err) {
if (err) {
return console.error(err);
}
console.log('created file "' + path + '"');
});
}
readHtmlDirList(path);
var geoJson = toGeoJson();
writeJson('grid.geojson', geoJson);
var topoJson = toTopoJson(geoJson);
writeJson('grid.topojson', topoJson);
{
"name": "brouter-rd5-grid",
"version": "0.1.0",
"description": "Creates a 5° grid for the areas covered by BRouter rd5 routing data files as GeoJSON and TopoJSON",
"dependencies": {
"file-size": "^1.0.0",
"topojson": "^1.6.19"
},
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment