Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
stephanbogner / getSquareAroundPointWithArea.js
Last active June 18, 2017 20:33
Create square around a centerpoint with a specified area
// Note: Not sure how well it works with very large squares due to projection distortion
var turf = require('turf'); // For NodeJS
function getSquareAroundPointWithArea(point, area) {
// Area in m2
var coordinates = [];
var areaInKm = area / 1000 / 1000;
var edgeLength = Math.sqrt(areaInKm);
var diagonal = Math.sqrt(Math.pow(edgeLength, 2) + Math.pow(edgeLength, 2));
@stephanbogner
stephanbogner / queryOpenStreetMapId
Last active June 18, 2017 20:39
Overpass Turbo Query Notes
// Query for a certain OSM ID in Overpass Turbo (https://overpass-turbo.eu/)
// e.g. for http://www.openstreetmap.org/way/329871559
// Replace the number below with the OSM ID
[out:json][timeout:25];
(
node(329871559);
way(329871559);
relation(329871559);
);
@stephanbogner
stephanbogner / input.geojson
Last active June 22, 2017 15:01
Creates an outline to a lineString similar to the outline stroke feature in illustrator
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@stephanbogner
stephanbogner / getSvgOutlineCircumference.js
Last active June 18, 2017 21:24
Gets the circumference of svg style coordinates
var coordinatesString = "223.177496408,3814.53101376 210.997794052,3807.13302253 195.852458671,3793.15810611 193.993838635,3791.43941224 180.933214269,3772.19079794 170.136151235,3747.26405193 165.198319283,3723.470457 163.682660844,3678.3761749 156.176550135,3555.30499258 151.509512401,3480.88344101 145.426837106,3400.82934564 143.794799706,3364.1990739 143.798834889,3326.0160711 150.571949561,3300.02762516 160.269931448,3276.84312163 172.710136954,3253.09881653 191.619853303,3230.83282556 216.520246839,3210.98593792 239.837973955,3200.03271458 260.488917349,3192.51986408 274.107463498,3190.53640455 288.34689427,3188.89624907 304.890225132,3189.71668734 328.216563439,3192.98221144 345.935882157,3196.87585617 358.827514541,3202.22143433 371.831727975,3208.85035743 388.203233763,3219.72972793 409.103972729,3237.94034309 425.300490135,3258.88452843 444.210350636,3303.2243898 447.967830543,3319.477736 448.705200294,3345.05481017 443.331961433,3372.210188 429.295830231,3405.64584587 412.766832771,3427.98420714 39
@stephanbogner
stephanbogner / calculateBendWay.js
Created June 18, 2017 23:39
Takes a line of vectors and calculates its movement (handy if you animate a line and want to control its speed depending on its complexity)
example();
function calculateBendWay(vectorLengths, deltaAngles){
if (vectorLengths.length !== deltaAngles.length) {
throw 'the number of vectors don\'t match up with the number of angles (one less angle than the number of vectors)'
}
var entireBendWay = 0;
for (var i = 0; i < vectorLengths.length; i++) {
var vectorLength = vectorLengths[i];
var deltaAngle = deltaAngles[i];
@stephanbogner
stephanbogner / getLineDistance.js
Created June 18, 2017 23:40
Take a geojson feature as file and returns its circumference/length
var fs = require('fs');
var turf = require('turf');
fs.readFile('feature.geojson', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var feature = JSON.parse(data);
var length = turf.lineDistance(feature);
console.log(length + 'km');
@stephanbogner
stephanbogner / ternaryplot.js
Created June 18, 2017 23:41
Example for ternary plotting (https://en.wikipedia.org/wiki/Ternary_plot) like in moovellab's mobility space explorer
console.log();
console.log('Examples');
console.log();
console.log('- All Bike (dot bottom right)');
console.log(' ', mobilityTriangle(1123, 0, 0) );
console.log();
console.log('- All Car (dot top)');
console.log(' ', mobilityTriangle(0, 124, 0) );
console.log();
console.log('- All Rail (dot bottom left)');
@stephanbogner
stephanbogner / addAverageData.js
Last active July 9, 2017 20:49
Calculating the average without knowing all previous values
var data = {};
addAverageData(data, 'averageAppleDiameter', 5);
addAverageData(data, 'averageAppleDiameter', 7);
addAverageData(data, 'averageAppleDiameter', 8);
addAverageData(data, 'averageAppleDiameter', 9);
addAverageData(data, 'averagePearDiameter', 9);
addAverageData(data, 'averagePearDiameter', 4);
addAverageData(data, 'averagePearDiameter', 6);
@stephanbogner
stephanbogner / mongo
Created June 22, 2017 06:57
Handy commands for mongoDB
mongodump --db <yourdb> --gzip --archive=/path/to/archive
mongorestore --gzip --archive=/path/to/archive
mongorestore --gzip --archive=/path/to/archive/fileName.gz
@stephanbogner
stephanbogner / getOffsetOutline.js
Last active June 22, 2017 15:55
Offsets creates a lineString offset from the original one
function getOffsetOutline(lineString, thickness, side) {
// thickness in km
var direction = 1;
if (side === 'left') {
direction = -1;
} else if (side === 'right') {
direction = 1;
} else {
console.log('Please specify direction as \'left\' or \'right\'');