Skip to content
Permalink
Newer
Older
100644 286 lines (233 sloc) 7.86 KB
October 15, 2012 16:47
1
L.OSM = {};
2
3
L.OSM.TileLayer = L.TileLayer.extend({
4
options: {
April 10, 2020 14:31
5
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
July 17, 2018 22:48
6
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
October 15, 2012 16:47
7
},
8
9
initialize: function (options) {
10
options = L.Util.setOptions(this, options);
11
L.TileLayer.prototype.initialize.call(this, options.url);
12
}
13
});
14
15
L.OSM.Mapnik = L.OSM.TileLayer.extend({
16
options: {
April 14, 2020 00:01
17
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
August 8, 2013 14:46
18
maxZoom: 19
October 15, 2012 16:47
19
}
20
});
21
December 6, 2020 21:23
22
L.OSM.CyclOSM = L.OSM.TileLayer.extend({
23
options: {
24
url: 'https://{s}.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
25
maxZoom: 20,
26
subdomains: 'abc',
27
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="https://www.openstreetmap.fr" target="_blank">OpenStreetMap France</a>'
28
}
29
});
30
October 15, 2012 16:47
31
L.OSM.CycleMap = L.OSM.TileLayer.extend({
32
options: {
July 17, 2018 22:48
33
url: 'https://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}{r}.png?apikey={apikey}',
34
maxZoom: 21,
July 17, 2018 22:48
35
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
October 15, 2012 16:47
36
}
37
});
38
39
L.OSM.TransportMap = L.OSM.TileLayer.extend({
40
options: {
July 17, 2018 22:48
41
url: 'https://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}{r}.png?apikey={apikey}',
42
maxZoom: 21,
July 17, 2018 22:48
43
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://www.thunderforest.com/" target="_blank">Andy Allan</a>'
October 15, 2012 16:47
44
}
45
});
46
June 25, 2020 20:23
47
L.OSM.OPNVKarte = L.OSM.TileLayer.extend({
48
options: {
49
url: 'https://tileserver.memomaps.de/tilegen/{z}/{x}/{y}.png',
50
maxZoom: 18,
51
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://memomaps.de/" target="_blank">MeMoMaps</a>'
52
}
53
});
54
September 23, 2013 17:50
55
L.OSM.HOT = L.OSM.TileLayer.extend({
56
options: {
July 17, 2018 22:48
57
url: 'https://tile-{s}.openstreetmap.fr/hot/{z}/{x}/{y}.png',
September 23, 2013 17:50
58
maxZoom: 20,
59
subdomains: 'abc',
July 17, 2018 22:48
60
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors. Tiles courtesy of <a href="http://hot.openstreetmap.org/" target="_blank">Humanitarian OpenStreetMap Team</a>'
September 23, 2013 17:50
61
}
62
});
63
64
L.OSM.GPS = L.OSM.TileLayer.extend({
65
options: {
April 10, 2020 14:31
66
url: 'https://gps.tile.openstreetmap.org/lines/{z}/{x}/{y}.png',
67
maxZoom: 21,
68
maxNativeZoom: 20,
69
subdomains: 'abc'
70
}
71
});
72
October 15, 2012 16:47
73
L.OSM.DataLayer = L.FeatureGroup.extend({
August 25, 2012 10:55
74
options: {
75
areaTags: ['area', 'building', 'leisure', 'tourism', 'ruins', 'historic', 'landuse', 'military', 'natural', 'sport'],
76
uninterestingTags: ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid'],
77
styles: {}
78
},
79
80
initialize: function (xml, options) {
81
L.Util.setOptions(this, options);
82
83
L.FeatureGroup.prototype.initialize.call(this);
84
85
if (xml) {
86
this.addData(xml);
87
}
88
},
89
October 19, 2012 10:57
90
addData: function (features) {
91
if (!(features instanceof Array)) {
92
features = this.buildFeatures(features);
93
}
August 25, 2012 10:55
94
October 19, 2012 10:57
95
for (var i = 0; i < features.length; i++) {
96
var feature = features[i], layer;
August 25, 2012 10:55
97
November 18, 2013 17:38
98
if (feature.type === "changeset") {
99
layer = L.rectangle(feature.latLngBounds, this.options.styles.changeset);
100
} else if (feature.type === "node") {
October 19, 2012 10:57
101
layer = L.circleMarker(feature.latLng, this.options.styles.node);
August 25, 2012 10:55
102
} else {
October 19, 2012 10:57
103
var latLngs = new Array(feature.nodes.length);
104
105
for (var j = 0; j < feature.nodes.length; j++) {
106
latLngs[j] = feature.nodes[j].latLng;
107
}
108
109
if (this.isWayArea(feature)) {
110
latLngs.pop(); // Remove last == first.
111
layer = L.polygon(latLngs, this.options.styles.area);
112
} else {
113
layer = L.polyline(latLngs, this.options.styles.way);
114
}
August 25, 2012 10:55
115
}
116
117
layer.addTo(this);
October 19, 2012 10:57
118
layer.feature = feature;
August 25, 2012 10:55
119
}
October 19, 2012 10:57
120
},
121
122
buildFeatures: function (xml) {
November 18, 2013 17:38
123
var features = L.OSM.getChangesets(xml),
October 19, 2012 10:57
124
nodes = L.OSM.getNodes(xml),
May 14, 2013 21:41
125
ways = L.OSM.getWays(xml, nodes),
126
relations = L.OSM.getRelations(xml, nodes, ways);
August 25, 2012 10:55
127
128
for (var node_id in nodes) {
129
var node = nodes[node_id];
May 14, 2013 21:41
130
if (this.interestingNode(node, ways, relations)) {
October 19, 2012 10:57
131
features.push(node);
August 25, 2012 10:55
132
}
133
}
October 19, 2012 10:57
134
135
for (var i = 0; i < ways.length; i++) {
136
var way = ways[i];
137
features.push(way);
138
}
139
140
return features;
August 25, 2012 10:55
141
},
142
143
isWayArea: function (way) {
144
if (way.nodes[0] != way.nodes[way.nodes.length - 1]) {
145
return false;
146
}
147
148
for (var key in way.tags) {
149
if (~this.options.areaTags.indexOf(key)) {
150
return true;
151
}
152
}
153
154
return false;
155
},
156
May 14, 2013 21:41
157
interestingNode: function (node, ways, relations) {
November 8, 2012 12:40
158
var used = false;
159
160
for (var i = 0; i < ways.length; i++) {
161
if (ways[i].nodes.indexOf(node) >= 0) {
162
used = true;
163
break;
164
}
165
}
166
167
if (!used) {
168
return true;
169
}
170
May 14, 2013 21:41
171
for (var i = 0; i < relations.length; i++) {
172
if (relations[i].members.indexOf(node) >= 0)
173
return true;
174
}
175
August 25, 2012 10:55
176
for (var key in node.tags) {
November 8, 2012 12:40
177
if (this.options.uninterestingTags.indexOf(key) < 0) {
August 25, 2012 10:55
178
return true;
179
}
180
}
181
182
return false;
183
}
184
});
185
186
L.Util.extend(L.OSM, {
November 18, 2013 17:38
187
getChangesets: function (xml) {
188
var result = [];
189
190
var nodes = xml.getElementsByTagName("changeset");
191
for (var i = 0; i < nodes.length; i++) {
192
var node = nodes[i], id = node.getAttribute("id");
193
result.push({
194
id: id,
195
type: "changeset",
196
latLngBounds: L.latLngBounds(
197
[node.getAttribute("min_lat"), node.getAttribute("min_lon")],
198
[node.getAttribute("max_lat"), node.getAttribute("max_lon")]),
199
tags: this.getTags(node)
200
});
201
}
202
203
return result;
204
},
205
August 25, 2012 10:55
206
getNodes: function (xml) {
207
var result = {};
208
209
var nodes = xml.getElementsByTagName("node");
210
for (var i = 0; i < nodes.length; i++) {
211
var node = nodes[i], id = node.getAttribute("id");
212
result[id] = {
213
id: id,
214
type: "node",
215
latLng: L.latLng(node.getAttribute("lat"),
216
node.getAttribute("lon"),
217
true),
218
tags: this.getTags(node)
219
};
220
}
221
222
return result;
223
},
224
October 19, 2012 10:57
225
getWays: function (xml, nodes) {
August 25, 2012 10:55
226
var result = [];
227
228
var ways = xml.getElementsByTagName("way");
229
for (var i = 0; i < ways.length; i++) {
230
var way = ways[i], nds = way.getElementsByTagName("nd");
231
232
var way_object = {
233
id: way.getAttribute("id"),
234
type: "way",
235
nodes: new Array(nds.length),
236
tags: this.getTags(way)
237
};
238
239
for (var j = 0; j < nds.length; j++) {
October 19, 2012 10:57
240
way_object.nodes[j] = nodes[nds[j].getAttribute("ref")];
August 25, 2012 10:55
241
}
242
243
result.push(way_object);
244
}
245
246
return result;
247
},
248
May 14, 2013 21:41
249
getRelations: function (xml, nodes, ways) {
250
var result = [];
251
252
var rels = xml.getElementsByTagName("relation");
253
for (var i = 0; i < rels.length; i++) {
254
var rel = rels[i], members = rel.getElementsByTagName("member");
255
256
var rel_object = {
257
id: rel.getAttribute("id"),
258
type: "relation",
259
members: new Array(members.length),
260
tags: this.getTags(rel)
261
};
262
263
for (var j = 0; j < members.length; j++) {
264
if (members[j].getAttribute("type") === "node")
265
rel_object.members[j] = nodes[members[j].getAttribute("ref")];
266
else // relation-way and relation-relation membership not implemented
267
rel_object.members[j] = null;
268
}
269
270
result.push(rel_object);
271
}
272
273
return result;
274
},
275
August 25, 2012 10:55
276
getTags: function (xml) {
277
var result = {};
278
279
var tags = xml.getElementsByTagName("tag");
280
for (var j = 0; j < tags.length; j++) {
281
result[tags[j].getAttribute("k")] = tags[j].getAttribute("v");
282
}
283
284
return result;
285
}
286
});