Skip to content

Commit

Permalink
Add support for OSM JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
mmd-osm committed Jan 22, 2025
1 parent eb4fd05 commit ac1f60f
Showing 1 changed file with 106 additions and 7 deletions.
113 changes: 106 additions & 7 deletions leaflet-osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@ L.OSM.DataLayer = L.FeatureGroup.extend({
}
},

buildFeatures: function (xml) {
var features = L.OSM.getChangesets(xml),
nodes = L.OSM.getNodes(xml),
ways = L.OSM.getWays(xml, nodes),
relations = L.OSM.getRelations(xml, nodes, ways);
buildFeatures: function (data, format = "application/xml") {

const parser = (format === "application/json") ? L.OSM.JSONParser : L.OSM.XMLParser;

var features = parser.getChangesets(data),
nodes = parser.getNodes(data),
ways = parser.getWays(data, nodes),
relations = parser.getRelations(data, nodes, ways);

var wayNodes = {}
for (var i = 0; i < ways.length; i++) {
Expand Down Expand Up @@ -228,7 +231,7 @@ L.OSM.DataLayer = L.FeatureGroup.extend({
},
});

L.Util.extend(L.OSM, {
L.OSM.XMLParser = {
getChangesets: function (xml) {
var result = [];

Expand Down Expand Up @@ -328,4 +331,100 @@ L.Util.extend(L.OSM, {

return result;
}
});
}

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

var nodes = json.elements.filter(d => d.type === "changeset");
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i], id = node["id"];
result.push({
id: id,
type: "changeset",
latLngBounds: L.latLngBounds(
[node["min_lat"], node["min_lon"]],
[node["max_lat"], node["max_lon"]]),
tags: this.getTags(node)
});
}

return result;
},

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"];

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

return result;
},

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

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"],
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;
},

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"],
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;
},

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

0 comments on commit ac1f60f

Please sign in to comment.