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

op2geojson = function() {

	var instance = {},
		geojson;

	instance.fetch = function(url, data, callback) {
    	$.post(url, data,
			function(data) {
				// List all of the returned nodes
				var nodes = [];
				$.each(data.elements, function(i, item) {
					if (item.type === 'node') {
						nodes[item.id] = item;
					}
				});

				// Add nodes and ways to the layer
				var features = [];
				$.each(data.elements, function(i, item) {
					if( item.type === 'node' && item.tags != undefined
							&& item.tags['amenity'] != undefined) {
						features.push( instance.point(item) );
					} else if (item.type === 'way') {
						features.push( instance.lineString(item, nodes) );
					}
				});
				geojson = instance.featureCollection(features);
				callback(geojson);
			}
		, "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.geojson = function() {
		url = "http://overpass-api.de/api/interpreter?data=[out:json];node[amenity=hospital](52.34,13.3,52.52,13.6);out;";
		instance.fetch(url, function(data) {
			return data;
		});
	}

	return instance;

};

})( jQuery );