export interface GeoPoint { lat: number lon: number } export interface ProjectedPoint { x: number y: number } export function createProjection( points: GeoPoint[], width = 1200, height = 800, padding = 80, ): (point: GeoPoint) => ProjectedPoint { const lats = points.map((point) => point.lat) const lons = points.map((point) => point.lon) const minLat = Math.min(...lats) const maxLat = Math.max(...lats) const minLon = Math.min(...lons) const maxLon = Math.max(...lons) const spanLat = Math.max(maxLat - minLat, 0.0001) const spanLon = Math.max(maxLon - minLon, 0.0001) const innerW = width - padding * 2 const innerH = height - padding * 2 return (point: GeoPoint) => ({ x: padding + ((point.lon - minLon) / spanLon) * innerW, y: padding + ((maxLat - point.lat) / spanLat) * innerH, }) } export function pathFromCoordinates( coordinates: Array<[number, number]>, project: (point: GeoPoint) => ProjectedPoint, ): string { return coordinates .map(([lon, lat], index) => { const point = project({ lat, lon }) return `${index === 0 ? 'M' : 'L'} ${point.x.toFixed(1)} ${point.y.toFixed(1)}` }) .join(' ') }