diff --git a/www/plugins/es/js/services/geo-services.js b/www/plugins/es/js/services/geo-services.js index 8e08e3c853243e6881419b36b351364d7da847c5..b338f93888622907022ccc9c881bf950d41c8ed6 100644 --- a/www/plugins/es/js/services/geo-services.js +++ b/www/plugins/es/js/services/geo-services.js @@ -18,8 +18,7 @@ angular.module('cesium.es.geo.services', ['cesium.services', 'cesium.es.http.ser that.raw = { osm: { - searchByString: csHttp.get('nominatim.openstreetmap.org', 443, '/search.php?format=json&q=:query'), - searchByQuery: csHttp.get('nominatim.openstreetmap.org', 443, '/search.php?format=json') + search: csHttp.get('nominatim.openstreetmap.org', 443, '/search.php?format=json') }, google: { apiKey: undefined, @@ -56,31 +55,16 @@ angular.module('cesium.es.geo.services', ['cesium.services', 'cesium.es.http.ser }); } - function searchPositionByString(address) { + function searchPositionByAddress(query) { - var now = new Date(); - console.debug('[ES] [geo] Searching position by string query [{0}]...'.format(address)); - - return that.raw.osm.searchByString({query: address}) - .then(function(res) { - console.debug('[ES] [geo] Found {0} address position(s) in {0}ms'.format(res && res.length || 0, new Date().getTime() - now.getTime())); - return res; - }) - - // Fallback service - .catch(function(err) { - return _fallbackSearchPositionByString(err, address); - }); - } - - function searchhPositionByQuery(query) { - - if (typeof query == 'string') return searchPositionByString(query); + if (typeof query == 'string') { + query = {q: query}; + } var now = new Date(); - console.debug('[ES] [geo] Searching position by query...', query); + //console.debug('[ES] [geo] Searching position...', query); - return that.raw.osm.searchByQuery(query) + return that.raw.osm.search(query) .then(function(res) { console.debug('[ES] [geo] Found {0} address position(s) in {0}ms'.format(res && res.length || 0, new Date().getTime() - now.getTime()), res); return res; @@ -88,7 +72,7 @@ angular.module('cesium.es.geo.services', ['cesium.services', 'cesium.es.http.ser // Fallback service .catch(function(err) { - var address = (query.street ? query.street +', ' : '') + query.city + (query.country ? ', '+ query.country : ''); + var address = query.q ? query.q : ((query.street ? query.street +', ' : '') + query.city + (query.country ? ', '+ query.country : '')); return _fallbackSearchPositionByString(err, address); }); } @@ -147,7 +131,7 @@ angular.module('cesium.es.geo.services', ['cesium.services', 'cesium.es.http.ser return { point: { current: getCurrentPosition, - searchByAddress: searchhPositionByQuery, + searchByAddress: searchPositionByAddress, searchByIP: searchPositionByIP }, google: { diff --git a/www/plugins/map/js/controllers/wot-controllers.js b/www/plugins/map/js/controllers/wot-controllers.js index ecd7d0c8d09a4d636f6597e11a59738f25ea080a..24e852ab451e51a0ac57f9f67d38e4e6e3b78a33 100644 --- a/www/plugins/map/js/controllers/wot-controllers.js +++ b/www/plugins/map/js/controllers/wot-controllers.js @@ -163,8 +163,14 @@ angular.module('cesium.map.wot.controllers', ['cesium.services', 'cesium.map.ser // Show loading indicator map.fire('dataloading'); + var options = { + fields: { + description: !UIUtils.screen.isSmall() + } + }; + // Load wot data - return mapWot.load() + return mapWot.load(options) .then(function(res) { var markers = {}; diff --git a/www/plugins/map/js/services/wot-services.js b/www/plugins/map/js/services/wot-services.js index 0246de649e15ca5df887ee86ff9165af43c9a8be..cffc91381ebae33dad6445f8c4704e7cd831f8b5 100644 --- a/www/plugins/map/js/services/wot-services.js +++ b/www/plugins/map/js/services/wot-services.js @@ -10,7 +10,7 @@ angular.module('cesium.map.wot.services', ['cesium.services']) DEFAULT_LOAD_SIZE: 1000 }, fields = { - profile: ["title", "geoPoint", "avatar._content_type", "address", "city", "description"] + profile: ["title", "geoPoint", "avatar._content_type", "address", "city"] }; that.raw = { @@ -64,11 +64,14 @@ angular.module('cesium.map.wot.services', ['cesium.services']) options.size = options.size || constants.DEFAULT_LOAD_SIZE; options.searchAddress = esGeo.google.isEnable() && (angular.isDefined(options.searchAddress) ? options.searchAddress : true); + options.fields = options.fields || {}; + options.fields.description = angular.isDefined(options.fields.description) ? options.fields.description : true; + var request = { query: createFilterQuery(options), - from: options.from, + from: 0, size: options.size, - _source: fields.profile + _source: options.fields.description ? fields.profile.concat("description") : fields.profile }; return $q.all([ @@ -104,86 +107,109 @@ angular.module('cesium.map.wot.services', ['cesium.services']) return res; }, {}); + var jobs = [ + processLoadHits(options, uids, memberships, res) + ]; + + // Additional slice requests + request.from += request.size; + while (request.from < res.hits.total) { + jobs.push(that.raw.profile.postSearch(angular.copy(request)) + .then(function(res) { + if (!res.hits || !res.hits.hits.length) return []; + return processLoadHits(options, uids, memberships, res); + })); + request.from += request.size; + } + return $q.all(jobs) + .then(function(res){ + return res.reduce(function(res, items) { + return res.concat(items); + }, []); + }); + }); + } - // Transform profile hits - var commaRegexp = new RegExp('[,]'); - var searchAddressItems = []; - var items = res.hits.hits.reduce(function(res, hit) { - var pubkey = hit._id; + function processLoadHits(options, uids, memberships, res) { + + // Transform profile hits + var commaRegexp = new RegExp('[,]'); + var searchAddressItems = []; + var items = res.hits.hits.reduce(function(res, hit) { + var pubkey = hit._id; + + var uid = uids[pubkey]; + var item = uid && {uid: uid} || memberships[pubkey] || {}; + item.pubkey = pubkey; + + // City & address + item.city = hit._source.city; + item.address = hit._source.address; + + // Set geo point + item.geoPoint = hit._source.geoPoint; + if (!item.geoPoint || !item.geoPoint.lat || !item.geoPoint.lon) { + if (!options.searchAddress || !item.city) return res; // no city: exclude this item + item.searchAddress = item.city && ((hit._source.address ? hit._source.address+ ', ' : '') + item.city); + searchAddressItems.push(item); + } + else { + // Convert lat/lon to float (if need) + if (item.geoPoint.lat && typeof item.geoPoint.lat === 'string') { + item.geoPoint.lat = parseFloat(item.geoPoint.lat.replace(commaRegexp, '.')); + } + if (item.geoPoint.lon && typeof item.geoPoint.lon === 'string') { + item.geoPoint.lon = parseFloat(item.geoPoint.lon.replace(commaRegexp, '.')); + } + } - var uid = uids[pubkey]; - var item = uid && {uid: uid} || memberships[pubkey] || {}; - item.pubkey = pubkey; + // Avatar + item.avatar = esHttp.image.fromHit(hit, 'avatar'); - // City & address - item.city = hit._source.city; + // Name + item.name = hit._source.title; + // Avoid too long name (workaround for #308) + if (item.name && item.name.length > 30) { + item.name = item.name.substr(0, 27) + '...'; + } - // Set geo point - item.geoPoint = hit._source.geoPoint; - if (!item.geoPoint || !item.geoPoint.lat || !item.geoPoint.lon) { - if (!options.searchAddress || !item.city) return res; // no city: exclude this item - item.searchAddress = item.city && ((hit._source.address ? hit._source.address+ ', ' : '') + item.city); - searchAddressItems.push(item); - } - else { - // Convert lat/lon to float (if need) - if (item.geoPoint.lat && typeof item.geoPoint.lat === 'string') { - item.geoPoint.lat = parseFloat(item.geoPoint.lat.replace(commaRegexp, '.')); - } - if (item.geoPoint.lon && typeof item.geoPoint.lon === 'string') { - item.geoPoint.lon = parseFloat(item.geoPoint.lon.replace(commaRegexp, '.')); - } - } + // Description + item.description = hit._source.description && esHttp.util.trustAsHtml(hit._source.description); - // Avatar - item.avatar = esHttp.image.fromHit(hit, 'avatar'); + return item.geoPoint ? res.concat(item) : res; + }, []); - // Name - item.name = hit._source.title; - // Avoid too long name (workaround for #308) - if (item.name && item.name.length > 30) { - item.name = item.name.substr(0, 27) + '...'; - } + // Resolve missing positions by addresses (only if google API enable) + if (searchAddressItems.length) { + var now = new Date().getTime(); + console.log('[map] [wot] Search positions of {0} addresses...'.format(searchAddressItems.length)); + var counter = 0; - // Description - item.description = esHttp.util.trustAsHtml(hit._source.description); - - return item.geoPoint ? res.concat(item) : res; - }, []); - - // Resolve missing positions by addresses (only if google API enable) - if (searchAddressItems.length) { - var now = new Date().getTime(); - console.log('[map] [wot] Search positions of {0} addresses...'.format(searchAddressItems.length)); - var counter = 0; - - return $q.all(searchAddressItems.reduce(function(res, item) { - return !item.searchAddress ? res : res.concat(esGeo.google.searchByAddress(item.searchAddress) - .then(function(res) { - if (!res || !res.length) return; - item.geoPoint = res[0]; - // If search on city, add a randomized delta to avoid superposition - if (item.city == item.searchAddress) { - item.geoPoint.lon += Math.random() / 1000; - item.geoPoint.lat += Math.random() / 1000; - } - delete item.searchAddress; // not need anymore - items.push(item); - counter++; - }) - .catch(function() {/*silent*/})); - }, [])) - .then(function(){ - console.log('[map] [wot] Resolved {0}/{1} addresses in {2}ms'.format(counter, searchAddressItems.length, new Date().getTime()-now)); - return items; - }); - } + return $q.all(searchAddressItems.reduce(function(res, item) { + return !item.city ? res : res.concat(esGeo.google.searchByAddress(item.searchAddress) + .then(function(res) { + if (!res || !res.length) return; + item.geoPoint = res[0]; + // If search on city, add a randomized delta to avoid superposition + if (item.city == item.searchAddress) { + item.geoPoint.lon += Math.random() / 1000; + item.geoPoint.lat += Math.random() / 1000; + } + delete item.searchAddress; // not need anymore + items.push(item); + counter++; + }) + .catch(function() {/*silent*/})); + }, [])) + .then(function(){ + console.log('[map] [wot] Resolved {0}/{1} addresses in {2}ms'.format(counter, searchAddressItems.length, new Date().getTime()-now)); + return items; + }); + } - return items; - }); + return $q.when(items); } - return { load: load };