Add aggregating geocoder - #369
Open
sdiltsDNRC wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds an AggregatingGeocoder to the geocoder module set, enabling a single geocoder instance to query multiple underlying geocoders and combine their results (addressing the “multiple geocoder sources” need from #368).
Changes:
- Introduces
AggregatingGeocoderand aaggregatingGeocoderfactory for combining multipleIGeocoderimplementations. - Exports the new geocoder from the geocoders barrel (
src/geocoders/index.ts). - Adds initial Vitest coverage and a brief README mention for the new feature.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| src/geocoders/index.ts | Re-exports the new aggregating geocoder module. |
| src/geocoders/aggregating-geocoder.ts | Adds the AggregatingGeocoder implementation and factory function. |
| spec/aggregating-geocoder.spec.ts | Adds initial tests for the new geocoder. |
| README.md | Mentions that multiple providers can be combined. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+5
to
+7
| export interface AgregatorOptions { | ||
| geocoders: IGeocoder[]; | ||
| } |
Comment on lines
+13
to
+19
| options: AgregatorOptions = { | ||
| geocoders: [] | ||
| } | ||
|
|
||
| constructor(options?: Partial<AgregatorOptions>) { | ||
| L.Util.setOptions(this, options) | ||
| } |
Comment on lines
+21
to
+28
| private async combineResults(queries: Array<Promise<GeocodingResult[]>>): Promise<GeocodingResult[]> { | ||
| const queryResults: Array<GeocodingResult[]> = await Promise.all(queries); | ||
| const results: Array<GeocodingResult> = []; | ||
| for(const r of queryResults) { | ||
| results.push(...r); | ||
| } | ||
| return results; | ||
| } |
Comment on lines
+38
to
+56
| suggest?(query: string, context?: GeocodingContext): Promise<GeocodingResult[]> { | ||
| const queries: Promise<GeocodingResult[]>[] = []; | ||
| for(const geocoder of this.options.geocoders) { | ||
| if (geocoder.suggest) { | ||
| queries.push(geocoder.suggest(query, context)); | ||
| } | ||
| } | ||
| return this.combineResults(queries); | ||
| } | ||
|
|
||
| reverse?(location: LatLngLiteral, scale: number): Promise<GeocodingResult[]> { | ||
| const queries: Promise<GeocodingResult[]>[] = []; | ||
| for(const geocoder of this.options.geocoders) { | ||
| if (geocoder.reverse) { | ||
| queries.push(geocoder.reverse(location, scale)); | ||
| } | ||
| } | ||
| return this.combineResults(queries); | ||
| } |
Comment on lines
+63
to
+65
| export function aggregatingGeocoder(options: Partial<AgregatorOptions>) { | ||
| return new AggregatingGeocoder(options); | ||
| } |
| @@ -0,0 +1,39 @@ | |||
| import { describe, expect, it, vi } from 'vitest'; | |||
| import { LatLngLiteral } from 'leaflet'; | ||
|
|
||
| describe('L.Control.Geocoder.AggregatingGeocoder', () => { | ||
| it('geocde returns empty when no geocoders', async () => { |
Comment on lines
+16
to
+39
| it('suggest returns empty when no geocoders', async () => { | ||
| const options: AgregatorOptions = { | ||
| geocoders: [] | ||
| } | ||
| const geocoder = new AggregatingGeocoder(options); | ||
|
|
||
| const results = await geocoder.suggest('foo'); | ||
| expect(results).toEqual([]); | ||
| }); | ||
|
|
||
| it('reverse returns emtpy when no geocoders', async () => { | ||
| const options: AgregatorOptions = { | ||
| geocoders: [] | ||
| } | ||
| const geocoder = new AggregatingGeocoder(options); | ||
|
|
||
| const loc: LatLngLiteral = { | ||
| lat: 0, | ||
| lng: 0 | ||
| }; | ||
| const results = await geocoder.reverse(loc, 1); | ||
| expect(results).toEqual([]); | ||
| }); | ||
| }); |
Comment on lines
+30
to
+31
| Multiple data providers can be combined by using the AggregatingGeocoder. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds the
AggregatingGeocoderclass, which combines the results of several geocoders together.Questions / Observations:
I'll add actual tests if this looks like a good solution.
See #368.