@@ -73,10 +73,14 @@ export class PresetSystem extends AbstractSystem {
7373 }
7474 }
7575
76- const assets = this . context . systems . assets ;
77- const urlhash = this . context . systems . urlhash ;
76+ const context = this . context ;
77+ const assets = context . systems . assets ;
78+ const locations = context . systems . locations ;
79+ const urlhash = context . systems . urlhash ;
80+
7881 const prerequisites = Promise . all ( [
7982 assets . initAsync ( ) ,
83+ locations . initAsync ( ) ,
8084 urlhash . initAsync ( )
8185 ] ) ;
8286
@@ -143,7 +147,7 @@ export class PresetSystem extends AbstractSystem {
143147 merge ( src ) {
144148 let newLocationSets = [ ] ;
145149 const context = this . context ;
146- const locationSystem = context . systems . locations ;
150+ const locations = context . systems . locations ;
147151
148152 // Merge Fields
149153 if ( src . fields ) {
@@ -246,12 +250,12 @@ if (c.icon) c.icon = c.icon.replace(/^iD-/, 'rapid-');
246250
247251 // Merge Custom Features
248252 if ( src . featureCollection && Array . isArray ( src . featureCollection . features ) ) {
249- locationSystem . mergeCustomGeoJSON ( src . featureCollection ) ;
253+ locations . mergeCustomGeoJSON ( src . featureCollection ) ;
250254 }
251255
252256 // Resolve all locationSet features.
253257 if ( newLocationSets . length ) {
254- locationSystem . mergeLocationSets ( newLocationSets ) ;
258+ locations . mergeLocationSets ( newLocationSets ) ;
255259 }
256260
257261 return this ;
@@ -294,29 +298,36 @@ if (c.icon) c.icon = c.icon.replace(/^iD-/, 'rapid-');
294298 */
295299 matchTags ( tags , geometry , loc ) {
296300 const keyIndex = this . _geometryIndex [ geometry ] ;
301+ const context = this . context ;
302+ const locations = context . systems . locations ;
303+
304+ // If we care about location, gather the locationSets allowed at this location
305+ const validHere = Array . isArray ( loc ) ? locations . locationSetsAt ( loc ) : null ;
306+
297307 let bestScore = - 1 ;
298- let bestMatch ;
308+ let bestMatch = null ;
299309 let matchCandidates = [ ] ;
300310
301311 for ( let k in tags ) {
302- let indexMatches = [ ] ;
303-
304- let valueIndex = keyIndex [ k ] ;
312+ const valueIndex = keyIndex [ k ] ;
305313 if ( ! valueIndex ) continue ;
306314
307- let keyValueMatches = valueIndex [ tags [ k ] ] ;
315+ const indexMatches = [ ] ;
316+ const keyValueMatches = valueIndex [ tags [ k ] ] ;
308317 if ( keyValueMatches ) indexMatches . push ( ...keyValueMatches ) ;
309- let keyStarMatches = valueIndex [ '*' ] ;
318+ const keyStarMatches = valueIndex [ '*' ] ;
310319 if ( keyStarMatches ) indexMatches . push ( ...keyStarMatches ) ;
311320
312321 if ( indexMatches . length === 0 ) continue ;
313322
314- for ( let i = 0 ; i < indexMatches . length ; i ++ ) {
315- const candidate = indexMatches [ i ] ;
323+ for ( const candidate of indexMatches ) {
316324 const score = candidate . matchScore ( tags ) ;
317325 if ( score === - 1 ) continue ;
318326
319- matchCandidates . push ( { score, candidate} ) ;
327+ // Exclude candidate if it is scoped to a location not valid here
328+ if ( validHere && candidate . locationSetID && ! validHere [ candidate . locationSetID ] ) continue ;
329+
330+ matchCandidates . push ( { score, candidate } ) ;
320331
321332 if ( score > bestScore ) {
322333 bestScore = score ;
@@ -325,22 +336,6 @@ if (c.icon) c.icon = c.icon.replace(/^iD-/, 'rapid-');
325336 }
326337 }
327338
328- const locationSystem = this . context . systems . locations ;
329- if ( bestMatch && bestMatch . locationSetID && bestMatch . locationSetID !== '+[Q2]' && Array . isArray ( loc ) ) {
330- const validHere = locationSystem . locationSetsAt ( loc ) ;
331- if ( ! validHere [ bestMatch . locationSetID ] ) {
332- matchCandidates . sort ( ( a , b ) => ( a . score < b . score ) ? 1 : - 1 ) ;
333- for ( let i = 0 ; i < matchCandidates . length ; i ++ ) {
334- const candidateScore = matchCandidates [ i ] ;
335- if ( ! candidateScore . candidate . locationSetID || validHere [ candidateScore . candidate . locationSetID ] ) {
336- bestMatch = candidateScore . candidate ;
337- bestScore = candidateScore . score ;
338- break ;
339- }
340- }
341- }
342- }
343-
344339 // If any part of an address is present, allow fallback to "Address" preset - iD#4353
345340 if ( ! bestMatch || bestMatch . isFallback ( ) ) {
346341 for ( let k in tags ) {
@@ -526,8 +521,8 @@ if (c.icon) c.icon = c.icon.replace(/^iD-/, 'rapid-');
526521 // If a location was provided, filter results to only those valid here.
527522 let arr = [ ...results . values ( ) ] ;
528523 if ( Array . isArray ( loc ) ) {
529- const locationSystem = this . context . systems . locations ;
530- const validHere = locationSystem . locationSetsAt ( loc ) ;
524+ const locations = this . context . systems . locations ;
525+ const validHere = locations . locationSetsAt ( loc ) ;
531526 arr = arr . filter ( item => ! item . locationSetID || validHere [ item . locationSetID ] ) ;
532527 }
533528
0 commit comments