diff options
author | Christopher Baines <cbaines8@gmail.com> | 2012-12-09 12:03:46 +0000 |
---|---|---|
committer | Christopher Baines <cbaines8@gmail.com> | 2012-12-09 13:29:24 +0000 |
commit | 23f22db20c725d6bb79db1d43d1ddd81c8a1d73b (patch) | |
tree | 5e58e68766d4f6a41b6d292d56e02f3d432edcee /resources/op2geojson.js | |
parent | ee1ad2aed2ffa67906be06a978d7e8cbbe8d5316 (diff) | |
download | health-map-23f22db20c725d6bb79db1d43d1ddd81c8a1d73b.tar health-map-23f22db20c725d6bb79db1d43d1ddd81c8a1d73b.tar.gz |
Add ability to display catchment areas
Still a bit broken in some places...
Diffstat (limited to 'resources/op2geojson.js')
-rw-r--r-- | resources/op2geojson.js | 75 |
1 files changed, 65 insertions, 10 deletions
diff --git a/resources/op2geojson.js b/resources/op2geojson.js index 4e4de16..cff2dea 100644 --- a/resources/op2geojson.js +++ b/resources/op2geojson.js @@ -8,24 +8,45 @@ op2geojson = function() { instance.fetch = function(url, data, callback) { $.post(url, data, function(data) { - // List all of the returned nodes - var nodes = []; + // 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; - } - }); - // Add nodes and ways to the layer - var features = []; - $.each(data.elements, function(i, item) { - if( item.type === 'node' && item.tags != undefined + // As the nodes do not relate to other bits, + // they can be added here + if (item.type === 'node' + && item.tags != undefined && item.tags['amenity'] != undefined) { - features.push( instance.point(item) ); + + features.push( instance.point(item) ); + } } else if (item.type === 'way') { - features.push( instance.lineString(item, nodes) ); + ways[item.id] = item; + } else if (item.type === 'relation') { + relations[item.id] = item; } }); + + $.each(ways, function(i, way) { + features.push( instance.lineString(way, nodes) ); + }); + + $.each(relations, function(i, relation) { + if (relation.tags != undefined && + relation.tags['type'] == 'boundary' && + relation.tags['boundary'] == 'catchment_area') { + + features.push( instance.polygon(relation, ways, nodes) ); + } + }); + geojson = instance.featureCollection(features); callback(geojson); } @@ -76,6 +97,40 @@ op2geojson = function() { return collection; } + instance.polygon = function(relation, ways, nodes) { + polyCoordinates = []; + + $.each(relation.members, function(i, member) { + if (member.role == "outer") { + var way = ways[member.ref]; + var wayCoordinates = instance.lineString(way, nodes).geometry.coordinates; + if (polyCoordinates.length != 0) { + // If this way is backward + if (polyCoordinates[polyCoordinates.length - 1][0] != wayCoordinates[0][0] || + polyCoordinates[polyCoordinates.length - 1][1] != wayCoordinates[0][1]) { + wayCoordinates.reverse(); + } + } + polyCoordinates.shift(); + polyCoordinates = polyCoordinates.concat( wayCoordinates ); + } + }); + + var poly = { + "type" : "Feature", + "geometry" : { + "type" : "Polygon", + "coordinates" : [polyCoordinates] + }, + "properties" : {} + }; + + // Add the tags + _.extend(poly.properties, relation.tags); + + return poly; + } + return instance; }; |