From a4cdfcdba0211fb2a8a68887a8903dcbc19353d5 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Wed, 19 Oct 2016 11:56:06 +0300 Subject: [PATCH] Return generic result if reverse geocode fails --- server/search.js | 18 ++++++++++++++++-- server/utils.js | 8 +++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/server/search.js b/server/search.js index f67720c7..2f2fa591 100644 --- a/server/search.js +++ b/server/search.js @@ -4,6 +4,7 @@ var Promise = require("promise"); var cheerio = require("cheerio"); var zlib = require("zlib"); var compressjs = require("compressjs"); +var utils = require("./utils"); request = request.defaults({ gzip: true, @@ -85,8 +86,18 @@ function find(query, loadUrls) { + "&zoom=" + (lonlat.zoom != null ? (lonlat.zoom >= 12 ? lonlat.zoom+2 : lonlat.zoom) : 17), json: true }).then(function(body) { - if(!body) - throw "Invalid response from name finder."; + if(!body || body) { + var name = utils.round(lonlat.lat, 5) + ", " + utils.round(lonlat.lon, 5); + return [ { + lat: lonlat.lat, + lon : lonlat.lon, + type : "coordinates", + short_name: name, + display_name : name, + zoom: lonlat.zoom != null ? lonlat.zoom : 15, + icon: "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png" + } ]; + } body.lat = lonlat.lat; body.lon = lonlat.lon; @@ -105,6 +116,9 @@ function find(query, loadUrls) { if(!body) throw "Invalid response from name finder."; + if(body.error) + throw body.error; + return body.map(prepareSearchResult); }); }); diff --git a/server/utils.js b/server/utils.js index 65356c73..54b45841 100644 --- a/server/utils.js +++ b/server/utils.js @@ -256,6 +256,11 @@ function isoDate(date) { return pad(date.getUTCFullYear(), 4) + '-' + pad(date.getUTCMonth()+1, 2) + '-' + pad(date.getUTCDate(), 2) + 'T' + pad(date.getUTCHours(), 2) + ':' + pad(date.getUTCMinutes(), 2) + ':' + pad(date.getUTCSeconds(), 2) + 'Z'; } +function round(number, digits) { + var fac = Math.pow(10, digits); + return Math.round(number*fac)/fac; +} + module.exports = { isInBbox : isInBbox, filterStream : filterStream, @@ -267,5 +272,6 @@ module.exports = { ArrayStream : ArrayStream, streamEachPromise : streamEachPromise, escapeXml : escapeXml, - isoDate : isoDate + isoDate : isoDate, + round: round }; \ No newline at end of file