mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
refactor(server): reverse geocoding (#2167)
* refactor(server): reverse geocoding * fix: nullable results
This commit is contained in:
44
server/libs/infra/src/repositories/geocoding.repository.ts
Normal file
44
server/libs/infra/src/repositories/geocoding.repository.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { GeoPoint, ReverseGeocodeResult } from '@app/domain';
|
||||
import { localGeocodingConfig } from '@app/infra';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { getName } from 'i18n-iso-countries';
|
||||
import geocoder, { AddressObject, InitOptions } from 'local-reverse-geocoder';
|
||||
import { promisify } from 'util';
|
||||
|
||||
export interface AdminCode {
|
||||
name: string;
|
||||
asciiName: string;
|
||||
geoNameId: string;
|
||||
}
|
||||
|
||||
export type GeoData = AddressObject & {
|
||||
admin1Code?: AdminCode | string;
|
||||
admin2Code?: AdminCode | string;
|
||||
};
|
||||
|
||||
const init = (options: InitOptions): Promise<void> => new Promise<void>((resolve) => geocoder.init(options, resolve));
|
||||
const lookup = promisify<GeoPoint[], number, AddressObject[][]>(geocoder.lookUp).bind(geocoder);
|
||||
|
||||
@Injectable()
|
||||
export class GeocodingRepository {
|
||||
private logger = new Logger(GeocodingRepository.name);
|
||||
|
||||
async init(): Promise<void> {
|
||||
await init(localGeocodingConfig);
|
||||
}
|
||||
|
||||
async reverseGeocode(point: GeoPoint): Promise<ReverseGeocodeResult> {
|
||||
this.logger.debug(`Request: ${point.latitude},${point.longitude}`);
|
||||
|
||||
const [address] = await lookup([point], 1);
|
||||
this.logger.verbose(`Raw: ${JSON.stringify(address, null, 2)}`);
|
||||
|
||||
const { countryCode, name: city, admin1Code, admin2Code } = address[0] as GeoData;
|
||||
const country = getName(countryCode, 'en');
|
||||
const stateParts = [(admin2Code as AdminCode)?.name, (admin1Code as AdminCode)?.name].filter((name) => !!name);
|
||||
const state = stateParts.length > 0 ? stateParts.join(', ') : null;
|
||||
this.logger.debug(`Normalized: ${JSON.stringify({ country, state, city })}`);
|
||||
|
||||
return { country, state, city };
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ export * from './communication.repository';
|
||||
export * from './crypto.repository';
|
||||
export * from './device-info.repository';
|
||||
export * from './filesystem.provider';
|
||||
export * from './geocoding.repository';
|
||||
export * from './job.repository';
|
||||
export * from './machine-learning.repository';
|
||||
export * from './media.repository';
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {
|
||||
IAssetJob,
|
||||
IAssetUploadedJob,
|
||||
IBaseJob,
|
||||
IJobRepository,
|
||||
IMetadataExtractionJob,
|
||||
JobCounts,
|
||||
JobItem,
|
||||
JobName,
|
||||
@@ -30,7 +30,7 @@ export class JobRepository implements IJobRepository {
|
||||
@InjectQueue(QueueName.BACKGROUND_TASK) private backgroundTask: Queue,
|
||||
@InjectQueue(QueueName.OBJECT_TAGGING) private objectTagging: Queue<IAssetJob | IBaseJob>,
|
||||
@InjectQueue(QueueName.CLIP_ENCODING) private clipEmbedding: Queue<IAssetJob | IBaseJob>,
|
||||
@InjectQueue(QueueName.METADATA_EXTRACTION) private metadataExtraction: Queue<IMetadataExtractionJob | IBaseJob>,
|
||||
@InjectQueue(QueueName.METADATA_EXTRACTION) private metadataExtraction: Queue<IAssetUploadedJob | IBaseJob>,
|
||||
@InjectQueue(QueueName.STORAGE_TEMPLATE_MIGRATION) private storageTemplateMigration: Queue,
|
||||
@InjectQueue(QueueName.THUMBNAIL_GENERATION) private generateThumbnail: Queue,
|
||||
@InjectQueue(QueueName.VIDEO_CONVERSION) private videoTranscode: Queue<IAssetJob | IBaseJob>,
|
||||
@@ -88,7 +88,6 @@ export class JobRepository implements IJobRepository {
|
||||
case JobName.QUEUE_METADATA_EXTRACTION:
|
||||
case JobName.EXIF_EXTRACTION:
|
||||
case JobName.EXTRACT_VIDEO_METADATA:
|
||||
case JobName.REVERSE_GEOCODING:
|
||||
await this.metadataExtraction.add(item.name, item.data);
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user