diff --git a/package-lock.json b/package-lock.json index aedf77a..e8a37cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -52,7 +52,7 @@ }, "@sinonjs/formatio": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz", "integrity": "sha512-ls6CAMA6/5gG+O/IdsBcblvnd8qcO/l1TYoNeAzp3wcISOxlPXQEus0mLcdwazEkWjaBdaJ3TaxmNgCLWwvWzg==", "dev": true, "requires": { @@ -3395,7 +3395,6 @@ "version": "1.0.5", "bundled": true, "dev": true, - "optional": true, "requires": { "delayed-stream": "~1.0.0" } @@ -3458,8 +3457,7 @@ "delayed-stream": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "delegates": { "version": "1.0.0", @@ -3747,14 +3745,12 @@ "mime-db": { "version": "1.27.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "mime-types": { "version": "2.1.15", "bundled": true, "dev": true, - "optional": true, "requires": { "mime-db": "~1.27.0" } @@ -3830,8 +3826,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "oauth-sign": { "version": "0.8.2", diff --git a/server.js b/server.js index 49a9fcf..542307f 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,7 @@ const foursquare = require('./server/foursquare'); const rightbyme = require('./server/RightByMe'); const agenda = require('./server/agenda'); const directions = require('./server/directions'); +const geocode = require('./server/geocode'); logger.level = 'debug'; @@ -113,6 +114,28 @@ app.get('/fsexplore', cache('15 minutes'), (req, res) => { } }); +app.get('/geocode', /*cache('15 minutes'),*/ (req, res) => { + if (req.query.hasOwnProperty('ll')) { + const ll = req.query.ll; + console.log('ll',ll); + geocode.doGetGeocode(ll) + .then((d) => { + res.set('Cache-Control', 'public, max-age=900'); + res.send(d); + }).catch((e) => { + logger.error(e); + res.status(500).send('There was an error!'); + }); + } + + else { + // throw new Error('Weather: LL missing'); + logger.warn('FS: LL missing'); + res.status(500).send('LL Missing'); + } +}); + + app.get('/rightbyme', cache('86400 seconds'), (req, res) => { if (req.query.hasOwnProperty('ll')) rightbyme.doGetRightByMe(req.query.ll) diff --git a/server/geocode.js b/server/geocode.js new file mode 100644 index 0000000..f303f41 --- /dev/null +++ b/server/geocode.js @@ -0,0 +1,96 @@ +const NodeGeocoder = require('node-geocoder'); +const logger = require('log4js').getLogger('GeoCode'); + +const { reduceOpencage } = require('./reducers/opencage'); +logger.level = 'debug'; + +const options = { + 'provider': 'opencage', + + // Optional depending on the providers + 'httpAdapter': 'https', // Default + 'apiKey': '893ab539eca84b5ca7a54cb03ef23443', // for Mapquest, OpenCage, Google Premier + 'formatter': null // 'gpx', 'string', ... +}; + +const geocoder = NodeGeocoder(options); + +function doGetGeocode(ll) { + return new Promise((resolve, reject) => { + const [lat, lon ] = ll.split(','); + + const latlong = { lat, lon }; + + logger.debug(latlong); + + geocoder.reverse(latlong) + .then(function(res) { + if (res.hasOwnProperty('raw')) { + const result = reduceOpencage(res.raw); + + return resolve(result[0]); + } + else + return resolve(res[0]); + }) + .catch(function(err) { + logger.error(err); + + return reject(err); + }); + }); +} + +module.exports = { doGetGeocode }; + +/* + +opencage + +{ + "latitude": 51.508751, + "longitude": -0.067457, + "country": "United Kingdom", + "city": "London", + "state": "England", + "zipcode": "SE15", + "streetName": "Vaughan Way", + "countryCode": "gb", + "suburb": "St.George in the East", + "extra": { + "flag": "🇬🇧", + "confidence": 9, + "confidenceKM": 0.5, + "map": "https://www.openstreetmap.org/?mlat=51.50875&mlon=-0.06746#map=17/51.50875/-0.06746" + } +} + +google + +[ + { + "administrativeLevels": { + "level1long": "England", + "level1short": "England", + "level2long": "Northamptonshire", + "level2short": "Northamptonshire" + }, + "city": "Northampton", + "country": "United Kingdom", + "countryCode": "GB", + "extra": { + "confidence": 0.7, + "establishment": "Daventy depot", + "googlePlaceId": "ChIJI8H0WFUVd0gRIIFzNwDQAuM", + "neighborhood": "Kilsby", + "premise": null, + "subpremise": null + }, + "formattedAddress": "Daventy depot, Kilsby, Northampton NN6 7GY, UK", + "latitude": 52.3546726, + "longitude": -1.1741823, + "provider": "google", + "zipcode": "NN6 7GY" + } +] + */ diff --git a/server/reducers/opencage.js b/server/reducers/opencage.js new file mode 100644 index 0000000..f39371d --- /dev/null +++ b/server/reducers/opencage.js @@ -0,0 +1,57 @@ +const logger = require('log4js').getLogger('GeoCode 🔧'); + +const { get, isEmpty, has, uniq } = require('lodash'); + +logger.level = 'debug'; + +var ConfidenceInKM = { + '10': 0.25, + '9': 0.5, + '8': 1, + '7': 5, + '6': 7.5, + '5': 10, + '4': 15, + '3': 20, + '2': 25, + '1': Number.POSITIVE_INFINITY, + '0': Number.NaN +}; + +function formatResult (result) { + var confidence = result.confidence || 0; + + return { + 'latitude': result.geometry.lat, + 'longitude': result.geometry.lng, + 'country': result.components.country, + 'city': result.components.city, + 'state': result.components.state, + 'zipcode': result.components.postcode, + 'streetName': result.components.road, + 'streetNumber': result.components.house_number, + 'countryCode': result.components.country_code, + 'county': result.components.county, + 'suburb': result.components.suburb || '', + 'extra': { + 'flag' : result.annotations.flag, + 'confidence': confidence, + 'confidenceKM': ConfidenceInKM[result.confidence] || Number.NaN, + 'map' : result.annotations.OSM.url + + } + }; +} + +function reduceOpencage(result) { + + var results = []; + + if (result && result.results instanceof Array) + for (var i = 0; i < result.results.length; i++) + results.push(formatResult(result.results[i])); + + return results; +} + +module.exports = { reduceOpencage }; diff --git a/server/reducers/rightbyme.js b/server/reducers/rightbyme.js index f46274b..b5c5b40 100644 --- a/server/reducers/rightbyme.js +++ b/server/reducers/rightbyme.js @@ -58,7 +58,7 @@ function reduceYelp(data) { const urlBit = url[2]; obj.viewIntent = `https://m.yelp.com/${urlBit}`; } - else + else obj.viewIntent = ''; return obj; @@ -71,14 +71,14 @@ function reduceFullFS(data) { const photoBlob = get(localObj, 'photos.groups'); let photoItems; - for (const i of photoBlob) - if (i.type === 'venue') + for (const i of photoBlob) + if (i.type === 'venue') photoItems = i.items; - const photosCount = photoItems.length; + const photosCount = (typeof(photoItems) !== 'undefined' && photoItems !== null) ? photoItems.length : 0; const tipsCount = get(localObj, 'tips.count', 0); - if (photosCount > 0) + if (photosCount > 0) obj.images = photoItems.map(item => { const prefix = get(item, 'prefix', ''); @@ -106,7 +106,7 @@ function reduceFullFS(data) { function reduceTwitter(data) { let obj = []; - if (data.length > 0) + if (data.length > 0) obj = data.map(item => { return get(item, 'text'); }); diff --git a/src/service-worker.js b/src/service-worker.js index 4b7873b..4f1b16c 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const CACHE_VERSION = { 'version': '0.0.933' }; +const CACHE_VERSION = { 'version': '0.0.934' }; const PRECACHE = `jubileeData-${CACHE_VERSION.version}`; const RUNTIME = 'runtime'; diff --git a/src/v1/js/Location.js b/src/v1/js/Location.js index 04a4c2f..909a6f5 100644 --- a/src/v1/js/Location.js +++ b/src/v1/js/Location.js @@ -86,14 +86,28 @@ const LocationModel = Backbone.Model.extend({ // const current = this.get('location'); const current = this.toJSON(); // console.log('># current', current); - const options = { + /*const options = { 'provider': 'google', // Optional depending on the providers 'httpAdapter': 'https', // Default 'apiKey': 'AIzaSyA7oGP6QS28tTwtT6UzA7hzh0b3MWwMYB8', // for Mapquest, OpenCage, Google Premier 'formatter': null // 'gpx', 'string', ... - }; + };*/ + + + // opencage 893ab539eca84b5ca7a54cb03ef23443 + + const options = { + 'provider': 'opencage', + + // Optional depending on the providers + 'httpAdapter': 'https', // Default + 'apiKey': '893ab539eca84b5ca7a54cb03ef23443', // for Mapquest, OpenCage, Google Premier + 'formatter': null // 'gpx', 'string', ... + }; + + const myCoords = { 'home': { 'lat':51.490002, 'long':-0.140245 diff --git a/src/v1/js/VenueDetail.js b/src/v1/js/VenueDetail.js index ee4511f..08db84d 100644 --- a/src/v1/js/VenueDetail.js +++ b/src/v1/js/VenueDetail.js @@ -130,7 +130,9 @@ const VenueDetailView = Backbone.View.extend({ 'zoom': 15 }); - L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', { + // L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', { + L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { + 'attribution': 'Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © Mapbox', 'maxZoom': 18, 'id': 'mapbox.streets',