-
Notifications
You must be signed in to change notification settings - Fork 2
/
osm.js
191 lines (154 loc) · 5.18 KB
/
osm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/** Make GeoJSON-esque objects from a XMLHTTPRequest.responseXML blob */
// namespacing!
if (!com) {
var com = { };
}
if (!com.stamen) {
com.stamen = {};
}
if (!com.stamen.osm) {
com.stamen.osm = {};
}
// Keys for Area tags
com.stamen.osm.POLYGON_TAGS = {
'aeroway': true,
'amenity': true,
'area': true,
'building': true,
'landuse': true,
'leisure': true,
'man_made': true,
'military': true,
'natural': true,
'power': true,
'sport': true,
'tourism': true
}
// OSM...
com.stamen.osm.OSM = function(responseXML) {
if (window.console && window.console.log) console.log('initing OSM object...');
var t = new Date().getTime();
this.nodes = [];
this.ways = [];
this.areas = [];
var boundsXML = responseXML.getElementsByTagName("bounds")[0];
// 0.6 only
this.bounds = {
minLat: parseFloat(boundsXML.attributes.getNamedItem('minlat').value),
minLon: parseFloat(boundsXML.attributes.getNamedItem('minlon').value),
maxLat: parseFloat(boundsXML.attributes.getNamedItem('maxlat').value),
maxLon: parseFloat(boundsXML.attributes.getNamedItem('maxlon').value)
};
if (window.console && window.console.log) console.log('got bounds ' + this.bounds);
// so we only have to loop over the xml nodes once
// (for when we're parsing node refs in ways)
// we'll make a hash of id --> node
var nodeHash = {};
var nodeXMLs = responseXML.getElementsByTagName("node");
for (var i = 0; i < nodeXMLs.length; i++) {
var nodeXML = nodeXMLs[i];
var id = nodeXML.attributes.getNamedItem('id').value;
var coordinates = [ parseFloat(nodeXML.attributes.getNamedItem('lon').value),
parseFloat(nodeXML.attributes.getNamedItem('lat').value) ];
var properties = {};
var tagXMLs = nodeXML.getElementsByTagName("tag");
for (var j = 0; j < tagXMLs.length; j++) {
var tagXML = tagXMLs[j];
var key = tagXML.attributes.getNamedItem('k').value;
var value = tagXML.attributes.getNamedItem('v').value;
properties[key] = value;
}
var node = new com.stamen.osm.Node(id, properties, coordinates);
this.nodes.push(node);
// remember for later
nodeHash[id] = node;
}
if (window.console && window.console.log) console.log('got ' + this.nodes.length + ' nodes');
var wayXMLs = responseXML.getElementsByTagName("way");
for (var i = 0; i < wayXMLs.length; i++) {
var wayXML = wayXMLs[i];
var coordinates = [];
var ndXMLs = wayXML.getElementsByTagName("nd");
for (var j = 0; j < ndXMLs.length; j++) {
var ndXML = ndXMLs[j];
var ref = ndXML.attributes.getNamedItem('ref').value;
var theNode = nodeHash[ref];
if (theNode) {
coordinates.push(theNode.geometry.coordinates);
}
else {
if (window.console && window.console.log) console.log('node ref ' + ref + ' not found');
}
}
var properties = {};
var tagXMLs = wayXML.getElementsByTagName("tag");
for (var j = 0; j < tagXMLs.length; j++) {
var tagXML = tagXMLs[j];
var key = tagXML.attributes.getNamedItem('k').value;
var value = tagXML.attributes.getNamedItem('v').value;
properties[key] = value;
}
var id = wayXML.attributes.getNamedItem('id').value;
var way = new com.stamen.osm.Way(id, properties, coordinates);
if (way.geometry.type == "Polygon") {
this.areas.push(way);
}
else {
this.ways.push(way);
}
} // for each way
if (window.console && window.console.log) console.log('got ' + this.ways.length + ' ways');
if (window.console && window.console.log) console.log('got ' + this.areas.length + ' areas');
if (window.console && window.console.log) console.log('done initing OSM object... ' + (new Date().getTime() - t) + ' ms');
};
com.stamen.osm.OSM.prototype = {
bounds: null,
nodes: null,
ways: null,
areas: null
// TODO: relations?
};
// Nodes...
com.stamen.osm.Node = function(id, properties, coordinates) {
this.id = id;
this.properties = properties;
this.geometry = {
"type": "Point",
"coordinates": coordinates
};
}
com.stamen.osm.Node.prototype = {
"id": null,
"type": "Feature",
"properties": null,
"geometry": null
}
// Ways...
com.stamen.osm.Way = function(id, properties, coordinates) {
this.id = id;
this.properties = properties;
var isArea = false;
for (var key in properties) {
if (com.stamen.osm.POLYGON_TAGS[key]) {
isArea = true;
}
}
if (isArea) {
this.geometry = {
"type": "Polygon",
"coordinates": [ coordinates ]
};
}
else {
this.geometry = {
"type": "LineString",
"coordinates": coordinates
};
}
}
com.stamen.osm.Way.prototype = {
"id": null,
"type": "Feature",
"properties": null,
"geometry": null
}