MapComplete/src/UI/Test.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
3.9 KiB
Svelte
Raw Normal View History

<script lang="ts">
2024-11-24 23:19:17 +01:00
import FileSelector from "./Base/FileSelector.svelte"
import { UIEventSource } from "../Logic/UIEventSource"
import ExifReader from "exifreader"
import Constants from "../Models/Constants"
import { AuthorizedPanoramax, ImageData } from "panoramax-js"
import PanoramaxImageProvider from "../Logic/ImageProviders/Panoramax"
let log = new UIEventSource<string[]>(["Select a file to test..."])
function l(...txt: ReadonlyArray<string | number>) {
console.log(...txt)
log.set([...log.data, txt.join(" ")])
}
async function onSubmit(fs: FileList) {
const f = fs[0]
l("Files are", f.name)
let [lon, lat] = [3.5, 51.2]
let datetime = new Date().toISOString()
try {
l("Trying to read EXIF-data from the file...")
const tags = await ExifReader.load(f)
l("Exif data loaded")
l("GPSLatitude.value is :", JSON.stringify(tags?.GPSLatitude.value))
l("GPSLongitude.value is :", JSON.stringify(tags?.GPSLongitude.value))
const [[latD], [latM], [latS, latSDenom]] = <
[[number, number], [number, number], [number, number]]
2024-11-28 12:00:23 +01:00
>tags?.GPSLatitude?.value
2024-11-24 23:19:17 +01:00
const [[lonD], [lonM], [lonS, lonSDenom]] = <
[[number, number], [number, number], [number, number]]
2024-11-28 12:00:23 +01:00
>tags?.GPSLongitude?.value
const exifLat = latD + latM / 60 + latS / (3600 * latSDenom)
const exifLon = lonD + lonM / 60 + lonS / (3600 * lonSDenom)
2024-12-10 02:38:00 +01:00
const directValueLat = tags?.GPSLatitude?.description
const directValueLon = tags?.GPSLongitude?.description
2024-11-28 12:00:23 +01:00
if (
typeof exifLat === "number" &&
!isNaN(exifLat) &&
typeof exifLon === "number" &&
!isNaN(exifLon) &&
!(exifLat === 0 && exifLon === 0)
) {
lat = exifLat
lon = exifLon
2024-12-11 02:45:44 +01:00
if (tags?.GPSLatitudeRef?.value?.[0] === "S") {
2024-12-10 02:38:00 +01:00
lat *= -1
}
2024-12-11 02:45:44 +01:00
if (tags?.GPSLongitudeRef?.value?.[0] === "W") {
2024-12-10 02:38:00 +01:00
lon *= -1
}
l("Using EXIFLAT + EXIFLON")
2024-11-28 12:00:23 +01:00
} else {
l("NOT using exifLat and exifLon: invalid value detected")
}
2024-11-24 23:19:17 +01:00
l("Lat and lon are", lat, lon)
2024-12-11 02:45:44 +01:00
l(
"ref lat is",
tags?.GPSLatitudeRef?.description,
JSON.stringify(tags?.GPSLatitudeRef?.value)
)
l(
"ref lon is",
tags?.GPSLongitudeRef?.description,
JSON.stringify(tags?.GPSLongitudeRef?.value)
)
2024-12-10 02:38:00 +01:00
2024-12-11 02:45:44 +01:00
l("Direct values are", directValueLat, directValueLon, "corrected:", lat, lon)
2024-11-24 23:19:17 +01:00
l("Datetime value is", JSON.stringify(tags.DateTime))
const [date, time] = tags.DateTime.value[0].split(" ")
datetime = new Date(date.replaceAll(":", "-") + "T" + time).toISOString()
l("Datetime parsed is", datetime)
console.log("Tags are", tags)
} catch (e) {
console.error("Could not read EXIF-tags")
l("Could not read the exif tags:", e, JSON.stringify(e))
}
try {
2024-11-28 12:00:23 +01:00
const p = new AuthorizedPanoramax(Constants.panoramax.url, Constants.panoramax.token)
2024-11-24 23:19:17 +01:00
const sequenceId = "7f34cf53-27ff-46c9-ac22-78511fa8457a" // test-sequence
l("Fetching sequence number...")
const sequence: { id: string; "stats:items": { count: number } } = (
await p.mySequences()
).find((s) => s.id === sequenceId)
l("Sequence number is", sequence["stats:items"].count, "now attempting upload")
const img = <ImageData>await p.addImage(f, sequence, {
lon,
lat,
datetime,
isBlurred: false,
exifOverride: {
2024-11-28 12:00:23 +01:00
Artist: "TEST ACCOUNT",
2024-11-24 23:19:17 +01:00
},
})
l("Upload completed. Adding meta")
PanoramaxImageProvider.singleton.addKnownMeta(img)
l("Meta added")
} catch (e) {
l("Error while uploading:", e)
}
}
</script>
2024-11-24 23:19:17 +01:00
<FileSelector accept="image/jpg" multiple={false} on:submit={(f) => onSubmit(f.detail)}>
<div class="border border-black p-1">Select file</div>
</FileSelector>
<div class="flex flex-col">
{#each $log as logl}
<div>{logl}</div>
{/each}
</div>