Skip to content

Commit

Permalink
Have mergeLocationSets work on Objects, add locationSetID property
Browse files Browse the repository at this point in the history
This means less work for the other parts of the code,
now they don't need to chain that extra step after the promise.
  • Loading branch information
bhousel committed Jan 9, 2021
1 parent c4daf1b commit 868db88
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
41 changes: 34 additions & 7 deletions modules/core/locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function coreLocations() {
let _loco = new LocationConflation(); // instance of a location-conflation resolver
let _wp; // instance of a which-polygon index

resolveLocationSet({ include: ['Q2'] }); // pre-resolve the worldwide locationSet
// pre-resolve the worldwide locationSet
const world = { locationSet: { include: ['Q2'] } };
resolveLocationSet(world);
rebuildIndex();

let _queue = [];
Expand All @@ -53,8 +55,14 @@ export function coreLocations() {
.then(() => processQueue());
}

function resolveLocationSet(locationSet) {
function resolveLocationSet(obj) {
if (obj.locationSetID) return; // work was done already

try {
const locationSet = obj.locationSet;
if (!locationSet) {
throw new Error('object missing locationSet property');
}
const resolved = _loco.resolveLocationSet(locationSet);
const locationSetID = resolved.id;
if (!resolved.feature.geometry.coordinates.length || !resolved.feature.properties.area) {
Expand All @@ -66,7 +74,10 @@ export function coreLocations() {
feature.properties.id = locationSetID;
_resolvedFeatures[locationSetID] = feature; // insert into cache
}
} catch (err) { /* ignore? */ }
} catch (err) {
obj.locationSet = { include: ['Q2'] }; // default worldwide
obj.locationSetID = '+[Q2]';
}
}

function rebuildIndex() {
Expand Down Expand Up @@ -119,12 +130,28 @@ export function coreLocations() {

//
// `mergeLocationSets`
// Accepts an Array of locationSets to merge into the index
// Accepts an Array of Objects containing `locationSet` properties.
// The locationSets will be resolved and indexed in the background.
// [
// { id: 'preset1', locationSet: {…} },
// { id: 'preset2', locationSet: {…} },
// { id: 'preset3', locationSet: {…} },
// …
// ]
// After resolving and indexing, the Objects will be decorated with a
// `locationSetID` property.
// [
// { id: 'preset1', locationSet: {…}, locationSetID: '+[Q2]' },
// { id: 'preset2', locationSet: {…}, locationSetID: '+[Q30]' },
// { id: 'preset3', locationSet: {…}, locationSetID: '+[Q2]' },
// …
// ]
//
// Returns a Promise fullfilled when the resolving/indexing has been completed
// This will take some seconds but happen in the background during browser idle time
//
_this.mergeLocationSets = (locationSets) => {
if (!Array.isArray(locationSets)) return Promise.reject('nothing to do');
_this.mergeLocationSets = (objects) => {
if (!Array.isArray(objects)) return Promise.reject('nothing to do');

// Resolve all locationSets -> geojson, processing data in chunks
//
Expand All @@ -136,7 +163,7 @@ export function coreLocations() {
// Some discussion and performance results on these tickets:
// https://github.com/ideditor/location-conflation/issues/26
// https://github.com/osmlab/name-suggestion-index/issues/4784#issuecomment-742003434
_queue = _queue.concat(utilArrayChunk(locationSets, 200));
_queue = _queue.concat(utilArrayChunk(objects, 200));

// Everything after here will be deferred.
if (!_inProcess) {
Expand Down
15 changes: 3 additions & 12 deletions modules/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ export function presetIndex() {
// featureCollection: {}
//}
_this.merge = (d) => {
let newFields = [];
let newPresets = [];
let newLocationSets = [];

// Merge Fields
Expand All @@ -100,8 +98,7 @@ export function presetIndex() {
if (f) { // add or replace
f = presetField(fieldID, f);
if (f.locationSet) {
newFields.push(f);
newLocationSets.push(f.locationSet);
newLocationSets.push(f);
} else {
f.locationSet = { include: ['Q2'] }; // default worldwide
f.locationSetID = '+[Q2]';
Expand All @@ -123,8 +120,7 @@ export function presetIndex() {
const isAddable = !_addablePresetIDs || _addablePresetIDs.has(presetID);
p = presetPreset(presetID, p, isAddable, _fields, _presets);
if (p.locationSet) {
newPresets.push(p);
newLocationSets.push(p.locationSet);
newLocationSets.push(p);
} else {
p.locationSet = { include: ['Q2'] }; // default worldwide
p.locationSetID = '+[Q2]';
Expand Down Expand Up @@ -195,13 +191,8 @@ export function presetIndex() {
}

// Resolve all locationSet features.
// When done, assign the locationSetIDs (we use these to quickly test where the preset/field is valid).
if (newLocationSets.length) {
locationManager.mergeLocationSets(newLocationSets)
.then(() => {
newFields.forEach(f => f.locationSetID = locationManager.locationSetID(f.locationSet));
newPresets.forEach(p => p.locationSetID = locationManager.locationSetID(p.locationSet));
});
locationManager.mergeLocationSets(newLocationSets);
}

return _this;
Expand Down
12 changes: 4 additions & 8 deletions modules/ui/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ export function uiSuccess(context) {
locationManager.mergeCustomGeoJSON(vals[0]);
}

const ociResources = Object.values(vals[1].resources);
const locationSets = ociResources.map(r => r.locationSet);

// Resolve all locationSet features.
// When done, assign the locationSetIDs (we use these to quickly test where each community is valid).
if (locationSets.length) {
return locationManager.mergeLocationSets(locationSets)
let ociResources = Object.values(vals[1].resources);
if (ociResources.length) {
// Resolve all locationSet features.
return locationManager.mergeLocationSets(ociResources)
.then(() => {
ociResources.forEach(r => r.locationSetID = locationManager.locationSetID(r.locationSet));
_oci = { resources: ociResources };
return _oci;
});
Expand Down

0 comments on commit 868db88

Please sign in to comment.