@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/api.js
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { API_PREFIX, MAX_CHUNK_BYTES, httpError, isTileTemplate } from './store.js';
|
|
6
|
+
|
|
7
|
+
const MAX_UPLOAD_BYTES = 200 * 1024 * 1024;
|
|
8
|
+
const GLYPH_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'assets', 'glyphs');
|
|
9
|
+
|
|
10
|
+
function sendJson(res, status, value) {
|
|
11
|
+
const body = JSON.stringify(value);
|
|
12
|
+
res.writeHead(status, {
|
|
13
|
+
'content-type': 'application/json; charset=utf-8',
|
|
14
|
+
'cache-control': 'no-store',
|
|
15
|
+
'content-length': Buffer.byteLength(body),
|
|
16
|
+
});
|
|
17
|
+
res.end(body);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function sendNoContent(res, status = 204) {
|
|
21
|
+
res.writeHead(status, { 'cache-control': 'no-store' });
|
|
22
|
+
res.end();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function readBody(req, limit = MAX_UPLOAD_BYTES) {
|
|
26
|
+
const declared = Number(req.headers['content-length']);
|
|
27
|
+
if (Number.isFinite(declared) && declared > limit) throw httpError(413, 'Request body is too large');
|
|
28
|
+
const chunks = [];
|
|
29
|
+
let total = 0;
|
|
30
|
+
for await (const chunk of req) {
|
|
31
|
+
total += chunk.length;
|
|
32
|
+
if (total > limit) throw httpError(413, 'Request body is too large');
|
|
33
|
+
chunks.push(chunk);
|
|
34
|
+
}
|
|
35
|
+
return Buffer.concat(chunks);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function readJson(req, limit = 2 * 1024 * 1024) {
|
|
39
|
+
const body = await readBody(req, limit);
|
|
40
|
+
if (body.length === 0) return {};
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(body.toString('utf8'));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
throw httpError(400, 'Request body must be valid JSON');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function decodeSegments(pathname) {
|
|
49
|
+
if (!pathname.startsWith(API_PREFIX)) return null;
|
|
50
|
+
const rest = pathname.slice(API_PREFIX.length);
|
|
51
|
+
if (rest === '' || rest === '/') return [];
|
|
52
|
+
return rest.split('/').filter(Boolean).map((segment) => decodeURIComponent(segment));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function safeUploadName(name) {
|
|
56
|
+
const base = path.basename(String(name || 'upload.bin'));
|
|
57
|
+
const sanitized = base.replace(/[^\w.\-\u4e00-\u9fa5]+/g, '_').replace(/^_+|_+$/g, '');
|
|
58
|
+
return (sanitized || 'upload.bin').slice(-140);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function parseUpload(req) {
|
|
62
|
+
const contentType = String(req.headers['content-type'] || '');
|
|
63
|
+
if (!contentType.toLowerCase().startsWith('multipart/form-data')) {
|
|
64
|
+
throw httpError(415, 'Upload must be multipart/form-data');
|
|
65
|
+
}
|
|
66
|
+
const buffer = await readBody(req);
|
|
67
|
+
let form;
|
|
68
|
+
try {
|
|
69
|
+
const request = new Request('http://localhost/upload', {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: { 'content-type': contentType },
|
|
72
|
+
body: buffer,
|
|
73
|
+
});
|
|
74
|
+
form = await request.formData();
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw httpError(400, 'Could not parse the upload form');
|
|
77
|
+
}
|
|
78
|
+
const file = form.get('file');
|
|
79
|
+
if (!file || typeof file.arrayBuffer !== 'function') throw httpError(400, 'A file field is required');
|
|
80
|
+
return { form, file };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function createApiHandler(store) {
|
|
84
|
+
return async function gisWorkbenchApi(req, res) {
|
|
85
|
+
try {
|
|
86
|
+
const url = new URL(req.url || '/', 'http://localhost');
|
|
87
|
+
const segments = decodeSegments(url.pathname);
|
|
88
|
+
if (segments === null) throw httpError(404, 'Not found');
|
|
89
|
+
const method = String(req.method || 'GET').toUpperCase();
|
|
90
|
+
|
|
91
|
+
if (segments.length === 0 && method === 'GET') {
|
|
92
|
+
const projects = store.listProjects();
|
|
93
|
+
sendJson(res, 200, {
|
|
94
|
+
projects,
|
|
95
|
+
roots: store.rootsForClient(),
|
|
96
|
+
defaultProjectId: projects[0] ? projects[0].id : null,
|
|
97
|
+
apiPrefix: API_PREFIX,
|
|
98
|
+
});
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (segments[0] === 'projects') {
|
|
103
|
+
if (segments.length === 1) {
|
|
104
|
+
if (method === 'GET') {
|
|
105
|
+
sendJson(res, 200, { projects: store.listProjects() });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (method === 'POST') {
|
|
109
|
+
const body = await readJson(req);
|
|
110
|
+
sendJson(res, 201, { project: store.createProject(body) });
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
throw httpError(405, 'Method not allowed');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const projectId = segments[1];
|
|
117
|
+
if (segments.length === 2) {
|
|
118
|
+
if (method === 'PATCH') {
|
|
119
|
+
const body = await readJson(req);
|
|
120
|
+
sendJson(res, 200, { project: store.updateProject(projectId, body) });
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
throw httpError(405, 'Method not allowed');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (segments.length === 4 && segments[2] === 'uploads' && segments[3] === 'init') {
|
|
127
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
128
|
+
const body = await readJson(req);
|
|
129
|
+
sendJson(res, 201, { upload: await store.initUpload(projectId, body) });
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (segments.length === 3 && segments[2] === 'vector-layers') {
|
|
134
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
135
|
+
const body = await readJson(req);
|
|
136
|
+
const dataset = await store.createVectorLayer(projectId, body);
|
|
137
|
+
const layer = store.createLayer(projectId, { datasetId: dataset.id, name: body.name || dataset.profile.name });
|
|
138
|
+
sendJson(res, 201, { dataset, layer });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (segments.length === 3 && segments[2] === 'databases') {
|
|
143
|
+
if (method === 'GET') {
|
|
144
|
+
sendJson(res, 200, { databases: store.listDatabases(projectId) });
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (method === 'POST') {
|
|
148
|
+
const body = await readJson(req);
|
|
149
|
+
sendJson(res, 201, { database: await store.createDatabase(projectId, body) });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
throw httpError(405, 'Method not allowed');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (segments.length === 3 && segments[2] === 'state') {
|
|
156
|
+
if (method !== 'PUT' && method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
157
|
+
const body = await readJson(req);
|
|
158
|
+
sendJson(res, 200, {
|
|
159
|
+
layers: store.replaceProjectLayerState(projectId, body),
|
|
160
|
+
project: store.listProjects().find((project) => project.id === projectId),
|
|
161
|
+
});
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (segments.length === 3 && segments[2] === 'datasets') {
|
|
166
|
+
if (method === 'GET') {
|
|
167
|
+
sendJson(res, 200, { datasets: store.listDatasets(projectId) });
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (method === 'POST') {
|
|
171
|
+
const body = await readJson(req);
|
|
172
|
+
if (body.uri && isTileTemplate(body.uri)) {
|
|
173
|
+
const dataset = store.createTileDataset(projectId, {
|
|
174
|
+
name: body.name,
|
|
175
|
+
tileUrl: body.uri,
|
|
176
|
+
tileSize: body.tileSize,
|
|
177
|
+
attribution: body.attribution,
|
|
178
|
+
});
|
|
179
|
+
const layer = body.addLayer === true && dataset.profile && dataset.profile.renderable !== false
|
|
180
|
+
? store.createLayer(projectId, { datasetId: dataset.id, name: body.name || undefined })
|
|
181
|
+
: null;
|
|
182
|
+
sendJson(res, 201, { dataset, layer });
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (!body.path) throw httpError(400, 'path is required for a file dataset');
|
|
186
|
+
const datasets = await store.createDatasetsFromPath(projectId, body.path, {
|
|
187
|
+
crs: body.crs,
|
|
188
|
+
bbox: body.bbox,
|
|
189
|
+
});
|
|
190
|
+
const layers = [];
|
|
191
|
+
if (body.addLayer === true) {
|
|
192
|
+
for (const dataset of datasets) {
|
|
193
|
+
if (!dataset.profile || dataset.profile.renderable === false) continue;
|
|
194
|
+
layers.push(store.createLayer(projectId, {
|
|
195
|
+
datasetId: dataset.id,
|
|
196
|
+
name: datasets.length === 1 ? body.name || undefined : undefined,
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
sendJson(res, 201, {
|
|
201
|
+
datasets,
|
|
202
|
+
layers,
|
|
203
|
+
dataset: datasets[0] || null,
|
|
204
|
+
layer: layers[0] || null,
|
|
205
|
+
});
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
throw httpError(405, 'Method not allowed');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (segments.length === 4 && segments[2] === 'datasets' && segments[3] === 'upload') {
|
|
212
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
213
|
+
const { form, file } = await parseUpload(req);
|
|
214
|
+
const fileName = safeUploadName(file.name);
|
|
215
|
+
const storedPath = path.join(store.uploadsDir, `${Date.now()}-${randomUUID()}-${fileName}`);
|
|
216
|
+
await fs.writeFile(storedPath, Buffer.from(await file.arrayBuffer()));
|
|
217
|
+
const datasets = await store.createDatasetsFromPath(projectId, storedPath, {
|
|
218
|
+
crs: form.get('crs'),
|
|
219
|
+
bbox: form.get('bbox'),
|
|
220
|
+
fileName,
|
|
221
|
+
});
|
|
222
|
+
const addLayer = String(form.get('addLayer') || '') === 'true';
|
|
223
|
+
const layers = addLayer
|
|
224
|
+
? datasets.filter((dataset) => dataset.profile && dataset.profile.renderable !== false)
|
|
225
|
+
.map((dataset) => store.createLayer(projectId, { datasetId: dataset.id }))
|
|
226
|
+
: [];
|
|
227
|
+
sendJson(res, 201, {
|
|
228
|
+
datasets,
|
|
229
|
+
layers,
|
|
230
|
+
dataset: datasets[0] || null,
|
|
231
|
+
layer: layers[0] || null,
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (segments.length === 4 && segments[2] === 'datasets' && segments[3] === 'tile') {
|
|
237
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
238
|
+
const body = await readJson(req);
|
|
239
|
+
const dataset = store.createTileDataset(projectId, body);
|
|
240
|
+
const layer = body.addLayer === false
|
|
241
|
+
? null
|
|
242
|
+
: store.createLayer(projectId, { datasetId: dataset.id, name: body.name || undefined });
|
|
243
|
+
sendJson(res, 201, { dataset, layer });
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (segments.length === 3 && segments[2] === 'layers') {
|
|
248
|
+
if (method === 'GET') {
|
|
249
|
+
sendJson(res, 200, { layers: store.listLayers(projectId) });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (method === 'POST') {
|
|
253
|
+
const body = await readJson(req);
|
|
254
|
+
sendJson(res, 201, { layer: store.createLayer(projectId, body) });
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
throw httpError(405, 'Method not allowed');
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (segments[0] === 'datasets') {
|
|
262
|
+
const datasetId = segments[1];
|
|
263
|
+
if (segments.length === 2) {
|
|
264
|
+
if (method === 'DELETE') {
|
|
265
|
+
const deleted = await store.deleteDataset(datasetId);
|
|
266
|
+
if (!deleted) throw httpError(404, 'Dataset not found');
|
|
267
|
+
sendNoContent(res);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
throw httpError(405, 'Method not allowed');
|
|
271
|
+
}
|
|
272
|
+
if (segments.length === 3 && segments[2] === 'convert') {
|
|
273
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
274
|
+
const body = await readJson(req);
|
|
275
|
+
sendJson(res, 200, { dataset: await store.convertDatasetToCog(datasetId, { force: body.force === true }) });
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (segments.length === 6 && segments[2] === 'tiles') {
|
|
280
|
+
if (method !== 'GET' && method !== 'HEAD') throw httpError(405, 'Method not allowed');
|
|
281
|
+
const tile = await store.tileInfo(datasetId, segments[3], segments[4], segments[5].replace(/\.png$/i, ''));
|
|
282
|
+
res.writeHead(200, {
|
|
283
|
+
'content-type': 'image/png',
|
|
284
|
+
'content-length': tile.size,
|
|
285
|
+
'cache-control': 'public, max-age=3600',
|
|
286
|
+
});
|
|
287
|
+
if (method === 'HEAD') {
|
|
288
|
+
res.end();
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
store.createReadStream(tile.filePath).pipe(res);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (segments.length === 4 && segments[2] === 'features' && segments[3] === 'update') {
|
|
296
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
297
|
+
const body = await readJson(req);
|
|
298
|
+
sendJson(res, 200, { dataset: await store.updateVectorFeature(datasetId, body) });
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (segments.length === 4 && segments[2] === 'features' && segments[3] === 'delete') {
|
|
303
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
304
|
+
const body = await readJson(req);
|
|
305
|
+
sendJson(res, 200, { dataset: await store.deleteVectorFeature(datasetId, body) });
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (segments.length === 3 && segments[2] === 'features') {
|
|
310
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
311
|
+
const body = await readJson(req);
|
|
312
|
+
sendJson(res, 201, { dataset: await store.addVectorFeature(datasetId, body) });
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (segments.length === 3 && segments[2] === 'content') {
|
|
317
|
+
if (method !== 'GET' && method !== 'HEAD') throw httpError(405, 'Method not allowed');
|
|
318
|
+
const info = await store.contentInfo(datasetId);
|
|
319
|
+
res.writeHead(200, {
|
|
320
|
+
'content-type': info.mime,
|
|
321
|
+
'content-length': info.size,
|
|
322
|
+
'cache-control': 'no-store',
|
|
323
|
+
});
|
|
324
|
+
if (method === 'HEAD') {
|
|
325
|
+
res.end();
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const stream = store.createReadStream(info.filePath);
|
|
329
|
+
stream.on('error', (error) => {
|
|
330
|
+
console.error('[gis-workbench] dataset stream failed', error);
|
|
331
|
+
res.destroy(error);
|
|
332
|
+
});
|
|
333
|
+
stream.pipe(res);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (segments[0] === 'layers' && segments.length === 2) {
|
|
339
|
+
const layerId = segments[1];
|
|
340
|
+
if (method === 'PATCH') {
|
|
341
|
+
const body = await readJson(req);
|
|
342
|
+
sendJson(res, 200, { layer: store.updateLayer(layerId, body) });
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (method === 'DELETE') {
|
|
346
|
+
const deleted = store.deleteLayer(layerId);
|
|
347
|
+
if (!deleted) throw httpError(404, 'Layer not found');
|
|
348
|
+
sendNoContent(res);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
throw httpError(405, 'Method not allowed');
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (segments[0] === 'uploads') {
|
|
355
|
+
const uploadId = segments[1];
|
|
356
|
+
if (!uploadId) throw httpError(404, 'Not found');
|
|
357
|
+
if (segments.length === 2) {
|
|
358
|
+
if (method === 'GET') {
|
|
359
|
+
sendJson(res, 200, { upload: store.getUpload(uploadId) });
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (method === 'DELETE') {
|
|
363
|
+
await store.deleteUpload(uploadId);
|
|
364
|
+
sendNoContent(res);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
throw httpError(405, 'Method not allowed');
|
|
368
|
+
}
|
|
369
|
+
if (segments.length === 4 && segments[2] === 'chunks') {
|
|
370
|
+
if (method !== 'PUT' && method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
371
|
+
const index = Number(segments[3]);
|
|
372
|
+
const buffer = await readBody(req, MAX_CHUNK_BYTES);
|
|
373
|
+
sendJson(res, 200, { upload: await store.writeUploadChunk(uploadId, index, buffer) });
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (segments.length === 3 && segments[2] === 'complete') {
|
|
377
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
378
|
+
const body = await readJson(req);
|
|
379
|
+
sendJson(res, 200, await store.completeUpload(uploadId, body));
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (segments[0] === 'glyphs' && segments.length === 3) {
|
|
385
|
+
if (method !== 'GET' && method !== 'HEAD') throw httpError(405, 'Method not allowed');
|
|
386
|
+
const range = segments[2].replace(/\.pbf$/i, '');
|
|
387
|
+
if (!/^\d+-\d+$/.test(range)) throw httpError(400, 'Invalid glyph range');
|
|
388
|
+
const glyphPath = path.join(GLYPH_DIR, range + '.pbf');
|
|
389
|
+
const stat = await fs.stat(glyphPath).catch(() => undefined);
|
|
390
|
+
if (!stat || !stat.isFile()) {
|
|
391
|
+
res.writeHead(404, { 'cache-control': 'no-store' });
|
|
392
|
+
res.end();
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
res.writeHead(200, {
|
|
396
|
+
'content-type': 'application/x-protobuf',
|
|
397
|
+
'content-length': stat.size,
|
|
398
|
+
'cache-control': 'public, max-age=86400',
|
|
399
|
+
});
|
|
400
|
+
if (method === 'HEAD') {
|
|
401
|
+
res.end();
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
store.createReadStream(glyphPath).pipe(res);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (segments[0] === 'databases') {
|
|
409
|
+
const databaseId = segments[1];
|
|
410
|
+
if (!databaseId) throw httpError(404, 'Not found');
|
|
411
|
+
if (segments.length === 2) {
|
|
412
|
+
if (method === 'DELETE') {
|
|
413
|
+
const deleted = store.deleteDatabase(databaseId);
|
|
414
|
+
if (!deleted) throw httpError(404, 'Database registration not found');
|
|
415
|
+
sendNoContent(res);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
throw httpError(405, 'Method not allowed');
|
|
419
|
+
}
|
|
420
|
+
if (segments.length === 3 && segments[2] === 'tables') {
|
|
421
|
+
if (method !== 'GET') throw httpError(405, 'Method not allowed');
|
|
422
|
+
sendJson(res, 200, { tables: await store.listDatabaseTables(databaseId) });
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (segments.length === 4 && segments[2] === 'tables' && segments[3] === 'import') {
|
|
426
|
+
if (method !== 'POST') throw httpError(405, 'Method not allowed');
|
|
427
|
+
const body = await readJson(req);
|
|
428
|
+
const dataset = await store.importPostgisTable(databaseId, body);
|
|
429
|
+
const layer = body.addLayer !== false && dataset.profile && dataset.profile.renderable !== false
|
|
430
|
+
? store.createLayer(dataset.projectId, { datasetId: dataset.id, name: body.name || dataset.profile.layerName })
|
|
431
|
+
: null;
|
|
432
|
+
sendJson(res, 201, { dataset, layer });
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (segments[0] === 'browse' && segments.length === 1) {
|
|
438
|
+
if (method !== 'GET') throw httpError(405, 'Method not allowed');
|
|
439
|
+
sendJson(res, 200, await store.browse(url.searchParams.get('path') || undefined));
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
throw httpError(404, 'Not found');
|
|
444
|
+
} catch (error) {
|
|
445
|
+
const status = Number(error && error.status) || 500;
|
|
446
|
+
if (status >= 500) console.error('[gis-workbench] API error', error);
|
|
447
|
+
if (res.headersSent) {
|
|
448
|
+
res.destroy(error);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
sendJson(res, status, {
|
|
452
|
+
error: error instanceof Error ? error.message : String(error),
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from osgeo import gdal
|
|
3
|
+
|
|
4
|
+
path, name = sys.argv[1], sys.argv[2]
|
|
5
|
+
ds = gdal.OpenEx(path, gdal.OF_UPDATE)
|
|
6
|
+
if ds is None:
|
|
7
|
+
raise SystemExit(0)
|
|
8
|
+
for index in range(ds.GetLayerCount()):
|
|
9
|
+
if ds.GetLayerByIndex(index).GetName() == name:
|
|
10
|
+
ds.DeleteLayer(index)
|
|
11
|
+
break
|
|
12
|
+
ds = None
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
import psycopg
|
|
7
|
+
cfg = json.loads(os.environ.get('GIS_PG_CONFIG', '{}'))
|
|
8
|
+
conn = psycopg.connect(
|
|
9
|
+
host=cfg.get('host'),
|
|
10
|
+
port=int(cfg.get('port') or 5432),
|
|
11
|
+
dbname=cfg.get('dbname'),
|
|
12
|
+
user=cfg.get('username'),
|
|
13
|
+
password=cfg.get('password'),
|
|
14
|
+
sslmode=cfg.get('sslmode') or 'prefer',
|
|
15
|
+
connect_timeout=5,
|
|
16
|
+
application_name='dsh-gis-workbench',
|
|
17
|
+
)
|
|
18
|
+
try:
|
|
19
|
+
with conn.cursor() as cur:
|
|
20
|
+
cur.execute("""
|
|
21
|
+
SELECT f_table_schema, f_table_name, f_geometry_column, srid, type
|
|
22
|
+
FROM geometry_columns
|
|
23
|
+
WHERE f_table_schema NOT IN ('pg_catalog', 'information_schema')
|
|
24
|
+
ORDER BY f_table_schema, f_table_name
|
|
25
|
+
""")
|
|
26
|
+
geometry = cur.fetchall()
|
|
27
|
+
cur.execute("""
|
|
28
|
+
SELECT table_schema, table_name, column_name
|
|
29
|
+
FROM information_schema.columns
|
|
30
|
+
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
|
31
|
+
ORDER BY table_schema, table_name, ordinal_position
|
|
32
|
+
""")
|
|
33
|
+
column_rows = cur.fetchall()
|
|
34
|
+
columns = {}
|
|
35
|
+
for schema, table, column in column_rows:
|
|
36
|
+
columns.setdefault((schema, table), []).append(column)
|
|
37
|
+
tables = []
|
|
38
|
+
for schema, table, geometry_column, srid, geometry_type in geometry:
|
|
39
|
+
tables.append({
|
|
40
|
+
'schema': schema,
|
|
41
|
+
'table': table,
|
|
42
|
+
'geometryColumn': geometry_column,
|
|
43
|
+
'srid': srid,
|
|
44
|
+
'geometryType': geometry_type,
|
|
45
|
+
'columns': columns.get((schema, table), []),
|
|
46
|
+
})
|
|
47
|
+
print(json.dumps({'tables': tables}))
|
|
48
|
+
finally:
|
|
49
|
+
conn.close()
|
|
50
|
+
except Exception as error:
|
|
51
|
+
print(json.dumps({'error': str(error)[:500]}))
|
|
52
|
+
sys.exit(1)
|