diff --git a/leaflet-osm.js b/leaflet-osm.js index 6a8fcde..840b1a4 100644 --- a/leaflet-osm.js +++ b/leaflet-osm.js @@ -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); } } @@ -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) { @@ -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); } diff --git a/test/osm_test.js b/test/osm_test.js index 3e40767..7807ba1 100644 --- a/test/osm_test.js +++ b/test/osm_test.js @@ -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); @@ -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; }); }); });