Skip to content

Commit

Permalink
JSON parser: more modern code style
Browse files Browse the repository at this point in the history
  • Loading branch information
mmd-osm committed Jan 24, 2025
1 parent 3ee5608 commit 8baac05
Showing 1 changed file with 41 additions and 79 deletions.
120 changes: 41 additions & 79 deletions leaflet-osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ L.OSM.XMLParser = {
},

getTags: function (xml) {

var result = {};

var tags = xml.getElementsByTagName("tag");
Expand All @@ -335,98 +334,61 @@ L.OSM.XMLParser = {
}

L.OSM.JSONParser = {
getChangesets: function (json) {
var result = [];

var changesets = json.changeset ? [json.changeset] : [];

for (var i = 0; i < changesets.length; i++) {
var cs = changesets[i];
result.push({
id: cs["id"].toString(),
type: "changeset",
latLngBounds: L.latLngBounds(
[cs["min_lat"], cs["min_lon"]],
[cs["max_lat"], cs["max_lon"]]),
tags: this.getTags(cs)
});
}

return result;
getChangesets(json) {
const changesets = json.changeset ? [json.changeset] : [];

return changesets.map(cs => ({
id: String(cs.id),
type: "changeset",
latLngBounds: L.latLngBounds(
[cs.min_lat, cs.min_lon],
[cs.max_lat, cs.max_lon]
),
tags: this.getTags(cs)
}));
},

getNodes: function (json) {
var result = {};

var nodes = json.elements?.filter(d => d.type === "node") || [];
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i], id = node["id"];
getNodes(json) {
const nodes = json.elements?.filter(el => el.type === "node") ?? [];
let result = {};

result[id] = {
id: id.toString(),
for (const node of nodes) {
result[node.id] = {
id: String(node.id),
type: "node",
latLng: L.latLng(node["lat"],
node["lon"],
true),
latLng: L.latLng(node.lat, node.lon, true),
tags: this.getTags(node)
};
}

return result;
},

getWays: function (json, nodes) {
var result = [];
getWays(json, nodes) {
const ways = json.elements?.filter(el => el.type === "way") ?? [];

var ways = json.elements?.filter(d => d.type === "way") || [];
for (var i = 0; i < ways.length; i++) {
var way = ways[i], nds = way.nodes;

var way_object = {
id: way["id"].toString(),
type: "way",
nodes: new Array(nds.length),
tags: this.getTags(way)
};

for (var j = 0; j < nds.length; j++) {
way_object.nodes[j] = nodes[nds[j]];
}

result.push(way_object);
}

return result;
return ways.map(way => ({
id: String(way.id),
type: "way",
nodes: way.nodes.map(nodeId => nodes[nodeId]),
tags: this.getTags(way)
}));
},

getRelations: function (json, nodes, ways) {
var result = [];

var rels = json.elements?.filter(d => d.type === "relation") || [];
for (var i = 0; i < rels.length; i++) {
var rel = rels[i], members = rel["members"];

var rel_object = {
id: rel["id"].toString(),
type: "relation",
members: new Array(members.length),
tags: this.getTags(rel)
};

for (var j = 0; j < members.length; j++) {
if (members[j]["type"] === "node")
rel_object.members[j] = nodes[members[j]["ref"]];
else // relation-way and relation-relation membership not implemented
rel_object.members[j] = null;
}
rel_object.members = rel_object.members.filter(i => i !== null && i !== undefined)
result.push(rel_object);
}

return result;
getRelations(json, nodes) {
const relations = json.elements?.filter(el => el.type === "relation") ?? [];

return relations.map(rel => ({
id: String(rel.id),
type: "relation",
members: (rel.members ?? []) // relation-way and relation-relation membership not implemented
.map(member => member.type === "node" ? nodes[member.ref] : null)
.filter(Boolean), // filter out null and undefined
tags: this.getTags(rel)
}));
},

getTags: function (json) {
return json["tags"];
getTags(json) {
return json.tags ?? {};
}
}
};

0 comments on commit 8baac05

Please sign in to comment.