fix(export): georeference satellite image for correct CAD scaling

Export rectangular PNGs with uniform m/px, Y-flip for DXF IMAGE alignment,
satellite.pgw world file, INSUNITS=meters, and extent clipping. Adds imagery
fence tracing, Esri/USGS tiles, Overpass retry/throttle, go-to-address/site
zoom, and Start.bat port 3456 cleanup.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Michael-A-SloanandCursor committed 2026-06-30 13:29:09 -04:00
1 parent 7c20a404bf
commit fadfb807ec
4 files changed
+1505 -259

No files matched your search

+131 -14
View File
@@ -5,19 +5,36 @@
*/
function extractCoordinates(url) {
const s = String(url).trim();
// Google Maps: ...@lat,lon... Google Earth Web: ...@lat,lon... or ...@lat,lon,altitudea,distanced...
let lat;
let lon;
const placePins = [];
const placePinRegex = /!3d(-?\d+\.?\d*)!4d(-?\d+\.?\d*)/g;
let pinMatch;
while ((pinMatch = placePinRegex.exec(s)) !== null) {
placePins.push({ lat: parseFloat(pinMatch[1]), lon: parseFloat(pinMatch[2]) });
}
const coordRegex = /@(-?\d+\.?\d*),(-?\d+\.?\d*)/;
const coordMatch = s.match(coordRegex);
if (!coordMatch || coordMatch.length < 3) return null;
const lat = parseFloat(coordMatch[1]);
const lon = parseFloat(coordMatch[2]);
let viewMeters = undefined;
// Google Earth Web: optional altitude (e.g. ,1234a) or distance (e.g. ,50000d) in meters; order may be ,100a,50000d
if (placePins.length > 0) {
const pin = placePins[placePins.length - 1];
lat = pin.lat;
lon = pin.lon;
} else if (coordMatch && coordMatch.length >= 3) {
lat = parseFloat(coordMatch[1]);
lon = parseFloat(coordMatch[2]);
} else {
return null;
}
if (isNaN(lat) || isNaN(lon)) return null;
let viewMeters;
const mapsZoomM = s.match(/@-?\d+\.?\d*,-?\d+\.?\d*,(\d+(?:\.\d*)?)\s*m\b/i);
const distRegex = /@-?\d+\.?\d*,-?\d+\.?\d*(?:,\d+(?:\.\d*)?\s*a)?\s*,(\d+(?:\.\d*)?)\s*d/i;
const altRegex = /@-?\d+\.?\d*,-?\d+\.?\d*,(\d+(?:\.\d*)?)\s*a/i;
const distMatch = s.match(distRegex);
const altMatch = s.match(altRegex);
if (distMatch) viewMeters = Math.min(50000, Math.max(100, parseFloat(distMatch[1])));
if (mapsZoomM) viewMeters = Math.min(50000, Math.max(50, parseFloat(mapsZoomM[1])));
else if (distMatch) viewMeters = Math.min(50000, Math.max(100, parseFloat(distMatch[1])));
else if (altMatch) viewMeters = Math.min(50000, Math.max(100, parseFloat(altMatch[1]) * 2));
return { lat, lon, viewMeters };
}
@@ -32,10 +49,10 @@ var DEFAULT_LAYER_OPTIONS = {
landuse: true,
parkingOutline: true,
parkingSpaces: true,
trees: true,
barriers: true,
power: true,
boundary: true,
trees: false,
barriers: false,
power: false,
boundary: false,
man_made: true
};
@@ -177,12 +194,28 @@ function buildPreviewData(osmData, origin, layerOptions) {
return out;
}
function generateHighDetailDXF(osmData, origin, layerOptions) {
function generateHighDetailDXF(osmData, origin, layerOptions, clipBounds) {
const opts = layerOptions || DEFAULT_LAYER_OPTIONS;
const on = function (key) { return opts[key] !== false; };
const nodes = {};
const metersPerLat = 111132.92;
const metersPerLon = 111412.84 * Math.cos((origin.lat * Math.PI) / 180);
const clipMargin = 8;
function inClip(x, y) {
if (!clipBounds) return true;
return x >= clipBounds.minX - clipMargin && x <= clipBounds.maxX + clipMargin
&& y >= clipBounds.minY - clipMargin && y <= clipBounds.maxY + clipMargin;
}
function wayTouchesClip(el) {
if (!clipBounds || !el.nodes) return true;
for (let i = 0; i < el.nodes.length; i++) {
const n = nodes[el.nodes[i]];
if (n && inClip(n.x, n.y)) return true;
}
return false;
}
osmData.elements.forEach((el) => {
if (el.type === 'node') {
@@ -194,7 +227,7 @@ function generateHighDetailDXF(osmData, origin, layerOptions) {
}
});
let dxf = '0\nSECTION\n2\nHEADER\n0\nENDSEC\n0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLTYPE\n70\n1\n';
let dxf = '0\nSECTION\n2\nHEADER\n9\n$INSUNITS\n70\n6\n9\n$MEASUREMENT\n70\n1\n0\nENDSEC\n0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLTYPE\n70\n1\n';
dxf += '0\nLTYPE\n2\nDASHED\n70\n0\n3\n_ _ _ _ _ _\n72\n65\n73\n2\n40\n2.0\n49\n1.25\n49\n-0.75\n0\nENDTAB\n0\nENDSEC\n';
dxf += '0\nSECTION\n2\nENTITIES\n';
@@ -207,6 +240,7 @@ function generateHighDetailDXF(osmData, origin, layerOptions) {
const PARKING_HH = 2.5;
for (const id in nodes) {
const n = nodes[id];
if (!inClip(n.x, n.y)) continue;
const nodeKey = getNodeLayerKey(n.tags);
if (!nodeKey || !on(nodeKey)) continue;
if (n.tags) {
@@ -233,6 +267,7 @@ function generateHighDetailDXF(osmData, origin, layerOptions) {
osmData.elements.forEach((el) => {
if (el.type !== 'way' || !el.nodes || el.nodes.length < 4) return;
if (el.nodes[0] !== el.nodes[el.nodes.length - 1]) return;
if (!wayTouchesClip(el)) return;
const wayKey = getWayLayerKey(el);
if (!wayKey || !on(wayKey)) return;
const points = [];
@@ -250,6 +285,7 @@ function generateHighDetailDXF(osmData, origin, layerOptions) {
osmData.elements.forEach((el) => {
if (el.type !== 'way' || !el.nodes || el.nodes.length < 2) return;
if (!wayTouchesClip(el)) return;
const wayKey = getWayLayerKey(el);
if (!wayKey || !on(wayKey)) return;
let layer = '0', color = '7', isRoad = false, roadHalfWidth = 0, isRail = false, lineType = 'CONTINUOUS';
@@ -651,6 +687,47 @@ function classifyImageryContour(poly) {
return null;
}
/**
* Closed OSM building footprints in world meters (for trace overlay / export).
*/
function getOSMBuildingFootprints(osmData, origin) {
if (!osmData || !origin) return [];
const nodes = getOSMNodesInMeters(osmData, origin);
const polygons = [];
osmData.elements.forEach((el) => {
if (el.type !== 'way' || !el.nodes || el.nodes.length < 4) return;
if (!el.tags || !el.tags.building) return;
const ids = el.nodes[0] === el.nodes[el.nodes.length - 1] ? el.nodes.slice(0, -1) : el.nodes;
const pts = [];
ids.forEach((id) => {
const n = nodes[id];
if (n) pts.push({ x: n.x, y: n.y });
});
if (pts.length >= 3) polygons.push(pts);
});
return polygons;
}
/**
* OSM barrier/fence ways as polylines in world meters (open or closed).
*/
function getOSMBarrierPolylines(osmData, origin) {
if (!osmData || !origin) return [];
const nodes = getOSMNodesInMeters(osmData, origin);
const lines = [];
osmData.elements.forEach((el) => {
if (el.type !== 'way' || !el.nodes || el.nodes.length < 2) return;
if (!el.tags || !el.tags.barrier) return;
const pts = [];
el.nodes.forEach((id) => {
const n = nodes[id];
if (n) pts.push({ x: n.x, y: n.y });
});
if (pts.length >= 2) lines.push(pts);
});
return lines;
}
/**
* Filter imagery polylines to only those suitable for land lot drawings (trees, buildings, lots).
* Returns { trees: [], buildings: [], lots: [], all: [] }.
@@ -774,6 +851,43 @@ function appendImageryTraceToDXF(dxfContent, polylines, minSegmentMeters, minLen
return dxfContent.replace('0\nENDSEC\n0\nEOF', block + '0\nENDSEC\n0\nEOF');
}
/**
* Pixel size for export: preserves geographic aspect ratio; 1 DXF unit = 1 meter.
*/
function extentToImagePixels(minX, maxX, minY, maxY, maxLongEdge) {
const w = maxX - minX;
const h = maxY - minY;
const long = Math.max(w, h, 1);
const maxPx = maxLongEdge || 2048;
const metersPerPixel = long / maxPx;
return {
width: Math.max(1, Math.round(w / metersPerPixel)),
height: Math.max(1, Math.round(h / metersPerPixel)),
metersPerPixel: metersPerPixel,
widthMeters: w,
heightMeters: h
};
}
/**
* ESRI world file (.pgw) for georeferenced raster attach in CAD/GIS.
* Coordinates match DXF: origin at map center, X east, Y north, units = meters.
*/
function generateWorldFilePgw(minX, minY, maxX, maxY, widthPx, heightPx, rowZeroAtSouth) {
const pixelW = (maxX - minX) / widthPx;
const pixelH = (maxY - minY) / heightPx;
const A = pixelW;
const C = minX + A / 2;
if (rowZeroAtSouth) {
const E = pixelH;
const F = minY + E / 2;
return A + '\n0\n0\n' + E + '\n' + C + '\n' + F + '\n';
}
const E = -pixelH;
const F = maxY + E / 2;
return A + '\n0\n0\n' + E + '\n' + C + '\n' + F + '\n';
}
/**
* Append satellite imagery reference to DXF so CAD shows the image as background.
* Also adds a visible bounding box (rectangle) on layer IMAGE_BOUNDS so the image extent is visible in CAD.
@@ -787,6 +901,9 @@ function appendSatelliteImageToDXF(dxfContent, imageFilename, minX, minY, maxX,
const h = maxY - minY;
const uPerPx = w / widthPx;
const vPerPx = h / heightPx;
const scaleNote =
`0\nTEXT\n8\nIMAGE_SCALE\n62\n3\n10\n${minX}\n20\n${minY - Math.max(3, h * 0.02)}\n40\n${Math.max(2, Math.min(w, h) * 0.03)}\n1\n` +
`Satellite ${w.toFixed(1)}m x ${h.toFixed(1)}m | ${uPerPx.toFixed(4)} m/px | INSUNITS=meters\n`;
/* Visible bounding box in CAD: four LINE entities (color 4 = cyan) so user sees the image extent */
const layer = 'IMAGE_BOUNDS';
const color = '4';
@@ -803,5 +920,5 @@ function appendSatelliteImageToDXF(dxfContent, imageFilename, minX, minY, maxX,
`0\nSECTION\n2\nOBJECTS\n` +
`0\nIMAGEDEF\n90\n${handle}\n1\n${imageFilename}\n13\n${widthPx}\n23\n${heightPx}\n` +
`0\nENDSEC\n0\nEOF`;
return dxfContent.replace('0\nENDSEC\n0\nEOF', boxLines + imageEntity + '0\nENDSEC\n' + objectsSection);
return dxfContent.replace('0\nENDSEC\n0\nEOF', boxLines + scaleNote + imageEntity + '0\nENDSEC\n' + objectsSection);
}
+1163 -142
View File
File diff suppressed because it is too large. Load diff