-
Notifications
You must be signed in to change notification settings - Fork 148
fixed duplicate geocode #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { describe, it, expect, beforeEach } from 'vitest'; | ||
| import { parseGeocodeResponse } from './nominatim'; | ||
| import type { NominationResponse } from '@/components/types'; | ||
|
|
||
| describe('parseGeocodeResponse', () => { | ||
| let counter = 0; | ||
|
|
||
| beforeEach(() => { | ||
| counter = 0; | ||
| }); | ||
|
|
||
| const makeResult = (name: string, lat?: string, lon?: string) => | ||
| ({ | ||
| display_name: name, | ||
| lat: lat ?? `${++counter}.0000`, | ||
| lon: lon ?? `${++counter}.0000`, | ||
| osm_type: 'node', | ||
| osm_id: counter, | ||
| }) as unknown as NominationResponse; | ||
|
|
||
| it('returns all results when there are no duplicates', () => { | ||
| const results = [makeResult('Place A'), makeResult('Place B')]; | ||
|
|
||
| const processed = parseGeocodeResponse(results); | ||
| expect(processed).toHaveLength(2); | ||
| expect(processed[0]!.title).toBe('Place A'); | ||
| expect(processed[1]!.title).toBe('Place B'); | ||
| }); | ||
|
|
||
| it('removes entries that have identical coordinates and same name after diacritic normalization', () => { | ||
| const lat = `${++counter}.0000`; | ||
| const lon = `${++counter}.0000`; | ||
|
|
||
| const results = [ | ||
| makeResult('Pláce C', lat, lon), | ||
| makeResult('Place C', lat, lon), | ||
| makeResult('PLÁCE C', lat, lon), | ||
| ]; | ||
|
|
||
| const processed = parseGeocodeResponse(results); | ||
| expect(processed).toHaveLength(1); | ||
| expect(processed[0]!.title).toBe('Pláce C'); | ||
| }); | ||
|
|
||
| it('keeps entries that have the same name but genuinely different coordinates', () => { | ||
| const results = [ | ||
| makeResult('Place D'), | ||
| makeResult('Place D'), | ||
| makeResult('Place D'), | ||
| ]; | ||
|
|
||
| const processed = parseGeocodeResponse(results); | ||
| expect(processed).toHaveLength(3); | ||
| expect(processed[0]!.title).toBe('Place D'); | ||
| expect(processed[1]!.title).toBe('Place D'); | ||
| expect(processed[2]!.title).toBe('Place D'); | ||
| }); | ||
|
|
||
| it('handles a single non-array result without throwing', () => { | ||
| const result = makeResult('Place E'); | ||
|
|
||
| const processed = parseGeocodeResponse(result); | ||
| expect(processed).toHaveLength(1); | ||
| expect(processed[0]!.title).toBe('Place E'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ export const parseGeocodeResponse = ( | |
| } | ||
|
|
||
| const processedResults = []; | ||
| const seenKeys = new Set<string>(); | ||
|
|
||
| for (const [index, result] of results.entries()) { | ||
| if ( | ||
| 'error' in result && | ||
|
|
@@ -48,6 +50,19 @@ export const parseGeocodeResponse = ( | |
| addressindex: index, | ||
| }); | ||
| } else { | ||
| const normalizedTitle = result.display_name | ||
| .toLowerCase() | ||
| .normalize('NFD') | ||
| .replace(/[\u0300-\u036f]/g, ''); | ||
|
Comment on lines
+53
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you leave some explanation what this represents?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually normalizes the dedup comparison, toLowercase makes it case insensitive , normalize('NFD') for characters like é and regex operation removes those . Eg: Île-de-France and Ile-de-France are same |
||
| const dedupeKey = result.boundingbox | ||
| ? `${normalizedTitle}${result.boundingbox.join(',')}` | ||
| : `${normalizedTitle}${result.lat}${result.lon}`; | ||
|
|
||
| if (seenKeys.has(dedupeKey)) { | ||
| continue; | ||
| } | ||
| seenKeys.add(dedupeKey); | ||
|
Comment on lines
+61
to
+64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but there has to be some sort of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah but seenKeys also does the same function :)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha ok then follow up question: why you check for "has"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the key is already in the set, we skip it - if not, we add it. that's it
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's the whole point of a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops you are right, we dont want .has() here , we can use .add() to keep it simple
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. processedResults.push({
title:
result.display_name.length > 0
? result.display_name
: lngLat?.toString() || '',
description: `https://www.openstreetmap.org/${result.osm_type}/${result.osm_id}`,
selected: false,
addresslnglat: [parseFloat(result.lon), parseFloat(result.lat)],
sourcelnglat:
lngLat === undefined
? [parseFloat(result.lon), parseFloat(result.lat)]
: lngLat,
displaylnglat:
lngLat !== undefined
? lngLat
: [parseFloat(result.lon), parseFloat(result.lat)],
key: index,
addressindex: index,
});but wait, the job of |
||
|
|
||
| processedResults.push({ | ||
| title: | ||
| result.display_name.length > 0 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated whitespace change, revert pls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure