From 679086b8d0bc393f698b289454d698a0f0c6b33c Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sat, 2 Mar 2013 22:13:01 +0000 Subject: Update some libs, and improve display on different zoom levels --- resources/op2geojson.js | 77 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 17 deletions(-) (limited to 'resources/op2geojson.js') diff --git a/resources/op2geojson.js b/resources/op2geojson.js index a4233fd..d928310 100644 --- a/resources/op2geojson.js +++ b/resources/op2geojson.js @@ -5,7 +5,7 @@ op2geojson = function() { var instance = {}, geojson; - instance.fetch = function(url, data, zoom, callback) { + instance.fetch = function(url, data, zoom, latitude, callback) { var postCallback = function(data) { // Add nodes and ways to the layer var features = []; @@ -31,23 +31,43 @@ op2geojson = function() { } }); + var format = new OpenLayers.Format.GeoJSON; + // circumference of the Earth * cos(latitude)/2^(zoom level +8) + var latitudeInRadians = latitude * Math.PI/180; + var metersPerPixel = (40075000 * Math.cos(latitudeInRadians))/(Math.pow(2, (zoom + 8))); + $.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)); - } + if (!_.isUndefined(way.tags) && ( + way.tags.amenity == "hospital" || + !_.isUndefined(way.tags.place) || + way.tags.landuse == "residential")) { + + var area = instance.wayToPolygon(way, nodes); + + var areaGeo = format.parseGeometry(area.geometry); + + var geodesicArea = areaGeo.getGeodesicArea(); + var averageWidth = Math.sqrt(geodesicArea); + var pixelsOfAverageWidth = averageWidth / metersPerPixel; + + if (pixelsOfAverageWidth < 10) { + var node = nodes[way.nodes[0]]; + var centroid = areaGeo.getCentroid(); + var point = { + type : "Feature", + id : way.id, + geometry : { + type : "Point", + coordinates : [centroid.x, centroid.y] + }, + properties : {} + }; + _.extend(point.properties, way.tags); + features.push(point); + } else { + features.push(area); + } + } }); $.each(relations, function(i, relation) { @@ -167,6 +187,29 @@ op2geojson = function() { return poly; } + instance.wayToPolygon = 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 poly = { + type : "Feature", + geometry : { + type : "Polygon", + coordinates : [coordinates] + }, + properties : {} + }; + + // Add the tags + _.extend(poly.properties, way.tags); + return poly; + } + return instance; }; -- cgit v1.2.3