From c4daf1b377c82d1b87ef6d5c9839cbabdd4ae600 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Fri, 8 Jan 2021 17:05:37 -0500 Subject: [PATCH] Use locationManager to resolve/query community index --- modules/ui/success.js | 77 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/modules/ui/success.js b/modules/ui/success.js index 051eaa22e..b1cc29f51 100644 --- a/modules/ui/success.js +++ b/modules/ui/success.js @@ -1,11 +1,10 @@ import { dispatch as d3_dispatch } from 'd3-dispatch'; import { select as d3_select } from 'd3-selection'; -import LocationConflation from '@ideditor/location-conflation'; -import whichPolygon from 'which-polygon'; - import { fileFetcher } from '../core/file_fetcher'; +import { locationManager } from '../core/locations'; import { t, localizer } from '../core/localizer'; + import { svgIcon } from '../svg/icon'; import { uiDisclosure } from '../ui/disclosure'; import { utilRebind } from '../util/rebind'; @@ -23,36 +22,34 @@ export function uiSuccess(context) { function ensureOSMCommunityIndex() { const data = fileFetcher; - return Promise.all([ data.get('oci_resources'), data.get('oci_features') ]) + return Promise.all([ + data.get('oci_features'), + data.get('oci_resources') + ]) .then(vals => { if (_oci) return _oci; - const ociResources = vals[0].resources; - const loco = new LocationConflation(vals[1]); - let ociFeatures = {}; + // Merge Custom Features + if (vals[0] && Array.isArray(vals[0].features)) { + locationManager.mergeCustomGeoJSON(vals[0]); + } - Object.values(ociResources).forEach(resource => { - let feature; - try { - feature = loco.resolveLocationSet(resource.locationSet).feature; - let ociFeature = ociFeatures[feature.id]; - if (!ociFeature) { - ociFeature = JSON.parse(JSON.stringify(feature)); // deep clone - ociFeature.properties.resourceIDs = new Set(); - ociFeatures[feature.id] = ociFeature; - } - ociFeature.properties.resourceIDs.add(resource.id); - } catch (err) { - /* ignore communities with an unresolvable locationSet */ - console.warn(`warning: skipping community resource ${resource.id}: ${err.message}`); // eslint-disable-line no-console - } - }); + const ociResources = Object.values(vals[1].resources); + const locationSets = ociResources.map(r => r.locationSet); - return _oci = { - features: ociFeatures, - resources: ociResources, - query: whichPolygon({ type: 'FeatureCollection', features: Object.values(ociFeatures) }) - }; + // 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) + .then(() => { + ociResources.forEach(r => r.locationSetID = locationManager.locationSetID(r.locationSet)); + _oci = { resources: ociResources }; + return _oci; + }); + } else { + _oci = { resources: [] }; // no resources? + return _oci; + } }); } @@ -156,19 +153,19 @@ export function uiSuccess(context) { // Get OSM community index features intersecting the map.. ensureOSMCommunityIndex() .then(oci => { - let communities = []; - const properties = oci.query(context.map().center(), true) || []; + const loc = context.map().center(); + const validLocations = locationManager.locationsAt(loc); - // Gather the communities from the result - properties.forEach(props => { - const resourceIDs = Array.from(props.resourceIDs); - resourceIDs.forEach(resourceID => { - const resource = oci.resources[resourceID]; - communities.push({ - area: props.area || Infinity, - order: resource.order || 0, - resource: resource - }); + // Gather the communities + let communities = []; + oci.resources.forEach(resource => { + let area = validLocations[resource.locationSetID]; + if (!area) return; + + communities.push({ + area: area, + order: resource.order || 0, + resource: resource }); });