Skip to content

Instantly share code, notes, and snippets.

@felixebert
Created August 18, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixebert/e59a5e0ed9acaed3b63797b9f216cc15 to your computer and use it in GitHub Desktop.
Save felixebert/e59a5e0ed9acaed3b63797b9f216cc15 to your computer and use it in GitHub Desktop.
Merge and Filter GeoJSON Example
var fs = require('fs');
var files = ['Carl-Peters.geojson']; // alle geojson Dateien
var allWayFeatures = []; // unsere Ziel-Liste mit allen Ways
files.forEach(function (file) { // für jede geojson Datei ...
var content = fs.readFileSync(file, {encoding: 'utf-8'}); // lese Dateiinhalt als Zeichenkette
var geojson = JSON.parse(content); // Umwandlung in JavaScript-Objekt
geojson.features.forEach(function (feature) { // für jedes GeoJSON Feature (way, node, etc.) ...
if (feature.id.indexOf('way') >= 0) { // wenn das Feld ID den Begriff "way" enthält ...
allWayFeatures.push(feature); // füge das Feature unserer Ziel-Liste mit allen Ways hinzu
}
})
});
var newGeojson = { // packe unsere Ziel-Liste mit allen Ways in ein GeoJSON-Objekt
"type": "FeatureCollection",
"features": allWayFeatures
};
var newGeojsonString = JSON.stringify(newGeojson, null, 4); // wandle das GeoJSON-Objekt in eine Zeichenkette um, um diese speichern zu können
fs.writeFileSync('ways.geojson', newGeojsonString, {encoding: 'utf-8'}); // speichere die Zeichenkette in der Datei ways.geojson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment