Extracting GPS coordinates from exif

This commit is contained in:
Eric van der Vlist 2023-04-20 17:50:30 +02:00
parent d44f9a257e
commit 4df529abb7
2 changed files with 30 additions and 10 deletions

View File

@ -20,6 +20,7 @@ import {
Encoding, Encoding,
} from '@capacitor/filesystem'; } from '@capacitor/filesystem';
import piexif from 'piexifjs'; import piexif from 'piexifjs';
import { getDate, getGps } from '../../lib/exif';
interface Props { interface Props {
file: File; file: File;
@ -106,16 +107,9 @@ const ImportSingleFile: Component<Props> = ({ file: file }) => {
async () => { async () => {
const imageUrl = reader.result; const imageUrl = reader.result;
const exifObj = piexif.load(imageUrl); const exifObj = piexif.load(imageUrl);
const id = ''; const id = exifObj.Exif[piexif.ExifIFD.ImageUniqueID];
const gps = {}; const gps = getGps(exifObj);
const dateSegments = const date = getDate(exifObj);
exifObj.Exif[piexif.ExifIFD.DateTimeOriginal].split(' ');
const date = `${dateSegments[0].replaceAll(':', '-')}T${
dateSegments[1]
}.${exifObj.Exif[piexif.ExifIFD.SubSecTimeOriginal] || '0'}${
exifObj.Exif[piexif.ExifIFD.OffsetTimeOriginal]
}`;
// const id = exif?.ImageUniqueID?.value[0];
// const gps = tags.gps; // const gps = tags.gps;
// const image = await Image.load(reader.result); // const image = await Image.load(reader.result);
// const thumbnail = image.resize({ // const thumbnail = image.resize({

26
src/lib/exif.ts Normal file
View File

@ -0,0 +1,26 @@
import piexif from 'piexifjs';
export const getDate = (exifObj: any) => {
const dateSegments = exifObj.Exif[piexif.ExifIFD.DateTimeOriginal].split(' ');
const date = `${dateSegments[0].replaceAll(':', '-')}T${dateSegments[1]}.${
exifObj.Exif[piexif.ExifIFD.SubSecTimeOriginal] || '0'
}${exifObj.Exif[piexif.ExifIFD.OffsetTimeOriginal]}`;
return new Date(date);
};
export const getGps = (exifObj: any) => {
const toNumber = (start: number) => {
const sign = ['N', 'E'].includes(exifObj.GPS[start]) ? 1 : -1;
const value = exifObj.GPS[start + 1];
return (
sign *
(value[0][0] / value[0][1] +
(value[1][0] / value[1][1] + value[2][0] / value[2][1] / 60) / 60)
);
};
return {
latitude: toNumber(1),
longitude: toNumber(3),
elevation: exifObj.GPS[6][0] / exifObj.GPS[6][1],
};
};