Skip to content

Commit

Permalink
Update leaflet-osm to latest upstream version
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Jan 26, 2025
1 parent 6316745 commit d76b7a5
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions vendor/assets/leaflet/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) {

const parser = data instanceof Document ? L.OSM.XMLParser : L.OSM.JSONParser;

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,64 @@ L.Util.extend(L.OSM, {

return result;
}
});
}

L.OSM.JSONParser = {
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(json) {
const nodes = json.elements?.filter(el => el.type === "node") ?? [];
let result = {};

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

return result;
},

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

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

getRelations(json, nodes, ways) {
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(json) {
return json.tags ?? {};
}
};

0 comments on commit d76b7a5

Please sign in to comment.