Self-hosted house planner: true-scale 2D floor plan, 3D, satellite map overlay, sewer, electrical, water and underfloor heating with calculations, priced bill of materials. React + PHP, no database. By the team behind homedesignsai.pro
TypeScript 3darchitecturebill-of-materialscadconstructionelectrical-designfloor-planfloorplan-editorhome-designhouse-plannerkonvamaplibrephpplumbingreactreact-three-fiberself-hostedthreejstypescriptunderfloor-heating
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import * as maplibregl from 'maplibre-gl'
import type { GeoJSONSource, ImageSource, StyleSpecification } from 'maplibre-gl'
import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url'
import 'maplibre-gl/dist/maplibre-gl.css'
import type { Feature, FeatureCollection, Geometry, Polygon, Position } from 'geojson'
import type { Plan, Point } from '../model'
import {
bearingForPlanUp,
boundsOf,
enOf,
geoJSONToBoundary,
imageryFromDisplayedOrigin,
imageryOf,
isIdentityImagery,
lngLatBounds,
openingOutline,
planToImageryLngLat,
ringAreaM2,
roundGeo,
roundImagery,
siteFromGeoJSON,
siteRing,
trueLngLatToImagery,
trueToImageryEN,
wallOutline,
type Geo,
type Imagery,
type LngLat,
} from './geo'
import { useTranslation } from 'react-i18next'
import { fmtNum, t as tr } from '../i18n'
import { useViewOnly } from '../store/viewOnly'
import { Button, Checkbox, Input, Panel, Segmented, type SegmentedOption } from '../ui'
import './map.css'
type ConvertedSite = { geo: Geo; boundary: Point[]; width: number; depth: number }
const orientation = (): readonly SegmentedOption<'plan' | 'north'>[] => [
{ value: 'plan', label: tr('map:orientation.plan') },
{ value: 'north', label: tr('map:orientation.north') },
]
maplibregl.setWorkerUrl(workerUrl)
type Props = {
plan: Plan
onChange: (next: Plan) => void
}
const IMAGERY_URL = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'
const IMAGERY_MAXZOOM = 18
const EMPTY: FeatureCollection = { type: 'FeatureCollection', features: [] }
const WALL_COLOR = '#f26b1d'
const SITE_COLOR = '#3fd0ff'
const STREET_COLOR = '#ff3b6b'
const NEIGHBOR_COLOR = '#c084fc'
const NEIGHBOR_FALLBACK = '#e5e7eb'
const AXIS_X = '#ff4d6d'
const AXIS_Y = '#4dff88'
const LEGEND = [
{ key: 'site', color: SITE_COLOR, kind: 'solid' },
{ key: 'neighbor', color: NEIGHBOR_COLOR, kind: 'dash' },
{ key: 'street', color: STREET_COLOR, kind: 'thick' },
{ key: 'walls', color: WALL_COLOR, kind: 'fill' },
] as const
const DOOR_COLOR = '#ffd84a'
const WINDOW_COLOR = '#9be7ff'
const MAX_CANVAS = 4096
const MM_PER_PX = 10
const style: StyleSpecification = {
version: 8,
sources: {
imagery: {
type: 'raster',
tiles: [IMAGERY_URL],
tileSize: 256,
maxzoom: IMAGERY_MAXZOOM,
get attribution() {
return tr('map:attribution')
},
},
reference: { type: 'geojson', data: EMPTY },
boundary: { type: 'geojson', data: EMPTY },
axes: { type: 'geojson', data: EMPTY },
},
layers: [
{ id: 'imagery', type: 'raster', source: 'imagery' },
{
id: 'reference-line',
type: 'line',
source: 'reference',
paint: {
'line-color': ['match', ['get', 'role'], 'neighbor', NEIGHBOR_COLOR, 'parcel', SITE_COLOR, NEIGHBOR_FALLBACK],
'line-width': 2,
'line-dasharray': [3, 2],
},
filter: ['!=', ['get', 'role'], 'street'],
},
{ id: 'boundary-fill', type: 'fill', source: 'boundary', paint: { 'fill-color': SITE_COLOR, 'fill-opacity': 0.1 } },
{ id: 'boundary-line', type: 'line', source: 'boundary', paint: { 'line-color': SITE_COLOR, 'line-width': 2.5 } },
{
id: 'street-line',
type: 'line',
source: 'reference',
filter: ['==', ['get', 'role'], 'street'],
layout: { 'line-cap': 'round' },
paint: { 'line-color': STREET_COLOR, 'line-width': 6, 'line-opacity': 0.9 },
},
{
id: 'axes-line',
type: 'line',
source: 'axes',
paint: { 'line-color': ['match', ['get', 'axis'], 'x', AXIS_X, AXIS_Y], 'line-width': 2 },
},
],
}
type PlanImage = { url: string; coordinates: [LngLat, LngLat, LngLat, LngLat] } | null
function planCanvas(plan: Plan) {
const walls = new Map(plan.walls.map((w) => [w.id, w]))
const wallShapes = plan.walls.map(wallOutline)
const openingShapes = plan.openings.flatMap((o) => {
const w = walls.get(o.wallId)
return w ? [{ type: o.type, pts: openingOutline(o, w) }] : []
})
if (wallShapes.length === 0) return null
const b = boundsOf([...wallShapes.flat(), ...openingShapes.flatMap((s) => s.pts)])
const pad = 100
const box = { minX: b.minX - pad, minY: b.minY - pad, maxX: b.maxX + pad, maxY: b.maxY + pad }
const spanX = box.maxX - box.minX
const spanY = box.maxY - box.minY
const k = Math.min(1 / MM_PER_PX, MAX_CANVAS / Math.max(spanX, spanY))
const canvas = document.createElement('canvas')
canvas.width = Math.max(1, Math.ceil(spanX * k))
canvas.height = Math.max(1, Math.ceil(spanY * k))
const ctx = canvas.getContext('2d')
if (!ctx) return null
const path = (pts: Point[]) => {
ctx.beginPath()
pts.forEach((p, i) => {
const x = (p.x - box.minX) * k
const y = (p.y - box.minY) * k
if (i === 0) ctx.moveTo(x, y)
else ctx.lineTo(x, y)
})
ctx.closePath()
}
ctx.fillStyle = WALL_COLOR
for (const s of wallShapes) {
path(s)
ctx.fill()
}
for (const s of openingShapes) {
ctx.fillStyle = s.type === 'door' ? DOOR_COLOR : WINDOW_COLOR
path(s.pts)
ctx.fill()
}
return { url: canvas.toDataURL('image/png'), box }
}
function markerEl(className: string, text?: string) {
const el = document.createElement('div')
el.className = className
if (text) el.textContent = text
return el
}
function ringCenter(f: Feature): LngLat | null {
if (f.geometry?.type === 'LineString') {
const c = f.geometry.coordinates
return [(c[0][0] + c[c.length - 1][0]) / 2, (c[0][1] + c[c.length - 1][1]) / 2]
}
if (f.geometry?.type !== 'Polygon') return null
const ring = (f.geometry as Polygon).coordinates[0].slice(0, -1)
const s = ring.reduce((acc, p) => [acc[0] + p[0], acc[1] + p[1]], [0, 0])
return [s[0] / ring.length, s[1] / ring.length]
}
function fitPadding(container: HTMLElement | null, panel: HTMLElement | null) {
const pad = { top: 80, right: 60, bottom: 40, left: 60 }
if (!container || !panel) return pad
const c = container.getBoundingClientRect()
const r = panel.getBoundingClientRect()
if (r.width === 0 || r.height === 0) return pad
if (r.top > c.top + c.height / 2) pad.bottom = Math.min(c.height * 0.6, c.bottom - r.top + 24)
else pad.left = Math.min(c.width * 0.6, r.right - c.left + 24)
return pad
}
function mapCoords(g: Geometry, f: (p: Position) => Position): Geometry {
switch (g.type) {
case 'Point':
return { ...g, coordinates: f(g.coordinates) }
case 'LineString':
case 'MultiPoint':
return { ...g, coordinates: g.coordinates.map(f) }
case 'Polygon':
case 'MultiLineString':
return { ...g, coordinates: g.coordinates.map((r) => r.map(f)) }
case 'MultiPolygon':
return { ...g, coordinates: g.coordinates.map((pl) => pl.map((r) => r.map(f))) }
default:
return g
}
}
function displayCollection(fc: FeatureCollection, geo: Geo, im: Imagery): FeatureCollection {
if (isIdentityImagery(im)) return fc
const f = (p: Position) => trueLngLatToImagery(p, geo, im)
return { ...fc, features: fc.features.map((ft) => (ft.geometry ? { ...ft, geometry: mapCoords(ft.geometry, f) } : ft)) }
}
export default function MapView({ plan, onChange }: Props) {
const { t } = useTranslation('map')
const containerRef = useRef<HTMLDivElement>(null)
const mapRef = useRef<maplibregl.Map | null>(null)
const originMarker = useRef<maplibregl.Marker | null>(null)
const handleMarker = useRef<maplibregl.Marker | null>(null)
const [ready, setReady] = useState(false)
const [reference, setReference] = useState<FeatureCollection | null>(null)
const [draft, setDraft] = useState<Imagery | null>(null)
const viewOnly = useViewOnly()
const [collapsed, setCollapsed] = useState(() => typeof window !== 'undefined' && window.innerWidth < 560)
const panelRef = useRef<HTMLElement>(null)
const [opacity, setOpacity] = useState(0.85)
const [binding, setBinding] = useState(false)
const [planUp, setPlanUp] = useState(true)
const [copied, setCopied] = useState('')
const [geojsonText, setGeojsonText] = useState('')
const [autoGeo, setAutoGeo] = useState(true)
const [startIndex, setStartIndex] = useState(0)
const [convertError, setConvertError] = useState('')
const [converted, setConverted] = useState<ConvertedSite | null>(null)
const didFit = useRef(false)
const savedGeo = plan.site.geo ? roundGeo(plan.site.geo) : null
const geo = savedGeo
const savedIm = imageryOf(plan.site as { imagery?: Partial<Imagery> })
const { offsetE: iE, offsetN: iN, rotationDeg: iR } = draft ?? savedIm
const im = useMemo<Imagery>(() => ({ offsetE: iE, offsetN: iN, rotationDeg: iR }), [iE, iN, iR])
const imRef = useRef(im)
const geoRef = useRef(geo)
const planRef = useRef(plan)
const onChangeRef = useRef(onChange)
const opacityRef = useRef(opacity)
useLayoutEffect(() => {
geoRef.current = geo
imRef.current = im
planRef.current = plan
onChangeRef.current = onChange
opacityRef.current = opacity
})
const handleLength = Math.max(plan.site.width, plan.site.depth, 10000) * 0.6
const image = useMemo(() => planCanvas(plan), [plan])
const commit = useCallback((next: Imagery) => {
const p = planRef.current
setDraft(null)
onChangeRef.current({ ...p, site: { ...p.site, imagery: roundImagery(next) } })
}, [])
useEffect(() => {
let alive = true
fetch(`${import.meta.env.BASE_URL}data/parcel.geojson`)
.then((r) => (r.ok ? r.json() : null))
.then((d) => alive && d && setReference(d as FeatureCollection))
.catch(() => undefined)
return () => {
alive = false
}
}, [])
useEffect(() => {
if (!containerRef.current) return
const map = new maplibregl.Map({
container: containerRef.current,
style,
center: [24.8577, 59.3002],
zoom: 17,
maxZoom: 22,
attributionControl: { compact: true },
})
map.addControl(new maplibregl.NavigationControl({ visualizePitch: false }), 'top-right')
map.addControl(new maplibregl.ScaleControl({ unit: 'metric' }), 'bottom-left')
map.on('load', () => setReady(true))
mapRef.current = map
return () => {
map.remove()
mapRef.current = null
}
}, [])
const fit = useCallback(
(animate: boolean) => {
const map = mapRef.current
if (!map) return
const g = geoRef.current
const cur = imRef.current
const pts: LngLat[] = g
? siteRing(planRef.current).map((p) => planToImageryLngLat(p, g, cur))
: (reference?.features ?? []).flatMap((f) =>
f.geometry?.type === 'Polygon' ? (f.geometry.coordinates[0] as LngLat[]) : [],
)
if (pts.length === 0) return
const bearing = planUp && g ? bearingForPlanUp(g.rotationDeg - cur.rotationDeg) : 0
map.fitBounds(lngLatBounds(pts), { padding: fitPadding(containerRef.current, panelRef.current), maxZoom: 19.5, bearing, animate })
},
[planUp, reference],
)
useEffect(() => {
if (!ready || didFit.current) return
if (!geo && !reference) return
didFit.current = true
fit(false)
}, [ready, geo, reference, fit])
useEffect(() => {
const map = mapRef.current
if (!map || !ready) return
const g = geoRef.current
const shown = reference && g ? displayCollection(reference, g, imRef.current) : reference
;(map.getSource('reference') as GeoJSONSource).setData(shown ?? EMPTY)
const markers = (shown?.features ?? []).flatMap((f) => {
const c = ringCenter(f)
const label = f.properties?.label as string | undefined
if (!c || !label) return []
return [new maplibregl.Marker({ element: markerEl('map-view__tag', label) }).setLngLat(c).addTo(map)]
})
return () => markers.forEach((m) => m.remove())
}, [reference, ready, geo, im])
useEffect(() => {
const map = mapRef.current
if (!map || !ready) return
const boundary = map.getSource('boundary') as GeoJSONSource
const axes = map.getSource('axes') as GeoJSONSource
if (!geo) {
boundary.setData(EMPTY)
axes.setData(EMPTY)
if (map.getLayer('plan-walls')) map.removeLayer('plan-walls')
if (map.getSource('plan-image')) map.removeSource('plan-image')
return
}
const at = (p: Point) => planToImageryLngLat(p, geo, im)
const ring = siteRing(plan).map(at)
boundary.setData({ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [[...ring, ring[0]]] } })
const o = at({ x: 0, y: 0 })
axes.setData({
type: 'FeatureCollection',
features: binding && !viewOnly
? [
{ type: 'Feature', properties: { axis: 'x' }, geometry: { type: 'LineString', coordinates: [o, at({ x: handleLength, y: 0 })] } },
{ type: 'Feature', properties: { axis: 'y' }, geometry: { type: 'LineString', coordinates: [o, at({ x: 0, y: handleLength * 0.5 })] } },
]
: [],
})
const next: PlanImage = image
? {
url: image.url,
coordinates: [
at({ x: image.box.minX, y: image.box.minY }),
at({ x: image.box.maxX, y: image.box.minY }),
at({ x: image.box.maxX, y: image.box.maxY }),
at({ x: image.box.minX, y: image.box.maxY }),
],
}
: null
const src = map.getSource('plan-image') as ImageSource | undefined
if (!next) {
if (map.getLayer('plan-walls')) map.removeLayer('plan-walls')
if (src) map.removeSource('plan-image')
} else if (!src) {
map.addSource('plan-image', { type: 'image', url: next.url, coordinates: next.coordinates })
map.addLayer(
{ id: 'plan-walls', type: 'raster', source: 'plan-image', paint: { 'raster-opacity': opacityRef.current, 'raster-fade-duration': 0 } },
'axes-line',
)
} else if (src.url !== next.url) {
src.updateImage({ url: next.url, coordinates: next.coordinates })
} else {
src.setCoordinates(next.coordinates)
}
originMarker.current?.setLngLat(o)
handleMarker.current?.setLngLat(at({ x: handleLength, y: 0 }))
}, [plan, geo, im, image, ready, binding, handleLength, viewOnly])
useEffect(() => {
const map = mapRef.current
if (!map || !ready) return
if (map.getLayer('plan-walls')) map.setPaintProperty('plan-walls', 'raster-opacity', opacity)
map.setPaintProperty('boundary-line', 'line-opacity', Math.max(0.25, opacity))
map.setPaintProperty('boundary-fill', 'fill-opacity', 0.12 * opacity)
}, [opacity, ready])
useEffect(() => {
const map = mapRef.current
const g = geoRef.current
if (!map || !ready || !binding || !g || viewOnly) return
const start = imRef.current
const origin = new maplibregl.Marker({ element: markerEl('map-view__handle map-view__handle--origin', '0'), draggable: true })
.setLngLat(planToImageryLngLat({ x: 0, y: 0 }, g, start))
.addTo(map)
const handle = new maplibregl.Marker({ element: markerEl('map-view__handle map-view__handle--rotate', 'X'), draggable: true })
.setLngLat(planToImageryLngLat({ x: handleLength, y: 0 }, g, start))
.addTo(map)
const originEN = () => {
const ll = origin.getLngLat()
return enOf([ll.lng, ll.lat], g)
}
origin.on('drag', () => {
setDraft(imageryFromDisplayedOrigin(g, imRef.current, originEN()))
})
handle.on('drag', () => {
const q = originEN()
const h = handle.getLngLat()
const [he, hn] = enOf([h.lng, h.lat], g)
const axis = (Math.atan2(hn - q[1], he - q[0]) * 180) / Math.PI
setDraft(imageryFromDisplayedOrigin(g, imRef.current, q, axis))
})
const done = () => commit(imRef.current)
origin.on('dragend', done)
handle.on('dragend', done)
originMarker.current = origin
handleMarker.current = handle
return () => {
origin.remove()
handle.remove()
originMarker.current = null
handleMarker.current = null
}
}, [binding, ready, handleLength, commit, geo, viewOnly])
useEffect(() => {
const map = mapRef.current
if (!map || !ready) return
const g = geoRef.current
map.easeTo({ bearing: planUp && g ? bearingForPlanUp(g.rotationDeg - imRef.current.rotationDeg) : 0, duration: 400 })
}, [planUp, ready])
const nudge = (delta: number) => {
if (!geo) return
const q = trueToImageryEN(0, 0, im)
commit(imageryFromDisplayedOrigin(geo, im, q, geo.rotationDeg - im.rotationDeg + delta))
}
const imageryText = JSON.stringify({ imagery: roundImagery(im) }, null, 2)
const copy = async (text: string, what: string) => {
try {
await navigator.clipboard.writeText(text)
setCopied(what)
window.setTimeout(() => setCopied(''), 1500)
} catch {
setCopied('')
}
}
const convert = () => {
setConvertError('')
setConverted(null)
try {
const parsed: unknown = JSON.parse(geojsonText)
if (autoGeo) {
setConverted(siteFromGeoJSON(parsed, startIndex))
} else {
if (!geo) throw new Error(t('geojson.notBound'))
const boundary = geoJSONToBoundary(parsed, geo)
const b = boundsOf(boundary)
setConverted({ geo, boundary, width: b.maxX - b.minX, depth: b.maxY - b.minY })
}
} catch (e) {
setConvertError(e instanceof Error ? e.message : String(e))
}
}
const apply = () => {
if (!converted) return
const p = planRef.current
setDraft(null)
onChangeRef.current({
...p,
site: { ...p.site, geo: converted.geo, boundary: converted.boundary, width: converted.width, depth: converted.depth },
})
didFit.current = false
}
return (
<div className="map-view">
<div className="map-view__map" ref={containerRef} />
<Panel
ref={panelRef}
className="map-view__panel"
eyebrow={t('panel.eyebrow')}
title={binding ? t('panel.titleAlign') : t('panel.titlePlan')}
actions={
<Button size="sm" variant="ghost" onClick={() => setCollapsed((c) => !c)} aria-expanded={!collapsed}>
{collapsed ? t('panel.expand') : t('panel.collapse')}
</Button>
}
>
{!collapsed && (
<div className="map-view__stack">
<label className="map-view__row">
<span className="muted">{t('panel.opacity')}</span>
<input
className="map-view__range"
type="range"
min={0}
max={1}
step={0.05}
value={opacity}
onChange={(e) => setOpacity(Number(e.target.value))}
/>
<span className="num">{Math.round(opacity * 100)}%</span>
</label>
<Segmented
size="sm"
options={orientation()}
value={planUp ? 'plan' : 'north'}
onChange={(v) => setPlanUp(v === 'plan')}
/>
<div className="map-view__buttons">
{!viewOnly && (
<Button
size="sm"
variant={binding ? 'primary' : 'default'}
pressed={binding}
disabled={!geo}
onClick={() => setBinding((b) => !b)}
>
{binding ? t('panel.done') : t('panel.align')}
</Button>
)}
<Button size="sm" onClick={() => fit(true)}>
{t('panel.toSite')}
</Button>
</div>
{!geo && <p className="map-view__hint muted">{t('panel.notBound')}</p>}
{!viewOnly && binding && geo && (
<>
<p className="map-view__hint muted">
{t('panel.alignHint')}
</p>
<div className="map-view__buttons">
<Button size="sm" onClick={() => nudge(1)}>↺ 1°</Button>
<Button size="sm" onClick={() => nudge(0.1)}>↺ 0.1°</Button>
<Button size="sm" onClick={() => nudge(-0.1)}>↻ 0.1°</Button>
<Button size="sm" onClick={() => nudge(-1)}>↻ 1°</Button>
<Button size="sm" variant="ghost" onClick={() => commit({ offsetE: 0, offsetN: 0, rotationDeg: 0 })}>
{t('panel.reset')}
</Button>
</div>
</>
)}
{geo && (
<div className="map-view__geo">
<code className="map-view__code">
{t('panel.imagery', { e: fmtNum(im.offsetE, 2, 2), n: fmtNum(im.offsetN, 2, 2), rot: fmtNum(im.rotationDeg, 3, 3) })}
</code>
<Button size="sm" variant="ghost" onClick={() => copy(imageryText, 'imagery')}>
{copied === 'imagery' ? t('panel.copied') : 'JSON'}
</Button>
</div>
)}
<section className="map-view__section">
<div className="eyebrow">{t('legend.title')}</div>
<ul className="map-view__legend">
{LEGEND.map((l) => (
<li key={l.key} className="map-view__legend-item">
<span className={`map-view__swatch map-view__swatch--${l.kind}`} style={{ color: l.color }} aria-hidden />
<span>{t(`legend.${l.key}`)}</span>
</li>
))}
</ul>
</section>
{!viewOnly && (
<details className="map-view__section">
<summary className="map-view__summary">{t('geojson.summary')}</summary>
<div className="map-view__stack">
<textarea
className="map-view__textarea"
placeholder='{"type":"Polygon","coordinates":[[[lng,lat],…]]}'
value={geojsonText}
onChange={(e) => setGeojsonText(e.target.value)}
/>
<Checkbox label={t('geojson.autoGeo')} checked={autoGeo} onChange={setAutoGeo} />
{autoGeo && (
<Input
size="sm"
label={t('geojson.startVertex')}
type="number"
min={0}
value={startIndex}
onChange={(e) => setStartIndex(Number(e.target.value))}
/>
)}
<div className="map-view__buttons">
<Button size="sm" onClick={convert}>{t('geojson.convert')}</Button>
{converted && (
<Button size="sm" variant="primary" onClick={apply}>
{t('geojson.apply')}
</Button>
)}
{converted && (
<Button size="sm" variant="ghost" onClick={() => copy(JSON.stringify(converted, null, 2), 'site')}>
{copied === 'site' ? t('panel.copied') : t('panel.copy')}
</Button>
)}
</div>
{convertError && <p className="map-view__hint map-view__error">{convertError}</p>}
{converted && (
<p className="map-view__hint muted">
{t('geojson.result', {
count: converted.boundary.length,
w: fmtNum(converted.width / 1000, 2, 2),
d: fmtNum(converted.depth / 1000, 2, 2),
area: fmtNum(ringAreaM2(converted.boundary), 1, 1),
rot: fmtNum(converted.geo.rotationDeg, 3, 3),
})}
</p>
)}
</div>
</details>
)}
</div>
)}
</Panel>
</div>
)
}