Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pull/43'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Jan 5, 2025
2 parents ebfaf22 + d72b634 commit eb4fd05
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
40 changes: 21 additions & 19 deletions leaflet-osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,25 @@ L.OSM.DataLayer = L.FeatureGroup.extend({
ways = L.OSM.getWays(xml, nodes),
relations = L.OSM.getRelations(xml, nodes, ways);

var wayNodes = {}
for (var i = 0; i < ways.length; i++) {
var way = ways[i];
for (var j = 0; j < way.nodes.length; j++) {
wayNodes[way.nodes[j].id] = true
}
}

var relationNodes = {}
for (var i = 0; i < relations.length; i++){
var relation = relations[i];
for (var j = 0; j < relation.members.length; j++) {
relationNodes[relation.members[j].id] = true
}
}

for (var node_id in nodes) {
var node = nodes[node_id];
if (this.interestingNode(node, ways, relations)) {
if (this.interestingNode(node, wayNodes, relationNodes)) {
features.push(node);
}
}
Expand All @@ -176,23 +192,9 @@ L.OSM.DataLayer = L.FeatureGroup.extend({
return false;
},

interestingNode: function (node, ways, relations) {
var used = false;

for (var i = 0; i < ways.length; i++) {
if (ways[i].nodes.indexOf(node) >= 0) {
used = true;
break;
}
}

if (!used) {
return true;
}

for (var i = 0; i < relations.length; i++) {
if (relations[i].members.indexOf(node) >= 0)
return true;
interestingNode: function (node, wayNodes, relationNodes) {
if (!wayNodes[node.id] || relationNodes[node.id]) {
return true
}

for (var key in node.tags) {
Expand Down Expand Up @@ -309,7 +311,7 @@ L.Util.extend(L.OSM, {
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);
}

Expand Down
30 changes: 15 additions & 15 deletions test/osm_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,54 +146,54 @@ describe("L.OSM.DataLayer", function () {
it("can be added to the map", function () {
(new L.OSM.DataLayer(null, {asynchronous: true})).addTo(this.map);
});

it("creates a Polyline for a way", async function () {
var osm = new L.OSM.DataLayer(fixture("way"), {asynchronous: true});
await sleep(1);
layers(osm).length.should.eq(21);
layers(osm)[20].should.be.an.instanceof(L.Polyline);
});

it("creates a Polygon for an area", async function () {
var osm = new L.OSM.DataLayer(fixture("area"), {asynchronous: true});
await sleep(1);
layers(osm).length.should.eq(15);
layers(osm)[14].should.be.an.instanceof(L.Polygon);
});

it("creates a CircleMarker for an interesting node", async function () {
var osm = new L.OSM.DataLayer(fixture("node"), {asynchronous: true});
await sleep(1);
layers(osm).length.should.eq(1);
layers(osm)[0].should.be.an.instanceof(L.CircleMarker);
});

it("creates a Rectangle for a changeset", async function () {
var osm = new L.OSM.DataLayer(fixture("changeset"), {asynchronous: true});
await sleep(1);
layers(osm).length.should.eq(1);
layers(osm)[0].should.be.an.instanceof(L.Rectangle);
});

it("sets the feature property on a layer", async function () {
var osm = new L.OSM.DataLayer(fixture("node"), {asynchronous: true});
await sleep(1);
layers(osm)[0].feature.should.have.property("type", "node");
layers(osm)[0].feature.should.have.property("id", "356552551");
});

it("sets a way's style", async function () {
var osm = new L.OSM.DataLayer(fixture("way"), {styles: {way: {color: "red"}}, asynchronous: true});
await sleep(1);
layers(osm)[20].options.should.have.property("color", "red");
});

it("sets an area's style", async function () {
var osm = new L.OSM.DataLayer(fixture("area"), {styles: {area: {color: "green"}}, asynchronous: true});
await sleep(1);
layers(osm)[14].options.should.have.property("color", "green");
});

it("sets a node's style", async function () {
var osm = new L.OSM.DataLayer(fixture("node"), {styles: {node: {color: "blue"}}, asynchronous: true});
await sleep(1);
Expand All @@ -219,22 +219,22 @@ describe("L.OSM.DataLayer", function () {
var layer = new L.OSM.DataLayer();

it("returns true when the node is not in any ways", function () {
layer.interestingNode({}, [], []).should.be.true;
layer.interestingNode({id: 1}, {}, {}).should.be.true;
});

it("returns true when the node has an interesting tag", function () {
var node = {tags: {interesting: true}};
layer.interestingNode(node, [{nodes: [node]}], []).should.be.true;
var node = {id: 1, tags: {interesting: true}};
layer.interestingNode(node, {1: true}, {1: true}).should.be.true;
});

it("returns false when the node is used in a way and has uninteresting tags", function () {
var node = {tags: {source: 'who cares?'}};
layer.interestingNode(node, [{nodes: [node]}], []).should.be.false;
var node = {id: 1, tags: {source: 'who cares?'}};
layer.interestingNode(node, {1: true}, {}).should.be.false;
});

it("returns true when the node is used in a way and is used in a relation", function () {
var node = {};
layer.interestingNode(node, [{nodes: [node]}], [{members: [node]}]).should.be.true;
var node = {id: 1};
layer.interestingNode(node, {1: true}, {1: true}).should.be.true;
});
});
});

0 comments on commit eb4fd05

Please sign in to comment.