Skip to content

Add aggregating geocoder - #369

Open
sdiltsDNRC wants to merge 1 commit into
perliedman:masterfrom
sdiltsDNRC:feat/aggregating-geocoder
Open

Add aggregating geocoder#369
sdiltsDNRC wants to merge 1 commit into
perliedman:masterfrom
sdiltsDNRC:feat/aggregating-geocoder

Conversation

@sdiltsDNRC

Copy link
Copy Markdown

This adds the AggregatingGeocoder class, which combines the results of several geocoders together.

Questions / Observations:

  • I couldn't think of a good name for this geocoder, I'm happy to update it to something better.
  • What documentation should I provide/
  • Should the default options argument have a default geocoder?

I'll add actual tests if this looks like a good solution.

See #368.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AggregatingGeocoder and a aggregatingGeocoder factory for combining multiple IGeocoder implementations.
  • 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 thread README.md
Comment on lines +30 to +31
Multiple data providers can be combined by using the AggregatingGeocoder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants