Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
fix(gallery): resolve EXIF coordinates consistently across maps
Timothy Yinf1bf60b0d370
diff --git a/packages/gallery/app/components/photo/InfoPanel.vue b/packages/gallery/app/components/photo/InfoPanel.vueindex 4f6c7ca1..0eed0b0c 100644--- a/packages/gallery/app/components/photo/InfoPanel.vue+++ b/packages/gallery/app/components/photo/InfoPanel.vue@@ -1,6 +1,9 @@<script setup lang="ts">import { computed } from 'vue'-import { hasValidCoordinates } from '@chronoframe/gallery/shared/utils/location'+import {+ hasValidCoordinates,+ resolvePhotoCoordinates,+} from '@chronoframe/gallery/shared/utils/location'import { albumPagePath } from '@chronoframe/gallery/shared/utils/albumPath'import { motion } from 'motion-v'import type { NeededExif } from '../../../shared/types/photo'@@ -101,30 +104,9 @@ const formatGPSCoordinatesMultiLine = (return `${latDeg}°${latMin}'${latSec.toFixed(2)}"${latDirection}\n${lngDeg}°${lngMin}'${lngSec.toFixed(2)}"${lngDirection}`}-const gpsCoordinates = computed(() => {- // 优先使用数据库中存储的坐标- if (- hasValidCoordinates(- props.currentPhoto.latitude,- props.currentPhoto.longitude,- )- ) {- return {- latitude: props.currentPhoto.latitude!,- longitude: props.currentPhoto.longitude!,- }- }-- // 如果数据库中没有,尝试从EXIF数据中获取- if (!props.exifData) return null- const { GPSLatitude, GPSLongitude } = props.exifData- if (GPSLatitude != null && GPSLongitude != null) {- const latitude = parseFloat(`${GPSLatitude}`)- const longitude = parseFloat(`${GPSLongitude}`)- if (hasValidCoordinates(latitude, longitude)) return { latitude, longitude }- }- return null-})+const gpsCoordinates = computed(() =>+ resolvePhotoCoordinates({ ...props.currentPhoto, exif: props.exifData }),+)const formatedExifData = computed<Record<string, KVData[]>>(() => {const sections: Record<string, KVData[]> = {}diff --git a/packages/gallery/app/pages/globe.vue b/packages/gallery/app/pages/globe.vueindex ff4482db..8e3ddd8b 100644--- a/packages/gallery/app/pages/globe.vue+++ b/packages/gallery/app/pages/globe.vue@@ -1,5 +1,5 @@<script lang="ts" setup>-import { hasValidCoordinates } from '@chronoframe/gallery/shared/utils/location'+import { resolvePhotoCoordinates } from '@chronoframe/gallery/shared/utils/location'import { motion } from 'motion-v'import { clusterMarkers, photosToMarkers } from '#gallery/utils/clustering'@@ -14,9 +14,10 @@ const dayjs = useDayjs()const { photos } = usePhotos()const photosWithLocation = computed(() => {- return photos.value.filter((photo) =>- hasValidCoordinates(photo.latitude, photo.longitude),- )+ return photos.value.flatMap((photo) => {+ const coordinates = resolvePhotoCoordinates(photo)+ return coordinates ? [{ ...photo, ...coordinates }] : []+ })})const timelineProgress = ref(100)diff --git a/packages/gallery/shared/utils/location.ts b/packages/gallery/shared/utils/location.tsindex a9fa0544..4180f59e 100644--- a/packages/gallery/shared/utils/location.ts+++ b/packages/gallery/shared/utils/location.ts@@ -1,3 +1,4 @@+import { hasValidCoordinates } from '@chronoframe/contracts/location'import {locationLevels,type LocationLevel,@@ -27,3 +28,28 @@ export function resolveLocationLabel(}export { hasValidCoordinates } from '@chronoframe/contracts/location'++/** Resolve both persisted coordinates and legacy photos whose GPS exists only in EXIF. */+export function resolvePhotoCoordinates(photo: {+ latitude?: number | null+ longitude?: number | null+ exif?: {+ GPSLatitude?: unknown+ GPSLongitude?: unknown+ GPSLatitudeRef?: unknown+ GPSLongitudeRef?: unknown+ } | null+}): { latitude: number; longitude: number } | null {+ if (hasValidCoordinates(photo.latitude, photo.longitude)) {+ return { latitude: photo.latitude!, longitude: photo.longitude! }+ }+ const exif = photo.exif+ if (exif?.GPSLatitude == null || exif.GPSLongitude == null) return null+ let latitude = Number.parseFloat(String(exif.GPSLatitude))+ let longitude = Number.parseFloat(String(exif.GPSLongitude))+ if (exif.GPSLatitudeRef === 'S') latitude = -Math.abs(latitude)+ if (exif.GPSLongitudeRef === 'W') longitude = -Math.abs(longitude)+ return hasValidCoordinates(latitude, longitude)+ ? { latitude, longitude }+ : null+}diff --git a/tests/photo-map-coordinates.test.ts b/tests/photo-map-coordinates.test.tsnew file mode 100644index 00000000..09303452--- /dev/null+++ b/tests/photo-map-coordinates.test.ts@@ -0,0 +1,51 @@+import test from 'node:test'+import assert from 'node:assert/strict'+import { resolvePhotoCoordinates } from '../packages/gallery/shared/utils/location.ts'++test('large and mini maps resolve legacy EXIF GPS with hemisphere references', () => {+ assert.deepEqual(+ resolvePhotoCoordinates({+ latitude: null,+ longitude: null,+ exif: {+ GPSLatitude: 31.2,+ GPSLongitude: 121.5,+ },+ }),+ { latitude: 31.2, longitude: 121.5 },+ )+ assert.deepEqual(+ resolvePhotoCoordinates({+ exif: {+ GPSLatitude: '33.8',+ GPSLongitude: '151.2',+ GPSLatitudeRef: 'S',+ GPSLongitudeRef: 'W',+ },+ }),+ { latitude: -33.8, longitude: -151.2 },+ )+})++test('persisted coordinates take precedence and absent or invalid GPS stays hidden', () => {+ assert.deepEqual(+ resolvePhotoCoordinates({+ latitude: 10,+ longitude: 20,+ exif: {+ GPSLatitude: 30,+ GPSLongitude: 40,+ },+ }),+ { latitude: 10, longitude: 20 },+ )+ for (const exif of [+ null,+ {},+ { GPSLatitude: 91, GPSLongitude: 20 },+ { GPSLatitude: 0, GPSLongitude: 0 },+ { GPSLatitude: 'invalid', GPSLongitude: 20 },+ ]) {+ assert.equal(resolvePhotoCoordinates({ exif }), null)+ }+})