Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created April 19, 2014 12:31
Show Gist options
  • Save qoobaa/11083137 to your computer and use it in GitHub Desktop.
Save qoobaa/11083137 to your computer and use it in GitHub Desktop.
var fs = require("fs");
function decode(encoded) {
var path = [];
for (var i = 0, j = 0, n = [0, 0]; i < encoded.length; i++) {
var k = encoded.charCodeAt(i) - 0x30;
n[j] = (n[j] << 5) | k & 0x1f;
if ((k & 0x20)) {
n[j] = ((n[j] & 0x01) ? (~(n[j] >> 1)) : (n[j] >> 1)) * 1e-7;
if (++j == 2) {
var latLon = path.length > 0 ? path[path.length - 1] : [0, 0];
path.push([latLon[0] + n[0], latLon[1] + n[1]]);
}
n[j = j % 2] = 0;
}
}
return path.map(function (latLon) {
return [latLon[1], latLon[0]];
});
}
var paths = {
type: "FeatureCollection",
crs: {
type: "name",
properties: {
name: "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: []
},
bases = {
type: "FeatureCollection",
crs: {
type: "name",
properties: {
name: "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: []
},
caves = {
type: "FeatureCollection",
crs: {
type: "name",
properties: {
name: "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: []
},
nodes = {
type: "FeatureCollection",
crs: {
type: "name",
properties: {
name: "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: []
},
places = {
type: "FeatureCollection",
crs: {
type: "name",
properties: {
name: "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: []
},
shelters = {
type: "FeatureCollection",
crs: {
type: "name",
properties: {
name: "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
features: []
};
fs.readdirSync("./dat").forEach(function (filename) {
var json,
file = fs.readFileSync("./dat/" + filename, { encoding: "utf-8" });
json = JSON.parse(file);
Object.keys(json.trails).forEach(function (id) {
var trail = json.trails[id];
paths.features.push({
type: "Feature",
geometry: {
type: "LineString",
coordinates: decode(trail.encoded)
},
properties: {
name: trail.name,
colors: trail.colors.map(function (hsl) {
return "hsl(" + hsl.split(",").join(", ") + ")";
}),
times: trail.times,
distance: trail.distance
}
});
});
Object.keys(json.nodes).forEach(function (id) {
var node = json.nodes[id],
feature = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [node.coord[1], node.coord[0]]
},
properties: {
name: node.name,
elevation: node.elevation
}
};
switch(node.icon) {
case "base,24,96":
bases.features.push(feature);
break;
case "cave,24,72":
caves.features.push(feature);
break;
case "node,10,24":
nodes.features.push(feature);
break;
case "place,24,120":
places.features.push(feature);
break;
case "shelter,24,48":
shelters.features.push(feature);
break;
default:
console.log("unknown type: " + node.icon);
}
});
});
fs.writeFileSync("paths.json", JSON.stringify(paths));
fs.writeFileSync("bases.json", JSON.stringify(bases));
fs.writeFileSync("caves.json", JSON.stringify(caves));
fs.writeFileSync("nodes.json", JSON.stringify(nodes));
fs.writeFileSync("places.json", JSON.stringify(places));
fs.writeFileSync("shelters.json", JSON.stringify(shelters));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment