@focus-daima/gis-workbench 0.0.0-stage → 0.9.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +349 -2
- package/assets/glyphs/0-255.pbf +0 -0
- package/assets/glyphs/1024-1279.pbf +0 -0
- package/assets/glyphs/256-511.pbf +0 -0
- package/assets/glyphs/512-767.pbf +0 -0
- package/assets/glyphs/768-1023.pbf +0 -0
- package/client.js +1516 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +20 -0
- package/index.js +417 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +48 -4
- package/server/api.js +456 -0
- package/server/drop_layer.py +12 -0
- package/server/pg_tables.py +52 -0
- package/server/store.js +1854 -0
- package/server/unzip.py +30 -0
- package/server/vector_edit.py +133 -0
- package/server/vector_info.py +48 -0
- package/shared.js +37 -0
- package/src/api.js +269 -0
- package/src/client.entry.js +9 -0
- package/src/plugin.js +4503 -0
- package/src/store.js +189 -0
- package/src/styles.js +710 -0
- package/style-schema.js +199 -0
package/server/store.js
ADDED
|
@@ -0,0 +1,1854 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { randomUUID } from 'node:crypto';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { STYLE_SCHEMA, mergeLayerStyle, normalizeStylePatch } from '../style-schema.js';
|
|
10
|
+
|
|
11
|
+
const API_PREFIX = '/api/gis-workbench';
|
|
12
|
+
const MAX_FILE_BYTES = 100 * 1024 * 1024 * 1024;
|
|
13
|
+
const MAX_CHUNK_BYTES = 64 * 1024 * 1024;
|
|
14
|
+
const INSPECT_FILE_LIMIT = 64 * 1024 * 1024;
|
|
15
|
+
const COG_MAX_ZOOM = 22;
|
|
16
|
+
const execFileAsync = promisify(execFile);
|
|
17
|
+
const UNZIP_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'unzip.py');
|
|
18
|
+
const VECTOR_INFO_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'vector_info.py');
|
|
19
|
+
const DROP_LAYER_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'drop_layer.py');
|
|
20
|
+
const VECTOR_EDIT_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'vector_edit.py');
|
|
21
|
+
const PG_TABLES_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), 'pg_tables.py');
|
|
22
|
+
const ALLOWED_FILE_EXTENSIONS = new Set([
|
|
23
|
+
'.geojson', '.json', '.csv', '.tif', '.tiff', '.png', '.jpg', '.jpeg', '.webp', '.shp', '.gpkg', '.zip',
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
function jsonParse(value, fallback) {
|
|
27
|
+
if (value === null || value === undefined || value === '') return fallback;
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(value);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return fallback;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function jsonString(value, fallback) {
|
|
36
|
+
try {
|
|
37
|
+
return JSON.stringify(value === undefined ? fallback : value);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return JSON.stringify(fallback);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isInside(root, target) {
|
|
44
|
+
return target === root || target.startsWith(root + path.sep);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parseBbox(value) {
|
|
48
|
+
if (value === null || value === undefined || value === '') return null;
|
|
49
|
+
let candidate = value;
|
|
50
|
+
if (typeof candidate === 'string') {
|
|
51
|
+
try {
|
|
52
|
+
candidate = JSON.parse(candidate);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
candidate = candidate.split(',').map((part) => Number(part.trim()));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!Array.isArray(candidate) || candidate.length !== 4) return null;
|
|
58
|
+
const numbers = candidate.map((part) => Number(part));
|
|
59
|
+
if (numbers.some((number) => !Number.isFinite(number))) return null;
|
|
60
|
+
const [west, south, east, north] = numbers;
|
|
61
|
+
if (west > east || south > north) return null;
|
|
62
|
+
return [west, south, east, north];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function parseCrs(value) {
|
|
66
|
+
if (value === null || value === undefined || value === '') return null;
|
|
67
|
+
const number = Number(value);
|
|
68
|
+
return Number.isInteger(number) ? number : null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function fieldType(value) {
|
|
72
|
+
if (value === null) return 'null';
|
|
73
|
+
if (Array.isArray(value)) return 'array';
|
|
74
|
+
return typeof value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function mergeFieldType(current, next) {
|
|
78
|
+
if (current === undefined || current === 'null') return next;
|
|
79
|
+
if (next === 'null' || current === next) return current;
|
|
80
|
+
return 'mixed';
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isTileTemplate(uri) {
|
|
84
|
+
return typeof uri === 'string'
|
|
85
|
+
&& /^(https?:)?\/\//i.test(uri)
|
|
86
|
+
&& uri.includes('{z}')
|
|
87
|
+
&& uri.includes('{x}')
|
|
88
|
+
&& uri.includes('{y}');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function safeUploadName(name) {
|
|
92
|
+
const base = path.basename(String(name || 'upload.bin'));
|
|
93
|
+
const sanitized = base.replace(/[^\w.\-\u4e00-\u9fa5]+/g, '_').replace(/^_+|_+$/g, '');
|
|
94
|
+
return (sanitized || 'upload.bin').slice(-140);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function serializeUpload(row) {
|
|
98
|
+
return {
|
|
99
|
+
id: row.id,
|
|
100
|
+
projectId: row.project_id,
|
|
101
|
+
fileName: row.file_name,
|
|
102
|
+
size: row.size,
|
|
103
|
+
chunkSize: row.chunk_size,
|
|
104
|
+
totalChunks: row.total_chunks,
|
|
105
|
+
receivedChunks: jsonParse(row.received, []),
|
|
106
|
+
status: row.status,
|
|
107
|
+
updatedAt: row.updated_at,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function kindHint(name) {
|
|
112
|
+
const ext = path.extname(name).toLowerCase();
|
|
113
|
+
if (['.geojson', '.json', '.shp', '.gpkg', '.zip'].includes(ext)) return 'vector';
|
|
114
|
+
if (['.tif', '.tiff', '.png', '.jpg', '.jpeg', '.webp'].includes(ext)) return 'raster';
|
|
115
|
+
if (ext === '.csv') return 'table';
|
|
116
|
+
return 'unknown';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function mimeForFormat(format, fileName) {
|
|
120
|
+
if (format === 'geojson') return 'application/geo+json; charset=utf-8';
|
|
121
|
+
if (format === 'cog') return 'image/tiff';
|
|
122
|
+
if (format === 'csv') return 'text/csv; charset=utf-8';
|
|
123
|
+
const ext = path.extname(fileName || '').toLowerCase();
|
|
124
|
+
if (ext === '.png') return 'image/png';
|
|
125
|
+
if (ext === '.jpg' || ext === '.jpeg') return 'image/jpeg';
|
|
126
|
+
if (ext === '.webp') return 'image/webp';
|
|
127
|
+
if (ext === '.tif' || ext === '.tiff') return 'image/tiff';
|
|
128
|
+
return 'application/octet-stream';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function httpError(status, message) {
|
|
132
|
+
const error = new Error(message);
|
|
133
|
+
error.status = status;
|
|
134
|
+
return error;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function collectGeoJson(text) {
|
|
138
|
+
let value;
|
|
139
|
+
try {
|
|
140
|
+
value = JSON.parse(text);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw httpError(400, 'The file is not valid JSON');
|
|
143
|
+
}
|
|
144
|
+
const features = value && value.type === 'FeatureCollection' && Array.isArray(value.features)
|
|
145
|
+
? value.features
|
|
146
|
+
: value && value.type === 'Feature'
|
|
147
|
+
? [value]
|
|
148
|
+
: value && value.type
|
|
149
|
+
? [{ type: 'Feature', properties: {}, geometry: value }]
|
|
150
|
+
: [];
|
|
151
|
+
const geometryTypes = new Set();
|
|
152
|
+
const fields = new Map();
|
|
153
|
+
let minX = Infinity;
|
|
154
|
+
let minY = Infinity;
|
|
155
|
+
let maxX = -Infinity;
|
|
156
|
+
let maxY = -Infinity;
|
|
157
|
+
let sawCoordinate = false;
|
|
158
|
+
|
|
159
|
+
const visitCoordinates = (coordinates) => {
|
|
160
|
+
if (!Array.isArray(coordinates)) return;
|
|
161
|
+
if (coordinates.length >= 2 && typeof coordinates[0] === 'number' && typeof coordinates[1] === 'number') {
|
|
162
|
+
const x = coordinates[0];
|
|
163
|
+
const y = coordinates[1];
|
|
164
|
+
if (Number.isFinite(x) && Number.isFinite(y)) {
|
|
165
|
+
sawCoordinate = true;
|
|
166
|
+
minX = Math.min(minX, x);
|
|
167
|
+
minY = Math.min(minY, y);
|
|
168
|
+
maxX = Math.max(maxX, x);
|
|
169
|
+
maxY = Math.max(maxY, y);
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
for (const part of coordinates) visitCoordinates(part);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
for (const feature of features) {
|
|
177
|
+
const geometry = feature && feature.geometry;
|
|
178
|
+
if (geometry && typeof geometry.type === 'string') {
|
|
179
|
+
geometryTypes.add(geometry.type);
|
|
180
|
+
visitCoordinates(geometry.coordinates);
|
|
181
|
+
}
|
|
182
|
+
const properties = feature && feature.properties;
|
|
183
|
+
if (properties && typeof properties === 'object' && !Array.isArray(properties)) {
|
|
184
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
185
|
+
fields.set(key, mergeFieldType(fields.get(key), fieldType(value)));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
kind: 'vector',
|
|
192
|
+
crs: 4326,
|
|
193
|
+
bbox: sawCoordinate ? [minX, minY, maxX, maxY] : null,
|
|
194
|
+
profile: {
|
|
195
|
+
format: 'geojson',
|
|
196
|
+
renderable: true,
|
|
197
|
+
featureCount: features.length,
|
|
198
|
+
geometryTypes: [...geometryTypes],
|
|
199
|
+
fields: Object.fromEntries(fields),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function inspectCsv(filePath, sizeBytes) {
|
|
205
|
+
const handle = await fs.open(filePath, 'r');
|
|
206
|
+
let text = '';
|
|
207
|
+
try {
|
|
208
|
+
const buffer = Buffer.alloc(Math.min(sizeBytes, 128 * 1024));
|
|
209
|
+
const result = await handle.read(buffer, 0, buffer.length, 0);
|
|
210
|
+
text = buffer.subarray(0, result.bytesRead).toString('utf8');
|
|
211
|
+
} finally {
|
|
212
|
+
await handle.close();
|
|
213
|
+
}
|
|
214
|
+
const lines = text.split(/\r?\n/).filter((line) => line.trim() !== '');
|
|
215
|
+
const header = lines[0] ? lines[0].split(',').map((part) => part.trim().replace(/^"|"$/g, '')) : [];
|
|
216
|
+
const fields = Object.fromEntries(header.filter(Boolean).map((name) => [name, 'string']));
|
|
217
|
+
return {
|
|
218
|
+
kind: 'table',
|
|
219
|
+
crs: null,
|
|
220
|
+
bbox: null,
|
|
221
|
+
profile: {
|
|
222
|
+
format: 'csv',
|
|
223
|
+
renderable: false,
|
|
224
|
+
rowCountSample: Math.max(0, lines.length - 1),
|
|
225
|
+
fields,
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function inspectFile(filePath, sizeBytes, options = {}) {
|
|
231
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
232
|
+
const fileName = path.basename(filePath);
|
|
233
|
+
if (!ALLOWED_FILE_EXTENSIONS.has(ext) && ext !== '') {
|
|
234
|
+
throw httpError(415, `Unsupported dataset file type: ${ext}`);
|
|
235
|
+
}
|
|
236
|
+
if (ext === '.geojson' || ext === '.json') {
|
|
237
|
+
if (sizeBytes > INSPECT_FILE_LIMIT) {
|
|
238
|
+
return {
|
|
239
|
+
kind: 'vector',
|
|
240
|
+
crs: 4326,
|
|
241
|
+
bbox: null,
|
|
242
|
+
profile: {
|
|
243
|
+
format: 'geojson',
|
|
244
|
+
renderable: false,
|
|
245
|
+
fileName: options.fileName || fileName,
|
|
246
|
+
sizeBytes,
|
|
247
|
+
note: 'GeoJSON is too large to inspect in full; register it now and convert or tile it for rendering.',
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
const text = await fs.readFile(filePath, 'utf8');
|
|
252
|
+
const inspected = collectGeoJson(text);
|
|
253
|
+
inspected.profile.sizeBytes = sizeBytes;
|
|
254
|
+
inspected.profile.fileName = options.fileName || fileName;
|
|
255
|
+
return inspected;
|
|
256
|
+
}
|
|
257
|
+
if (ext === '.csv') {
|
|
258
|
+
const inspected = await inspectCsv(filePath, sizeBytes);
|
|
259
|
+
inspected.profile.sizeBytes = sizeBytes;
|
|
260
|
+
inspected.profile.fileName = options.fileName || fileName;
|
|
261
|
+
return inspected;
|
|
262
|
+
}
|
|
263
|
+
if (['.png', '.jpg', '.jpeg', '.webp'].includes(ext)) {
|
|
264
|
+
const bbox = parseBbox(options.bbox);
|
|
265
|
+
return {
|
|
266
|
+
kind: 'raster',
|
|
267
|
+
crs: parseCrs(options.crs) ?? (bbox ? 4326 : null),
|
|
268
|
+
bbox,
|
|
269
|
+
profile: {
|
|
270
|
+
format: 'image',
|
|
271
|
+
renderable: Boolean(bbox),
|
|
272
|
+
fileName: options.fileName || fileName,
|
|
273
|
+
sizeBytes,
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
if (ext === '.tif' || ext === '.tiff') {
|
|
278
|
+
const bbox = parseBbox(options.bbox);
|
|
279
|
+
return {
|
|
280
|
+
kind: 'raster',
|
|
281
|
+
crs: parseCrs(options.crs) ?? (bbox ? 4326 : null),
|
|
282
|
+
bbox,
|
|
283
|
+
profile: {
|
|
284
|
+
format: 'geotiff',
|
|
285
|
+
renderable: false,
|
|
286
|
+
fileName: options.fileName || fileName,
|
|
287
|
+
sizeBytes,
|
|
288
|
+
note: 'GeoTIFF is registered but needs tiling before MapLibre can render it.',
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
if (ext === '.shp' || ext === '.zip') {
|
|
293
|
+
return {
|
|
294
|
+
kind: 'vector',
|
|
295
|
+
crs: parseCrs(options.crs),
|
|
296
|
+
bbox: null,
|
|
297
|
+
profile: {
|
|
298
|
+
format: ext === '.zip' ? 'shapefile-zip' : 'shapefile',
|
|
299
|
+
renderable: false,
|
|
300
|
+
fileName: options.fileName || fileName,
|
|
301
|
+
sizeBytes,
|
|
302
|
+
note: 'Shapefile is registered but needs conversion to GeoJSON before rendering.',
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
kind: 'table',
|
|
308
|
+
crs: null,
|
|
309
|
+
bbox: null,
|
|
310
|
+
profile: {
|
|
311
|
+
format: ext.replace(/^\./, '') || 'unknown',
|
|
312
|
+
renderable: false,
|
|
313
|
+
fileName: options.fileName || fileName,
|
|
314
|
+
sizeBytes,
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function walkFiles(dir) {
|
|
320
|
+
const result = [];
|
|
321
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
322
|
+
for (const entry of entries) {
|
|
323
|
+
const full = path.join(dir, entry.name);
|
|
324
|
+
if (entry.isDirectory()) result.push(...await walkFiles(full));
|
|
325
|
+
else if (entry.isFile()) result.push(full);
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function geoJsonBounds(data) {
|
|
331
|
+
let west = Infinity;
|
|
332
|
+
let south = Infinity;
|
|
333
|
+
let east = -Infinity;
|
|
334
|
+
let north = -Infinity;
|
|
335
|
+
const visit = (coordinates) => {
|
|
336
|
+
if (!Array.isArray(coordinates)) return;
|
|
337
|
+
if (coordinates.length >= 2 && coordinates.every((value) => typeof value === 'number')) {
|
|
338
|
+
west = Math.min(west, coordinates[0]);
|
|
339
|
+
east = Math.max(east, coordinates[0]);
|
|
340
|
+
south = Math.min(south, coordinates[1]);
|
|
341
|
+
north = Math.max(north, coordinates[1]);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
for (const child of coordinates) visit(child);
|
|
345
|
+
};
|
|
346
|
+
const features = data && Array.isArray(data.features) ? data.features : [];
|
|
347
|
+
for (const feature of features) {
|
|
348
|
+
if (feature && feature.geometry) visit(feature.geometry.coordinates);
|
|
349
|
+
}
|
|
350
|
+
return {
|
|
351
|
+
count: features.length,
|
|
352
|
+
bbox: Number.isFinite(west) && Number.isFinite(south) && Number.isFinite(east) && Number.isFinite(north)
|
|
353
|
+
? [west, south, east, north]
|
|
354
|
+
: null,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async function isGeoJsonFile(filePath) {
|
|
359
|
+
try {
|
|
360
|
+
const handle = await fs.open(filePath, 'r');
|
|
361
|
+
try {
|
|
362
|
+
const buffer = Buffer.alloc(8192);
|
|
363
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
364
|
+
const head = buffer.subarray(0, bytesRead).toString('utf8').replace(/^\uFEFF/, '').trimStart();
|
|
365
|
+
return /^\{/.test(head) && /"type"\s*:\s*"(FeatureCollection|Feature|GeometryCollection)"/.test(head);
|
|
366
|
+
} finally {
|
|
367
|
+
await handle.close();
|
|
368
|
+
}
|
|
369
|
+
} catch (error) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function bboxFromGdalInfo(info) {
|
|
375
|
+
const ring = info && info.wgs84Extent && info.wgs84Extent.coordinates && info.wgs84Extent.coordinates[0];
|
|
376
|
+
if (!Array.isArray(ring) || ring.length === 0) return null;
|
|
377
|
+
let west = Infinity;
|
|
378
|
+
let south = Infinity;
|
|
379
|
+
let east = -Infinity;
|
|
380
|
+
let north = -Infinity;
|
|
381
|
+
for (const point of ring) {
|
|
382
|
+
if (!Array.isArray(point) || point.length < 2) continue;
|
|
383
|
+
const x = Number(point[0]);
|
|
384
|
+
const y = Number(point[1]);
|
|
385
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) continue;
|
|
386
|
+
west = Math.min(west, x);
|
|
387
|
+
south = Math.min(south, y);
|
|
388
|
+
east = Math.max(east, x);
|
|
389
|
+
north = Math.max(north, y);
|
|
390
|
+
}
|
|
391
|
+
if (!Number.isFinite(west) || !Number.isFinite(south) || !Number.isFinite(east) || !Number.isFinite(north)) return null;
|
|
392
|
+
return [west, south, east, north];
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function epsgFromGdalInfo(info) {
|
|
396
|
+
const wkt = info && info.coordinateSystem && info.coordinateSystem.wkt ? info.coordinateSystem.wkt : '';
|
|
397
|
+
const matches = [...wkt.matchAll(/AUTHORITY\[\"EPSG\",\"(\d+)\"\]/g)];
|
|
398
|
+
if (matches.length > 0) {
|
|
399
|
+
const value = Number(matches[matches.length - 1][1]);
|
|
400
|
+
return Number.isInteger(value) ? value : null;
|
|
401
|
+
}
|
|
402
|
+
return null;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function chooseMaxZoom(bbox) {
|
|
406
|
+
if (!Array.isArray(bbox) || bbox.length !== 4) return COG_MAX_ZOOM;
|
|
407
|
+
const [west, south, east, north] = bbox;
|
|
408
|
+
const area = Math.max(0, (east - west) / 360) * Math.max(0, (north - south) / 180);
|
|
409
|
+
if (!(area > 0)) return COG_MAX_ZOOM;
|
|
410
|
+
let chosen = 0;
|
|
411
|
+
for (let zoom = 0; zoom <= COG_MAX_ZOOM; zoom += 1) {
|
|
412
|
+
if (Math.pow(4, zoom) * area <= 6000) chosen = zoom;
|
|
413
|
+
}
|
|
414
|
+
return chosen;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function gdalError(error) {
|
|
418
|
+
const text = error && (error.stderr || error.message) ? String(error.stderr || error.message) : 'GDAL command failed';
|
|
419
|
+
return text.replace(/\s+/g, ' ').trim().slice(0, 500);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function serializeProject(row) {
|
|
423
|
+
return {
|
|
424
|
+
id: row.id,
|
|
425
|
+
name: row.name,
|
|
426
|
+
createdAt: row.created_at,
|
|
427
|
+
meta: jsonParse(row.meta, {}),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function serializeDataset(row) {
|
|
432
|
+
const profile = jsonParse(row.profile, {});
|
|
433
|
+
return {
|
|
434
|
+
id: row.id,
|
|
435
|
+
projectId: row.project_id,
|
|
436
|
+
kind: row.kind,
|
|
437
|
+
uri: row.uri,
|
|
438
|
+
crs: row.crs,
|
|
439
|
+
bbox: jsonParse(row.bbox, null),
|
|
440
|
+
profile,
|
|
441
|
+
createdAt: row.created_at,
|
|
442
|
+
contentUrl: profile.format === 'xyz' || !row.uri ? null : `${API_PREFIX}/datasets/${encodeURIComponent(row.id)}/content`,
|
|
443
|
+
tilesUrl: profile.format === 'cog'
|
|
444
|
+
? `${API_PREFIX}/datasets/${encodeURIComponent(row.id)}/tiles/{z}/{x}/{y}.png`
|
|
445
|
+
: null,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function serializeLayer(row) {
|
|
450
|
+
const dataset = row.dataset_id ? {
|
|
451
|
+
id: row.dataset_id,
|
|
452
|
+
kind: row.dataset_kind,
|
|
453
|
+
uri: row.dataset_uri,
|
|
454
|
+
crs: row.dataset_crs,
|
|
455
|
+
bbox: jsonParse(row.dataset_bbox, null),
|
|
456
|
+
profile: jsonParse(row.dataset_profile, {}),
|
|
457
|
+
format: row.dataset_format,
|
|
458
|
+
contentUrl: row.dataset_uri && row.dataset_format !== 'xyz'
|
|
459
|
+
? `${API_PREFIX}/datasets/${encodeURIComponent(row.dataset_id)}/content`
|
|
460
|
+
: null,
|
|
461
|
+
tilesUrl: row.dataset_format === 'cog'
|
|
462
|
+
? `${API_PREFIX}/datasets/${encodeURIComponent(row.dataset_id)}/tiles/{z}/{x}/{y}.png`
|
|
463
|
+
: null,
|
|
464
|
+
} : null;
|
|
465
|
+
return {
|
|
466
|
+
id: row.id,
|
|
467
|
+
projectId: row.project_id,
|
|
468
|
+
datasetId: row.dataset_id,
|
|
469
|
+
name: row.name,
|
|
470
|
+
style: jsonParse(row.style, {}),
|
|
471
|
+
visible: Boolean(row.visible),
|
|
472
|
+
z: row.z,
|
|
473
|
+
dataset,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export class GisStore {
|
|
478
|
+
constructor(workspaceRoot, options = {}) {
|
|
479
|
+
this.workspaceRoot = path.resolve(workspaceRoot);
|
|
480
|
+
this.dataDir = path.join(this.workspaceRoot, '.gis-workbench');
|
|
481
|
+
this.uploadsDir = path.join(this.dataDir, 'uploads');
|
|
482
|
+
this.dbPath = path.join(this.dataDir, 'gis.sqlite');
|
|
483
|
+
const defaultRoots = ['/home/agent', '/data', '/mnt/data', '/srv/data'];
|
|
484
|
+
const configuredRoots = (options.extraRoots ?? (process.env.GIS_WORKBENCH_ROOTS ?? '').split(path.delimiter))
|
|
485
|
+
.map((root) => root.trim())
|
|
486
|
+
.filter(Boolean);
|
|
487
|
+
this.extraRoots = [...new Set([...defaultRoots, ...configuredRoots])];
|
|
488
|
+
this.roots = [];
|
|
489
|
+
this.db = null;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async init() {
|
|
493
|
+
await fs.mkdir(this.uploadsDir, { recursive: true });
|
|
494
|
+
this.workspaceRoot = await fs.realpath(this.workspaceRoot);
|
|
495
|
+
this.dataDir = await fs.realpath(this.dataDir);
|
|
496
|
+
this.uploadsDir = await fs.realpath(this.uploadsDir);
|
|
497
|
+
this.roots = [this.workspaceRoot];
|
|
498
|
+
if (!this.roots.includes(this.dataDir)) this.roots.push(this.dataDir);
|
|
499
|
+
for (const root of this.extraRoots) {
|
|
500
|
+
try {
|
|
501
|
+
const real = await fs.realpath(root);
|
|
502
|
+
if (!this.roots.includes(real)) this.roots.push(real);
|
|
503
|
+
} catch (error) {
|
|
504
|
+
// Configured roots that do not exist are ignored.
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
this.db = new DatabaseSync(this.dbPath);
|
|
508
|
+
this.db.exec(`
|
|
509
|
+
PRAGMA journal_mode = WAL;
|
|
510
|
+
PRAGMA foreign_keys = ON;
|
|
511
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
512
|
+
id TEXT PRIMARY KEY,
|
|
513
|
+
name TEXT NOT NULL,
|
|
514
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
515
|
+
meta TEXT NOT NULL DEFAULT '{}'
|
|
516
|
+
);
|
|
517
|
+
CREATE TABLE IF NOT EXISTS datasets (
|
|
518
|
+
id TEXT PRIMARY KEY,
|
|
519
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
520
|
+
kind TEXT NOT NULL,
|
|
521
|
+
uri TEXT NOT NULL,
|
|
522
|
+
crs INTEGER,
|
|
523
|
+
bbox TEXT,
|
|
524
|
+
profile TEXT NOT NULL DEFAULT '{}',
|
|
525
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
526
|
+
);
|
|
527
|
+
CREATE TABLE IF NOT EXISTS layers (
|
|
528
|
+
id TEXT PRIMARY KEY,
|
|
529
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
530
|
+
dataset_id TEXT REFERENCES datasets(id) ON DELETE CASCADE,
|
|
531
|
+
name TEXT,
|
|
532
|
+
style TEXT NOT NULL DEFAULT '{}',
|
|
533
|
+
visible INTEGER NOT NULL DEFAULT 1,
|
|
534
|
+
z INTEGER NOT NULL DEFAULT 0
|
|
535
|
+
);
|
|
536
|
+
CREATE INDEX IF NOT EXISTS datasets_project_idx ON datasets(project_id);
|
|
537
|
+
CREATE INDEX IF NOT EXISTS layers_project_idx ON layers(project_id);
|
|
538
|
+
CREATE INDEX IF NOT EXISTS layers_dataset_idx ON layers(dataset_id);
|
|
539
|
+
CREATE TABLE IF NOT EXISTS uploads (
|
|
540
|
+
id TEXT PRIMARY KEY,
|
|
541
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
542
|
+
fingerprint TEXT,
|
|
543
|
+
file_name TEXT NOT NULL,
|
|
544
|
+
size INTEGER NOT NULL,
|
|
545
|
+
chunk_size INTEGER NOT NULL,
|
|
546
|
+
total_chunks INTEGER NOT NULL,
|
|
547
|
+
received TEXT NOT NULL DEFAULT '[]',
|
|
548
|
+
temp_path TEXT NOT NULL,
|
|
549
|
+
status TEXT NOT NULL DEFAULT 'uploading',
|
|
550
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
551
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
552
|
+
);
|
|
553
|
+
CREATE INDEX IF NOT EXISTS uploads_project_idx ON uploads(project_id);
|
|
554
|
+
CREATE INDEX IF NOT EXISTS uploads_fingerprint_idx ON uploads(project_id, fingerprint, status);
|
|
555
|
+
CREATE TABLE IF NOT EXISTS databases (
|
|
556
|
+
id TEXT PRIMARY KEY,
|
|
557
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
558
|
+
name TEXT NOT NULL,
|
|
559
|
+
host TEXT NOT NULL,
|
|
560
|
+
port INTEGER NOT NULL DEFAULT 5432,
|
|
561
|
+
dbname TEXT NOT NULL,
|
|
562
|
+
username TEXT NOT NULL,
|
|
563
|
+
password TEXT NOT NULL,
|
|
564
|
+
sslmode TEXT NOT NULL DEFAULT 'prefer',
|
|
565
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
566
|
+
);
|
|
567
|
+
CREATE INDEX IF NOT EXISTS databases_project_idx ON databases(project_id);
|
|
568
|
+
`);
|
|
569
|
+
const count = this.db.prepare('SELECT COUNT(*) AS count FROM projects').get().count;
|
|
570
|
+
if (count === 0) {
|
|
571
|
+
this.createProject({
|
|
572
|
+
name: '默认工程',
|
|
573
|
+
meta: { units: 'metric', basemap: null, range: null },
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
return this;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
close() {
|
|
580
|
+
try {
|
|
581
|
+
if (this.db) this.db.close();
|
|
582
|
+
} catch (error) {
|
|
583
|
+
// Closing a disposed database is harmless.
|
|
584
|
+
}
|
|
585
|
+
this.db = null;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
assertProject(projectId) {
|
|
589
|
+
const project = this.db.prepare('SELECT * FROM projects WHERE id = ?').get(projectId);
|
|
590
|
+
if (!project) throw httpError(404, 'Project not found');
|
|
591
|
+
return project;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
listProjects() {
|
|
595
|
+
return this.db.prepare('SELECT * FROM projects ORDER BY created_at, rowid').all().map(serializeProject);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
createProject(input = {}) {
|
|
599
|
+
const name = String(input.name || '').trim() || '未命名工程';
|
|
600
|
+
const meta = input.meta && typeof input.meta === 'object' ? input.meta : {};
|
|
601
|
+
const id = randomUUID();
|
|
602
|
+
this.db.prepare('INSERT INTO projects (id, name, meta) VALUES (?, ?, ?)').run(id, name, jsonString(meta, {}));
|
|
603
|
+
return serializeProject(this.db.prepare('SELECT * FROM projects WHERE id = ?').get(id));
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
updateProject(id, input = {}) {
|
|
607
|
+
const current = this.assertProject(id);
|
|
608
|
+
const name = input.name === undefined ? current.name : String(input.name).trim() || current.name;
|
|
609
|
+
const meta = input.meta === undefined ? jsonParse(current.meta, {}) : input.meta;
|
|
610
|
+
this.db.prepare('UPDATE projects SET name = ?, meta = ? WHERE id = ?').run(name, jsonString(meta, {}), id);
|
|
611
|
+
return serializeProject(this.db.prepare('SELECT * FROM projects WHERE id = ?').get(id));
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
listDatasets(projectId) {
|
|
615
|
+
this.assertProject(projectId);
|
|
616
|
+
return this.db.prepare('SELECT * FROM datasets WHERE project_id = ? ORDER BY created_at, rowid').all(projectId).map(serializeDataset);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
getDataset(id) {
|
|
620
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id);
|
|
621
|
+
return row ? serializeDataset(row) : undefined;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
async convertRasterToCog(filePath, datasetId, options = {}) {
|
|
625
|
+
const derivedDir = path.join(this.dataDir, 'derived', datasetId);
|
|
626
|
+
await fs.rm(derivedDir, { recursive: true, force: true });
|
|
627
|
+
await fs.mkdir(derivedDir, { recursive: true });
|
|
628
|
+
const cogPath = path.join(derivedDir, 'data.cog.tif');
|
|
629
|
+
const tilesDir = path.join(derivedDir, 'tiles');
|
|
630
|
+
const baseProfile = {
|
|
631
|
+
sourceFormat: 'geotiff',
|
|
632
|
+
originalUri: filePath,
|
|
633
|
+
fileName: options.fileName || path.basename(filePath),
|
|
634
|
+
sizeBytes: options.sizeBytes,
|
|
635
|
+
};
|
|
636
|
+
try {
|
|
637
|
+
await execFileAsync('gdal_translate', [
|
|
638
|
+
'-q',
|
|
639
|
+
'-of', 'COG',
|
|
640
|
+
'-co', 'COMPRESS=DEFLATE',
|
|
641
|
+
'-co', 'BIGTIFF=IF_SAFER',
|
|
642
|
+
'-co', 'OVERVIEWS=AUTO',
|
|
643
|
+
filePath,
|
|
644
|
+
cogPath,
|
|
645
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
646
|
+
const infoText = (await execFileAsync('gdalinfo', ['-json', cogPath], { maxBuffer: 20 * 1024 * 1024 })).stdout;
|
|
647
|
+
const info = JSON.parse(infoText);
|
|
648
|
+
const bbox = bboxFromGdalInfo(info);
|
|
649
|
+
const crs = epsgFromGdalInfo(info) || (bbox ? 4326 : null);
|
|
650
|
+
const maxZoom = chooseMaxZoom(bbox);
|
|
651
|
+
await fs.mkdir(tilesDir, { recursive: true });
|
|
652
|
+
await execFileAsync('gdal2tiles.py', [
|
|
653
|
+
'--xyz',
|
|
654
|
+
'-p', 'mercator',
|
|
655
|
+
'-w', 'none',
|
|
656
|
+
'-r', 'bilinear',
|
|
657
|
+
'-z', '0-' + maxZoom,
|
|
658
|
+
'--processes=1',
|
|
659
|
+
'--quiet',
|
|
660
|
+
cogPath,
|
|
661
|
+
tilesDir,
|
|
662
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
663
|
+
return {
|
|
664
|
+
uri: cogPath,
|
|
665
|
+
crs,
|
|
666
|
+
bbox,
|
|
667
|
+
profile: Object.assign({}, baseProfile, {
|
|
668
|
+
format: 'cog',
|
|
669
|
+
renderable: Boolean(bbox),
|
|
670
|
+
cogPath,
|
|
671
|
+
tilePath: tilesDir,
|
|
672
|
+
tileSize: 256,
|
|
673
|
+
minzoom: 0,
|
|
674
|
+
maxzoom: maxZoom,
|
|
675
|
+
bbox,
|
|
676
|
+
note: bbox ? undefined : 'The COG has no WGS84 extent, so it cannot be placed on the map.',
|
|
677
|
+
}),
|
|
678
|
+
};
|
|
679
|
+
} catch (error) {
|
|
680
|
+
return {
|
|
681
|
+
uri: filePath,
|
|
682
|
+
crs: null,
|
|
683
|
+
bbox: null,
|
|
684
|
+
profile: Object.assign({}, baseProfile, {
|
|
685
|
+
format: 'geotiff',
|
|
686
|
+
renderable: false,
|
|
687
|
+
conversionError: gdalError(error),
|
|
688
|
+
}),
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
async convertDatasetToCog(id, options = {}) {
|
|
694
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id);
|
|
695
|
+
if (!row) throw httpError(404, 'Dataset not found');
|
|
696
|
+
const profile = jsonParse(row.profile, {});
|
|
697
|
+
if (!options.force && profile.format === 'cog' && profile.renderable) return serializeDataset(row);
|
|
698
|
+
const sourcePath = profile.originalUri || row.uri;
|
|
699
|
+
const ext = path.extname(sourcePath).toLowerCase();
|
|
700
|
+
if (ext !== '.tif' && ext !== '.tiff') throw httpError(400, 'Only GeoTIFF datasets can be converted to COG');
|
|
701
|
+
const stat = await fs.stat(sourcePath).catch(() => undefined);
|
|
702
|
+
if (!stat) throw httpError(404, 'Dataset source file not found');
|
|
703
|
+
const converted = await this.convertRasterToCog(sourcePath, id, {
|
|
704
|
+
fileName: profile.fileName || path.basename(sourcePath),
|
|
705
|
+
sizeBytes: stat.size,
|
|
706
|
+
});
|
|
707
|
+
this.db.prepare('UPDATE datasets SET uri = ?, crs = ?, bbox = ?, profile = ? WHERE id = ?').run(
|
|
708
|
+
converted.uri,
|
|
709
|
+
converted.crs,
|
|
710
|
+
converted.bbox ? jsonString(converted.bbox, null) : null,
|
|
711
|
+
jsonString(converted.profile, {}),
|
|
712
|
+
id,
|
|
713
|
+
);
|
|
714
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id));
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
sanitizeDatabase(row) {
|
|
718
|
+
return {
|
|
719
|
+
id: row.id,
|
|
720
|
+
projectId: row.project_id,
|
|
721
|
+
name: row.name,
|
|
722
|
+
host: row.host,
|
|
723
|
+
port: row.port,
|
|
724
|
+
dbname: row.dbname,
|
|
725
|
+
username: row.username,
|
|
726
|
+
sslmode: row.sslmode,
|
|
727
|
+
createdAt: row.created_at,
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
listDatabases(projectId) {
|
|
732
|
+
this.assertProject(projectId);
|
|
733
|
+
return this.db.prepare('SELECT * FROM databases WHERE project_id = ? ORDER BY created_at, rowid').all(projectId)
|
|
734
|
+
.map((row) => this.sanitizeDatabase(row));
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
async fetchPostgisTables(config) {
|
|
738
|
+
const env = Object.assign({}, process.env, { GIS_PG_CONFIG: JSON.stringify(config) });
|
|
739
|
+
try {
|
|
740
|
+
const { stdout } = await execFileAsync('python3', [PG_TABLES_SCRIPT], { env, maxBuffer: 20 * 1024 * 1024 });
|
|
741
|
+
const data = JSON.parse(stdout);
|
|
742
|
+
if (data.error) throw httpError(400, data.error);
|
|
743
|
+
return Array.isArray(data.tables) ? data.tables : [];
|
|
744
|
+
} catch (error) {
|
|
745
|
+
if (error && error.status) throw error;
|
|
746
|
+
let message = error && error.message ? error.message : 'PostgreSQL connection failed';
|
|
747
|
+
try {
|
|
748
|
+
const payload = JSON.parse(error.stdout || '{}');
|
|
749
|
+
if (payload.error) message = payload.error;
|
|
750
|
+
} catch (parseError) {
|
|
751
|
+
// Keep the process error.
|
|
752
|
+
}
|
|
753
|
+
throw httpError(400, message);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async createDatabase(projectId, input = {}) {
|
|
758
|
+
this.assertProject(projectId);
|
|
759
|
+
const name = String(input.name || '').trim() || 'PostgreSQL';
|
|
760
|
+
const host = String(input.host || '').trim();
|
|
761
|
+
const port = Math.min(65535, Math.max(1, Number(input.port) || 5432));
|
|
762
|
+
const dbname = String(input.dbname || '').trim();
|
|
763
|
+
const username = String(input.username || '').trim();
|
|
764
|
+
const password = String(input.password || '');
|
|
765
|
+
const sslmode = String(input.sslmode || 'prefer').trim() || 'prefer';
|
|
766
|
+
if (!host || !dbname || !username) throw httpError(400, 'host, dbname, and username are required');
|
|
767
|
+
await this.fetchPostgisTables({ host, port, dbname, username, password, sslmode });
|
|
768
|
+
const id = randomUUID();
|
|
769
|
+
this.db.prepare(`
|
|
770
|
+
INSERT INTO databases (id, project_id, name, host, port, dbname, username, password, sslmode)
|
|
771
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
772
|
+
`).run(id, projectId, name.slice(0, 120), host, port, dbname, username, password, sslmode);
|
|
773
|
+
return this.sanitizeDatabase(this.db.prepare('SELECT * FROM databases WHERE id = ?').get(id));
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
async listDatabaseTables(id) {
|
|
777
|
+
const row = this.db.prepare('SELECT * FROM databases WHERE id = ?').get(id);
|
|
778
|
+
if (!row) throw httpError(404, 'Database registration not found');
|
|
779
|
+
const tables = await this.fetchPostgisTables({
|
|
780
|
+
host: row.host,
|
|
781
|
+
port: row.port,
|
|
782
|
+
dbname: row.dbname,
|
|
783
|
+
username: row.username,
|
|
784
|
+
password: row.password,
|
|
785
|
+
sslmode: row.sslmode,
|
|
786
|
+
});
|
|
787
|
+
return tables.map((table) => Object.assign({ databaseId: id }, table));
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
deleteDatabase(id) {
|
|
791
|
+
const result = this.db.prepare('DELETE FROM databases WHERE id = ?').run(id);
|
|
792
|
+
return result.changes > 0;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
async importPostgisTable(databaseId, input = {}) {
|
|
796
|
+
const database = this.db.prepare('SELECT * FROM databases WHERE id = ?').get(databaseId);
|
|
797
|
+
if (!database) throw httpError(404, 'Database registration not found');
|
|
798
|
+
this.assertProject(database.project_id);
|
|
799
|
+
const schema = String(input.schema || 'public');
|
|
800
|
+
const tableName = String(input.table || '').trim();
|
|
801
|
+
if (!tableName) throw httpError(400, 'table is required');
|
|
802
|
+
const tables = await this.fetchPostgisTables({
|
|
803
|
+
host: database.host,
|
|
804
|
+
port: database.port,
|
|
805
|
+
dbname: database.dbname,
|
|
806
|
+
username: database.username,
|
|
807
|
+
password: database.password,
|
|
808
|
+
sslmode: database.sslmode,
|
|
809
|
+
});
|
|
810
|
+
const source = tables.find((item) => item.schema === schema && item.table === tableName);
|
|
811
|
+
if (!source) throw httpError(404, 'Spatial table not found');
|
|
812
|
+
const projectId = database.project_id;
|
|
813
|
+
const id = randomUUID();
|
|
814
|
+
const targetTable = 'ds_' + id.replace(/-/g, '');
|
|
815
|
+
const gpkgPath = await this.projectGpkgPath(projectId);
|
|
816
|
+
const derivedDir = path.join(this.dataDir, 'derived', id);
|
|
817
|
+
await fs.mkdir(derivedDir, { recursive: true });
|
|
818
|
+
const previewPath = path.join(derivedDir, 'preview.geojson');
|
|
819
|
+
const connection = 'PG:host=' + database.host + ' port=' + database.port + ' dbname=' + database.dbname
|
|
820
|
+
+ ' user=' + database.username + ' sslmode=' + database.sslmode;
|
|
821
|
+
const env = Object.assign({}, process.env, { PGPASSWORD: database.password });
|
|
822
|
+
try {
|
|
823
|
+
await execFileAsync('ogr2ogr', [
|
|
824
|
+
'-f', 'GPKG',
|
|
825
|
+
'-t_srs', 'EPSG:4326',
|
|
826
|
+
'-lco', 'SPATIAL_INDEX=YES',
|
|
827
|
+
'-nlt', 'PROMOTE_TO_MULTI',
|
|
828
|
+
'-makevalid',
|
|
829
|
+
'-nln', targetTable,
|
|
830
|
+
gpkgPath,
|
|
831
|
+
connection,
|
|
832
|
+
source.schema + '.' + source.table,
|
|
833
|
+
], { env, maxBuffer: 20 * 1024 * 1024 });
|
|
834
|
+
const info = JSON.parse((await execFileAsync('python3', [VECTOR_INFO_SCRIPT, gpkgPath, targetTable], {
|
|
835
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
836
|
+
})).stdout);
|
|
837
|
+
const layer = info.layer || {};
|
|
838
|
+
const bbox = Array.isArray(layer.bbox) && layer.bbox.length === 4 && layer.bbox.every(Number.isFinite)
|
|
839
|
+
? layer.bbox.map(Number)
|
|
840
|
+
: null;
|
|
841
|
+
await execFileAsync('ogr2ogr', [
|
|
842
|
+
'-f', 'GeoJSON', '-t_srs', 'EPSG:4326', previewPath, gpkgPath, targetTable,
|
|
843
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
844
|
+
const previewStat = await fs.stat(previewPath);
|
|
845
|
+
const profile = {
|
|
846
|
+
format: 'gpkg',
|
|
847
|
+
storage: 'project-gpkg',
|
|
848
|
+
sourceFormat: 'postgis',
|
|
849
|
+
databaseId,
|
|
850
|
+
databaseName: database.name,
|
|
851
|
+
sourceSchema: source.schema,
|
|
852
|
+
sourceTable: source.table,
|
|
853
|
+
geometryColumn: source.geometryColumn,
|
|
854
|
+
geometryType: source.geometryType,
|
|
855
|
+
name: source.table,
|
|
856
|
+
layerName: source.table,
|
|
857
|
+
fileName: source.schema + '.' + source.table,
|
|
858
|
+
gpkgPath,
|
|
859
|
+
tableName: targetTable,
|
|
860
|
+
previewPath,
|
|
861
|
+
previewSizeBytes: previewStat.size,
|
|
862
|
+
updatedAt: new Date().toISOString(),
|
|
863
|
+
featureCount: Number.isFinite(Number(layer.featureCount)) ? Number(layer.featureCount) : 0,
|
|
864
|
+
fields: Array.isArray(layer.fields) ? layer.fields.map((field) => field.name).filter(Boolean) : [],
|
|
865
|
+
bbox,
|
|
866
|
+
crs: 4326,
|
|
867
|
+
renderable: Boolean(bbox),
|
|
868
|
+
};
|
|
869
|
+
this.db.prepare(
|
|
870
|
+
'INSERT INTO datasets (id, project_id, kind, uri, crs, bbox, profile) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
871
|
+
).run(
|
|
872
|
+
id,
|
|
873
|
+
projectId,
|
|
874
|
+
'vector',
|
|
875
|
+
gpkgPath,
|
|
876
|
+
4326,
|
|
877
|
+
bbox ? jsonString(bbox, null) : null,
|
|
878
|
+
jsonString(profile, {}),
|
|
879
|
+
);
|
|
880
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id));
|
|
881
|
+
} catch (error) {
|
|
882
|
+
await fs.rm(derivedDir, { recursive: true, force: true }).catch(() => {});
|
|
883
|
+
throw httpError(422, 'PostGIS import failed: ' + gdalError(error));
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
async projectGpkgPath(projectId) {
|
|
888
|
+
const dir = path.join(this.dataDir, 'projects');
|
|
889
|
+
await fs.mkdir(dir, { recursive: true });
|
|
890
|
+
return path.join(dir, projectId + '.gpkg');
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
async listGpkgLayers(gpkgPath) {
|
|
894
|
+
const text = (await execFileAsync('python3', [VECTOR_INFO_SCRIPT, gpkgPath], { maxBuffer: 20 * 1024 * 1024 })).stdout;
|
|
895
|
+
const info = JSON.parse(text);
|
|
896
|
+
return (info.layers || []).map((layer) => layer.name).filter(Boolean);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
async importVectorLayer(projectId, sourcePath, options = {}) {
|
|
900
|
+
const ext = path.extname(sourcePath).toLowerCase();
|
|
901
|
+
const sourceStat = await fs.stat(sourcePath);
|
|
902
|
+
const layerName = String(options.layerName || path.basename(sourcePath, ext) || 'vector').trim() || 'vector';
|
|
903
|
+
const fileName = options.fileName || path.basename(sourcePath);
|
|
904
|
+
const id = randomUUID();
|
|
905
|
+
const tableName = 'ds_' + id.replace(/-/g, '');
|
|
906
|
+
const gpkgPath = await this.projectGpkgPath(projectId);
|
|
907
|
+
const derivedDir = path.join(this.dataDir, 'derived', id);
|
|
908
|
+
await fs.mkdir(derivedDir, { recursive: true });
|
|
909
|
+
const previewPath = path.join(derivedDir, 'preview.geojson');
|
|
910
|
+
try {
|
|
911
|
+
const args = [
|
|
912
|
+
'-f', 'GPKG',
|
|
913
|
+
'-update',
|
|
914
|
+
'-t_srs', 'EPSG:4326',
|
|
915
|
+
'-lco', 'SPATIAL_INDEX=YES',
|
|
916
|
+
'-nlt', 'PROMOTE_TO_MULTI',
|
|
917
|
+
'-makevalid',
|
|
918
|
+
'-nln', tableName,
|
|
919
|
+
gpkgPath,
|
|
920
|
+
sourcePath,
|
|
921
|
+
];
|
|
922
|
+
if (options.sourceLayer) args.push(options.sourceLayer);
|
|
923
|
+
await execFileAsync('ogr2ogr', args, { maxBuffer: 20 * 1024 * 1024 });
|
|
924
|
+
const info = JSON.parse((await execFileAsync('python3', [VECTOR_INFO_SCRIPT, gpkgPath, tableName], {
|
|
925
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
926
|
+
})).stdout);
|
|
927
|
+
const layer = info.layer || {};
|
|
928
|
+
const bbox = Array.isArray(layer.bbox) && layer.bbox.length === 4 && layer.bbox.every(Number.isFinite)
|
|
929
|
+
? layer.bbox.map(Number)
|
|
930
|
+
: null;
|
|
931
|
+
await execFileAsync('ogr2ogr', [
|
|
932
|
+
'-f', 'GeoJSON',
|
|
933
|
+
'-t_srs', 'EPSG:4326',
|
|
934
|
+
previewPath,
|
|
935
|
+
gpkgPath,
|
|
936
|
+
tableName,
|
|
937
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
938
|
+
const previewStat = await fs.stat(previewPath);
|
|
939
|
+
const profile = {
|
|
940
|
+
format: 'gpkg',
|
|
941
|
+
storage: 'project-gpkg',
|
|
942
|
+
sourceFormat: options.sourceFormat || ext.replace(/^\./, ''),
|
|
943
|
+
name: layerName,
|
|
944
|
+
layerName,
|
|
945
|
+
fileName,
|
|
946
|
+
sizeBytes: options.sizeBytes || sourceStat.size,
|
|
947
|
+
originalUri: options.originalUri || sourcePath,
|
|
948
|
+
sourceEntry: options.sourceEntry,
|
|
949
|
+
gpkgPath,
|
|
950
|
+
tableName,
|
|
951
|
+
previewPath,
|
|
952
|
+
previewSizeBytes: previewStat.size,
|
|
953
|
+
updatedAt: new Date().toISOString(),
|
|
954
|
+
featureCount: Number.isFinite(Number(layer.featureCount)) ? Number(layer.featureCount) : 0,
|
|
955
|
+
fields: Array.isArray(layer.fields) ? layer.fields.map((field) => field.name).filter(Boolean) : [],
|
|
956
|
+
bbox,
|
|
957
|
+
crs: 4326,
|
|
958
|
+
renderable: Boolean(bbox),
|
|
959
|
+
};
|
|
960
|
+
this.db.prepare(
|
|
961
|
+
'INSERT INTO datasets (id, project_id, kind, uri, crs, bbox, profile) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
962
|
+
).run(
|
|
963
|
+
id,
|
|
964
|
+
projectId,
|
|
965
|
+
'vector',
|
|
966
|
+
gpkgPath,
|
|
967
|
+
4326,
|
|
968
|
+
bbox ? jsonString(bbox, null) : null,
|
|
969
|
+
jsonString(profile, {}),
|
|
970
|
+
);
|
|
971
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id));
|
|
972
|
+
} catch (error) {
|
|
973
|
+
throw httpError(422, 'Vector import failed: ' + gdalError(error));
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
async importVectorFile(projectId, sourcePath, options = {}) {
|
|
978
|
+
const ext = path.extname(sourcePath).toLowerCase();
|
|
979
|
+
if (ext === '.gpkg') {
|
|
980
|
+
const layers = await this.listGpkgLayers(sourcePath);
|
|
981
|
+
if (layers.length === 0) throw httpError(400, 'GeoPackage contains no vector layers');
|
|
982
|
+
const datasets = [];
|
|
983
|
+
for (const layerName of layers) {
|
|
984
|
+
datasets.push(await this.importVectorLayer(projectId, sourcePath, Object.assign({}, options, {
|
|
985
|
+
sourceFormat: 'gpkg',
|
|
986
|
+
sourceLayer: layerName,
|
|
987
|
+
layerName,
|
|
988
|
+
fileName: options.fileName || path.basename(sourcePath),
|
|
989
|
+
})));
|
|
990
|
+
}
|
|
991
|
+
return datasets;
|
|
992
|
+
}
|
|
993
|
+
return [await this.importVectorLayer(projectId, sourcePath, Object.assign({}, options, {
|
|
994
|
+
sourceFormat: ext.replace(/^\./, ''),
|
|
995
|
+
layerName: options.layerName || path.basename(sourcePath, ext),
|
|
996
|
+
}))];
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
async createDatasetsFromPath(projectId, filePath, options = {}) {
|
|
1000
|
+
this.assertProject(projectId);
|
|
1001
|
+
const realPath = await this.resolveAllowed(filePath);
|
|
1002
|
+
const stat = await fs.stat(realPath);
|
|
1003
|
+
if (!stat.isFile()) throw httpError(400, 'Dataset path must be a file');
|
|
1004
|
+
if (stat.size > MAX_FILE_BYTES) throw httpError(413, 'Dataset file is too large');
|
|
1005
|
+
const ext = path.extname(realPath).toLowerCase();
|
|
1006
|
+
if (ext === '.zip') {
|
|
1007
|
+
const importDir = path.join(this.dataDir, 'imports', randomUUID());
|
|
1008
|
+
await fs.mkdir(importDir, { recursive: true });
|
|
1009
|
+
try {
|
|
1010
|
+
await execFileAsync('python3', [UNZIP_SCRIPT, realPath, importDir], { maxBuffer: 20 * 1024 * 1024 });
|
|
1011
|
+
const files = await walkFiles(importDir);
|
|
1012
|
+
const sources = [];
|
|
1013
|
+
for (const file of files) {
|
|
1014
|
+
const fileExt = path.extname(file).toLowerCase();
|
|
1015
|
+
if (fileExt === '.shp') {
|
|
1016
|
+
sources.push({ type: 'shp', path: file });
|
|
1017
|
+
} else if (fileExt === '.gpkg') {
|
|
1018
|
+
const layers = await this.listGpkgLayers(file);
|
|
1019
|
+
for (const layerName of layers) sources.push({ type: 'gpkg', path: file, layerName });
|
|
1020
|
+
} else if (fileExt === '.geojson' || (fileExt === '.json' && await isGeoJsonFile(file))) {
|
|
1021
|
+
sources.push({ type: 'geojson', path: file });
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
if (sources.length === 0) {
|
|
1025
|
+
throw httpError(400, '压缩包内没有识别到 SHP、GeoJSON 或 GeoPackage 矢量数据');
|
|
1026
|
+
}
|
|
1027
|
+
const datasets = [];
|
|
1028
|
+
for (const source of sources) {
|
|
1029
|
+
const sourceStat = await fs.stat(source.path);
|
|
1030
|
+
if (sourceStat.size > MAX_FILE_BYTES) throw httpError(413, 'Extracted vector file is too large');
|
|
1031
|
+
const sourceEntry = path.relative(importDir, source.path);
|
|
1032
|
+
if (source.type === 'gpkg') {
|
|
1033
|
+
datasets.push(await this.importVectorLayer(projectId, source.path, {
|
|
1034
|
+
sourceFormat: 'gpkg',
|
|
1035
|
+
sourceLayer: source.layerName,
|
|
1036
|
+
layerName: source.layerName,
|
|
1037
|
+
fileName: path.basename(source.path),
|
|
1038
|
+
originalUri: realPath,
|
|
1039
|
+
sourceEntry,
|
|
1040
|
+
sizeBytes: sourceStat.size,
|
|
1041
|
+
}));
|
|
1042
|
+
} else {
|
|
1043
|
+
datasets.push(...await this.importVectorFile(projectId, source.path, {
|
|
1044
|
+
sourceFormat: source.type,
|
|
1045
|
+
fileName: path.basename(source.path),
|
|
1046
|
+
originalUri: realPath,
|
|
1047
|
+
sourceEntry,
|
|
1048
|
+
sizeBytes: sourceStat.size,
|
|
1049
|
+
}));
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
return datasets;
|
|
1053
|
+
} finally {
|
|
1054
|
+
await fs.rm(importDir, { recursive: true, force: true });
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
if (ext === '.gpkg') {
|
|
1058
|
+
return this.importVectorFile(projectId, realPath, {
|
|
1059
|
+
originalUri: realPath,
|
|
1060
|
+
sourceEntry: path.basename(realPath),
|
|
1061
|
+
sizeBytes: stat.size,
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
if (ext === '.shp' || ext === '.geojson' || (ext === '.json' && await isGeoJsonFile(realPath))) {
|
|
1065
|
+
return this.importVectorFile(projectId, realPath, {
|
|
1066
|
+
originalUri: realPath,
|
|
1067
|
+
sourceEntry: path.basename(realPath),
|
|
1068
|
+
sizeBytes: stat.size,
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
return [await this.createDatasetFromPath(projectId, realPath, options)];
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
async createDatasetFromPath(projectId, filePath, options = {}) {
|
|
1075
|
+
this.assertProject(projectId);
|
|
1076
|
+
const realPath = await this.resolveAllowed(filePath);
|
|
1077
|
+
const stat = await fs.stat(realPath);
|
|
1078
|
+
if (!stat.isFile()) throw httpError(400, 'Dataset path must be a file');
|
|
1079
|
+
if (stat.size > MAX_FILE_BYTES) throw httpError(413, 'Dataset file is too large');
|
|
1080
|
+
const id = randomUUID();
|
|
1081
|
+
const ext = path.extname(realPath).toLowerCase();
|
|
1082
|
+
let inspected;
|
|
1083
|
+
if (ext === '.tif' || ext === '.tiff') {
|
|
1084
|
+
inspected = await this.convertRasterToCog(realPath, id, {
|
|
1085
|
+
fileName: options.fileName,
|
|
1086
|
+
sizeBytes: stat.size,
|
|
1087
|
+
});
|
|
1088
|
+
inspected.kind = 'raster';
|
|
1089
|
+
} else {
|
|
1090
|
+
inspected = await inspectFile(realPath, stat.size, options);
|
|
1091
|
+
}
|
|
1092
|
+
const storedUri = inspected.uri || realPath;
|
|
1093
|
+
this.db.prepare(
|
|
1094
|
+
'INSERT INTO datasets (id, project_id, kind, uri, crs, bbox, profile) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
1095
|
+
).run(
|
|
1096
|
+
id,
|
|
1097
|
+
projectId,
|
|
1098
|
+
inspected.kind,
|
|
1099
|
+
storedUri,
|
|
1100
|
+
inspected.crs,
|
|
1101
|
+
inspected.bbox ? jsonString(inspected.bbox, null) : null,
|
|
1102
|
+
jsonString(inspected.profile, {}),
|
|
1103
|
+
);
|
|
1104
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id));
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
findLayerRow(layerId) {
|
|
1108
|
+
return this.db.prepare(`
|
|
1109
|
+
SELECT l.*, d.kind AS dataset_kind, d.uri AS dataset_uri, d.crs AS dataset_crs,
|
|
1110
|
+
d.bbox AS dataset_bbox, d.profile AS dataset_profile,
|
|
1111
|
+
json_extract(d.profile, '$.format') AS dataset_format
|
|
1112
|
+
FROM layers l LEFT JOIN datasets d ON d.id = l.dataset_id
|
|
1113
|
+
WHERE l.id = ?
|
|
1114
|
+
`).get(layerId);
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
projectName(projectId) {
|
|
1118
|
+
const row = this.db.prepare('SELECT name FROM projects WHERE id = ?').get(projectId);
|
|
1119
|
+
return row ? row.name : undefined;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
listAllLayers(input = {}) {
|
|
1123
|
+
const query = String(input.query || '').trim().toLowerCase();
|
|
1124
|
+
const limit = Math.min(200, Math.max(1, Number(input.limit) || 100));
|
|
1125
|
+
const output = [];
|
|
1126
|
+
for (const project of this.listProjects()) {
|
|
1127
|
+
for (const layer of this.listLayers(project.id)) {
|
|
1128
|
+
const profile = layer.dataset && layer.dataset.profile ? layer.dataset.profile : {};
|
|
1129
|
+
const haystack = [
|
|
1130
|
+
layer.name,
|
|
1131
|
+
project.name,
|
|
1132
|
+
profile.name,
|
|
1133
|
+
profile.fileName,
|
|
1134
|
+
profile.tableName,
|
|
1135
|
+
layer.dataset && layer.dataset.format,
|
|
1136
|
+
].map((value) => String(value || '').toLowerCase());
|
|
1137
|
+
if (query && !haystack.some((value) => value.includes(query))) continue;
|
|
1138
|
+
output.push({
|
|
1139
|
+
layerId: layer.id,
|
|
1140
|
+
projectId: project.id,
|
|
1141
|
+
projectName: project.name,
|
|
1142
|
+
name: layer.name,
|
|
1143
|
+
datasetId: layer.datasetId,
|
|
1144
|
+
datasetName: profile.name || profile.fileName,
|
|
1145
|
+
format: layer.dataset && layer.dataset.format,
|
|
1146
|
+
kind: layer.dataset && layer.dataset.kind,
|
|
1147
|
+
featureCount: profile.featureCount,
|
|
1148
|
+
fields: Array.isArray(profile.fields) ? profile.fields : [],
|
|
1149
|
+
bbox: layer.dataset && layer.dataset.bbox,
|
|
1150
|
+
visible: layer.visible,
|
|
1151
|
+
z: layer.z,
|
|
1152
|
+
style: layer.style,
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
return output.slice(0, limit);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
describeLayer(layerId) {
|
|
1160
|
+
const row = this.findLayerRow(layerId);
|
|
1161
|
+
if (!row) throw httpError(404, 'Layer not found');
|
|
1162
|
+
const layer = serializeLayer(row);
|
|
1163
|
+
const profile = layer.dataset && layer.dataset.profile ? layer.dataset.profile : {};
|
|
1164
|
+
return {
|
|
1165
|
+
layer,
|
|
1166
|
+
projectId: row.project_id,
|
|
1167
|
+
projectName: this.projectName(row.project_id),
|
|
1168
|
+
fields: Array.isArray(profile.fields) ? profile.fields : [],
|
|
1169
|
+
featureCount: profile.featureCount,
|
|
1170
|
+
format: layer.dataset && layer.dataset.format,
|
|
1171
|
+
kind: layer.dataset && layer.dataset.kind,
|
|
1172
|
+
bbox: layer.dataset && layer.dataset.bbox,
|
|
1173
|
+
style: layer.style,
|
|
1174
|
+
renderable: profile.renderable !== false,
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
async sampleLayerField(layerId, field, limitInput) {
|
|
1179
|
+
const inputField = String(field || '').trim();
|
|
1180
|
+
if (!inputField) throw httpError(400, 'field is required');
|
|
1181
|
+
const limit = Math.min(500, Math.max(1, Number(limitInput) || 50));
|
|
1182
|
+
const row = this.findLayerRow(layerId);
|
|
1183
|
+
if (!row) throw httpError(404, 'Layer not found');
|
|
1184
|
+
const profile = jsonParse(row.dataset_profile, {});
|
|
1185
|
+
const fields = Array.isArray(profile.fields) ? profile.fields : [];
|
|
1186
|
+
if (fields.length > 0 && !fields.includes(inputField)) {
|
|
1187
|
+
throw httpError(400, 'Field "' + inputField + '" is not present on this layer. Available fields: ' + fields.join(', '));
|
|
1188
|
+
}
|
|
1189
|
+
if (profile.format === 'gpkg' && profile.gpkgPath && profile.tableName) {
|
|
1190
|
+
const table = String(profile.tableName);
|
|
1191
|
+
if (!/^[A-Za-z0-9_]+$/.test(table)) throw httpError(400, 'Invalid GeoPackage table name');
|
|
1192
|
+
const quotedField = '"' + inputField.replace(/"/g, '""') + '"';
|
|
1193
|
+
const db = new DatabaseSync(profile.gpkgPath, { readOnly: true });
|
|
1194
|
+
try {
|
|
1195
|
+
const values = db.prepare(`SELECT DISTINCT ${quotedField} AS value FROM "${table}" WHERE ${quotedField} IS NOT NULL LIMIT ?`)
|
|
1196
|
+
.all(limit + 1)
|
|
1197
|
+
.map((item) => item.value);
|
|
1198
|
+
return {
|
|
1199
|
+
layerId,
|
|
1200
|
+
field: inputField,
|
|
1201
|
+
values: values.slice(0, limit),
|
|
1202
|
+
truncated: values.length > limit,
|
|
1203
|
+
};
|
|
1204
|
+
} finally {
|
|
1205
|
+
db.close();
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (profile.format === 'geojson') {
|
|
1209
|
+
const sourcePath = profile.previewPath || row.dataset_uri;
|
|
1210
|
+
const text = await fs.readFile(sourcePath, 'utf8');
|
|
1211
|
+
const parsed = JSON.parse(text);
|
|
1212
|
+
const values = [];
|
|
1213
|
+
const seen = new Set();
|
|
1214
|
+
for (const feature of Array.isArray(parsed.features) ? parsed.features : []) {
|
|
1215
|
+
const value = feature && feature.properties ? feature.properties[inputField] : undefined;
|
|
1216
|
+
if (value === undefined || value === null) continue;
|
|
1217
|
+
const key = String(value);
|
|
1218
|
+
if (seen.has(key)) continue;
|
|
1219
|
+
seen.add(key);
|
|
1220
|
+
values.push(value);
|
|
1221
|
+
if (values.length > limit) break;
|
|
1222
|
+
}
|
|
1223
|
+
return {
|
|
1224
|
+
layerId,
|
|
1225
|
+
field: inputField,
|
|
1226
|
+
values: values.slice(0, limit),
|
|
1227
|
+
truncated: values.length > limit,
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
throw httpError(415, 'This layer format does not expose attribute fields');
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
updateLayerStyle(layerId, stylePatch) {
|
|
1234
|
+
this.findLayerRow(layerId);
|
|
1235
|
+
const row = this.db.prepare('SELECT * FROM layers WHERE id = ?').get(layerId);
|
|
1236
|
+
if (!row) throw httpError(404, 'Layer not found');
|
|
1237
|
+
const base = jsonParse(row.style, {});
|
|
1238
|
+
let merged;
|
|
1239
|
+
try {
|
|
1240
|
+
merged = mergeLayerStyle(base, stylePatch);
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
throw httpError(400, error.message);
|
|
1243
|
+
}
|
|
1244
|
+
const datasetRow = this.db.prepare('SELECT profile FROM datasets WHERE id = ?').get(row.dataset_id);
|
|
1245
|
+
const datasetProfile = datasetRow ? jsonParse(datasetRow.profile, {}) : {};
|
|
1246
|
+
const fields = Array.isArray(datasetProfile.fields) ? datasetProfile.fields : [];
|
|
1247
|
+
if (merged.label && merged.label.field && fields.length > 0 && !fields.includes(merged.label.field)) {
|
|
1248
|
+
throw httpError(400, 'Label field "' + merged.label.field + '" is not present. Available fields: ' + fields.join(', '));
|
|
1249
|
+
}
|
|
1250
|
+
if (merged.categorical && merged.categorical.field && fields.length > 0 && !fields.includes(merged.categorical.field)) {
|
|
1251
|
+
throw httpError(400, 'Categorical field "' + merged.categorical.field + '" is not present. Available fields: ' + fields.join(', '));
|
|
1252
|
+
}
|
|
1253
|
+
this.db.prepare('UPDATE layers SET style = ? WHERE id = ?').run(jsonString(merged, {}), layerId);
|
|
1254
|
+
const updated = this.listLayers(row.project_id).find((layer) => layer.id === layerId);
|
|
1255
|
+
return { layer: updated, style: merged, projectId: row.project_id, projectName: this.projectName(row.project_id) };
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
mapCapabilities() {
|
|
1259
|
+
return {
|
|
1260
|
+
styleSchema: STYLE_SCHEMA,
|
|
1261
|
+
fonts: STYLE_SCHEMA.fonts,
|
|
1262
|
+
glyphsUrl: STYLE_SCHEMA.glyphsUrl,
|
|
1263
|
+
supportsLabel: true,
|
|
1264
|
+
notes: 'Label text uses the default MapLibre font stack; CJK glyphs are rendered locally by the browser. Unknown label.range glyph requests fall back to local rendering.',
|
|
1265
|
+
};
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
async updateVectorFeature(datasetId, input = {}) {
|
|
1269
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(datasetId);
|
|
1270
|
+
if (!row) throw httpError(404, 'Dataset not found');
|
|
1271
|
+
const profile = jsonParse(row.profile, {});
|
|
1272
|
+
if (profile.sourceFormat !== 'drawn' || !profile.tableName || !profile.gpkgPath) {
|
|
1273
|
+
throw httpError(400, 'This dataset is not an editable polygon layer');
|
|
1274
|
+
}
|
|
1275
|
+
const geometry = input.geometry;
|
|
1276
|
+
if (!geometry || !['Polygon', 'MultiPolygon'].includes(geometry.type) || !Array.isArray(geometry.coordinates)) {
|
|
1277
|
+
throw httpError(400, 'geometry must be a GeoJSON Polygon or MultiPolygon');
|
|
1278
|
+
}
|
|
1279
|
+
const env = Object.assign({}, process.env, {
|
|
1280
|
+
GIS_VECTOR_EDIT: JSON.stringify({
|
|
1281
|
+
gpkg: profile.gpkgPath,
|
|
1282
|
+
table: profile.tableName,
|
|
1283
|
+
geometry,
|
|
1284
|
+
name: input.name,
|
|
1285
|
+
createdAt: input.createdAt,
|
|
1286
|
+
}),
|
|
1287
|
+
});
|
|
1288
|
+
try {
|
|
1289
|
+
const output = await execFileAsync('python3', [VECTOR_EDIT_SCRIPT, 'update'], { env, maxBuffer: 20 * 1024 * 1024 });
|
|
1290
|
+
const result = JSON.parse(output.stdout || '{}');
|
|
1291
|
+
if (!result.updated) throw httpError(404, 'Feature not found');
|
|
1292
|
+
await execFileAsync('ogr2ogr', [
|
|
1293
|
+
'-f', 'GeoJSON', '-t_srs', 'EPSG:4326', profile.previewPath, profile.gpkgPath, profile.tableName,
|
|
1294
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
1295
|
+
const info = JSON.parse((await execFileAsync('python3', [VECTOR_INFO_SCRIPT, profile.gpkgPath, profile.tableName], {
|
|
1296
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
1297
|
+
})).stdout);
|
|
1298
|
+
const layer = info.layer || {};
|
|
1299
|
+
const previewData = JSON.parse(await fs.readFile(profile.previewPath, 'utf8'));
|
|
1300
|
+
const bounds = geoJsonBounds(previewData);
|
|
1301
|
+
const bbox = bounds.bbox;
|
|
1302
|
+
const previewStat = await fs.stat(profile.previewPath);
|
|
1303
|
+
const nextProfile = Object.assign({}, profile, {
|
|
1304
|
+
bbox,
|
|
1305
|
+
previewSizeBytes: previewStat.size,
|
|
1306
|
+
updatedAt: new Date().toISOString(),
|
|
1307
|
+
featureCount: bounds.count,
|
|
1308
|
+
fields: Array.isArray(layer.fields) ? layer.fields.map((field) => field.name).filter(Boolean) : profile.fields,
|
|
1309
|
+
renderable: true,
|
|
1310
|
+
});
|
|
1311
|
+
this.db.prepare('UPDATE datasets SET bbox = ?, profile = ? WHERE id = ?').run(
|
|
1312
|
+
bbox ? jsonString(bbox, null) : null,
|
|
1313
|
+
jsonString(nextProfile, {}),
|
|
1314
|
+
datasetId,
|
|
1315
|
+
);
|
|
1316
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(datasetId));
|
|
1317
|
+
} catch (error) {
|
|
1318
|
+
if (error && error.status) throw error;
|
|
1319
|
+
throw httpError(422, 'Could not update feature: ' + gdalError(error));
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
async deleteVectorFeature(datasetId, input = {}) {
|
|
1324
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(datasetId);
|
|
1325
|
+
if (!row) throw httpError(404, 'Dataset not found');
|
|
1326
|
+
const profile = jsonParse(row.profile, {});
|
|
1327
|
+
if (profile.sourceFormat !== 'drawn' || !profile.tableName || !profile.gpkgPath) {
|
|
1328
|
+
throw httpError(400, 'This dataset is not an editable polygon layer');
|
|
1329
|
+
}
|
|
1330
|
+
const name = input.name !== undefined ? String(input.name) : undefined;
|
|
1331
|
+
const createdAt = input.createdAt !== undefined ? String(input.createdAt) : undefined;
|
|
1332
|
+
if ((name === undefined || name === '') && (createdAt === undefined || createdAt === '')) {
|
|
1333
|
+
throw httpError(400, 'Feature name or createdAt is required');
|
|
1334
|
+
}
|
|
1335
|
+
const env = Object.assign({}, process.env, {
|
|
1336
|
+
GIS_VECTOR_EDIT: JSON.stringify({
|
|
1337
|
+
gpkg: profile.gpkgPath,
|
|
1338
|
+
table: profile.tableName,
|
|
1339
|
+
name,
|
|
1340
|
+
createdAt,
|
|
1341
|
+
}),
|
|
1342
|
+
});
|
|
1343
|
+
try {
|
|
1344
|
+
const output = await execFileAsync('python3', [VECTOR_EDIT_SCRIPT, 'delete'], { env, maxBuffer: 20 * 1024 * 1024 });
|
|
1345
|
+
const result = JSON.parse(output.stdout || '{}');
|
|
1346
|
+
if (!result.deleted) throw httpError(404, 'Feature not found');
|
|
1347
|
+
await execFileAsync('ogr2ogr', [
|
|
1348
|
+
'-f', 'GeoJSON', '-t_srs', 'EPSG:4326', profile.previewPath, profile.gpkgPath, profile.tableName,
|
|
1349
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
1350
|
+
const info = JSON.parse((await execFileAsync('python3', [VECTOR_INFO_SCRIPT, profile.gpkgPath, profile.tableName], {
|
|
1351
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
1352
|
+
})).stdout);
|
|
1353
|
+
const layer = info.layer || {};
|
|
1354
|
+
const previewData = JSON.parse(await fs.readFile(profile.previewPath, 'utf8'));
|
|
1355
|
+
const bounds = geoJsonBounds(previewData);
|
|
1356
|
+
const bbox = bounds.bbox;
|
|
1357
|
+
const previewStat = await fs.stat(profile.previewPath);
|
|
1358
|
+
const nextProfile = Object.assign({}, profile, {
|
|
1359
|
+
bbox,
|
|
1360
|
+
previewSizeBytes: previewStat.size,
|
|
1361
|
+
updatedAt: new Date().toISOString(),
|
|
1362
|
+
featureCount: bounds.count,
|
|
1363
|
+
fields: Array.isArray(layer.fields) ? layer.fields.map((field) => field.name).filter(Boolean) : profile.fields,
|
|
1364
|
+
renderable: true,
|
|
1365
|
+
});
|
|
1366
|
+
this.db.prepare('UPDATE datasets SET bbox = ?, profile = ? WHERE id = ?').run(
|
|
1367
|
+
bbox ? jsonString(bbox, null) : null,
|
|
1368
|
+
jsonString(nextProfile, {}),
|
|
1369
|
+
datasetId,
|
|
1370
|
+
);
|
|
1371
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(datasetId));
|
|
1372
|
+
} catch (error) {
|
|
1373
|
+
if (error && error.status) throw error;
|
|
1374
|
+
throw httpError(422, 'Could not delete feature: ' + gdalError(error));
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
async createVectorLayer(projectId, input = {}) {
|
|
1379
|
+
this.assertProject(projectId);
|
|
1380
|
+
const name = String(input.name || '').trim() || '新建矢量图层';
|
|
1381
|
+
const id = randomUUID();
|
|
1382
|
+
const tableName = 'ds_' + id.replace(/-/g, '');
|
|
1383
|
+
const gpkgPath = await this.projectGpkgPath(projectId);
|
|
1384
|
+
const derivedDir = path.join(this.dataDir, 'derived', id);
|
|
1385
|
+
await fs.mkdir(derivedDir, { recursive: true });
|
|
1386
|
+
const previewPath = path.join(derivedDir, 'preview.geojson');
|
|
1387
|
+
const env = Object.assign({}, process.env, {
|
|
1388
|
+
GIS_VECTOR_EDIT: JSON.stringify({ gpkg: gpkgPath, table: tableName }),
|
|
1389
|
+
});
|
|
1390
|
+
try {
|
|
1391
|
+
await execFileAsync('python3', [VECTOR_EDIT_SCRIPT, 'create'], { env, maxBuffer: 20 * 1024 * 1024 });
|
|
1392
|
+
await fs.writeFile(previewPath, JSON.stringify({ type: 'FeatureCollection', features: [] }), 'utf8');
|
|
1393
|
+
const previewStat = await fs.stat(previewPath);
|
|
1394
|
+
const profile = {
|
|
1395
|
+
format: 'gpkg',
|
|
1396
|
+
storage: 'project-gpkg',
|
|
1397
|
+
sourceFormat: 'drawn',
|
|
1398
|
+
name,
|
|
1399
|
+
layerName: name,
|
|
1400
|
+
fileName: name + '.gpkg',
|
|
1401
|
+
gpkgPath,
|
|
1402
|
+
tableName,
|
|
1403
|
+
previewPath,
|
|
1404
|
+
previewSizeBytes: previewStat.size,
|
|
1405
|
+
updatedAt: new Date().toISOString(),
|
|
1406
|
+
featureCount: 0,
|
|
1407
|
+
fields: ['name', 'created_at'],
|
|
1408
|
+
bbox: null,
|
|
1409
|
+
crs: 4326,
|
|
1410
|
+
renderable: true,
|
|
1411
|
+
};
|
|
1412
|
+
this.db.prepare(
|
|
1413
|
+
'INSERT INTO datasets (id, project_id, kind, uri, crs, bbox, profile) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
1414
|
+
).run(id, projectId, 'vector', gpkgPath, 4326, null, jsonString(profile, {}));
|
|
1415
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id));
|
|
1416
|
+
} catch (error) {
|
|
1417
|
+
await fs.rm(derivedDir, { recursive: true, force: true }).catch(() => {});
|
|
1418
|
+
throw httpError(422, 'Could not create vector layer: ' + gdalError(error));
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
async addVectorFeature(datasetId, input = {}) {
|
|
1423
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(datasetId);
|
|
1424
|
+
if (!row) throw httpError(404, 'Dataset not found');
|
|
1425
|
+
const profile = jsonParse(row.profile, {});
|
|
1426
|
+
if (profile.sourceFormat !== 'drawn' || !profile.tableName || !profile.gpkgPath) {
|
|
1427
|
+
throw httpError(400, 'This dataset is not an editable polygon layer');
|
|
1428
|
+
}
|
|
1429
|
+
const geometry = input.geometry;
|
|
1430
|
+
if (!geometry || !['Polygon', 'MultiPolygon'].includes(geometry.type) || !Array.isArray(geometry.coordinates)) {
|
|
1431
|
+
throw httpError(400, 'geometry must be a GeoJSON Polygon or MultiPolygon');
|
|
1432
|
+
}
|
|
1433
|
+
const count = Number(profile.featureCount) || 0;
|
|
1434
|
+
const featureName = String(input.name || '').trim() || ('多边形 ' + (count + 1));
|
|
1435
|
+
const createdAt = new Date().toISOString();
|
|
1436
|
+
const env = Object.assign({}, process.env, {
|
|
1437
|
+
GIS_VECTOR_EDIT: JSON.stringify({
|
|
1438
|
+
gpkg: profile.gpkgPath,
|
|
1439
|
+
table: profile.tableName,
|
|
1440
|
+
geometry,
|
|
1441
|
+
name: featureName,
|
|
1442
|
+
createdAt,
|
|
1443
|
+
}),
|
|
1444
|
+
});
|
|
1445
|
+
try {
|
|
1446
|
+
await execFileAsync('python3', [VECTOR_EDIT_SCRIPT, 'add'], { env, maxBuffer: 20 * 1024 * 1024 });
|
|
1447
|
+
await execFileAsync('ogr2ogr', [
|
|
1448
|
+
'-f', 'GeoJSON', '-t_srs', 'EPSG:4326', profile.previewPath, profile.gpkgPath, profile.tableName,
|
|
1449
|
+
], { maxBuffer: 20 * 1024 * 1024 });
|
|
1450
|
+
const info = JSON.parse((await execFileAsync('python3', [VECTOR_INFO_SCRIPT, profile.gpkgPath, profile.tableName], {
|
|
1451
|
+
maxBuffer: 20 * 1024 * 1024,
|
|
1452
|
+
})).stdout);
|
|
1453
|
+
const layer = info.layer || {};
|
|
1454
|
+
const previewData = JSON.parse(await fs.readFile(profile.previewPath, 'utf8'));
|
|
1455
|
+
const bounds = geoJsonBounds(previewData);
|
|
1456
|
+
const bbox = bounds.bbox;
|
|
1457
|
+
const previewStat = await fs.stat(profile.previewPath);
|
|
1458
|
+
const nextProfile = Object.assign({}, profile, {
|
|
1459
|
+
bbox,
|
|
1460
|
+
previewSizeBytes: previewStat.size,
|
|
1461
|
+
updatedAt: new Date().toISOString(),
|
|
1462
|
+
featureCount: bounds.count,
|
|
1463
|
+
fields: Array.isArray(layer.fields) ? layer.fields.map((field) => field.name).filter(Boolean) : profile.fields,
|
|
1464
|
+
renderable: true,
|
|
1465
|
+
});
|
|
1466
|
+
this.db.prepare('UPDATE datasets SET bbox = ?, profile = ? WHERE id = ?').run(
|
|
1467
|
+
bbox ? jsonString(bbox, null) : null,
|
|
1468
|
+
jsonString(nextProfile, {}),
|
|
1469
|
+
datasetId,
|
|
1470
|
+
);
|
|
1471
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(datasetId));
|
|
1472
|
+
} catch (error) {
|
|
1473
|
+
throw httpError(422, 'Could not add polygon: ' + gdalError(error));
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
createTileDataset(projectId, input = {}) {
|
|
1478
|
+
this.assertProject(projectId);
|
|
1479
|
+
const uri = String(input.tileUrl || input.uri || '').trim();
|
|
1480
|
+
if (!isTileTemplate(uri)) throw httpError(400, 'tileUrl must be an XYZ tile template containing {z}, {x}, and {y}');
|
|
1481
|
+
const id = randomUUID();
|
|
1482
|
+
const profile = {
|
|
1483
|
+
format: 'xyz',
|
|
1484
|
+
renderable: true,
|
|
1485
|
+
tileSize: Number(input.tileSize) === 512 ? 512 : 256,
|
|
1486
|
+
attribution: String(input.attribution || '天地图'),
|
|
1487
|
+
name: String(input.name || '天地图影像'),
|
|
1488
|
+
};
|
|
1489
|
+
this.db.prepare(
|
|
1490
|
+
'INSERT INTO datasets (id, project_id, kind, uri, crs, bbox, profile) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
1491
|
+
).run(id, projectId, 'raster', uri, 3857, null, jsonString(profile, {}));
|
|
1492
|
+
return serializeDataset(this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id));
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
listLayers(projectId) {
|
|
1496
|
+
this.assertProject(projectId);
|
|
1497
|
+
return this.db.prepare(`
|
|
1498
|
+
SELECT l.*, d.kind AS dataset_kind, d.uri AS dataset_uri, d.crs AS dataset_crs,
|
|
1499
|
+
d.bbox AS dataset_bbox, d.profile AS dataset_profile,
|
|
1500
|
+
json_extract(d.profile, '$.format') AS dataset_format
|
|
1501
|
+
FROM layers l LEFT JOIN datasets d ON d.id = l.dataset_id
|
|
1502
|
+
WHERE l.project_id = ? ORDER BY l.z, l.rowid
|
|
1503
|
+
`).all(projectId).map(serializeLayer);
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
replaceProjectLayerState(projectId, input = {}) {
|
|
1507
|
+
this.assertProject(projectId);
|
|
1508
|
+
const requested = Array.isArray(input.layers) ? input.layers : [];
|
|
1509
|
+
const layers = [];
|
|
1510
|
+
const seen = new Set();
|
|
1511
|
+
for (const raw of requested) {
|
|
1512
|
+
if (!raw || typeof raw !== 'object') continue;
|
|
1513
|
+
const datasetId = String(raw.datasetId || '');
|
|
1514
|
+
if (!datasetId) continue;
|
|
1515
|
+
const dataset = this.db.prepare('SELECT * FROM datasets WHERE id = ? AND project_id = ?').get(datasetId, projectId);
|
|
1516
|
+
if (!dataset) throw httpError(400, 'A layer references a dataset outside this project');
|
|
1517
|
+
const id = String(raw.id || randomUUID());
|
|
1518
|
+
if (seen.has(id)) throw httpError(400, 'Duplicate layer id in the project state');
|
|
1519
|
+
seen.add(id);
|
|
1520
|
+
const profile = jsonParse(dataset.profile, {});
|
|
1521
|
+
const name = String(raw.name || '').trim()
|
|
1522
|
+
|| profile.name
|
|
1523
|
+
|| profile.fileName
|
|
1524
|
+
|| path.basename(dataset.uri)
|
|
1525
|
+
|| '未命名图层';
|
|
1526
|
+
const style = raw.style && typeof raw.style === 'object' ? raw.style : {};
|
|
1527
|
+
layers.push({
|
|
1528
|
+
id,
|
|
1529
|
+
datasetId,
|
|
1530
|
+
name,
|
|
1531
|
+
style,
|
|
1532
|
+
visible: raw.visible === false ? 0 : 1,
|
|
1533
|
+
z: Number.isFinite(Number(raw.z)) ? Number(raw.z) : 0,
|
|
1534
|
+
});
|
|
1535
|
+
}
|
|
1536
|
+
this.db.exec('BEGIN IMMEDIATE');
|
|
1537
|
+
try {
|
|
1538
|
+
if (input.meta !== undefined && input.meta !== null) {
|
|
1539
|
+
this.db.prepare('UPDATE projects SET meta = ? WHERE id = ?').run(jsonString(input.meta, {}), projectId);
|
|
1540
|
+
}
|
|
1541
|
+
this.db.prepare('DELETE FROM layers WHERE project_id = ?').run(projectId);
|
|
1542
|
+
const insert = this.db.prepare(
|
|
1543
|
+
'INSERT INTO layers (id, project_id, dataset_id, name, style, visible, z) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
1544
|
+
);
|
|
1545
|
+
for (const layer of layers) {
|
|
1546
|
+
insert.run(layer.id, projectId, layer.datasetId, layer.name, jsonString(layer.style, {}), layer.visible, layer.z);
|
|
1547
|
+
}
|
|
1548
|
+
this.db.exec('COMMIT');
|
|
1549
|
+
} catch (error) {
|
|
1550
|
+
try {
|
|
1551
|
+
this.db.exec('ROLLBACK');
|
|
1552
|
+
} catch (rollbackError) {
|
|
1553
|
+
// Preserve the original failure.
|
|
1554
|
+
}
|
|
1555
|
+
throw error;
|
|
1556
|
+
}
|
|
1557
|
+
return this.listLayers(projectId);
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
createLayer(projectId, input = {}) {
|
|
1561
|
+
this.assertProject(projectId);
|
|
1562
|
+
if (!input.datasetId) throw httpError(400, 'datasetId is required');
|
|
1563
|
+
const dataset = this.db.prepare('SELECT * FROM datasets WHERE id = ? AND project_id = ?').get(input.datasetId, projectId);
|
|
1564
|
+
if (!dataset) throw httpError(404, 'Dataset not found in this project');
|
|
1565
|
+
const max = this.db.prepare('SELECT COALESCE(MAX(z), -1) AS z FROM layers WHERE project_id = ?').get(projectId).z;
|
|
1566
|
+
const id = randomUUID();
|
|
1567
|
+
const datasetProfile = jsonParse(dataset.profile, {});
|
|
1568
|
+
const name = String(input.name || '').trim()
|
|
1569
|
+
|| datasetProfile.name
|
|
1570
|
+
|| datasetProfile.fileName
|
|
1571
|
+
|| path.basename(dataset.uri)
|
|
1572
|
+
|| '未命名图层';
|
|
1573
|
+
const z = Number.isFinite(Number(input.z)) ? Number(input.z) : max + 1;
|
|
1574
|
+
const style = input.style && typeof input.style === 'object' ? input.style : {};
|
|
1575
|
+
const visible = input.visible === false ? 0 : 1;
|
|
1576
|
+
this.db.prepare(
|
|
1577
|
+
'INSERT INTO layers (id, project_id, dataset_id, name, style, visible, z) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
1578
|
+
).run(id, projectId, input.datasetId, name, jsonString(style, {}), visible, z);
|
|
1579
|
+
return this.listLayers(projectId).find((layer) => layer.id === id);
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
updateLayer(id, input = {}) {
|
|
1583
|
+
const row = this.db.prepare('SELECT * FROM layers WHERE id = ?').get(id);
|
|
1584
|
+
if (!row) throw httpError(404, 'Layer not found');
|
|
1585
|
+
const name = input.name === undefined ? row.name : String(input.name);
|
|
1586
|
+
const style = input.style === undefined ? jsonParse(row.style, {}) : input.style;
|
|
1587
|
+
const visible = input.visible === undefined ? row.visible : input.visible ? 1 : 0;
|
|
1588
|
+
const z = input.z === undefined ? row.z : Number(input.z);
|
|
1589
|
+
this.db.prepare('UPDATE layers SET name = ?, style = ?, visible = ?, z = ? WHERE id = ?')
|
|
1590
|
+
.run(name, jsonString(style, {}), visible, z, id);
|
|
1591
|
+
return this.listLayers(row.project_id).find((layer) => layer.id === id);
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
deleteLayer(id) {
|
|
1595
|
+
const result = this.db.prepare('DELETE FROM layers WHERE id = ?').run(id);
|
|
1596
|
+
return result.changes > 0;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
async deleteDataset(id) {
|
|
1600
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id);
|
|
1601
|
+
if (!row) return false;
|
|
1602
|
+
const profile = jsonParse(row.profile, {});
|
|
1603
|
+
const result = this.db.prepare('DELETE FROM datasets WHERE id = ?').run(id);
|
|
1604
|
+
if (result.changes > 0) {
|
|
1605
|
+
if (profile.format === 'gpkg' && profile.gpkgPath && profile.tableName) {
|
|
1606
|
+
await execFileAsync('python3', [DROP_LAYER_SCRIPT, profile.gpkgPath, profile.tableName], { maxBuffer: 20 * 1024 * 1024 })
|
|
1607
|
+
.catch((error) => console.warn('[gis-workbench] could not drop GeoPackage layer', error.message));
|
|
1608
|
+
}
|
|
1609
|
+
if (profile.previewPath) {
|
|
1610
|
+
await fs.rm(path.dirname(profile.previewPath), { recursive: true, force: true }).catch(() => {});
|
|
1611
|
+
}
|
|
1612
|
+
if (profile.format === 'cog' && profile.cogPath) {
|
|
1613
|
+
await fs.rm(path.dirname(profile.cogPath), { recursive: true, force: true }).catch(() => {});
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
return result.changes > 0;
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
rootsForClient() {
|
|
1620
|
+
return this.roots.slice();
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
async resolveAllowed(inputPath) {
|
|
1624
|
+
if (!inputPath || typeof inputPath !== 'string') throw httpError(400, 'Path is required');
|
|
1625
|
+
let real;
|
|
1626
|
+
try {
|
|
1627
|
+
real = await fs.realpath(path.resolve(inputPath));
|
|
1628
|
+
} catch (error) {
|
|
1629
|
+
throw httpError(404, 'Path does not exist');
|
|
1630
|
+
}
|
|
1631
|
+
if (!this.roots.some((root) => isInside(root, real))) {
|
|
1632
|
+
throw httpError(403, 'Path is outside the allowed data roots');
|
|
1633
|
+
}
|
|
1634
|
+
return real;
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
async browse(inputPath) {
|
|
1638
|
+
const target = inputPath ? await this.resolveAllowed(inputPath) : this.workspaceRoot;
|
|
1639
|
+
const stat = await fs.stat(target);
|
|
1640
|
+
if (!stat.isDirectory()) throw httpError(400, 'Path is not a directory');
|
|
1641
|
+
const dirents = await fs.readdir(target, { withFileTypes: true });
|
|
1642
|
+
const entries = [];
|
|
1643
|
+
for (const dirent of dirents.slice(0, 500)) {
|
|
1644
|
+
const entryPath = path.join(target, dirent.name);
|
|
1645
|
+
let entryStat;
|
|
1646
|
+
try {
|
|
1647
|
+
entryStat = await fs.stat(entryPath);
|
|
1648
|
+
} catch (error) {
|
|
1649
|
+
continue;
|
|
1650
|
+
}
|
|
1651
|
+
const isDirectory = entryStat.isDirectory();
|
|
1652
|
+
const ext = path.extname(dirent.name).toLowerCase();
|
|
1653
|
+
entries.push({
|
|
1654
|
+
name: dirent.name,
|
|
1655
|
+
path: entryPath,
|
|
1656
|
+
type: isDirectory ? 'directory' : 'file',
|
|
1657
|
+
size: isDirectory ? null : entryStat.size,
|
|
1658
|
+
extension: ext,
|
|
1659
|
+
kindHint: isDirectory ? null : kindHint(dirent.name),
|
|
1660
|
+
supported: isDirectory || ALLOWED_FILE_EXTENSIONS.has(ext),
|
|
1661
|
+
});
|
|
1662
|
+
}
|
|
1663
|
+
entries.sort((left, right) => {
|
|
1664
|
+
if (left.type !== right.type) return left.type === 'directory' ? -1 : 1;
|
|
1665
|
+
return left.name.localeCompare(right.name, 'zh-CN');
|
|
1666
|
+
});
|
|
1667
|
+
const parent = target === this.workspaceRoot ? null : path.dirname(target);
|
|
1668
|
+
return {
|
|
1669
|
+
root: this.workspaceRoot,
|
|
1670
|
+
roots: this.rootsForClient(),
|
|
1671
|
+
path: target,
|
|
1672
|
+
parent: parent && this.roots.some((root) => isInside(root, parent)) ? parent : null,
|
|
1673
|
+
entries,
|
|
1674
|
+
};
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
async tileInfo(id, z, x, y) {
|
|
1678
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id);
|
|
1679
|
+
if (!row) throw httpError(404, 'Dataset not found');
|
|
1680
|
+
const profile = jsonParse(row.profile, {});
|
|
1681
|
+
if (profile.format !== 'cog' || !profile.tilePath) throw httpError(404, 'This dataset has no tile pyramid');
|
|
1682
|
+
const zz = Number(z);
|
|
1683
|
+
const xx = Number(x);
|
|
1684
|
+
const yy = Number(y);
|
|
1685
|
+
if (![zz, xx, yy].every(Number.isInteger)) throw httpError(400, 'Invalid tile coordinates');
|
|
1686
|
+
const tilePath = path.join(profile.tilePath, String(zz), String(xx), String(yy) + '.png');
|
|
1687
|
+
const resolved = path.resolve(tilePath);
|
|
1688
|
+
if (!isInside(profile.tilePath, resolved)) throw httpError(400, 'Invalid tile path');
|
|
1689
|
+
const stat = await fs.stat(resolved).catch(() => undefined);
|
|
1690
|
+
if (!stat || !stat.isFile()) throw httpError(404, 'Tile not found');
|
|
1691
|
+
return { filePath: resolved, size: stat.size };
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
async contentInfo(id) {
|
|
1695
|
+
const row = this.db.prepare('SELECT * FROM datasets WHERE id = ?').get(id);
|
|
1696
|
+
if (!row) throw httpError(404, 'Dataset not found');
|
|
1697
|
+
const profile = jsonParse(row.profile, {});
|
|
1698
|
+
if (profile.format === 'gpkg') {
|
|
1699
|
+
if (!profile.previewPath) throw httpError(415, 'This GeoPackage dataset has no display preview');
|
|
1700
|
+
const previewPath = await this.resolveAllowed(profile.previewPath);
|
|
1701
|
+
const previewStat = await fs.stat(previewPath);
|
|
1702
|
+
if (!previewStat.isFile()) throw httpError(404, 'Dataset preview not found');
|
|
1703
|
+
return {
|
|
1704
|
+
filePath: previewPath,
|
|
1705
|
+
mime: 'application/geo+json; charset=utf-8',
|
|
1706
|
+
size: previewStat.size,
|
|
1707
|
+
fileName: path.basename(previewPath),
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1710
|
+
if (!row.uri || isTileTemplate(row.uri)) throw httpError(415, 'This dataset does not have a file body');
|
|
1711
|
+
if (!['geojson', 'csv', 'image', 'cog'].includes(profile.format)) {
|
|
1712
|
+
throw httpError(415, 'This dataset format cannot be previewed in the browser');
|
|
1713
|
+
}
|
|
1714
|
+
const realPath = await this.resolveAllowed(row.uri);
|
|
1715
|
+
const stat = await fs.stat(realPath);
|
|
1716
|
+
if (!stat.isFile()) throw httpError(404, 'Dataset file not found');
|
|
1717
|
+
return {
|
|
1718
|
+
filePath: realPath,
|
|
1719
|
+
mime: mimeForFormat(profile.format, realPath),
|
|
1720
|
+
size: stat.size,
|
|
1721
|
+
fileName: path.basename(realPath),
|
|
1722
|
+
};
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
async initUpload(projectId, input = {}) {
|
|
1727
|
+
this.assertProject(projectId);
|
|
1728
|
+
const size = Number(input.size);
|
|
1729
|
+
if (!Number.isSafeInteger(size) || size < 0) throw httpError(400, 'A valid file size is required');
|
|
1730
|
+
if (size > MAX_FILE_BYTES) throw httpError(413, 'File exceeds the 100 GB limit');
|
|
1731
|
+
const fileName = String(input.fileName || '').trim() || 'upload.bin';
|
|
1732
|
+
const fingerprint = String(input.fingerprint || '').trim().slice(0, 240);
|
|
1733
|
+
const requestedChunk = Number(input.chunkSize) || 8 * 1024 * 1024;
|
|
1734
|
+
let chunkSize = Math.min(MAX_CHUNK_BYTES, Math.max(1024 * 1024, Math.floor(requestedChunk)));
|
|
1735
|
+
const existing = fingerprint
|
|
1736
|
+
? this.db.prepare(`
|
|
1737
|
+
SELECT * FROM uploads
|
|
1738
|
+
WHERE project_id = ? AND fingerprint = ? AND status = 'uploading' AND size = ?
|
|
1739
|
+
ORDER BY created_at DESC LIMIT 1
|
|
1740
|
+
`).get(projectId, fingerprint, size)
|
|
1741
|
+
: undefined;
|
|
1742
|
+
if (existing) {
|
|
1743
|
+
chunkSize = existing.chunk_size;
|
|
1744
|
+
try {
|
|
1745
|
+
await fs.stat(existing.temp_path);
|
|
1746
|
+
} catch (error) {
|
|
1747
|
+
this.db.prepare("UPDATE uploads SET received = '[]', updated_at = datetime('now') WHERE id = ?").run(existing.id);
|
|
1748
|
+
existing.received = '[]';
|
|
1749
|
+
}
|
|
1750
|
+
return serializeUpload(existing);
|
|
1751
|
+
}
|
|
1752
|
+
const id = randomUUID();
|
|
1753
|
+
const tempPath = path.join(this.uploadsDir, '.' + id + '.part');
|
|
1754
|
+
await fs.writeFile(tempPath, Buffer.alloc(0), { flag: 'w' });
|
|
1755
|
+
const totalChunks = chunkSize > 0 ? Math.ceil(size / chunkSize) : 0;
|
|
1756
|
+
this.db.prepare(`
|
|
1757
|
+
INSERT INTO uploads (id, project_id, fingerprint, file_name, size, chunk_size, total_chunks, received, temp_path, status)
|
|
1758
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, '[]', ?, 'uploading')
|
|
1759
|
+
`).run(id, projectId, fingerprint || null, fileName, size, chunkSize, totalChunks, tempPath);
|
|
1760
|
+
return serializeUpload(this.db.prepare('SELECT * FROM uploads WHERE id = ?').get(id));
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
getUpload(id) {
|
|
1764
|
+
const row = this.db.prepare('SELECT * FROM uploads WHERE id = ?').get(id);
|
|
1765
|
+
if (!row) throw httpError(404, 'Upload not found');
|
|
1766
|
+
return serializeUpload(row);
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
async writeUploadChunk(id, index, buffer) {
|
|
1770
|
+
const row = this.db.prepare('SELECT * FROM uploads WHERE id = ?').get(id);
|
|
1771
|
+
if (!row) throw httpError(404, 'Upload not found');
|
|
1772
|
+
if (row.status !== 'uploading') throw httpError(409, 'Upload is not accepting chunks');
|
|
1773
|
+
const chunkIndex = Number(index);
|
|
1774
|
+
if (!Number.isInteger(chunkIndex) || chunkIndex < 0 || chunkIndex >= row.total_chunks) {
|
|
1775
|
+
throw httpError(400, 'Chunk index is out of range');
|
|
1776
|
+
}
|
|
1777
|
+
if (!Buffer.isBuffer(buffer) || buffer.length === 0) throw httpError(400, 'Chunk body is empty');
|
|
1778
|
+
const offset = chunkIndex * row.chunk_size;
|
|
1779
|
+
const expected = Math.min(row.chunk_size, row.size - offset);
|
|
1780
|
+
if (buffer.length !== expected) {
|
|
1781
|
+
throw httpError(400, `Chunk ${chunkIndex} must be ${expected} bytes, got ${buffer.length}`);
|
|
1782
|
+
}
|
|
1783
|
+
let handle;
|
|
1784
|
+
try {
|
|
1785
|
+
handle = await fs.open(row.temp_path, 'r+');
|
|
1786
|
+
} catch (error) {
|
|
1787
|
+
handle = await fs.open(row.temp_path, 'w+');
|
|
1788
|
+
}
|
|
1789
|
+
try {
|
|
1790
|
+
await handle.write(buffer, 0, buffer.length, offset);
|
|
1791
|
+
} finally {
|
|
1792
|
+
await handle.close();
|
|
1793
|
+
}
|
|
1794
|
+
const received = new Set(jsonParse(row.received, []));
|
|
1795
|
+
received.add(chunkIndex);
|
|
1796
|
+
this.db.prepare("UPDATE uploads SET received = ?, updated_at = datetime('now') WHERE id = ?")
|
|
1797
|
+
.run(jsonString([...received].sort((left, right) => left - right), []), id);
|
|
1798
|
+
return serializeUpload(this.db.prepare('SELECT * FROM uploads WHERE id = ?').get(id));
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
async completeUpload(id, options = {}) {
|
|
1802
|
+
const row = this.db.prepare('SELECT * FROM uploads WHERE id = ?').get(id);
|
|
1803
|
+
if (!row) throw httpError(404, 'Upload not found');
|
|
1804
|
+
if (row.status !== 'uploading') throw httpError(409, 'Upload is not in progress');
|
|
1805
|
+
const received = new Set(jsonParse(row.received, []));
|
|
1806
|
+
const missing = [];
|
|
1807
|
+
for (let index = 0; index < row.total_chunks; index += 1) {
|
|
1808
|
+
if (!received.has(index)) missing.push(index);
|
|
1809
|
+
}
|
|
1810
|
+
if (missing.length > 0) throw httpError(400, 'Upload is missing chunks: ' + missing.slice(0, 20).join(', '));
|
|
1811
|
+
const stat = await fs.stat(row.temp_path).catch(() => undefined);
|
|
1812
|
+
if (!stat) throw httpError(404, 'Upload temporary file is missing');
|
|
1813
|
+
if (stat.size < row.size) throw httpError(400, 'Upload file is incomplete');
|
|
1814
|
+
const finalPath = path.join(this.uploadsDir, `${Date.now()}-${randomUUID()}-${safeUploadName(row.file_name)}`);
|
|
1815
|
+
await fs.rename(row.temp_path, finalPath);
|
|
1816
|
+
let datasets;
|
|
1817
|
+
try {
|
|
1818
|
+
datasets = await this.createDatasetsFromPath(row.project_id, finalPath, { fileName: row.file_name });
|
|
1819
|
+
} catch (error) {
|
|
1820
|
+
this.db.prepare("UPDATE uploads SET status = 'failed', temp_path = ?, updated_at = datetime('now') WHERE id = ?")
|
|
1821
|
+
.run(finalPath, id);
|
|
1822
|
+
throw error;
|
|
1823
|
+
}
|
|
1824
|
+
const layers = [];
|
|
1825
|
+
if (options.addLayer !== false) {
|
|
1826
|
+
for (const dataset of datasets) {
|
|
1827
|
+
if (dataset.profile && dataset.profile.renderable !== false) {
|
|
1828
|
+
layers.push(this.createLayer(row.project_id, { datasetId: dataset.id }));
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
this.db.prepare('DELETE FROM uploads WHERE id = ?').run(id);
|
|
1833
|
+
return {
|
|
1834
|
+
datasets,
|
|
1835
|
+
layers,
|
|
1836
|
+
dataset: datasets[0] || null,
|
|
1837
|
+
layer: layers[0] || null,
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
async deleteUpload(id) {
|
|
1842
|
+
const row = this.db.prepare('SELECT * FROM uploads WHERE id = ?').get(id);
|
|
1843
|
+
if (!row) throw httpError(404, 'Upload not found');
|
|
1844
|
+
this.db.prepare('DELETE FROM uploads WHERE id = ?').run(id);
|
|
1845
|
+
await fs.rm(row.temp_path, { force: true }).catch(() => {});
|
|
1846
|
+
return true;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
createReadStream(filePath) {
|
|
1850
|
+
return createReadStream(filePath);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
export { API_PREFIX, MAX_CHUNK_BYTES, MAX_FILE_BYTES, httpError, isTileTemplate, parseBbox, parseCrs };
|