|
2 | 2 | import _ from 'lodash'; |
3 | 3 | import { isWktGeometry, stringifyCellValue } from 'dbgate-tools'; |
4 | 4 | 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]; |
13 | 6 |
|
14 | 7 | const LON_PRIORITY_PATTERNS = [ |
15 | 8 | /^lon$/i, |
|
81 | 74 | return false; |
82 | 75 | } |
83 | 76 |
|
| 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 | +
|
84 | 98 | function createColumnsTable(cells) { |
85 | 99 | if (cells.length == 0) return ''; |
86 | 100 | return `<table>${cells |
|
121 | 135 | if (geoValues.length > 0) { |
122 | 136 | // parse WKT to geoJSON array |
123 | 137 | 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 | + })) |
131 | 148 | ); |
132 | 149 | } |
133 | 150 | } |
|
0 commit comments