import { BASE } from './basemap' import { buildings, catColor, catLabel, catOrder } from './categories' import { m2ll } from './geo' import { ruianUseName } from './i18n' import { drawLegend, drawScene, legendLayout, pxPerMeter } from './render' import { base, hidden, S, showBase, vp } from './state' import type { Ring } from './types' /* ================= export ================= */ function download(blob: Blob, name: string) { const a = document.createElement('a') a.href = URL.createObjectURL(blob) a.download = name document.body.appendChild(a) a.click() a.remove() setTimeout(() => URL.revokeObjectURL(a.href), 4000) } export function exportPng() { if (!S.loaded) return const { W, H } = vp, r = 2, lh = legendLayout(W).h const oc = document.createElement('canvas') oc.width = W * r oc.height = (H + lh) * r const c = oc.getContext('2d')! drawScene(c, W, H, S.view, r, {}) drawLegend(c, H, W, r) oc.toBlob((b) => { if (b) download(b, 'building-use-map.png') }, 'image/png') } export function exportSvg() { if (!S.loaded) return const { W, H } = vp, v = S.view, ppm = pxPerMeter(v) const sx = (x: number) => ((x - v.cx) * v.s + W / 2).toFixed(1), sy = (y: number) => (H / 2 - (y - v.cy) * v.s).toFixed(1) const d = (rings: Ring[], close: boolean) => rings .map((r) => 'M' + r.map((p) => sx(p[0]) + ' ' + sy(p[1])).join('L') + (close ? 'Z' : '')) .join('') const { e, colW, rows, h: lh } = legendLayout(W) let o = '' o += `` o += `` if (showBase.value) for (const st of BASE) { const rings = base.value[st.k] if (!rings || !rings.length) continue if (st.t === 'poly') o += `` else o += `` } for (const key of catOrder()) { if (hidden.value.has(key)) continue const rings: Ring[] = [] for (const b of buildings.value) if (b.cat === key) for (const r of b.rings) rings.push(r) if (rings.length) o += `` } o += '' o += `` e.forEach((it, i) => { const c = Math.floor(i / rows), rr = i % rows, x = 14 + c * colW, y = H + 14 + rr * 20 o += `${it.label.replace(/&/g, '&').replace(/` }) o += '' download(new Blob([o], { type: 'image/svg+xml' }), 'building-use-map.svg') } export function exportCsv() { if (!S.loaded) return const q = (s: unknown) => '"' + String(s).replace(/"/g, '""') + '"' let o = 'house_no,ruian_use_code,ruian_use,category,flats,floors,built_up_area_m2,completed,lat,lon\n' for (const b of buildings.value) { const ll = m2ll((b.bb[0] + b.bb[2]) / 2, (b.bb[1] + b.bb[3]) / 2) o += [ q(b.cp), b.use, q(ruianUseName(b.use)), q(catLabel(b.cat)), b.flats ?? '', b.floors ?? '', b.area ?? '', b.year ?? '', ll[1].toFixed(6), ll[0].toFixed(6), ].join(',') + '\n' } download(new Blob(['' + o], { type: 'text/csv;charset=utf-8' }), 'buildings.csv') // BOM so Excel reads UTF-8 }