diff options
Diffstat (limited to 'resources/op2geojson.js')
-rw-r--r-- | resources/op2geojson.js | 36 |
1 files changed, 35 insertions, 1 deletions
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", |