Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/44'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Jan 26, 2025
2 parents 6ff8187 + 6777e17 commit 75f2c0b
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 140 deletions.
77 changes: 70 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) {

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 ?? {};
}
};
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"leaflet": "0.6"
},
"devDependencies": {
"mocha": "^10.7.3",
"chai": "^4.2.0",
"jsdom": "^16.2.2"
},
"optionalDependencies": {}
"jsdom": "^16.2.2",
"jsdom-global": "^3.0.2",
"mocha": "^10.7.3"
}
}
1 change: 1 addition & 0 deletions test/fixtures/area.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.6","generator":"OpenStreetMap server","copyright":"OpenStreetMap and contributors","attribution":"http://www.openstreetmap.org/copyright","license":"http://opendatacommons.org/licenses/odbl/1-0/","elements":[{"type":"node","id":1911003400,"lat":44.6641548,"lon":-73.0149544,"timestamp":"2012-09-12T04:59:23Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003422,"lat":44.6639106,"lon":-73.0183662,"timestamp":"2012-09-12T04:59:23Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003428,"lat":44.6639144,"lon":-73.0164457,"timestamp":"2012-09-12T04:59:24Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003434,"lat":44.6632593,"lon":-73.0149372,"timestamp":"2012-09-12T04:59:24Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003459,"lat":44.6642311,"lon":-73.0152709,"timestamp":"2012-09-12T04:59:24Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003477,"lat":44.6639881,"lon":-73.0163749,"timestamp":"2012-09-12T04:59:24Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003481,"lat":44.6640251,"lon":-73.0181462,"timestamp":"2012-09-12T04:59:25Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003483,"lat":44.6640861,"lon":-73.0163599,"timestamp":"2012-09-12T04:59:25Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003505,"lat":44.663365,"lon":-73.0184144,"timestamp":"2012-09-12T04:59:25Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003534,"lat":44.6631742,"lon":-73.0151529,"timestamp":"2012-09-12T04:59:26Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003542,"lat":44.6631857,"lon":-73.0149866,"timestamp":"2012-09-12T04:59:26Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003614,"lat":44.6640251,"lon":-73.018275,"timestamp":"2012-09-12T04:59:27Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003629,"lat":44.6639945,"lon":-73.0183447,"timestamp":"2012-09-12T04:59:27Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"node","id":1911003725,"lat":44.6633765,"lon":-73.0149061,"timestamp":"2012-09-12T04:59:29Z","version":1,"changeset":13078014,"user":"jfire","uid":67236},{"type":"way","id":180655479,"timestamp":"2012-09-12T04:59:32Z","version":1,"changeset":13078014,"user":"jfire","uid":67236,"nodes":[1911003422,1911003505,1911003534,1911003542,1911003434,1911003725,1911003400,1911003459,1911003483,1911003477,1911003428,1911003481,1911003614,1911003629,1911003422],"tags":{"leisure":"pitch"}}]}
1 change: 1 addition & 0 deletions test/fixtures/changeset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.6","generator":"openstreetmap-cgimap 2.0.1 (3448772 spike-06.openstreetmap.org)","copyright":"OpenStreetMap and contributors","attribution":"http://www.openstreetmap.org/copyright","license":"http://opendatacommons.org/licenses/odbl/1-0/","changeset":{"id":1,"created_at":"2005-04-09T19:54:13Z","open":false,"comments_count":48,"changes_count":2,"closed_at":"2005-04-09T20:54:39Z","min_lat":51.5288506,"min_lon":-0.1465242,"max_lat":51.5288620,"max_lon":-0.1464925,"uid":1,"user":"Steve"}}
1 change: 1 addition & 0 deletions test/fixtures/node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.6","generator":"OpenStreetMap server","copyright":"OpenStreetMap and contributors","attribution":"http://www.openstreetmap.org/copyright","license":"http://opendatacommons.org/licenses/odbl/1-0/","elements":[{"type":"node","id":356552551,"lat":44.6636172,"lon":-73.0132525,"timestamp":"2009-03-07T03:26:33Z","version":1,"changeset":747176,"user":"iandees","uid":4732,"tags":{"amenity":"school","ele":"114","gnis:county_id":"011","gnis:created":"10/29/1980","gnis:edited":"05/27/2008","gnis:feature_id":"1456383","gnis:state_id":"50","name":"Bellows Free Academy"}}]}
1 change: 1 addition & 0 deletions test/fixtures/way.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"0.6","generator":"OpenStreetMap server","copyright":"OpenStreetMap and contributors","attribution":"http://www.openstreetmap.org/copyright","license":"http://opendatacommons.org/licenses/odbl/1-0/","elements":[{"type":"node","id":204587613,"lat":44.665983,"lon":-72.920587,"timestamp":"2009-10-11T18:01:27Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595896,"lat":44.665883,"lon":-72.923053,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595901,"lat":44.66597,"lon":-72.922201,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595904,"lat":44.665989,"lon":-72.921986,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595911,"lat":44.666015,"lon":-72.921474,"timestamp":"2009-11-17T05:45:06Z","version":2,"changeset":3138486,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595915,"lat":44.666015,"lon":-72.921254,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595919,"lat":44.666007,"lon":-72.921033,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595923,"lat":44.665938,"lon":-72.920089,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595927,"lat":44.665901,"lon":-72.919727,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595932,"lat":44.665837,"lon":-72.919284,"timestamp":"2009-11-17T05:45:06Z","version":2,"changeset":3138486,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595935,"lat":44.665821,"lon":-72.91914,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595939,"lat":44.66581,"lon":-72.918999,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595943,"lat":44.665814,"lon":-72.918855,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595947,"lat":44.665823,"lon":-72.918783,"timestamp":"2009-12-11T06:32:30Z","version":2,"changeset":3346870,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595950,"lat":44.665839,"lon":-72.91871,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595953,"lat":44.66587,"lon":-72.918641,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595955,"lat":44.665923,"lon":-72.918586,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595957,"lat":44.665985,"lon":-72.918559,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595961,"lat":44.666193,"lon":-72.918523,"timestamp":"2009-11-17T05:45:06Z","version":2,"changeset":3138486,"user":"woodpeck_fixbot","uid":147510},{"type":"node","id":204595963,"lat":44.666342,"lon":-72.918513,"timestamp":"2009-10-11T18:03:23Z","version":2,"changeset":2817006,"user":"woodpeck_fixbot","uid":147510},{"type":"way","id":19698713,"timestamp":"2008-01-03T05:24:43Z","version":1,"changeset":522559,"user":"DaveHansenTiger","uid":7168,"nodes":[204595896,204595901,204595904,204595911,204595915,204595919,204587613,204595923,204595927,204595932,204595935,204595939,204595943,204595947,204595950,204595953,204595955,204595957,204595961,204595963],"tags":{"name":"Oustinoff Rd","tiger:separated":"no","tiger:source":"tiger_import_dch_v0.6_20070830","tiger:tlid":"136533324:136533325","tiger:upload_uuid":"bulk_upload.pl-62def9fe-abee-42f3-ac1f-9d9f4b4ee78b","tiger:reviewed":"no","tiger:cfcc":"A41","tiger:county":"Franklin, VT","tiger:zip_left":"05444","tiger:zip_right":"05444","highway":"residential","tiger:name_base":"Oustinoff","tiger:name_type":"Rd"}}]}
Loading

0 comments on commit 75f2c0b

Please sign in to comment.