Skip to content

Commit 5376a90

Browse files
authored
Merge pull request dbgate#1453 from dbgate/feature/postgis-error
Improve map bounds handling and validate geometry in SelectionMapView
2 parents b7e717d + 6f681b2 commit 5376a90

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

packages/web/src/elements/MapView.svelte

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@
5555
})
5656
.addTo(map);
5757
// geoJsonObj.bindPopup('This is the Transamerica Pyramid'); //.openPopup();
58-
map.fitBounds(geoJsonObj.getBounds());
58+
try {
59+
const bounds = geoJsonObj.getBounds();
60+
if (bounds.isValid()) {
61+
map.fitBounds(bounds);
62+
}
63+
} catch (e) {
64+
// bounds not valid (e.g. all-NaN coordinates), leave the default map view
65+
console.warn('Failed to fit map bounds from GeoJSON; leaving the default map view.', e);
66+
}
5967
layers.push(geoJsonObj);
6068
}
6169

packages/web/src/elements/SelectionMapView.svelte

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
import _ from 'lodash';
33
import { isWktGeometry, stringifyCellValue } from 'dbgate-tools';
44
import wellknown from 'wellknown';
5-
const LAT_PRIORITY_PATTERNS = [
6-
/^lat$/i,
7-
/^latitude$/i,
8-
/latitude$/i,
9-
/lat$/i,
10-
/latitude/i,
11-
/lat/i,
12-
];
5+
const LAT_PRIORITY_PATTERNS = [/^lat$/i, /^latitude$/i, /latitude$/i, /lat$/i, /latitude/i, /lat/i];
136
147
const LON_PRIORITY_PATTERNS = [
158
/^lon$/i,
@@ -81,6 +74,27 @@
8174
return false;
8275
}
8376
77+
function isValidPosition(pos: any[]): boolean {
78+
return Array.isArray(pos) && pos.length >= 2 && pos.every(c => typeof c === 'number' && Number.isFinite(c));
79+
}
80+
81+
function isValidCoordinates(coords: any): boolean {
82+
if (!Array.isArray(coords) || coords.length === 0) return false;
83+
// Leaf level: array of numbers — a single position
84+
if (typeof coords[0] === 'number') return isValidPosition(coords);
85+
// Nested level: recurse into each element
86+
return coords.every(isValidCoordinates);
87+
}
88+
89+
function isValidGeometry(geometry): boolean {
90+
if (!geometry) return false;
91+
if (geometry.type === 'GeometryCollection') {
92+
return Array.isArray(geometry.geometries) && geometry.geometries.length > 0 && geometry.geometries.every(isValidGeometry);
93+
}
94+
if (!Array.isArray(geometry.coordinates)) return false;
95+
return isValidCoordinates(geometry.coordinates);
96+
}
97+
8498
function createColumnsTable(cells) {
8599
if (cells.length == 0) return '';
86100
return `<table>${cells
@@ -121,13 +135,16 @@
121135
if (geoValues.length > 0) {
122136
// parse WKT to geoJSON array
123137
features.push(
124-
...geoValues.map(wellknown).map(geometry => ({
125-
type: 'Feature',
126-
properties: {
127-
popupContent: createColumnsTable(cells.filter(x => !isWktGeometry(x.value))),
128-
},
129-
geometry,
130-
}))
138+
...geoValues
139+
.map(wellknown)
140+
.filter(geometry => geometry != null && isValidGeometry(geometry))
141+
.map(geometry => ({
142+
type: 'Feature',
143+
properties: {
144+
popupContent: createColumnsTable(cells.filter(x => !isWktGeometry(x.value))),
145+
},
146+
geometry,
147+
}))
131148
);
132149
}
133150
}

0 commit comments

Comments
 (0)