From 40f11f8748cc704fe6f0c5e89cd56d9a37ba2aa4 Mon Sep 17 00:00:00 2001 From: Harry Cutts Date: Sun, 2 Dec 2012 13:08:31 +0000 Subject: Show polygon hospitals as well as nodes --- resources/map.js | 10 ++++++++-- resources/op2geojson.js | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/resources/map.js b/resources/map.js index 50fdd87..35ea6ef 100644 --- a/resources/map.js +++ b/resources/map.js @@ -34,6 +34,12 @@ $(document).ready(function() { $('.leaflet-control-layers-selector').first().trigger('click') } + function createQueryURL(bbox) { + return "http://overpass-api.de/api/interpreter?" + + "data=[out:json];(node[amenity=hospital](" + bbox + + ");way[amenity=hospital]("+ bbox +");node(w););out;"; + } + map.on('hospitalsfetched', addHospitalLayer); map.on('hospitalsfetched', initFilters); map.on('locationfound', onLocationFound); @@ -50,7 +56,7 @@ $(document).ready(function() { var sw = bounds.getSouthWest(); var ne = bounds.getNorthEast(); bbox = [sw.lat, sw.lng, ne.lat, ne.lng].join(','); - var url = "http://overpass-api.de/api/interpreter?data=[out:json];node[amenity=hospital](" + bbox + ");out;"; + var url = createQueryURL(bbox); converter = new op2geojson(); converter.fetch(url, function(data) { self.hospitals = data; @@ -85,7 +91,7 @@ $(document).ready(function() { } function geojsonLayer() { - url = "http://overpass-api.de/api/interpreter?data=[out:json];node[amenity=hospital](52.34,13.3,52.52,13.6);out;"; + url = createQueryURL(52.34,13.3,52.52,13.6); converter = new op2geojson(); converter.fetch(url, function(data) { self.hospitals = data; diff --git a/resources/op2geojson.js b/resources/op2geojson.js index 74431d3..55be9e3 100644 --- a/resources/op2geojson.js +++ b/resources/op2geojson.js @@ -8,10 +8,21 @@ op2geojson = function() { instance.fetch = function(url, callback) { $.getJSON(url, { format: "json" }, 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' ) { + if( item.type === 'node' && item.tags != null ) { features.push( instance.point(item) ); + } else if (item.type === 'way') { + features.push( instance.lineString(item, nodes) ); } }); geojson = instance.featureCollection(features); @@ -33,6 +44,29 @@ op2geojson = function() { 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", -- cgit v1.2.3