- Sidebar, legend, status, info panel and tooltip are Preact components (src/components) - App state that the UI or map depends on is held in @preact/signals; the map redraws through a signal effect, pan/zoom still redraw directly - Classification is a computed signal (buildings) instead of manual classifyAll calls - index.html is just a mount point; CSS is imported from main.tsx Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
4 KiB
TypeScript
102 lines
4 KiB
TypeScript
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 += `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H + lh}" viewBox="0 0 ${W} ${H + lh}"><defs><clipPath id="c"><rect width="${W}" height="${H}"/></clipPath></defs>`
|
||
o += `<rect width="${W}" height="${H}" fill="#f5f2ea"/><g clip-path="url(#c)" stroke-linejoin="round" stroke-linecap="round">`
|
||
if (showBase.value)
|
||
for (const st of BASE) {
|
||
const rings = base.value[st.k]
|
||
if (!rings || !rings.length) continue
|
||
if (st.t === 'poly')
|
||
o += `<path d="${d(rings, true)}" fill="${st.fill}" fill-rule="evenodd"${st.stroke ? ` stroke="${st.stroke}" stroke-width="1"` : ''}/>`
|
||
else
|
||
o += `<path d="${d(rings, false)}" fill="none" stroke="${st.c}" stroke-width="${Math.max(st.min, st.m * ppm).toFixed(1)}"${st.dash ? ` stroke-dasharray="${st.dash.map((x) => x * Math.max(1, ppm / 2)).join(' ')}"` : ''}/>`
|
||
}
|
||
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 += `<path d="${d(rings, true)}" fill="${catColor(key)}" fill-rule="evenodd" stroke="${key === 'res' || key === 'u7' ? '#ffffff88' : '#00000088'}" stroke-width=".5"/>`
|
||
}
|
||
o += '</g>'
|
||
o += `<rect y="${H}" width="${W}" height="${lh}" fill="#fff"/><g font-family="system-ui,sans-serif" font-size="12">`
|
||
e.forEach((it, i) => {
|
||
const c = Math.floor(i / rows),
|
||
rr = i % rows,
|
||
x = 14 + c * colW,
|
||
y = H + 14 + rr * 20
|
||
o += `<rect x="${x}" y="${y - 6}" width="18" height="12" fill="${it.color}" stroke="${it.border || '#555'}"/><text x="${x + 26}" y="${y + 4}">${it.label.replace(/&/g, '&').replace(/</g, '<')}</text>`
|
||
})
|
||
o += '</g></svg>'
|
||
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
|
||
}
|