aboutsummaryrefslogtreecommitdiff
path: root/resources/op2geojson.js
blob: a4233fd6ff12f0cd6ea78f01b9e6ed28d18ae183 (plain)
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
( function ( $ ) {

op2geojson = function() {

	var instance = {},
		geojson;

	instance.fetch = function(url, data, zoom, callback) {
		var postCallback = function(data) {
			// Add nodes and ways to the layer
			var features = [];

			// Process the data
			var nodes = {};
			var ways = {};
			var relations = {};
			$.each(data.elements, function(i, item) {
				if (item.type === 'node') {
					nodes[item.id] = item;

					// As the nodes do not relate to other bits,
					// they can be added here
					if (item.tags != undefined) {
						features.push( instance.point(item) );
					}

				} else if (item.type === 'way') {
					ways[item.id] = item;
				} else if (item.type === 'relation') {
					relations[item.id] = item;
				}
			});

			$.each(ways, function(i, way) {
				if (zoom < 16) {
					var node = nodes[way.nodes[0]];
					var point = {
						type : "Feature",
						id : way.id,
						geometry : {
							type : "Point",
							coordinates : [node.lon,node.lat]
						},
						properties : {}
					};
					_.extend(point.properties, way.tags);
					features.push(point);
				} else {
					features.push(instance.lineString(way, nodes));
				}
			});

			$.each(relations, function(i, relation) {
				if (relation.tags != undefined &&
					relation.tags['type'] == 'boundary' &&
					relation.tags['boundary'] == 'catchment_area') {

					var poly = instance.polygon(relation, ways, nodes);
					poly.id = relation.id;
					features.push(poly);
				}
			});

			geojson = instance.featureCollection(features);
			callback(geojson);
		}
    	$.post(url, data, postCallback, "json");
	};

	instance.point = function(node) {
		var point = {
			type : "Feature",
			geometry : {
				type : "Point",
				coordinates : [node.lon,node.lat]
			},
			properties : {}
		};
		_.extend(point.properties, node.tags);
		return point;
	}

	instance.lineString = function(way, nodeArray) {
		// Get the node coordinates from nodeArray
		var coordinates = [];
		for (id in way.nodes) {
			var node = nodeArray[way.nodes[id]];
			coordinates.push([node.lon,node.lat]);
		}

		// Create the LineString
		var lineString = {
			type : "Feature",
			geometry : {
				type : "LineString",
				coordinates : coordinates
			},
			properties : {}
		};

		// Add the tags
		_.extend(lineString.properties, way.tags);
		return lineString;
	}

	instance.featureCollection = function(features) {
		collection = {
			type : "FeatureCollection",
			features : features
		};
		return collection;
	}

	instance.polygon = function(relation, ways, nodes) {
        polyCoords = [];
        var firstCheck = true;
        var subject;

		$.each(relation.members, function(i, member) {
            if (member.role == "outer") {
                var way = ways[member.ref];
                if (typeof way == 'undefined') return;
                var wayCoords = instance.lineString(way, nodes).geometry.coordinates;
				var numNodes = wayCoords.length

                // Need to ensure that the first way is in the correct direction, but this can
                // only be checked when looking at the second way
                if (firstCheck && polyCoords.length != 0) {
                    firstCheck = false;
                    if ((polyCoords[0][0] == wayCoords[0][0] &&
                        polyCoords[0][1] == wayCoords[0][1]) ||
                        (polyCoords[0][0] == wayCoords[numNodes - 1][0] &&
                        polyCoords[0][1] == wayCoords[numNodes - 1][1])) {

                        polyCoords.reverse();
                    }
                }

                if (polyCoords.length != 0) {
                    // If this way is backward
                    if (polyCoords[polyCoords.length - 1][0] != wayCoords[0][0] ||
                        polyCoords[polyCoords.length - 1][1] != wayCoords[0][1]) {

                        wayCoords.reverse();
                    }
                    polyCoords.pop();
                }
			    polyCoords = polyCoords.concat( wayCoords );
            } else if (member.role == "subject") {
                subject = member.ref;
            }
		});

        var poly = {
			type : "Feature",
			geometry : {
				type : "Polygon",
				coordinates : [polyCoords]
			},
			properties : {}
		};

		// Add the tags
		_.extend(poly.properties, relation.tags);
        poly.properties["subject"] = subject;

		return poly;
	}

	return instance;

};

})( jQuery );