여러 줄 주석이 설명보다 경위(예전·실측·지적)를 적고 있어 읽는 사람이 결론을 찾기 어려웠다. - ts·tsx·js·mjs·css·py 478개: 여러 줄 주석은 첫 문장 한 줄로, 과거형·날짜 문장은 삭제 - 주석 위치는 TypeScript 파서·파이썬 tokenize/ast 로 찾는다 — 문자열 안의 # · /* 는 건드리지 않는다 - eslint·ts·noqa·type: ignore 같은 지시 주석은 그대로 둔다 파이썬 275개 정리 전후 AST 동일, TS 298개 주석 뺀 토큰 동일(빈 JSX 주석 10곳만 차이). site·frontend·admin tsc, site vitest 105 passed Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
207 lines
7.5 KiB
TypeScript
207 lines
7.5 KiB
TypeScript
/** 정거장 지도 — 타일 <img> 를 격자로 깔고 그 위에 번호 핀과 경로선을 얹는다. */
|
|
import type {PlannerStop} from '@o2o/shared';
|
|
import {useSite} from '@site/lib/site-context';
|
|
import {naverDirectionsUrl, naverRouteUrl} from '@site/lib/format';
|
|
import {ITEM_ACCENT, ITEM_BORDER, ITEM_INVERSE_INK} from './common';
|
|
|
|
const TILE = 256;
|
|
/** 승차권 카드 안쪽 폭(320 − 좌우 테두리). */
|
|
const WIDTH = 318;
|
|
const HEIGHT = 168;
|
|
/** 가장자리 여백. */
|
|
const PAD = 34;
|
|
const MAX_ZOOM = 16;
|
|
const MIN_ZOOM = 4;
|
|
|
|
type XY = {x: number; y: number};
|
|
|
|
/** 위경도 → 그 배율에서의 세계 픽셀 좌표(웹 메르카토르). */
|
|
function project(lat: number, lng: number, zoom: number): XY {
|
|
const n = TILE * 2 ** zoom;
|
|
const rad = (lat * Math.PI) / 180;
|
|
return {
|
|
x: ((lng + 180) / 360) * n,
|
|
y: ((1 - Math.log(Math.tan(rad) + 1 / Math.cos(rad)) / Math.PI) / 2) * n,
|
|
};
|
|
}
|
|
|
|
/** 모든 점이 한 화면에 들어가는 가장 가까운 배율. */
|
|
function fitZoom(points: {lat: number; lng: number}[]): number {
|
|
if (points.length < 2) return 15;
|
|
for (let zoom = MAX_ZOOM; zoom > MIN_ZOOM; zoom -= 1) {
|
|
const xy = points.map((p) => project(p.lat, p.lng, zoom));
|
|
const w = Math.max(...xy.map((p) => p.x)) - Math.min(...xy.map((p) => p.x));
|
|
const h = Math.max(...xy.map((p) => p.y)) - Math.min(...xy.map((p) => p.y));
|
|
if (w <= WIDTH - PAD * 2 && h <= HEIGHT - PAD * 2) return zoom;
|
|
}
|
|
return MIN_ZOOM;
|
|
}
|
|
|
|
type Place = {name: string; lat: number; lng: number};
|
|
type Pin = {label: string; lat: number; lng: number; stop: PlannerStop; from?: Place};
|
|
|
|
/** 좌표가 있는 칸만 핀으로. */
|
|
function pinsOf(stops: PlannerStop[], origin?: Place): Pin[] {
|
|
// 길찾기 화면에 박히는 이름은 지도 검색어를 먼저 쓴다 — 시간표의 "저녁 · 빈해원" 은 손님에게 하는 말이지 지도에 넣을 상호가 아니다.
|
|
const at = (stop: PlannerStop): Place | undefined =>
|
|
stop.latitude != null && stop.longitude != null
|
|
? {name: stop.searchQuery ?? stop.name, lat: stop.latitude, lng: stop.longitude}
|
|
: undefined;
|
|
|
|
const pins: Pin[] = [];
|
|
let previous = origin;
|
|
stops.forEach((stop, index) => {
|
|
const here = at(stop);
|
|
if (!here) return;
|
|
// 앞 칸이 같은 자리면(체크인 칸이 업소인 날) 구간 길찾기가 제자리걸음이 된다 — 안 건다.
|
|
pins.push({
|
|
label: String(index + 1),
|
|
...here,
|
|
stop,
|
|
from: previous && !same(previous, here) ? previous : undefined,
|
|
});
|
|
previous = here;
|
|
});
|
|
return pins;
|
|
}
|
|
|
|
function same(a: Place, b: Place): boolean {
|
|
return Math.abs(a.lat - b.lat) < 1e-5 && Math.abs(a.lng - b.lng) < 1e-5;
|
|
}
|
|
|
|
/** 겹치는 핀을 떼어놓는다. */
|
|
function separate(points: XY[]): XY[] {
|
|
// 핀 지름이 22px 이라 중심 간격이 그보다 좁으면 뒤 핀이 앞 핀을 덮는다.
|
|
const GAP = 27;
|
|
const out = points.map((p) => ({...p}));
|
|
for (let i = 1; i < out.length; i += 1) {
|
|
for (let pass = 0; pass < 6; pass += 1) {
|
|
const hit = out
|
|
.slice(0, i)
|
|
.find((q) => Math.hypot(out[i].x - q.x, out[i].y - q.y) < GAP);
|
|
if (!hit) break;
|
|
const dx = out[i].x - hit.x;
|
|
const dy = out[i].y - hit.y;
|
|
// 완전히 같은 점이면 방향이 없다 — 오른쪽으로 민다.
|
|
const d = Math.hypot(dx, dy) || 1;
|
|
const push = (GAP - d) / d;
|
|
out[i].x += (dx || 1) * push;
|
|
out[i].y += dy * push;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** 핀을 누르면 **앞 칸에서 여기까지의 길찾기**가 열린다. */
|
|
function pinUrl(pin: Pin): string {
|
|
if (pin.from) return naverRouteUrl(pin.from, {name: pin.stop.name, lat: pin.lat, lng: pin.lng});
|
|
return naverDirectionsUrl(pin.stop.searchQuery ?? pin.stop.name, pin.lat, pin.lng);
|
|
}
|
|
|
|
export function RouteMap({stops}: {stops: PlannerStop[]}) {
|
|
const {place} = useSite();
|
|
const origin =
|
|
place.latitude != null && place.longitude != null
|
|
? {name: place.name, lat: place.latitude, lng: place.longitude}
|
|
: undefined;
|
|
|
|
const pinned = stops.filter((s) => s.latitude != null && s.longitude != null);
|
|
// 좌표가 있는 칸이 하나뿐인데 칸은 여럿이면 지도를 그리지 않는다.
|
|
if (pinned.length === 0 || (pinned.length === 1 && stops.length > 1)) return null;
|
|
|
|
const pins = pinsOf(stops, origin);
|
|
|
|
const zoom = fitZoom(pins);
|
|
const xy = separate(pins.map((p) => project(p.lat, p.lng, zoom)));
|
|
const cx = (Math.min(...xy.map((p) => p.x)) + Math.max(...xy.map((p) => p.x))) / 2;
|
|
const cy = (Math.min(...xy.map((p) => p.y)) + Math.max(...xy.map((p) => p.y))) / 2;
|
|
const originX = cx - WIDTH / 2;
|
|
const originY = cy - HEIGHT / 2;
|
|
|
|
const span = 2 ** zoom;
|
|
const tiles: {key: string; url: string; left: number; top: number}[] = [];
|
|
for (let tx = Math.floor(originX / TILE); tx <= Math.floor((originX + WIDTH) / TILE); tx += 1) {
|
|
for (let ty = Math.floor(originY / TILE); ty <= Math.floor((originY + HEIGHT) / TILE); ty += 1) {
|
|
if (ty < 0 || ty >= span) continue;
|
|
// 날짜변경선을 넘는 격자는 x 를 감아 준다.
|
|
const wrapped = ((tx % span) + span) % span;
|
|
tiles.push({
|
|
key: `${tx}-${ty}`,
|
|
url: `https://tile.openstreetmap.org/${zoom}/${wrapped}/${ty}.png`,
|
|
left: tx * TILE - originX,
|
|
top: ty * TILE - originY,
|
|
});
|
|
}
|
|
}
|
|
|
|
// 경로선은 핀을 순서대로 잇는다 — 떼어놓은 자리(xy)를 쓴다.
|
|
const line = xy.map((p) => `${(p.x - originX).toFixed(1)},${(p.y - originY).toFixed(1)}`).join(' ');
|
|
|
|
return (
|
|
<div
|
|
className="relative mt-3.5 overflow-hidden border-y"
|
|
style={{height: HEIGHT, borderColor: ITEM_BORDER, backgroundColor: '#e8e5df'}}
|
|
>
|
|
{tiles.map((tile) => (
|
|
<img
|
|
key={tile.key}
|
|
src={tile.url}
|
|
alt=""
|
|
width={TILE}
|
|
height={TILE}
|
|
loading="lazy"
|
|
decoding="async"
|
|
className="pointer-events-none absolute max-w-none"
|
|
style={{left: tile.left, top: tile.top, filter: 'grayscale(1) contrast(0.95) opacity(0.55)'}}
|
|
/>
|
|
))}
|
|
|
|
<svg
|
|
className="pointer-events-none absolute inset-0"
|
|
width={WIDTH}
|
|
height={HEIGHT}
|
|
viewBox={`0 0 ${WIDTH} ${HEIGHT}`}
|
|
>
|
|
<polyline
|
|
points={line}
|
|
fill="none"
|
|
stroke={ITEM_ACCENT}
|
|
strokeWidth="2"
|
|
strokeDasharray="5 4"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
opacity="0.85"
|
|
/>
|
|
</svg>
|
|
|
|
{pins.map((pin, index) => {
|
|
const p = xy[index];
|
|
return (
|
|
<a
|
|
key={pin.label}
|
|
href={pinUrl(pin)}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
title={pin.from ? `${pin.from.name} → ${pin.stop.name} 길찾기` : `${pin.stop.name} — 네이버 지도`}
|
|
className="absolute grid h-[22px] min-w-[22px] -translate-x-1/2 -translate-y-1/2 place-items-center rounded-full px-1.5 text-[length:var(--fs-xs)] font-bold whitespace-nowrap shadow-sm transition-transform hover:scale-110"
|
|
style={{
|
|
left: p.x - originX,
|
|
top: p.y - originY,
|
|
backgroundColor: ITEM_ACCENT,
|
|
color: ITEM_INVERSE_INK,
|
|
border: `1.5px solid ${ITEM_INVERSE_INK}`,
|
|
}}
|
|
>
|
|
{pin.label}
|
|
</a>
|
|
);
|
|
})}
|
|
|
|
{/* 타일 출처 표기는 OSM 이용 조건이다. */}
|
|
<span className="absolute right-1 bottom-0.5 bg-white/70 px-1 text-[length:var(--fs-xs)] leading-tight text-stone-600">
|
|
© OpenStreetMap
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|