# AI DNA — MAPS to DXF Standalone Quick reference for AI and developers jumping back into this repo: purpose, structure, conventions, and where to change what. --- ## Purpose - **Input:** Google Maps URL (with `@lat,lon` in the URL). - **Output:** DXF file (+ optional PNG satellite image) for use in CAD, with map features and **user-defined lot boundaries**. - **Flow:** URL → coordinates → Overpass (OSM) data → preview on canvas → user adds/edits lots → export DXF/PNG. --- ## Stack - **Backend:** Node.js + Express. Single server: `server.js`. - **Frontend:** Vanilla JS, no build step. One HTML file with inline script + one shared JS file. - **APIs:** Overpass (OSM), ESRI World Imagery tiles (proxied by server to avoid CORS). --- ## Repo layout ``` MAPS-to-DXF-standalone/ ├── server.js # Express: static files, /api/overpass, /api/tile ├── package.json # express only ├── Start.bat # Windows: install Node if missing, npm install, npm start ├── README.md # User-facing run + workflow ├── ai_dna.md # This file — project DNA for AI/developers └── public/ ├── index.html # UI + canvas + all client state and map/lot/edit logic └── app.js # Pure functions: extractCoordinates, buildPreviewData, # generateHighDetailDXF, appendLotsToDXF, # appendSatelliteImageToDXF, OSM lot/concave-hull helpers ``` - **Logic lives in:** `public/index.html` (state, drawing, mouse, detection, export) and `public/app.js` (OSM→preview, OSM→lots, DXF strings). - **Server:** Only serves static files and proxies Overpass + tile URLs; no map/lot logic. --- ## Coordinate systems - **Lat/lon:** From Google Maps URL; origin for the map is `centerOrigin = { lat, lon }`. - **World (meters):** All OSM and lot geometry is in meters relative to `centerOrigin` (e.g. `x` = east, `y` = north). Used in preview data, lots array, and DXF. - **Screen:** Canvas pixel coords. `toScreen(wx, wy)` and `screenToWorld(sx, sy)` convert using `scale`, `offsetX`, `offsetY`. - **Detection rect:** Stored in world coords: `detectionRect = { minX, maxX, minY, maxY }`. When a “selection as lot” is used, that rectangle (4 corners in world) becomes one lot polygon. --- ## Lots - **Storage:** `lots` is an array of polygons; each polygon is an array of `{ x, y }` in **world (meters)**. - **Drawing:** Yellow dashed outline + light fill; in Edit mode, vertices are drawn and draggable. - **Sources of lots:** 1. **Add selection as lot** — `detectionRect` (4 corners) pushed as one polygon. 2. **Detect lots from imagery** — edge detection + contours on satellite (full view or crop to `detectionRect`); contours converted to world, simplified, min area ~80 m². 3. **OSM fallback** — if imagery detection finds nothing: site boundary (concave hull of buildings/parking) or landuse polygons, optionally snapped to imagery edges. 4. **Draw lot** — user clicks to add vertices; close with double-click or “Close lot”. - **Edit lots:** In Edit mode, `selectedVertex = { lotIndex, vertexIndex }`; mousemove updates `lots[lotIndex][vertexIndex]` in world coords. - **DXF:** `appendLotsToDXF(dxfContent, lots)` writes LOTS layer LINE entities in the same world (meters) system. --- ## Key UI state (index.html) - **Map:** `centerOrigin`, `previewData`, `osmData`, `scale`, `offsetX`, `offsetY`, `showSatellite`. - **Lots:** `lots`, `currentLot` (in progress), `drawLotMode`, `editLotMode`, `selectedVertex`. - **Detection area:** `detectionRect`, `selectAreaMode`, `rectStart`, `rectCurrent`. - **Canvas:** Pan (drag), zoom (wheel), resize; in Draw/Edit/Select-area modes, mouse handlers branch before pan. --- ## Where to change what | Goal | Where | |------|--------| | Add a new map layer or style | `public/index.html`: `drawVectorLayer()`, `color()`; `public/app.js`: `buildPreviewData`, `generateHighDetailDXF` | | Change lot detection (min area, thresholds) | `public/index.html`: `detectLotsFromImagery()`, `minAreaWorld`, contour/edge params | | Change DXF layers or format | `public/app.js`: `generateHighDetailDXF`, `appendLotsToDXF`, `appendSatelliteImageToDXF` | | Add a new API or proxy | `server.js`: new route (e.g. `/api/...`), keep CORS/tile proxy pattern | | Change OSM query or tags | `server.js`: Overpass query string; `public/app.js`: tag handling in `buildPreviewData` / `generateHighDetailDXF` | | Fix coordinate math | `public/index.html`: `worldToLatLon`, `latLonToWorld`, `toScreen`, `screenToWorld`; tile helpers for extent/zoom | | Add a new button/mode | `public/index.html`: new button, new state flag, branch in mousedown/mousemove/mouseup, and in `draw()` / `drawLotsLayer()` | --- ## Conventions - **No build step** — edit `public/index.html` and `public/app.js`; refresh browser. - **World = meters from origin** — keep all persistent geometry (preview, lots, DXF) in world coords; convert to/from screen only at draw and input. - **Lots are closed polygons** — stored as lists of vertices; first and last are not duplicated; DXF writes segments between consecutive and last→first. - **Server is stateless** — no session; all map/lot state lives in the client. Export is client-side (blob download). --- ## Quick test 1. Run `npm start`, open http://localhost:3456. 2. Paste a Google Maps URL with `@lat,lon`, click Load preview. 3. Enable satellite, use “Select detection area” → “Add selection as lot” → “Edit lots” to move a corner → Export DXF. Check DXF + PNG in same folder.