Skip to content
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

feat: extract City/State/Country from IPTC tags #15344

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/features/reverse-geocoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Immich supports local [Reverse Geocoding](https://en.wikipedia.org/wiki/Reverse_

During Exif Extraction, assets with latitudes and longitudes are reverse geocoded to determine their City, State, and Country.

Note that if your assets already contain any of the IPTC tags (`City`, `Country-PrimaryLocationName`, `Province-State`), the reverse geocoding will not be performed and these tag values will be used instead

## Usage

Data from a reverse geocode is displayed in the image details, and used in [Smart Search](/docs/features/searching.md).
Expand Down
6 changes: 6 additions & 0 deletions server/src/interfaces/metadata.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type TagsWithWrongTypes =
| 'Duration'
| 'Description'
| 'ImageDescription'
| 'City'
| 'State'
| 'Country'
| 'RegionInfo'
| 'TagsList'
| 'Keywords'
Expand All @@ -39,6 +42,9 @@ export interface ImmichTags extends Omit<Tags, TagsWithWrongTypes> {
// Type is wrong, can also be number.
Description?: StringOrNumber;
ImageDescription?: StringOrNumber;
City?: StringOrNumber;
State?: StringOrNumber;
Country?: StringOrNumber;

// Extended properties for image regions, such as faces
RegionInfo?: {
Expand Down
25 changes: 25 additions & 0 deletions server/src/services/metadata.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,31 @@ describe(MetadataService.name, () => {
});
});

it('should use existing tags instead of geocoding', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.withLocation]);
systemMock.get.mockResolvedValue({ reverseGeocoding: { enabled: true } });
mockReadTags({
GPSLatitude: assetStub.withLocation.exifInfo!.latitude!,
GPSLongitude: assetStub.withLocation.exifInfo!.longitude!,
City: 'City',
State: 'State',
Country: 'Country',
});

await sut.handleMetadataExtraction({ id: assetStub.image.id });
expect(assetMock.getByIds).toHaveBeenCalledWith([assetStub.image.id], { faces: { person: false } });
expect(assetMock.upsertExif).toHaveBeenCalledWith(
expect.objectContaining({ city: 'City', state: 'State', country: 'Country' }),
);
expect(assetMock.update).toHaveBeenCalledWith({
id: assetStub.withLocation.id,
duration: null,
fileCreatedAt: assetStub.withLocation.createdAt,
localDateTime: new Date('2023-02-22T05:06:29.716Z'),
});
expect(mapMock.reverseGeocode).not.toHaveBeenCalled();
});

it('should discard latitude and longitude on null island', async () => {
assetMock.getByIds.mockResolvedValue([assetStub.withLocation]);
mockReadTags({
Expand Down
7 changes: 7 additions & 0 deletions server/src/services/metadata.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,13 @@ export class MetadataService extends BaseService {
longitude = null;
}

const city = String(tags.City || '');
const state = String(tags.State || '');
const country = String(tags.Country || '');
if (city || state || country) {
return { city, state, country, latitude, longitude };
}

let result: ReverseGeocodeResult = { country: null, state: null, city: null };
if (reverseGeocoding.enabled && longitude && latitude) {
result = await this.mapRepository.reverseGeocode({ latitude, longitude });
Expand Down
Loading