Skip to content

Instantly share code, notes, and snippets.

@amiad
Last active January 13, 2020 12:56
Show Gist options
  • Save amiad/082f0508cc4942a6704cec5f79147380 to your computer and use it in GitHub Desktop.
Save amiad/082f0508cc4942a6704cec5f79147380 to your computer and use it in GitHub Desktop.
Convert odata nature reverses file to OSM format
const file = "resgan_2020-01-heb.osm";
const newFile = 'new_' + file;
let fs = require("fs");
let xml2js = require("xml2js");
let parseString = require("xml2js").parseString;
let deletedWaysId = [], orphanNodes = [];
fs.readFile(file, "utf-8", (err, data) => {
if (err) throw err;
parseString(data, (err, result) => {
if (err) console.log(err);
var json = result;
convert(json.osm.way);
convert(json.osm.relation, true);
deletedOrphanNodes(json.osm.node);
saveFile(json);
});
});
function convert(ways, isRelation = false){
for (let wayid in ways) {
let isReverseOrPark; // 1 is nature reverse, 2 is national park
let name, nameEn, ref;
(() => {
let tags = ways[wayid].tag;
for (let tagid in tags) {
let tag = tags[tagid]['$'];
switch (tag['k']) {
case 'STATUS_DES':
if (tag['v'].includes('מאושר') || tag['v'].includes('מופקד')){
deletedWaysId.push(ways[wayid]['$']['id']);
for (let nodeid in ways[wayid].nd)
orphanNodes.push(ways[wayid].nd[nodeid]['$']['ref']);
delete ways[wayid];
//console.log('deleted!');
return;
}
break;
case 'PLAN_NUMBE':
ref = tag['v'];
break;
case 'PARK_TYPE1':
isReverseOrPark = tag['v'];
break;
case 'PARK_ENG_N':
nameEn = tag['v'];
break;
case 'PARK_HEB_1':
name = tag['v'];
break;
}
delete tags[tagid];
}
})();
let newTags;
if (isReverseOrPark == undefined || ways[wayid] == undefined)
continue;
else if (isReverseOrPark == 1)
newTags =
{
'boundary': 'protected_area',
'protection_title': 'Nature Reserve',
'protect_class': isReverseOrPark,
'name': 'שמורת טבע ' + name,
'name:he': 'שמורת טבע ' + name,
'name:en': nameEn + ' Nature Reserve',
};
else
newTags =
{
'boundary': 'national_park',
'protection_title': 'National Park',
'protect_class': isReverseOrPark,
'name': 'גן לאומי ' + name,
'name:he': 'גן לאומי ' + name,
'name:en': nameEn + ' National Park',
};
newTags.source = 'https://www.odata.org.il/dataset/f101af02-97fc-45db-bbfc-737b6f3fb626';
newTags.ref = ref;
for (let key in newTags) {
ways[wayid].tag.push({'$': {'k': key, 'v': newTags[key]}});
}
if (isRelation)
checkMembers(ways[wayid]);
}
}
function saveFile(json){
let builder = new xml2js.Builder();
let xml = builder.buildObject(json);
fs.writeFile(newFile, xml, (err) => {
if (err) throw err;
console.log("done!!!\nThe converted data saved to " + newFile);
});
}
function checkMembers(relation){
for (let memberid in relation.member){
if (deletedWaysId.includes(relation.member[memberid]['$']['ref']))
delete relation.member[memberid];
}
}
function deletedOrphanNodes(nodes){
for (let nodeid in nodes)
if (orphanNodes.includes(nodes[nodeid]['$']['id']))
delete nodes[nodeid];
}
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"define-properties": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
"integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
"requires": {
"object-keys": "^1.0.12"
}
},
"es-abstract": {
"version": "1.16.2",
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.16.2.tgz",
"integrity": "sha512-jYo/J8XU2emLXl3OLwfwtuFfuF2w6DYPs+xy9ZfVyPkDcrauu6LYrw/q2TyCtrbc/KUdCiC5e9UajRhgNkVopA==",
"requires": {
"es-to-primitive": "^1.2.1",
"function-bind": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.1",
"is-callable": "^1.1.4",
"is-regex": "^1.0.4",
"object-inspect": "^1.7.0",
"object-keys": "^1.1.1",
"string.prototype.trimleft": "^2.1.0",
"string.prototype.trimright": "^2.1.0"
}
},
"es-to-primitive": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz",
"integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==",
"requires": {
"is-callable": "^1.1.4",
"is-date-object": "^1.0.1",
"is-symbol": "^1.0.2"
}
},
"function-bind": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
"integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
"requires": {
"function-bind": "^1.1.1"
}
},
"has-symbols": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz",
"integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg=="
},
"is-callable": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz",
"integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA=="
},
"is-date-object": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz",
"integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY="
},
"is-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz",
"integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=",
"requires": {
"has": "^1.0.1"
}
},
"is-symbol": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz",
"integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==",
"requires": {
"has-symbols": "^1.0.1"
}
},
"object-inspect": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.7.0.tgz",
"integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw=="
},
"object-keys": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
"integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
},
"object.getownpropertydescriptors": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz",
"integrity": "sha1-h1jIRvW0B62rDyNuCYbxSwUcqhY=",
"requires": {
"define-properties": "^1.1.2",
"es-abstract": "^1.5.1"
}
},
"sax": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
},
"string.prototype.trimleft": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/string.prototype.trimleft/-/string.prototype.trimleft-2.1.0.tgz",
"integrity": "sha512-FJ6b7EgdKxxbDxc79cOlok6Afd++TTs5szo+zJTUyow3ycrRfJVE2pq3vcN53XexvKZu/DJMDfeI/qMiZTrjTw==",
"requires": {
"define-properties": "^1.1.3",
"function-bind": "^1.1.1"
}
},
"string.prototype.trimright": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/string.prototype.trimright/-/string.prototype.trimright-2.1.0.tgz",
"integrity": "sha512-fXZTSV55dNBwv16uw+hh5jkghxSnc5oHq+5K/gXgizHwAvMetdAJlHqqoFC1FSDVPYWLkAKl2cxpUT41sV7nSg==",
"requires": {
"define-properties": "^1.1.3",
"function-bind": "^1.1.1"
}
},
"util.promisify": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/util.promisify/-/util.promisify-1.0.0.tgz",
"integrity": "sha512-i+6qA2MPhvoKLuxnJNpXAGhg7HphQOSUq2LKMZD0m15EiskXUkMvKdF4Uui0WYeCUGea+o2cw/ZuwehtfsrNkA==",
"requires": {
"define-properties": "^1.1.2",
"object.getownpropertydescriptors": "^2.0.3"
}
},
"xml2js": {
"version": "0.4.22",
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.22.tgz",
"integrity": "sha512-MWTbxAQqclRSTnehWWe5nMKzI3VmJ8ltiJEco8akcC6j3miOhjjfzKum5sId+CWhfxdOs/1xauYr8/ZDBtQiRw==",
"requires": {
"sax": ">=0.6.0",
"util.promisify": "~1.0.0",
"xmlbuilder": "~11.0.0"
}
},
"xmlbuilder": {
"version": "11.0.1",
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz",
"integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA=="
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment