Skip to content

Commit

Permalink
Fix kartaview
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Oct 20, 2024
1 parent caf35ad commit 7b4b117
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/services/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface FetchOpts extends RequestInit {
nocache?: boolean;
}

export const fetchText = async (url, opts: FetchOpts = {}) => {
export const fetchText = async (url: string, opts: FetchOpts = {}) => {
const key = getKey(url, opts);
const item = getCache(key);
if (item) return item;
Expand Down
11 changes: 6 additions & 5 deletions src/services/images/getImageFromCenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const subtractAngle = (a: number, b: number): number =>
type ImageProvider<T> = {
getImages: (pos: Position) => Promise<T[]>;
getImageCoords: (img: T) => Position;
getImageAngle: (img: T) => number;
getImageAngle: (img: T) => number | undefined;
isPano: (img: T) => boolean;
getImageUrl: (img: T) => string;
getImageDate: (img: T) => Date;
Expand Down Expand Up @@ -44,13 +44,14 @@ export const getImageFromCenterFactory =

const photos = apiImages.map((pic) => {
const photoCoords = getImageCoords(pic);
const angle = getImageAngle(pic);
return {
...pic,
angleFromPhotoToPoi: getBearing(photoCoords, poiCoords),
deviationFromStraightSight: subtractAngle(
getBearing(photoCoords, poiCoords),
getImageAngle(pic),
),
deviationFromStraightSight:
angle === undefined
? Infinity
: subtractAngle(getBearing(photoCoords, poiCoords), angle),
};
});

Expand Down
16 changes: 10 additions & 6 deletions src/services/images/getkartaViewImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type KartaViewImage = {
match_lng: string;
way_id: string;
shot_date: string;
heading: string;
heading: string | null;
headers: any;
gps_accuracy: any;
projection: string;
Expand All @@ -43,8 +43,9 @@ export const getKartaViewImage = getImageFromCenterFactory('KartaView', {
top: poiCoords[1] + 0.0004,
};

const apiImages = await fetchJson<KartaViewResponse>(
`${KARTAVIEW_BASE}/1.0/list/nearby-photos/`,
const apiResponse = await fetchJson<KartaViewResponse>(
// bbox in url to prevent nextjs caching
`${KARTAVIEW_BASE}/1.0/list/nearby-photos/?bbox=${bbox.left},${bbox.top}`,
{
method: 'POST',
body: new URLSearchParams({
Expand All @@ -55,12 +56,15 @@ export const getKartaViewImage = getImageFromCenterFactory('KartaView', {
}),
},
);
return apiImages.currentPageItems;
return apiResponse.currentPageItems;
},
getImageCoords: ({ lng, lat }) => [parseInt(lng), parseInt(lat)],
getImageAngle: ({ heading }) => parseInt(heading),
getImageAngle: ({ heading }) => (heading ? parseInt(heading) : undefined),
isPano: () => false,
getImageUrl: ({ name }) => `${KARTAVIEW_BASE}/${name}`,
getImageUrl: ({ lth_name }) => {
const [storage, uri] = lth_name.split(/\/(.*)/);
return `https://${storage}.openstreetcam.org/${uri}`;
},
getImageDate: ({ timestamp }) => new Date(parseInt(timestamp) * 1000),
getImageLink: ({ id }) => id,
getImageLinkUrl: ({ sequence_id, sequence_index }) =>
Expand Down

0 comments on commit 7b4b117

Please sign in to comment.