@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/src/plugin.js
ADDED
|
@@ -0,0 +1,4503 @@
|
|
|
1
|
+
import maplibreDefault from 'maplibre-gl';
|
|
2
|
+
import maplibreCss from 'maplibre-gl/dist/maplibre-gl.css';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_ATTRIBUTION,
|
|
5
|
+
DEFAULT_LAYER_NAME,
|
|
6
|
+
DEFAULT_TILE_SIZE,
|
|
7
|
+
DEFAULT_TILE_URL,
|
|
8
|
+
TOOL_NAME,
|
|
9
|
+
VIEW_TOOL_NAME,
|
|
10
|
+
UPDATE_STYLE_TOOL_NAME,
|
|
11
|
+
} from '../shared.js';
|
|
12
|
+
import { MAP_GLYPHS_URL } from '../style-schema.js';
|
|
13
|
+
import {
|
|
14
|
+
addLayer,
|
|
15
|
+
getLayers,
|
|
16
|
+
getView,
|
|
17
|
+
removeLayer,
|
|
18
|
+
reorderLayers,
|
|
19
|
+
replaceProjectLayers,
|
|
20
|
+
updateProjectLayerStyle,
|
|
21
|
+
resolveTileUrl,
|
|
22
|
+
setView,
|
|
23
|
+
subscribe,
|
|
24
|
+
toggleLayerVisibility,
|
|
25
|
+
} from './store.js';
|
|
26
|
+
import { BASE_CSS } from './styles.js';
|
|
27
|
+
import * as api from './api.js';
|
|
28
|
+
|
|
29
|
+
const maplibregl = maplibreDefault && maplibreDefault.Map ? maplibreDefault : maplibreDefault.default;
|
|
30
|
+
let React;
|
|
31
|
+
let h;
|
|
32
|
+
let useState;
|
|
33
|
+
let useEffect;
|
|
34
|
+
let useRef;
|
|
35
|
+
let useCallback;
|
|
36
|
+
let useMemo;
|
|
37
|
+
let ChatBoundary;
|
|
38
|
+
let workbenchOpen = false;
|
|
39
|
+
const workbenchListeners = new Set();
|
|
40
|
+
const NS = 'gisWorkbench';
|
|
41
|
+
const CHAT_KIND = 'gis-chat';
|
|
42
|
+
const CHAT_ID = '@focus-daima/gis-workbench/chat';
|
|
43
|
+
const TOOL_VIEW_KEY = TOOL_NAME;
|
|
44
|
+
const DEFAULT_CENTER = [104.2, 35.5];
|
|
45
|
+
const DEFAULT_ZOOM = 3.4;
|
|
46
|
+
function ringArea(ring) {
|
|
47
|
+
let area = 0;
|
|
48
|
+
for (let index = 0; index < ring.length - 1; index += 1) {
|
|
49
|
+
area += ring[index][0] * ring[index + 1][1] - ring[index + 1][0] * ring[index][1];
|
|
50
|
+
}
|
|
51
|
+
return area / 2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ringCentroid(ring) {
|
|
55
|
+
let twiceArea = 0;
|
|
56
|
+
let x = 0;
|
|
57
|
+
let y = 0;
|
|
58
|
+
for (let index = 0; index < ring.length - 1; index += 1) {
|
|
59
|
+
const [x1, y1] = ring[index];
|
|
60
|
+
const [x2, y2] = ring[index + 1];
|
|
61
|
+
const cross = x1 * y2 - x2 * y1;
|
|
62
|
+
twiceArea += cross;
|
|
63
|
+
x += (x1 + x2) * cross;
|
|
64
|
+
y += (y1 + y2) * cross;
|
|
65
|
+
}
|
|
66
|
+
if (Math.abs(twiceArea) < 1e-12) {
|
|
67
|
+
const points = ring.slice(0, -1);
|
|
68
|
+
if (points.length === 0) return null;
|
|
69
|
+
return [
|
|
70
|
+
points.reduce((sum, point) => sum + point[0], 0) / points.length,
|
|
71
|
+
points.reduce((sum, point) => sum + point[1], 0) / points.length,
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
return [x / (3 * twiceArea), y / (3 * twiceArea)];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function geometryLabelPoint(geometry) {
|
|
78
|
+
if (!geometry || !Array.isArray(geometry.coordinates)) return null;
|
|
79
|
+
if (geometry.type === 'Point') return geometry.coordinates.slice(0, 2);
|
|
80
|
+
if (geometry.type === 'MultiPoint') {
|
|
81
|
+
const points = geometry.coordinates;
|
|
82
|
+
if (points.length === 0) return null;
|
|
83
|
+
return [
|
|
84
|
+
points.reduce((sum, point) => sum + point[0], 0) / points.length,
|
|
85
|
+
points.reduce((sum, point) => sum + point[1], 0) / points.length,
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
if (geometry.type === 'LineString') {
|
|
89
|
+
const points = geometry.coordinates;
|
|
90
|
+
return points.length > 0 ? points[Math.floor(points.length / 2)].slice(0, 2) : null;
|
|
91
|
+
}
|
|
92
|
+
if (geometry.type === 'MultiLineString') {
|
|
93
|
+
const lines = geometry.coordinates;
|
|
94
|
+
return lines.length > 0 ? geometryLabelPoint({ type: 'LineString', coordinates: lines[0] }) : null;
|
|
95
|
+
}
|
|
96
|
+
if (geometry.type === 'Polygon') {
|
|
97
|
+
const ring = geometry.coordinates[0];
|
|
98
|
+
if (!ring || ring.length < 4) return null;
|
|
99
|
+
return ringCentroid(ring);
|
|
100
|
+
}
|
|
101
|
+
if (geometry.type === 'MultiPolygon') {
|
|
102
|
+
let best = null;
|
|
103
|
+
let bestArea = -Infinity;
|
|
104
|
+
for (const polygon of geometry.coordinates) {
|
|
105
|
+
const ring = polygon && polygon[0];
|
|
106
|
+
if (!ring || ring.length < 4) continue;
|
|
107
|
+
const area = Math.abs(ringArea(ring));
|
|
108
|
+
if (area > bestArea) {
|
|
109
|
+
bestArea = area;
|
|
110
|
+
best = ringCentroid(ring);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return best;
|
|
114
|
+
}
|
|
115
|
+
if (geometry.type === 'GeometryCollection') {
|
|
116
|
+
for (const child of geometry.geometries || []) {
|
|
117
|
+
const point = geometryLabelPoint(child);
|
|
118
|
+
if (point) return point;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function categoricalFillExpression(categorical, fallbackColor) {
|
|
125
|
+
if (!categorical || !categorical.field || !categorical.values) return fallbackColor;
|
|
126
|
+
const expression = ['match', ['to-string', ['get', categorical.field]]];
|
|
127
|
+
for (const [value, item] of Object.entries(categorical.values)) {
|
|
128
|
+
expression.push(String(value), item.color);
|
|
129
|
+
}
|
|
130
|
+
expression.push(categorical.fallbackColor || fallbackColor);
|
|
131
|
+
return expression;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function editRingFromGeometry(geometry) {
|
|
135
|
+
if (!geometry || !Array.isArray(geometry.coordinates)) return null;
|
|
136
|
+
if (geometry.type === 'Polygon') return geometry.coordinates[0].map((point) => [point[0], point[1]]);
|
|
137
|
+
if (geometry.type === 'MultiPolygon') {
|
|
138
|
+
let best = null;
|
|
139
|
+
let bestArea = -Infinity;
|
|
140
|
+
for (const polygon of geometry.coordinates) {
|
|
141
|
+
const ring = polygon && polygon[0];
|
|
142
|
+
if (!ring || ring.length < 4) continue;
|
|
143
|
+
const area = Math.abs(ringArea(ring));
|
|
144
|
+
if (area > bestArea) {
|
|
145
|
+
bestArea = area;
|
|
146
|
+
best = ring.map((point) => [point[0], point[1]]);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return best;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function editFeatureCollection(ring) {
|
|
155
|
+
if (!ring || ring.length < 2) return { type: 'FeatureCollection', features: [] };
|
|
156
|
+
const closed = ring.length >= 3 ? [...ring, ring[0]] : ring;
|
|
157
|
+
const features = [{ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: closed } }];
|
|
158
|
+
if (ring.length >= 3) features.push({ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [closed] } });
|
|
159
|
+
for (const point of ring.slice(0, -1)) {
|
|
160
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: point } });
|
|
161
|
+
}
|
|
162
|
+
return { type: 'FeatureCollection', features };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function labelTextExpression(label) {
|
|
166
|
+
const field = ['to-string', ['get', label.field]];
|
|
167
|
+
if (!label || !label.valueMap || Object.keys(label.valueMap).length === 0) return field;
|
|
168
|
+
const expression = ['match', field];
|
|
169
|
+
for (const [code, text] of Object.entries(label.valueMap)) {
|
|
170
|
+
expression.push(String(code), String(text));
|
|
171
|
+
}
|
|
172
|
+
expression.push(label.fallback !== undefined ? String(label.fallback) : field);
|
|
173
|
+
return expression;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function labelLayer(id, sourceId, label) {
|
|
177
|
+
const layer = {
|
|
178
|
+
id,
|
|
179
|
+
type: 'symbol',
|
|
180
|
+
source: sourceId,
|
|
181
|
+
minzoom: label.minzoom !== undefined ? label.minzoom : 12,
|
|
182
|
+
layout: {
|
|
183
|
+
'text-field': labelTextExpression(label),
|
|
184
|
+
'text-size': label.size !== undefined ? label.size : 12,
|
|
185
|
+
'text-max-width': 8,
|
|
186
|
+
'text-allow-overlap': Boolean(label.allowOverlap),
|
|
187
|
+
'text-ignore-placement': false,
|
|
188
|
+
'text-padding': 2,
|
|
189
|
+
},
|
|
190
|
+
paint: {
|
|
191
|
+
'text-color': label.color || '#16423a',
|
|
192
|
+
'text-halo-color': label.haloColor || 'rgba(255, 255, 255, 0.92)',
|
|
193
|
+
'text-halo-width': label.haloWidth !== undefined ? label.haloWidth : 1.2,
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
if (label.maxzoom !== undefined) layer.maxzoom = label.maxzoom;
|
|
197
|
+
if (label.anchor) layer.layout['text-anchor'] = label.anchor;
|
|
198
|
+
if (Array.isArray(label.font) && label.font.length > 0) layer.layout['text-font'] = label.font;
|
|
199
|
+
return layer;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function emptyMapStyle(scheme) {
|
|
203
|
+
const glyphs = typeof window !== 'undefined' && window.location && window.location.origin
|
|
204
|
+
? window.location.origin + MAP_GLYPHS_URL
|
|
205
|
+
: MAP_GLYPHS_URL;
|
|
206
|
+
return {
|
|
207
|
+
version: 8,
|
|
208
|
+
glyphs,
|
|
209
|
+
sources: {},
|
|
210
|
+
layers: [{
|
|
211
|
+
id: 'gis-background',
|
|
212
|
+
type: 'background',
|
|
213
|
+
paint: { 'background-color': scheme === 'dark' ? '#121b1a' : '#e8ece4' },
|
|
214
|
+
}],
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const zh = {
|
|
219
|
+
'mode.enter': '进入 GIS 工作台',
|
|
220
|
+
'mode.exit': '返回原版模式',
|
|
221
|
+
'mode.workbench': 'GIS 工作台',
|
|
222
|
+
'mode.badge': '工作台模式',
|
|
223
|
+
'mode.framework': 'MapLibre GL JS 预览',
|
|
224
|
+
'mode.original': '原版模式',
|
|
225
|
+
'brand.sub': 'MapLibre GL JS · 对话驱动图层',
|
|
226
|
+
'topbar.stats': '{layers} 个图层',
|
|
227
|
+
'chat.tab': '地图会话',
|
|
228
|
+
'chat.toggle': '对话面板',
|
|
229
|
+
'chat.title': '地图对话',
|
|
230
|
+
'chat.subtitle': '当前会话 · 地图交互',
|
|
231
|
+
'chat.collapse': '收起对话面板',
|
|
232
|
+
'chat.unavailable': '右侧对话暂不可用',
|
|
233
|
+
'chat.noSession': '选择一个会话后即可开始对话',
|
|
234
|
+
'layers.title': '图层',
|
|
235
|
+
'layers.emptyTitle': '暂无图层',
|
|
236
|
+
'layers.emptyBody': '从数据集添加图层,或在右侧对话中描述要添加的图层。',
|
|
237
|
+
'layers.example': '打开顶部「数据集」注册文件或瓦片图层',
|
|
238
|
+
'layers.raster': '栅格',
|
|
239
|
+
'layers.show': '显示图层',
|
|
240
|
+
'layers.hide': '隐藏图层',
|
|
241
|
+
'layers.remove': '移除图层',
|
|
242
|
+
'layers.project': '工程图层',
|
|
243
|
+
'layers.session': '会话图层',
|
|
244
|
+
'layers.unsupported': '不可渲染',
|
|
245
|
+
'layers.noProject': '未选择工程',
|
|
246
|
+
'layers.added': '图层已添加',
|
|
247
|
+
'layers.saveToProject': '保存到工程',
|
|
248
|
+
'layers.zoomTo': '定位到数据范围',
|
|
249
|
+
'layers.addToChat': '添加到对话',
|
|
250
|
+
'layers.newVector': '新建矢量图层',
|
|
251
|
+
'layers.refresh': '刷新图层',
|
|
252
|
+
'layers.editVector': '编辑多边形',
|
|
253
|
+
'layers.appendVector': '追加多边形',
|
|
254
|
+
'layers.editMode': '编辑多边形',
|
|
255
|
+
'layers.editSelect': '点击一个多边形开始编辑',
|
|
256
|
+
'layers.editHint': '拖动节点或拖动多边形内部,修改会自动保存',
|
|
257
|
+
'layers.editClear': '取消选择',
|
|
258
|
+
'layers.editExit': '退出编辑',
|
|
259
|
+
'layers.featureUpdated': '要素已更新',
|
|
260
|
+
'layers.featureUpdateFailed': '更新要素失败',
|
|
261
|
+
'layers.deleteFeature': '删除该要素',
|
|
262
|
+
'layers.featureDeleted': '要素已删除',
|
|
263
|
+
'layers.featureDeleteFailed': '删除要素失败',
|
|
264
|
+
'layers.polygon': '多边形',
|
|
265
|
+
'layers.newVectorTitle': '新建矢量图层',
|
|
266
|
+
'layers.vectorName': '图层名称',
|
|
267
|
+
'layers.vectorHint': '创建后进入多边形绘制,左键加点,右键或“完成”闭合保存。',
|
|
268
|
+
'layers.createVector': '创建并绘制',
|
|
269
|
+
'layers.drawing': '绘制多边形',
|
|
270
|
+
'layers.drawHint': '左键添加点,右键或“完成”闭合多边形',
|
|
271
|
+
'layers.drawComplete': '完成',
|
|
272
|
+
'layers.drawUndo': '撤销',
|
|
273
|
+
'layers.drawCancel': '取消',
|
|
274
|
+
'layers.drawMin': '至少需要 3 个点才能保存多边形',
|
|
275
|
+
'layers.drawSaved': '多边形已保存',
|
|
276
|
+
'layers.drawFailed': '保存多边形失败',
|
|
277
|
+
'layers.drawPoints': '{count} 个点',
|
|
278
|
+
'layers.addedToChat': '图层信息已加入对话输入框',
|
|
279
|
+
'layers.chatUnavailable': '无法定位对话输入框,请先打开地图对话',
|
|
280
|
+
'measure.distance': '距离',
|
|
281
|
+
'measure.area': '面积',
|
|
282
|
+
'measure.hint': '左键点击地图添加点,右键结束测量',
|
|
283
|
+
'measure.finished': '测量已完成(右键结束)',
|
|
284
|
+
'measure.finish': '完成',
|
|
285
|
+
'measure.resume': '继续测量',
|
|
286
|
+
'measure.empty': '还没有测量点',
|
|
287
|
+
'measure.undo': '撤销',
|
|
288
|
+
'measure.clear': '清空',
|
|
289
|
+
'measure.points': '{count} 个点',
|
|
290
|
+
'annotate.default': '标记',
|
|
291
|
+
'annotate.hint': '点击地图添加标记',
|
|
292
|
+
'annotate.empty': '还没有标记',
|
|
293
|
+
'annotate.clear': '清空',
|
|
294
|
+
'annotate.delete': '删除标记',
|
|
295
|
+
'annotate.locate': '定位到标记',
|
|
296
|
+
'annotate.name': '标记名称',
|
|
297
|
+
'project.title': '工程',
|
|
298
|
+
'project.select': '选择工程',
|
|
299
|
+
'project.new': '新建工程',
|
|
300
|
+
'project.namePlaceholder': '工程名称',
|
|
301
|
+
'project.create': '创建',
|
|
302
|
+
'project.cancel': '取消',
|
|
303
|
+
'project.loading': '加载工程…',
|
|
304
|
+
'project.none': '暂无工程',
|
|
305
|
+
'project.created': '工程已创建',
|
|
306
|
+
'project.failed': '工程操作失败',
|
|
307
|
+
'project.save': '保存工程',
|
|
308
|
+
'project.saving': '保存中…',
|
|
309
|
+
'project.saved': '工程已保存',
|
|
310
|
+
'project.saveFailed': '保存工程失败',
|
|
311
|
+
'project.unsaved': '有未保存修改',
|
|
312
|
+
'dataset.manage': '数据集',
|
|
313
|
+
'dataset.title': '数据集管理',
|
|
314
|
+
'dataset.tabList': '已有数据集',
|
|
315
|
+
'dataset.tabBrowse': '服务器目录',
|
|
316
|
+
'dataset.tabUpload': '上传文件',
|
|
317
|
+
'dataset.tabUrl': '瓦片 URL',
|
|
318
|
+
'dataset.tabDatabase': '数据库',
|
|
319
|
+
'dataset.pgDatabase': 'pg数据库',
|
|
320
|
+
'dataset.dbTitle': 'PostgreSQL / PostGIS',
|
|
321
|
+
'dataset.dbHint': '注册 PostGIS 数据库,选择空间表导入当前工程 GPKG 并添加为图层。',
|
|
322
|
+
'dataset.dbName': '连接名称',
|
|
323
|
+
'dataset.dbHost': '主机',
|
|
324
|
+
'dataset.dbPort': '端口',
|
|
325
|
+
'dataset.dbDatabase': '数据库',
|
|
326
|
+
'dataset.dbUser': '用户名',
|
|
327
|
+
'dataset.dbPassword': '密码',
|
|
328
|
+
'dataset.dbSsl': 'SSL 模式',
|
|
329
|
+
'dataset.dbRegister': '注册数据库',
|
|
330
|
+
'dataset.dbConnecting': '正在连接并读取表...',
|
|
331
|
+
'dataset.dbRegistered': '数据库已注册',
|
|
332
|
+
'dataset.dbNoTables': '该数据库中没有可用的 PostGIS 空间表',
|
|
333
|
+
'dataset.dbTables': '空间表',
|
|
334
|
+
'dataset.dbAddTable': '添加为图层',
|
|
335
|
+
'dataset.dbImported': '数据表已导入并添加图层',
|
|
336
|
+
'dataset.dbDelete': '删除连接',
|
|
337
|
+
'dataset.dbSelect': '选择已注册数据库',
|
|
338
|
+
'dataset.dbColumns': '{count} 个字段',
|
|
339
|
+
'dataset.dbFailed': '数据库操作失败',
|
|
340
|
+
'dataset.loading': '加载数据集…',
|
|
341
|
+
'dataset.empty': '还没有数据集',
|
|
342
|
+
'dataset.addLayer': '添加为图层',
|
|
343
|
+
'dataset.convertCog': '转为 COG',
|
|
344
|
+
'dataset.delete': '删除数据集',
|
|
345
|
+
'dataset.deleteConfirm': '删除该数据集及其图层?',
|
|
346
|
+
'dataset.register': '注册数据集',
|
|
347
|
+
'dataset.upload': '上传并注册',
|
|
348
|
+
'dataset.uploadHint': '支持 GeoJSON、CSV、GeoTIFF、图片和 Shapefile',
|
|
349
|
+
'dataset.urlPlaceholder': 'https://…/{z}/{x}/{y}',
|
|
350
|
+
'dataset.urlName': '图层名称',
|
|
351
|
+
'dataset.addUrl': '添加瓦片数据集',
|
|
352
|
+
'dataset.browsePath': '路径',
|
|
353
|
+
'dataset.go': '前往',
|
|
354
|
+
'dataset.up': '上级目录',
|
|
355
|
+
'dataset.selectFile': '选择文件',
|
|
356
|
+
'dataset.selected': '已选择',
|
|
357
|
+
'dataset.noProject': '请先创建或选择一个工程',
|
|
358
|
+
'dataset.registered': '数据集已注册',
|
|
359
|
+
'dataset.uploadSuccess': '文件已上传并注册',
|
|
360
|
+
'dataset.uploadSuccessCount': '已导入 {count} 个数据集并添加图层',
|
|
361
|
+
'dataset.uploading': '正在上传…',
|
|
362
|
+
'dataset.paused': '已暂停,可继续上传',
|
|
363
|
+
'dataset.completing': '正在处理数据(导入矢量 / 转换栅格)…',
|
|
364
|
+
'dataset.resume': '继续上传',
|
|
365
|
+
'dataset.pause': '暂停',
|
|
366
|
+
'dataset.uploadResumable': '分片上传 · 支持断点续传',
|
|
367
|
+
'dataset.uploadProgress': '{percent}% · {uploaded} / {total}',
|
|
368
|
+
'dataset.uploadFailed': '上传失败',
|
|
369
|
+
'dataset.conversionFailed': 'COG 转换失败',
|
|
370
|
+
'dataset.deleted': '数据集已删除',
|
|
371
|
+
'dataset.layerAdded': '图层已添加',
|
|
372
|
+
'dataset.failed': '数据集操作失败',
|
|
373
|
+
'dataset.kind.vector': '矢量',
|
|
374
|
+
'dataset.kind.raster': '栅格',
|
|
375
|
+
'dataset.kind.table': '表',
|
|
376
|
+
'dataset.features': '{count} 个要素',
|
|
377
|
+
'dataset.rows': '{count} 行',
|
|
378
|
+
'dataset.crs': 'CRS {crs}',
|
|
379
|
+
'layers.count': '{count} 个图层',
|
|
380
|
+
'tools.title': '工具',
|
|
381
|
+
'tools.layers': '图层',
|
|
382
|
+
'tools.measure': '测量',
|
|
383
|
+
'tools.annotate': '标记',
|
|
384
|
+
'tools.analyze': '空间分析',
|
|
385
|
+
'tools.data': '数据',
|
|
386
|
+
'tools.pending': '该工具将在后续版本接入',
|
|
387
|
+
'map.title': 'GIS 地图视窗',
|
|
388
|
+
'map.subtitleEmpty': '空图层 · 等待对话添加',
|
|
389
|
+
'map.subtitleActive': '{count} 个图层 · 对话驱动',
|
|
390
|
+
'map.aria': 'GIS 地图视窗,可拖拽平移、滚轮缩放',
|
|
391
|
+
'map.reset': '重置视图',
|
|
392
|
+
'map.zoomIn': '放大',
|
|
393
|
+
'map.zoomOut': '缩小',
|
|
394
|
+
'map.lon': '经度',
|
|
395
|
+
'map.lat': '纬度',
|
|
396
|
+
'map.zoom': '缩放',
|
|
397
|
+
'map.hint': '拖拽平移 · 滚轮缩放 · 图层通过右侧对话添加',
|
|
398
|
+
'map.attribution': '© MapLibre',
|
|
399
|
+
'map.emptyTitle': '地图还没有图层',
|
|
400
|
+
'map.emptyBody': '在右侧对话中说明要添加的图层,例如天地图影像。',
|
|
401
|
+
'map.emptyPrompt': '添加天地图影像图层',
|
|
402
|
+
'map.loading': '地图初始化中…',
|
|
403
|
+
'map.failed': '地图初始化失败,请检查 WebGL 或网络',
|
|
404
|
+
'gis.tool.title': '添加地图图层',
|
|
405
|
+
'gis.tool.preparing': '准备中',
|
|
406
|
+
'gis.tool.adding': '正在添加',
|
|
407
|
+
'gis.tool.added': '已添加',
|
|
408
|
+
'gis.tool.failed': '添加失败',
|
|
409
|
+
'gis.tool.defaultName': '天地图影像',
|
|
410
|
+
'gis.tool.viewMap': '查看地图',
|
|
411
|
+
'gis.style.title': '地图样式',
|
|
412
|
+
'gis.style.updating': '正在应用样式',
|
|
413
|
+
'gis.style.updated': '样式已应用',
|
|
414
|
+
'gis.style.failed': '样式应用失败',
|
|
415
|
+
'gis.style.pendingHint': '等待后端校验并保存图层样式',
|
|
416
|
+
'gis.style.errorFallback': '样式更新失败',
|
|
417
|
+
'gis.style.savedOnly': '样式已保存;当前地图未加载该图层',
|
|
418
|
+
'gis.tool.remove': '从地图移除',
|
|
419
|
+
'gis.tool.pendingHint': '图层会在结果返回后加入地图',
|
|
420
|
+
'gis.tool.errorFallback': '图层参数无效,请重试。',
|
|
421
|
+
'gis.view.title': '地图视野',
|
|
422
|
+
'gis.view.locating': '正在定位',
|
|
423
|
+
'gis.view.located': '已定位',
|
|
424
|
+
'gis.view.failed': '定位失败',
|
|
425
|
+
'gis.view.defaultLabel': '目标区域',
|
|
426
|
+
'gis.view.pendingHint': '视野会在结果返回后移动',
|
|
427
|
+
'gis.view.errorFallback': '视野参数无效,请重试。',
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const en = {
|
|
431
|
+
'mode.enter': 'Open the GIS workbench',
|
|
432
|
+
'mode.exit': 'Return to the standard layout',
|
|
433
|
+
'mode.workbench': 'GIS workbench',
|
|
434
|
+
'mode.badge': 'Workbench mode',
|
|
435
|
+
'mode.framework': 'MapLibre GL JS preview',
|
|
436
|
+
'mode.original': 'Standard layout',
|
|
437
|
+
'brand.sub': 'MapLibre GL JS · conversation-driven layers',
|
|
438
|
+
'topbar.stats': '{layers} layers',
|
|
439
|
+
'chat.tab': 'Map session',
|
|
440
|
+
'chat.toggle': 'Conversation',
|
|
441
|
+
'chat.title': 'Map chat',
|
|
442
|
+
'chat.subtitle': 'Current session · map controls',
|
|
443
|
+
'chat.collapse': 'Collapse the conversation panel',
|
|
444
|
+
'chat.unavailable': 'The conversation panel is unavailable',
|
|
445
|
+
'chat.noSession': 'Select a session to start a conversation',
|
|
446
|
+
'layers.title': 'Layers',
|
|
447
|
+
'layers.emptyTitle': 'No layers yet',
|
|
448
|
+
'layers.emptyBody': 'Add layers from datasets, or describe a layer in the conversation.',
|
|
449
|
+
'layers.example': 'Open Datasets to register a file or tile layer',
|
|
450
|
+
'layers.raster': 'Raster',
|
|
451
|
+
'layers.show': 'Show layer',
|
|
452
|
+
'layers.hide': 'Hide layer',
|
|
453
|
+
'layers.remove': 'Remove layer',
|
|
454
|
+
'layers.project': 'Project layers',
|
|
455
|
+
'layers.session': 'Session layer',
|
|
456
|
+
'layers.unsupported': 'Not renderable',
|
|
457
|
+
'layers.noProject': 'No project selected',
|
|
458
|
+
'layers.added': 'Layer added',
|
|
459
|
+
'layers.saveToProject': 'Save to project',
|
|
460
|
+
'layers.zoomTo': 'Zoom to data extent',
|
|
461
|
+
'layers.addToChat': 'Add to conversation',
|
|
462
|
+
'layers.newVector': 'New vector layer',
|
|
463
|
+
'layers.refresh': 'Refresh layers',
|
|
464
|
+
'layers.editVector': 'Edit polygon',
|
|
465
|
+
'layers.appendVector': 'Append polygon',
|
|
466
|
+
'layers.editMode': 'Edit polygon',
|
|
467
|
+
'layers.editSelect': 'Click a polygon to start editing',
|
|
468
|
+
'layers.editHint': 'Drag a vertex or drag inside the polygon; changes save automatically',
|
|
469
|
+
'layers.editClear': 'Clear selection',
|
|
470
|
+
'layers.editExit': 'Exit edit',
|
|
471
|
+
'layers.featureUpdated': 'Feature updated',
|
|
472
|
+
'layers.featureUpdateFailed': 'Could not update feature',
|
|
473
|
+
'layers.deleteFeature': 'Delete feature',
|
|
474
|
+
'layers.featureDeleted': 'Feature deleted',
|
|
475
|
+
'layers.featureDeleteFailed': 'Could not delete feature',
|
|
476
|
+
'layers.polygon': 'Polygon',
|
|
477
|
+
'layers.newVectorTitle': 'New vector layer',
|
|
478
|
+
'layers.vectorName': 'Layer name',
|
|
479
|
+
'layers.vectorHint': 'After creation, draw a polygon: left-click points, right-click or Finish to close and save.',
|
|
480
|
+
'layers.createVector': 'Create and draw',
|
|
481
|
+
'layers.drawing': 'Draw polygon',
|
|
482
|
+
'layers.drawHint': 'Left-click points; right-click or Finish to close the polygon',
|
|
483
|
+
'layers.drawComplete': 'Finish',
|
|
484
|
+
'layers.drawUndo': 'Undo',
|
|
485
|
+
'layers.drawCancel': 'Cancel',
|
|
486
|
+
'layers.drawMin': 'At least three points are required',
|
|
487
|
+
'layers.drawSaved': 'Polygon saved',
|
|
488
|
+
'layers.drawFailed': 'Could not save polygon',
|
|
489
|
+
'layers.drawPoints': '{count} points',
|
|
490
|
+
'layers.addedToChat': 'Layer context added to the conversation composer',
|
|
491
|
+
'layers.chatUnavailable': 'Could not find the conversation composer; open the map chat first',
|
|
492
|
+
'measure.distance': 'Distance',
|
|
493
|
+
'measure.area': 'Area',
|
|
494
|
+
'measure.hint': 'Left-click to add points; right-click to finish',
|
|
495
|
+
'measure.finished': 'Measurement finished (right-click)',
|
|
496
|
+
'measure.finish': 'Finish',
|
|
497
|
+
'measure.resume': 'Resume measuring',
|
|
498
|
+
'measure.empty': 'No measurement points yet',
|
|
499
|
+
'measure.undo': 'Undo',
|
|
500
|
+
'measure.clear': 'Clear',
|
|
501
|
+
'measure.points': '{count} points',
|
|
502
|
+
'annotate.default': 'Mark',
|
|
503
|
+
'annotate.hint': 'Click the map to add a marker',
|
|
504
|
+
'annotate.empty': 'No markers yet',
|
|
505
|
+
'annotate.clear': 'Clear',
|
|
506
|
+
'annotate.delete': 'Delete marker',
|
|
507
|
+
'annotate.locate': 'Center on marker',
|
|
508
|
+
'annotate.name': 'Marker name',
|
|
509
|
+
'project.title': 'Project',
|
|
510
|
+
'project.select': 'Select project',
|
|
511
|
+
'project.new': 'New project',
|
|
512
|
+
'project.namePlaceholder': 'Project name',
|
|
513
|
+
'project.create': 'Create',
|
|
514
|
+
'project.cancel': 'Cancel',
|
|
515
|
+
'project.loading': 'Loading projects…',
|
|
516
|
+
'project.none': 'No projects',
|
|
517
|
+
'project.created': 'Project created',
|
|
518
|
+
'project.failed': 'Project operation failed',
|
|
519
|
+
'project.save': 'Save project',
|
|
520
|
+
'project.saving': 'Saving…',
|
|
521
|
+
'project.saved': 'Project saved',
|
|
522
|
+
'project.saveFailed': 'Could not save project',
|
|
523
|
+
'project.unsaved': 'Unsaved changes',
|
|
524
|
+
'dataset.manage': 'Datasets',
|
|
525
|
+
'dataset.title': 'Dataset manager',
|
|
526
|
+
'dataset.tabList': 'Datasets',
|
|
527
|
+
'dataset.tabBrowse': 'Server directory',
|
|
528
|
+
'dataset.tabUpload': 'Upload file',
|
|
529
|
+
'dataset.tabUrl': 'Tile URL',
|
|
530
|
+
'dataset.tabDatabase': 'Database',
|
|
531
|
+
'dataset.pgDatabase': 'PostgreSQL',
|
|
532
|
+
'dataset.dbTitle': 'PostgreSQL / PostGIS',
|
|
533
|
+
'dataset.dbHint': 'Register a PostGIS database, import a spatial table into the project GeoPackage, and add it as a layer.',
|
|
534
|
+
'dataset.dbName': 'Connection name',
|
|
535
|
+
'dataset.dbHost': 'Host',
|
|
536
|
+
'dataset.dbPort': 'Port',
|
|
537
|
+
'dataset.dbDatabase': 'Database',
|
|
538
|
+
'dataset.dbUser': 'Username',
|
|
539
|
+
'dataset.dbPassword': 'Password',
|
|
540
|
+
'dataset.dbSsl': 'SSL mode',
|
|
541
|
+
'dataset.dbRegister': 'Register database',
|
|
542
|
+
'dataset.dbConnecting': 'Connecting and reading tables...',
|
|
543
|
+
'dataset.dbRegistered': 'Database registered',
|
|
544
|
+
'dataset.dbNoTables': 'No PostGIS spatial tables found in this database',
|
|
545
|
+
'dataset.dbTables': 'Spatial tables',
|
|
546
|
+
'dataset.dbAddTable': 'Add as layer',
|
|
547
|
+
'dataset.dbImported': 'Table imported and added as a layer',
|
|
548
|
+
'dataset.dbDelete': 'Delete connection',
|
|
549
|
+
'dataset.dbSelect': 'Select a registered database',
|
|
550
|
+
'dataset.dbColumns': '{count} columns',
|
|
551
|
+
'dataset.dbFailed': 'Database operation failed',
|
|
552
|
+
'dataset.loading': 'Loading datasets…',
|
|
553
|
+
'dataset.empty': 'No datasets yet',
|
|
554
|
+
'dataset.addLayer': 'Add as layer',
|
|
555
|
+
'dataset.convertCog': 'Convert to COG',
|
|
556
|
+
'dataset.delete': 'Delete dataset',
|
|
557
|
+
'dataset.deleteConfirm': 'Delete this dataset and its layers?',
|
|
558
|
+
'dataset.register': 'Register dataset',
|
|
559
|
+
'dataset.upload': 'Upload and register',
|
|
560
|
+
'dataset.uploadHint': 'GeoJSON, CSV, GeoTIFF, images, and Shapefile',
|
|
561
|
+
'dataset.urlPlaceholder': 'https://…/{z}/{x}/{y}',
|
|
562
|
+
'dataset.urlName': 'Layer name',
|
|
563
|
+
'dataset.addUrl': 'Add tile dataset',
|
|
564
|
+
'dataset.browsePath': 'Path',
|
|
565
|
+
'dataset.go': 'Go',
|
|
566
|
+
'dataset.up': 'Parent directory',
|
|
567
|
+
'dataset.selectFile': 'Select file',
|
|
568
|
+
'dataset.selected': 'Selected',
|
|
569
|
+
'dataset.noProject': 'Create or select a project first',
|
|
570
|
+
'dataset.registered': 'Dataset registered',
|
|
571
|
+
'dataset.uploadSuccess': 'File uploaded and registered',
|
|
572
|
+
'dataset.uploadSuccessCount': 'Imported {count} datasets and added layers',
|
|
573
|
+
'dataset.uploading': 'Uploading…',
|
|
574
|
+
'dataset.paused': 'Paused — upload can resume',
|
|
575
|
+
'dataset.completing': 'Processing data (vector import / raster conversion)…',
|
|
576
|
+
'dataset.resume': 'Resume upload',
|
|
577
|
+
'dataset.pause': 'Pause',
|
|
578
|
+
'dataset.uploadResumable': 'Chunked upload · resumable',
|
|
579
|
+
'dataset.uploadProgress': '{percent}% · {uploaded} / {total}',
|
|
580
|
+
'dataset.uploadFailed': 'Upload failed',
|
|
581
|
+
'dataset.conversionFailed': 'COG conversion failed',
|
|
582
|
+
'dataset.deleted': 'Dataset deleted',
|
|
583
|
+
'dataset.layerAdded': 'Layer added',
|
|
584
|
+
'dataset.failed': 'Dataset operation failed',
|
|
585
|
+
'dataset.kind.vector': 'Vector',
|
|
586
|
+
'dataset.kind.raster': 'Raster',
|
|
587
|
+
'dataset.kind.table': 'Table',
|
|
588
|
+
'dataset.features': '{count} features',
|
|
589
|
+
'dataset.rows': '{count} rows',
|
|
590
|
+
'dataset.crs': 'CRS {crs}',
|
|
591
|
+
'layers.count': '{count} layers',
|
|
592
|
+
'tools.title': 'Tools',
|
|
593
|
+
'tools.layers': 'Layers',
|
|
594
|
+
'tools.measure': 'Measure',
|
|
595
|
+
'tools.annotate': 'Annotate',
|
|
596
|
+
'tools.analyze': 'Spatial analysis',
|
|
597
|
+
'tools.data': 'Data',
|
|
598
|
+
'tools.pending': 'This tool arrives in a later iteration',
|
|
599
|
+
'map.title': 'GIS map view',
|
|
600
|
+
'map.subtitleEmpty': 'Empty layers · waiting for a conversation',
|
|
601
|
+
'map.subtitleActive': '{count} layers · conversation-driven',
|
|
602
|
+
'map.aria': 'GIS map view. Drag to pan and scroll to zoom.',
|
|
603
|
+
'map.reset': 'Reset view',
|
|
604
|
+
'map.zoomIn': 'Zoom in',
|
|
605
|
+
'map.zoomOut': 'Zoom out',
|
|
606
|
+
'map.lon': 'Lon',
|
|
607
|
+
'map.lat': 'Lat',
|
|
608
|
+
'map.zoom': 'Zoom',
|
|
609
|
+
'map.hint': 'Drag to pan · scroll to zoom · add layers from the conversation on the right',
|
|
610
|
+
'map.attribution': '© MapLibre',
|
|
611
|
+
'map.emptyTitle': 'The map has no layers yet',
|
|
612
|
+
'map.emptyBody': 'Describe a layer in the conversation on the right, for example Tianditu imagery.',
|
|
613
|
+
'map.emptyPrompt': 'Add the Tianditu image layer',
|
|
614
|
+
'map.loading': 'Initializing the map…',
|
|
615
|
+
'map.failed': 'The map could not start. Check WebGL and network access.',
|
|
616
|
+
'gis.tool.title': 'Add map layer',
|
|
617
|
+
'gis.tool.preparing': 'Preparing',
|
|
618
|
+
'gis.tool.adding': 'Adding',
|
|
619
|
+
'gis.tool.added': 'Added',
|
|
620
|
+
'gis.tool.failed': 'Failed',
|
|
621
|
+
'gis.tool.defaultName': 'Tianditu imagery',
|
|
622
|
+
'gis.tool.viewMap': 'View map',
|
|
623
|
+
'gis.style.title': 'Map style',
|
|
624
|
+
'gis.style.updating': 'Applying style',
|
|
625
|
+
'gis.style.updated': 'Style applied',
|
|
626
|
+
'gis.style.failed': 'Style update failed',
|
|
627
|
+
'gis.style.pendingHint': 'Waiting for the backend to validate and persist the layer style',
|
|
628
|
+
'gis.style.errorFallback': 'Style update failed',
|
|
629
|
+
'gis.style.savedOnly': 'Style saved; the layer is not loaded in the current map',
|
|
630
|
+
'gis.tool.remove': 'Remove from map',
|
|
631
|
+
'gis.tool.pendingHint': 'The layer joins the map after the result settles',
|
|
632
|
+
'gis.tool.errorFallback': 'The layer parameters were invalid. Please try again.',
|
|
633
|
+
'gis.view.title': 'Map view',
|
|
634
|
+
'gis.view.locating': 'Locating',
|
|
635
|
+
'gis.view.located': 'Located',
|
|
636
|
+
'gis.view.failed': 'Failed',
|
|
637
|
+
'gis.view.defaultLabel': 'Target area',
|
|
638
|
+
'gis.view.pendingHint': 'The camera moves once the result settles',
|
|
639
|
+
'gis.view.errorFallback': 'The view parameters were invalid. Please try again.',
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
function clamp(value, min, max) {
|
|
643
|
+
return Math.min(max, Math.max(min, value));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function SvgIcon({ name, size = 16, strokeWidth = 1.6 }) {
|
|
647
|
+
const base = {
|
|
648
|
+
width: size,
|
|
649
|
+
height: size,
|
|
650
|
+
viewBox: '0 0 24 24',
|
|
651
|
+
fill: 'none',
|
|
652
|
+
stroke: 'currentColor',
|
|
653
|
+
strokeWidth,
|
|
654
|
+
strokeLinecap: 'round',
|
|
655
|
+
strokeLinejoin: 'round',
|
|
656
|
+
'aria-hidden': 'true',
|
|
657
|
+
focusable: 'false',
|
|
658
|
+
};
|
|
659
|
+
if (name === 'map') {
|
|
660
|
+
return h('svg', base,
|
|
661
|
+
h('path', { d: 'M9 3 3 6v15l6-3 6 3 6-3V3l-6 3-6-3Z' }),
|
|
662
|
+
h('path', { d: 'M9 3v15M15 6v15' }),
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
if (name === 'layers') {
|
|
666
|
+
return h('svg', base,
|
|
667
|
+
h('path', { d: 'm12 3-9 5 9 5 9-5-9-5Z' }),
|
|
668
|
+
h('path', { d: 'm3 12 9 5 9-5M3 16l9 5 9-5' }),
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
if (name === 'image') {
|
|
672
|
+
return h('svg', base,
|
|
673
|
+
h('rect', { x: 3, y: 4, width: 18, height: 16, rx: 2 }),
|
|
674
|
+
h('circle', { cx: 8.5, cy: 9, r: 1.5 }),
|
|
675
|
+
h('path', { d: 'm21 16-5.5-5L7 20' }),
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
if (name === 'eye') {
|
|
679
|
+
return h('svg', base,
|
|
680
|
+
h('path', { d: 'M2.5 12s3.5-6 9.5-6 9.5 6 9.5 6-3.5 6-9.5 6-9.5-6-9.5-6Z' }),
|
|
681
|
+
h('circle', { cx: 12, cy: 12, r: 2.5 }),
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
if (name === 'eye-off') {
|
|
685
|
+
return h('svg', base,
|
|
686
|
+
h('path', { d: 'M3 3l18 18' }),
|
|
687
|
+
h('path', { d: 'M10.6 6.2A10.8 10.8 0 0 1 12 6c6 0 9.5 6 9.5 6a17 17 0 0 1-2.2 3.1M6.3 7.5A17 17 0 0 0 2.5 12s3.5 6 9.5 6a10.8 10.8 0 0 0 4.2-.8' }),
|
|
688
|
+
h('path', { d: 'M9.9 9.9A3 3 0 0 0 14.1 14' }),
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
if (name === 'trash') {
|
|
692
|
+
return h('svg', base,
|
|
693
|
+
h('path', { d: 'M4 7h16M9 7V4h6v3M6 7l1 14h10l1-14M10 11v6M14 11v6' }),
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
if (name === 'ruler') {
|
|
697
|
+
return h('svg', base,
|
|
698
|
+
h('path', { d: 'M3 17 17 3l4 4L7 21l-4-4Z' }),
|
|
699
|
+
h('path', { d: 'm7 13 2 2M10 10l2 2M13 7l2 2' }),
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
if (name === 'pin') {
|
|
703
|
+
return h('svg', base,
|
|
704
|
+
h('path', { d: 'M12 21s7-6.2 7-11a7 7 0 1 0-14 0c0 4.8 7 11 7 11Z' }),
|
|
705
|
+
h('circle', { cx: 12, cy: 10, r: 2.5 }),
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
if (name === 'chart') {
|
|
709
|
+
return h('svg', base,
|
|
710
|
+
h('path', { d: 'M4 20V10M10 20V4M16 20v-7M22 20V8' }),
|
|
711
|
+
h('path', { d: 'M2 20h20' }),
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
if (name === 'folder') {
|
|
715
|
+
return h('svg', base,
|
|
716
|
+
h('path', { d: 'M3 6a2 2 0 0 1 2-2h5l2 2h7a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6Z' }),
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
if (name === 'file') {
|
|
720
|
+
return h('svg', base,
|
|
721
|
+
h('path', { d: 'M14 3H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9l-6-6Z' }),
|
|
722
|
+
h('path', { d: 'M14 3v6h6' }),
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
if (name === 'locate') {
|
|
726
|
+
return h('svg', base,
|
|
727
|
+
h('circle', { cx: 12, cy: 12, r: 6.5 }),
|
|
728
|
+
h('path', { d: 'M12 2v3M12 19v3M2 12h3M19 12h3' }),
|
|
729
|
+
h('circle', { cx: 12, cy: 12, r: 1.5, fill: 'currentColor', stroke: 'none' }),
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
if (name === 'refresh') {
|
|
733
|
+
return h('svg', base,
|
|
734
|
+
h('path', { d: 'M20 11a8 8 0 0 0-14.8-3.5L3 10' }),
|
|
735
|
+
h('path', { d: 'M3 5v5h5' }),
|
|
736
|
+
h('path', { d: 'M4 13a8 8 0 0 0 14.8 3.5L21 14' }),
|
|
737
|
+
h('path', { d: 'M21 19v-5h-5' }),
|
|
738
|
+
);
|
|
739
|
+
}
|
|
740
|
+
if (name === 'pencil') {
|
|
741
|
+
return h('svg', base,
|
|
742
|
+
h('path', { d: 'M4 20l4-1L19 8l-3-3L5 16l-1 4Z' }),
|
|
743
|
+
h('path', { d: 'M14 5l3 3' }),
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
if (name === 'save') {
|
|
747
|
+
return h('svg', base,
|
|
748
|
+
h('path', { d: 'M5 3h11l3 3v15H5V3Z' }),
|
|
749
|
+
h('path', { d: 'M8 3v6h8V3M8 14h8v6H8z' }),
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
if (name === 'upload') {
|
|
753
|
+
return h('svg', base,
|
|
754
|
+
h('path', { d: 'M12 16V4M7 9l5-5 5 5M4 16v4h16v-4' }),
|
|
755
|
+
);
|
|
756
|
+
}
|
|
757
|
+
if (name === 'chevron-left') {
|
|
758
|
+
return h('svg', base, h('path', { d: 'm15 6-6 6 6 6' }));
|
|
759
|
+
}
|
|
760
|
+
if (name === 'database') {
|
|
761
|
+
return h('svg', base,
|
|
762
|
+
h('ellipse', { cx: 12, cy: 5.5, rx: 7.5, ry: 3 }),
|
|
763
|
+
h('path', { d: 'M4.5 5.5v6c0 1.7 3.4 3 7.5 3s7.5-1.3 7.5-3v-6' }),
|
|
764
|
+
h('path', { d: 'M4.5 11.5v6c0 1.7 3.4 3 7.5 3s7.5-1.3 7.5-3v-6' }),
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
if (name === 'chat') {
|
|
768
|
+
return h('svg', base,
|
|
769
|
+
h('path', { d: 'M20 15a2 2 0 0 1-2 2H8l-4 4V5a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v10Z' }),
|
|
770
|
+
h('path', { d: 'M8 9h8M8 13h5' }),
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
if (name === 'layout') {
|
|
774
|
+
return h('svg', base,
|
|
775
|
+
h('rect', { x: 3, y: 4, width: 18, height: 16, rx: 2 }),
|
|
776
|
+
h('path', { d: 'M9 4v16M9 11h12' }),
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
if (name === 'chevron-right') {
|
|
780
|
+
return h('svg', base, h('path', { d: 'm9 6 6 6-6 6' }));
|
|
781
|
+
}
|
|
782
|
+
if (name === 'plus') {
|
|
783
|
+
return h('svg', base, h('path', { d: 'M12 5v14M5 12h14' }));
|
|
784
|
+
}
|
|
785
|
+
if (name === 'minus') {
|
|
786
|
+
return h('svg', base, h('path', { d: 'M5 12h14' }));
|
|
787
|
+
}
|
|
788
|
+
if (name === 'reset') {
|
|
789
|
+
return h('svg', base,
|
|
790
|
+
h('path', { d: 'M3 12a9 9 0 1 0 3-6.7' }),
|
|
791
|
+
h('path', { d: 'M3 4v5h5' }),
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
if (name === 'close') {
|
|
795
|
+
return h('svg', base, h('path', { d: 'M6 6l12 12M18 6 6 18' }));
|
|
796
|
+
}
|
|
797
|
+
if (name === 'warning') {
|
|
798
|
+
return h('svg', base,
|
|
799
|
+
h('path', { d: 'M12 3 2.5 20h19L12 3Z' }),
|
|
800
|
+
h('path', { d: 'M12 9v5M12 17.5v.01' }),
|
|
801
|
+
);
|
|
802
|
+
}
|
|
803
|
+
return h('svg', base, h('circle', { cx: 12, cy: 12, r: 8 }));
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
function useLayerStore() {
|
|
807
|
+
const [snapshot, setSnapshot] = useState(getLayers);
|
|
808
|
+
useEffect(() => subscribe(() => setSnapshot(getLayers())), []);
|
|
809
|
+
return snapshot;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function useViewStore() {
|
|
813
|
+
const [snapshot, setSnapshot] = useState(getView);
|
|
814
|
+
useEffect(() => subscribe(() => setSnapshot(getView())), []);
|
|
815
|
+
return snapshot;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function useScheme(runtime) {
|
|
819
|
+
const read = useCallback(() => {
|
|
820
|
+
try {
|
|
821
|
+
const snapshot = runtime.theme.getTheme();
|
|
822
|
+
return snapshot && snapshot.active && snapshot.active.colorScheme === 'dark' ? 'dark' : 'light';
|
|
823
|
+
} catch (error) {
|
|
824
|
+
return 'light';
|
|
825
|
+
}
|
|
826
|
+
}, [runtime]);
|
|
827
|
+
const [scheme, setScheme] = useState(read);
|
|
828
|
+
useEffect(() => runtime.on('theme/change', () => setScheme(read())), [runtime, read]);
|
|
829
|
+
return scheme;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
function setWorkbenchOpen(value) {
|
|
833
|
+
if (workbenchOpen === value) return;
|
|
834
|
+
workbenchOpen = value;
|
|
835
|
+
for (const listener of [...workbenchListeners]) listener();
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
function useWorkbenchOpen() {
|
|
839
|
+
const [open, setOpen] = useState(workbenchOpen);
|
|
840
|
+
useEffect(() => {
|
|
841
|
+
const listener = () => setOpen(workbenchOpen);
|
|
842
|
+
workbenchListeners.add(listener);
|
|
843
|
+
return () => {
|
|
844
|
+
workbenchListeners.delete(listener);
|
|
845
|
+
};
|
|
846
|
+
}, []);
|
|
847
|
+
return open;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
function GisStyle({ withMap = false }) {
|
|
851
|
+
return h('style', { 'data-gis-workbench': 'styles' }, withMap ? maplibreCss + '\n' + BASE_CSS : BASE_CSS);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function MapCanvas({ runtime, t, activeTool, onToggleLayer, onRemoveLayer, onSaveLayer, onReorderLayers, onNotify, onLayerMenu, onFeatureMenu, drawTarget, onSavePolygon, onCancelDraw, onNewVectorLayer, onEditLayer, onAppendLayer, onRefreshLayers, editTarget, onUpdateFeature, onCancelEdit, projectName }) {
|
|
855
|
+
const scheme = useScheme(runtime);
|
|
856
|
+
const layers = useLayerStore();
|
|
857
|
+
const view = useViewStore();
|
|
858
|
+
const appliedViewRef = useRef(0);
|
|
859
|
+
const containerRef = useRef(null);
|
|
860
|
+
const mapRef = useRef(null);
|
|
861
|
+
const knownRef = useRef(new Map());
|
|
862
|
+
const fittedRef = useRef(new Set());
|
|
863
|
+
const labelLoadsRef = useRef(new Map());
|
|
864
|
+
const [ready, setReady] = useState(false);
|
|
865
|
+
const [mapError, setMapError] = useState(null);
|
|
866
|
+
const [position, setPosition] = useState(null);
|
|
867
|
+
const [zoom, setZoom] = useState(DEFAULT_ZOOM);
|
|
868
|
+
const [measureMode, setMeasureMode] = useState('distance');
|
|
869
|
+
const [measurePoints, setMeasurePoints] = useState([]);
|
|
870
|
+
const [measureFinished, setMeasureFinished] = useState(false);
|
|
871
|
+
const [measureHover, setMeasureHover] = useState(null);
|
|
872
|
+
const [annotations, setAnnotations] = useState([]);
|
|
873
|
+
const [drawPoints, setDrawPoints] = useState([]);
|
|
874
|
+
const [drawSaving, setDrawSaving] = useState(false);
|
|
875
|
+
const [editFeature, setEditFeature] = useState(null);
|
|
876
|
+
const editFeatureRef = useRef(null);
|
|
877
|
+
const editMarkersRef = useRef([]);
|
|
878
|
+
const editDragRef = useRef(null);
|
|
879
|
+
const hoverStampRef = useRef(0);
|
|
880
|
+
const measureSourceId = 'gis-measure-source';
|
|
881
|
+
const markersRef = useRef(new Map());
|
|
882
|
+
const annotationSeqRef = useRef(0);
|
|
883
|
+
|
|
884
|
+
useEffect(() => {
|
|
885
|
+
const container = containerRef.current;
|
|
886
|
+
if (!container || mapRef.current) return undefined;
|
|
887
|
+
let map;
|
|
888
|
+
try {
|
|
889
|
+
if (maplibregl.supported && !maplibregl.supported()) {
|
|
890
|
+
setMapError(t('map.failed'));
|
|
891
|
+
return undefined;
|
|
892
|
+
}
|
|
893
|
+
map = new maplibregl.Map({
|
|
894
|
+
container,
|
|
895
|
+
style: emptyMapStyle(scheme),
|
|
896
|
+
center: DEFAULT_CENTER,
|
|
897
|
+
zoom: DEFAULT_ZOOM,
|
|
898
|
+
minZoom: 1,
|
|
899
|
+
maxZoom: 22,
|
|
900
|
+
localIdeographFontFamily: 'sans-serif',
|
|
901
|
+
attributionControl: false,
|
|
902
|
+
logoPosition: 'bottom-left',
|
|
903
|
+
dragRotate: false,
|
|
904
|
+
pitchWithRotate: false,
|
|
905
|
+
fadeDuration: 120,
|
|
906
|
+
antialias: true,
|
|
907
|
+
});
|
|
908
|
+
} catch (error) {
|
|
909
|
+
console.error('[gis-workbench] map initialization failed', error);
|
|
910
|
+
setMapError(t('map.failed'));
|
|
911
|
+
return undefined;
|
|
912
|
+
}
|
|
913
|
+
mapRef.current = map;
|
|
914
|
+
map.on('load', () => setReady(true));
|
|
915
|
+
map.on('style.load', () => setReady(true));
|
|
916
|
+
map.on('mousemove', (event) => {
|
|
917
|
+
if (event && event.lngLat) setPosition({ lng: event.lngLat.lng, lat: event.lngLat.lat });
|
|
918
|
+
});
|
|
919
|
+
map.on('zoom', () => setZoom(map.getZoom()));
|
|
920
|
+
map.on('mouseout', () => setPosition(null));
|
|
921
|
+
map.on('error', (event) => {
|
|
922
|
+
console.warn('[gis-workbench] map error', event && event.error ? event.error : event);
|
|
923
|
+
});
|
|
924
|
+
try {
|
|
925
|
+
map.addControl(new maplibregl.ScaleControl({ maxWidth: 88, unit: 'metric' }), 'bottom-left');
|
|
926
|
+
} catch (error) {
|
|
927
|
+
console.warn('[gis-workbench] scale control unavailable', error);
|
|
928
|
+
}
|
|
929
|
+
let observer;
|
|
930
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
931
|
+
observer = new ResizeObserver(() => {
|
|
932
|
+
try {
|
|
933
|
+
map.resize();
|
|
934
|
+
} catch (error) {
|
|
935
|
+
// The map may already be removing.
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
observer.observe(container);
|
|
939
|
+
}
|
|
940
|
+
return () => {
|
|
941
|
+
if (observer) observer.disconnect();
|
|
942
|
+
mapRef.current = null;
|
|
943
|
+
knownRef.current = new Map();
|
|
944
|
+
try {
|
|
945
|
+
map.remove();
|
|
946
|
+
} catch (error) {
|
|
947
|
+
// Removal is best effort after a failed initialization.
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
}, []);
|
|
951
|
+
|
|
952
|
+
useEffect(() => {
|
|
953
|
+
const map = mapRef.current;
|
|
954
|
+
if (!map || !ready) return;
|
|
955
|
+
try {
|
|
956
|
+
map.setPaintProperty('gis-background', 'background-color', scheme === 'dark' ? '#121b1a' : '#e8ece4');
|
|
957
|
+
} catch (error) {
|
|
958
|
+
console.warn('[gis-workbench] could not update the map background', error);
|
|
959
|
+
}
|
|
960
|
+
}, [scheme, ready]);
|
|
961
|
+
|
|
962
|
+
useEffect(() => {
|
|
963
|
+
const map = mapRef.current;
|
|
964
|
+
if (!map || !ready) return;
|
|
965
|
+
const desired = new Map();
|
|
966
|
+
const styleColor = (layer) => {
|
|
967
|
+
const style = layer.style && typeof layer.style === 'object' ? layer.style : {};
|
|
968
|
+
return style.color || style['fill-color'] || '#2f7d6d';
|
|
969
|
+
};
|
|
970
|
+
const setVisible = (layer, ids) => {
|
|
971
|
+
for (const id of ids) {
|
|
972
|
+
try {
|
|
973
|
+
map.setLayoutProperty(id, 'visibility', layer.visible ? 'visible' : 'none');
|
|
974
|
+
} catch (error) {
|
|
975
|
+
// A layer that failed to add is skipped.
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
};
|
|
979
|
+
|
|
980
|
+
for (const layer of layers) {
|
|
981
|
+
const base = layer.id;
|
|
982
|
+
try {
|
|
983
|
+
if ((layer.renderKind === 'raster-tiles' || (layer.tileUrl && !layer.renderKind)) && layer.tileUrl) {
|
|
984
|
+
const sourceId = base + '-src';
|
|
985
|
+
if (!map.getSource(sourceId)) {
|
|
986
|
+
map.addSource(sourceId, {
|
|
987
|
+
type: 'raster',
|
|
988
|
+
tiles: [resolveTileUrl(layer.tileUrl)],
|
|
989
|
+
tileSize: layer.tileSize || 256,
|
|
990
|
+
minzoom: layer.minzoom ?? 0,
|
|
991
|
+
maxzoom: layer.maxzoom ?? 18,
|
|
992
|
+
scheme: 'xyz',
|
|
993
|
+
attribution: layer.attribution || '',
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
if (!map.getLayer(base)) {
|
|
997
|
+
map.addLayer({
|
|
998
|
+
id: base,
|
|
999
|
+
type: 'raster',
|
|
1000
|
+
source: sourceId,
|
|
1001
|
+
paint: {
|
|
1002
|
+
'raster-opacity': layer.style && layer.style.opacity !== undefined ? layer.style.opacity : 1,
|
|
1003
|
+
'raster-fade-duration': 120,
|
|
1004
|
+
},
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
setVisible(layer, [base]);
|
|
1008
|
+
desired.set(base, { sourceId, layerIds: [base] });
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
if (layer.renderKind === 'geojson' && layer.url) {
|
|
1013
|
+
const sourceId = base + '-src';
|
|
1014
|
+
const previous = knownRef.current.get(base);
|
|
1015
|
+
if (!map.getSource(sourceId)) {
|
|
1016
|
+
map.addSource(sourceId, { type: 'geojson', data: layer.url });
|
|
1017
|
+
} else if (previous && previous.url !== layer.url) {
|
|
1018
|
+
const source = map.getSource(sourceId);
|
|
1019
|
+
if (source && typeof source.setData === 'function') source.setData(layer.url);
|
|
1020
|
+
}
|
|
1021
|
+
const style = layer.style && typeof layer.style === 'object' ? layer.style : {};
|
|
1022
|
+
const color = style.fillColor || style.lineColor || style.circleColor || style.color || '#2f7d6d';
|
|
1023
|
+
const fillOpacity = style.fillOpacity !== undefined ? style.fillOpacity : (style.opacity !== undefined ? style.opacity : 0.16);
|
|
1024
|
+
const lineOpacity = style.lineOpacity !== undefined ? style.lineOpacity : (style.opacity !== undefined ? style.opacity : 0.9);
|
|
1025
|
+
const circleOpacity = style.circleOpacity !== undefined ? style.circleOpacity : (style.opacity !== undefined ? style.opacity : 1);
|
|
1026
|
+
const fillColor = categoricalFillExpression(style.categorical, color);
|
|
1027
|
+
const ids = [base + '-fill', base + '-line', base + '-circle'];
|
|
1028
|
+
if (!map.getLayer(ids[0])) {
|
|
1029
|
+
map.addLayer({ id: ids[0], type: 'fill', source: sourceId, paint: { 'fill-color': fillColor, 'fill-opacity': fillOpacity } });
|
|
1030
|
+
} else {
|
|
1031
|
+
map.setPaintProperty(ids[0], 'fill-color', fillColor);
|
|
1032
|
+
map.setPaintProperty(ids[0], 'fill-opacity', fillOpacity);
|
|
1033
|
+
}
|
|
1034
|
+
if (!map.getLayer(ids[1])) {
|
|
1035
|
+
map.addLayer({ id: ids[1], type: 'line', source: sourceId, paint: { 'line-color': style.lineColor || color, 'line-width': style.lineWidth !== undefined ? style.lineWidth : 1.4, 'line-opacity': lineOpacity } });
|
|
1036
|
+
} else {
|
|
1037
|
+
map.setPaintProperty(ids[1], 'line-color', style.lineColor || color);
|
|
1038
|
+
map.setPaintProperty(ids[1], 'line-width', style.lineWidth !== undefined ? style.lineWidth : 1.4);
|
|
1039
|
+
map.setPaintProperty(ids[1], 'line-opacity', lineOpacity);
|
|
1040
|
+
}
|
|
1041
|
+
if (!map.getLayer(ids[2])) {
|
|
1042
|
+
map.addLayer({ id: ids[2], type: 'circle', source: sourceId, paint: { 'circle-color': style.circleColor || color, 'circle-radius': style.circleRadius !== undefined ? style.circleRadius : 3.5, 'circle-opacity': circleOpacity, 'circle-stroke-color': '#ffffff', 'circle-stroke-width': 1 } });
|
|
1043
|
+
} else {
|
|
1044
|
+
map.setPaintProperty(ids[2], 'circle-color', style.circleColor || color);
|
|
1045
|
+
map.setPaintProperty(ids[2], 'circle-radius', style.circleRadius !== undefined ? style.circleRadius : 3.5);
|
|
1046
|
+
map.setPaintProperty(ids[2], 'circle-opacity', circleOpacity);
|
|
1047
|
+
}
|
|
1048
|
+
const label = style.label && style.label.field ? style.label : null;
|
|
1049
|
+
const labelId = base + '-label';
|
|
1050
|
+
const labelSourceId = base + '-label-src';
|
|
1051
|
+
if (label) {
|
|
1052
|
+
const onePerFeature = Boolean(label.onePerFeature);
|
|
1053
|
+
const labelSource = onePerFeature ? labelSourceId : sourceId;
|
|
1054
|
+
if (onePerFeature) {
|
|
1055
|
+
if (!map.getSource(labelSourceId)) {
|
|
1056
|
+
map.addSource(labelSourceId, { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
|
|
1057
|
+
}
|
|
1058
|
+
if (labelLoadsRef.current.get(labelSourceId) !== layer.url) {
|
|
1059
|
+
labelLoadsRef.current.set(labelSourceId, layer.url);
|
|
1060
|
+
fetch(layer.url, { credentials: 'same-origin' })
|
|
1061
|
+
.then((response) => {
|
|
1062
|
+
if (!response.ok) throw new Error('Label source HTTP ' + response.status);
|
|
1063
|
+
return response.json();
|
|
1064
|
+
})
|
|
1065
|
+
.then((data) => {
|
|
1066
|
+
const features = data && data.type === 'Feature' ? [data] : (data && Array.isArray(data.features) ? data.features : []);
|
|
1067
|
+
const points = [];
|
|
1068
|
+
for (const feature of features) {
|
|
1069
|
+
const point = geometryLabelPoint(feature && feature.geometry);
|
|
1070
|
+
if (!point) continue;
|
|
1071
|
+
points.push({
|
|
1072
|
+
type: 'Feature',
|
|
1073
|
+
properties: feature.properties || {},
|
|
1074
|
+
geometry: { type: 'Point', coordinates: point },
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
const source = mapRef.current && mapRef.current.getSource(labelSourceId);
|
|
1078
|
+
if (source) source.setData({ type: 'FeatureCollection', features: points });
|
|
1079
|
+
})
|
|
1080
|
+
.catch((error) => {
|
|
1081
|
+
labelLoadsRef.current.delete(labelSourceId);
|
|
1082
|
+
console.warn('[gis-workbench] could not build one-per-feature label points', error);
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
} else if (map.getSource(labelSourceId)) {
|
|
1086
|
+
if (map.getLayer(labelId)) map.removeLayer(labelId);
|
|
1087
|
+
map.removeSource(labelSourceId);
|
|
1088
|
+
labelLoadsRef.current.delete(labelSourceId);
|
|
1089
|
+
}
|
|
1090
|
+
if (map.getLayer(labelId) && map.getLayer(labelId).source !== labelSource) {
|
|
1091
|
+
map.removeLayer(labelId);
|
|
1092
|
+
}
|
|
1093
|
+
if (!map.getLayer(labelId)) {
|
|
1094
|
+
map.addLayer(labelLayer(labelId, labelSource, label));
|
|
1095
|
+
} else {
|
|
1096
|
+
map.setLayoutProperty(labelId, 'text-field', labelTextExpression(label));
|
|
1097
|
+
map.setLayoutProperty(labelId, 'text-size', label.size !== undefined ? label.size : 12);
|
|
1098
|
+
map.setLayoutProperty(labelId, 'text-allow-overlap', Boolean(label.allowOverlap));
|
|
1099
|
+
if (label.anchor) map.setLayoutProperty(labelId, 'text-anchor', label.anchor);
|
|
1100
|
+
if (Array.isArray(label.font) && label.font.length > 0) map.setLayoutProperty(labelId, 'text-font', label.font);
|
|
1101
|
+
map.setPaintProperty(labelId, 'text-color', label.color || '#16423a');
|
|
1102
|
+
map.setPaintProperty(labelId, 'text-halo-color', label.haloColor || 'rgba(255, 255, 255, 0.92)');
|
|
1103
|
+
map.setPaintProperty(labelId, 'text-halo-width', label.haloWidth !== undefined ? label.haloWidth : 1.2);
|
|
1104
|
+
map.setLayerZoomRange(labelId, label.minzoom !== undefined ? label.minzoom : 12, label.maxzoom !== undefined ? label.maxzoom : 24);
|
|
1105
|
+
}
|
|
1106
|
+
ids.push(labelId);
|
|
1107
|
+
} else {
|
|
1108
|
+
if (map.getLayer(labelId)) map.removeLayer(labelId);
|
|
1109
|
+
if (map.getSource(labelSourceId)) {
|
|
1110
|
+
map.removeSource(labelSourceId);
|
|
1111
|
+
labelLoadsRef.current.delete(labelSourceId);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
setVisible(layer, ids);
|
|
1115
|
+
try {
|
|
1116
|
+
map.setLayoutProperty(ids[1], 'visibility', layer.visible && style.lineVisible !== false ? 'visible' : 'none');
|
|
1117
|
+
map.setLayoutProperty(ids[2], 'visibility', layer.visible && style.circleVisible !== false ? 'visible' : 'none');
|
|
1118
|
+
} catch (error) {
|
|
1119
|
+
// The component layer may be absent on an older map instance.
|
|
1120
|
+
}
|
|
1121
|
+
desired.set(base, { sourceId, layerIds: ids, url: layer.url });
|
|
1122
|
+
continue;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
if (layer.renderKind === 'image' && layer.url && Array.isArray(layer.bbox) && layer.bbox.length === 4) {
|
|
1126
|
+
const sourceId = base + '-src';
|
|
1127
|
+
const [west, south, east, north] = layer.bbox;
|
|
1128
|
+
if (!map.getSource(sourceId)) {
|
|
1129
|
+
map.addSource(sourceId, {
|
|
1130
|
+
type: 'image',
|
|
1131
|
+
url: layer.url,
|
|
1132
|
+
coordinates: [[west, north], [east, north], [east, south], [west, south]],
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
if (!map.getLayer(base)) {
|
|
1136
|
+
map.addLayer({
|
|
1137
|
+
id: base,
|
|
1138
|
+
type: 'raster',
|
|
1139
|
+
source: sourceId,
|
|
1140
|
+
paint: { 'raster-opacity': layer.style && layer.style.opacity !== undefined ? layer.style.opacity : 1 },
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
setVisible(layer, [base]);
|
|
1144
|
+
desired.set(base, { sourceId, layerIds: [base] });
|
|
1145
|
+
}
|
|
1146
|
+
} catch (error) {
|
|
1147
|
+
console.warn('[gis-workbench] could not add the map layer', error);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
for (const [id, info] of knownRef.current) {
|
|
1152
|
+
if (desired.has(id)) continue;
|
|
1153
|
+
try {
|
|
1154
|
+
for (const layerId of info.layerIds) {
|
|
1155
|
+
if (map.getLayer(layerId)) map.removeLayer(layerId);
|
|
1156
|
+
}
|
|
1157
|
+
if (map.getSource(info.sourceId)) map.removeSource(info.sourceId);
|
|
1158
|
+
if (map.getSource(id + '-label-src')) map.removeSource(id + '-label-src');
|
|
1159
|
+
labelLoadsRef.current.delete(id + '-label-src');
|
|
1160
|
+
} catch (error) {
|
|
1161
|
+
console.warn('[gis-workbench] could not remove the map layer', error);
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
knownRef.current = desired;
|
|
1165
|
+
|
|
1166
|
+
for (const layer of layers) {
|
|
1167
|
+
const info = desired.get(layer.id);
|
|
1168
|
+
if (!info) continue;
|
|
1169
|
+
for (const layerId of info.layerIds) {
|
|
1170
|
+
if (!map.getLayer(layerId)) continue;
|
|
1171
|
+
try {
|
|
1172
|
+
map.moveLayer(layerId);
|
|
1173
|
+
} catch (error) {
|
|
1174
|
+
// A layer that failed to add is skipped.
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
const pending = layers.filter((layer) => Array.isArray(layer.bbox) && layer.bbox.length === 4 && layer.renderKind !== 'unsupported' && !fittedRef.current.has(layer.id));
|
|
1180
|
+
if (pending.length > 0) {
|
|
1181
|
+
let west = Infinity;
|
|
1182
|
+
let south = Infinity;
|
|
1183
|
+
let east = -Infinity;
|
|
1184
|
+
let north = -Infinity;
|
|
1185
|
+
for (const layer of pending) {
|
|
1186
|
+
const [w, s, e, n] = layer.bbox.map(Number);
|
|
1187
|
+
if (![w, s, e, n].every(Number.isFinite)) continue;
|
|
1188
|
+
west = Math.min(west, w);
|
|
1189
|
+
south = Math.min(south, s);
|
|
1190
|
+
east = Math.max(east, e);
|
|
1191
|
+
north = Math.max(north, n);
|
|
1192
|
+
}
|
|
1193
|
+
if (Number.isFinite(west) && Number.isFinite(south) && Number.isFinite(east) && Number.isFinite(north)) {
|
|
1194
|
+
try {
|
|
1195
|
+
map.fitBounds([[west, south], [east, north]], { padding: 70, maxZoom: 20, duration: 0 });
|
|
1196
|
+
for (const layer of pending) fittedRef.current.add(layer.id);
|
|
1197
|
+
} catch (error) {
|
|
1198
|
+
console.warn('[gis-workbench] could not fit the map to the dataset bounds', error);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
}, [layers, ready]);
|
|
1203
|
+
|
|
1204
|
+
const finishDraw = useCallback(async () => {
|
|
1205
|
+
if (!drawTarget || drawSaving) return;
|
|
1206
|
+
if (drawPoints.length < 3) {
|
|
1207
|
+
if (onNotify) onNotify(t('layers.drawMin'));
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
const ring = [...drawPoints, drawPoints[0]];
|
|
1211
|
+
setDrawSaving(true);
|
|
1212
|
+
try {
|
|
1213
|
+
await onSavePolygon(drawTarget, { type: 'Polygon', coordinates: [ring] });
|
|
1214
|
+
setDrawPoints([]);
|
|
1215
|
+
} catch (error) {
|
|
1216
|
+
if (onNotify) onNotify(error instanceof Error ? error.message : t('layers.drawFailed'));
|
|
1217
|
+
} finally {
|
|
1218
|
+
setDrawSaving(false);
|
|
1219
|
+
}
|
|
1220
|
+
}, [drawTarget, drawPoints, drawSaving, onSavePolygon, onNotify, t]);
|
|
1221
|
+
|
|
1222
|
+
const cancelDraw = useCallback(() => {
|
|
1223
|
+
setDrawPoints([]);
|
|
1224
|
+
if (onCancelDraw) onCancelDraw();
|
|
1225
|
+
}, [onCancelDraw]);
|
|
1226
|
+
|
|
1227
|
+
const saveEditGeometry = useCallback(async () => {
|
|
1228
|
+
const feature = editFeatureRef.current;
|
|
1229
|
+
if (!editTarget || !feature || !Array.isArray(feature.ring) || feature.ring.length < 3) return;
|
|
1230
|
+
try {
|
|
1231
|
+
await onUpdateFeature(editTarget, feature, feature.ring);
|
|
1232
|
+
} catch (error) {
|
|
1233
|
+
if (onNotify) onNotify(error instanceof Error ? error.message : t('layers.featureUpdateFailed'));
|
|
1234
|
+
}
|
|
1235
|
+
}, [editTarget, onUpdateFeature, onNotify, t]);
|
|
1236
|
+
|
|
1237
|
+
useEffect(() => {
|
|
1238
|
+
editFeatureRef.current = editFeature;
|
|
1239
|
+
}, [editFeature]);
|
|
1240
|
+
|
|
1241
|
+
const locateLayer = (layer) => {
|
|
1242
|
+
const map = mapRef.current;
|
|
1243
|
+
if (!map || !layer || !Array.isArray(layer.bbox) || layer.bbox.length !== 4) return;
|
|
1244
|
+
const [west, south, east, north] = layer.bbox.map(Number);
|
|
1245
|
+
if (![west, south, east, north].every(Number.isFinite) || west === east || south === north) return;
|
|
1246
|
+
try {
|
|
1247
|
+
map.fitBounds([[west, south], [east, north]], { padding: 70, maxZoom: 20, duration: 800 });
|
|
1248
|
+
} catch (error) {
|
|
1249
|
+
console.warn('[gis-workbench] could not locate the layer extent', error);
|
|
1250
|
+
}
|
|
1251
|
+
};
|
|
1252
|
+
|
|
1253
|
+
const zoomBy = (factor) => {
|
|
1254
|
+
const map = mapRef.current;
|
|
1255
|
+
if (!map) return;
|
|
1256
|
+
if (factor > 1) map.zoomIn();
|
|
1257
|
+
else map.zoomOut();
|
|
1258
|
+
};
|
|
1259
|
+
|
|
1260
|
+
useEffect(() => {
|
|
1261
|
+
const map = mapRef.current;
|
|
1262
|
+
if (!map || !ready || !view) return;
|
|
1263
|
+
if (appliedViewRef.current === view.token) return;
|
|
1264
|
+
appliedViewRef.current = view.token;
|
|
1265
|
+
try {
|
|
1266
|
+
map.flyTo({
|
|
1267
|
+
center: view.center,
|
|
1268
|
+
zoom: view.zoom,
|
|
1269
|
+
bearing: 0,
|
|
1270
|
+
pitch: 0,
|
|
1271
|
+
duration: 1400,
|
|
1272
|
+
essential: true,
|
|
1273
|
+
});
|
|
1274
|
+
} catch (error) {
|
|
1275
|
+
console.warn('[gis-workbench] could not move the map view', error);
|
|
1276
|
+
}
|
|
1277
|
+
}, [view, ready]);
|
|
1278
|
+
|
|
1279
|
+
const resetView = () => {
|
|
1280
|
+
const map = mapRef.current;
|
|
1281
|
+
if (!map) return;
|
|
1282
|
+
map.flyTo({ center: DEFAULT_CENTER, zoom: DEFAULT_ZOOM, bearing: 0, pitch: 0 });
|
|
1283
|
+
};
|
|
1284
|
+
|
|
1285
|
+
useEffect(() => {
|
|
1286
|
+
const map = mapRef.current;
|
|
1287
|
+
if (!map || !ready) return undefined;
|
|
1288
|
+
if (!map.getSource(measureSourceId)) {
|
|
1289
|
+
map.addSource(measureSourceId, {
|
|
1290
|
+
type: 'geojson',
|
|
1291
|
+
data: { type: 'FeatureCollection', features: [] },
|
|
1292
|
+
});
|
|
1293
|
+
map.addLayer({
|
|
1294
|
+
id: 'gis-measure-fill',
|
|
1295
|
+
type: 'fill',
|
|
1296
|
+
source: measureSourceId,
|
|
1297
|
+
filter: ['==', ['geometry-type'], 'Polygon'],
|
|
1298
|
+
paint: { 'fill-color': '#2f7d6d', 'fill-opacity': 0.18 },
|
|
1299
|
+
});
|
|
1300
|
+
map.addLayer({
|
|
1301
|
+
id: 'gis-measure-line',
|
|
1302
|
+
type: 'line',
|
|
1303
|
+
source: measureSourceId,
|
|
1304
|
+
filter: ['==', ['geometry-type'], 'LineString'],
|
|
1305
|
+
paint: { 'line-color': '#2f7d6d', 'line-width': 2, 'line-dasharray': [2, 1] },
|
|
1306
|
+
});
|
|
1307
|
+
map.addLayer({
|
|
1308
|
+
id: 'gis-measure-points',
|
|
1309
|
+
type: 'circle',
|
|
1310
|
+
source: measureSourceId,
|
|
1311
|
+
filter: ['==', ['geometry-type'], 'Point'],
|
|
1312
|
+
paint: {
|
|
1313
|
+
'circle-radius': 4,
|
|
1314
|
+
'circle-color': '#ffffff',
|
|
1315
|
+
'circle-stroke-color': '#2f7d6d',
|
|
1316
|
+
'circle-stroke-width': 2,
|
|
1317
|
+
},
|
|
1318
|
+
});
|
|
1319
|
+
map.addSource('gis-measure-rubber-source', {
|
|
1320
|
+
type: 'geojson',
|
|
1321
|
+
data: { type: 'FeatureCollection', features: [] },
|
|
1322
|
+
});
|
|
1323
|
+
map.addLayer({
|
|
1324
|
+
id: 'gis-measure-rubber-fill',
|
|
1325
|
+
type: 'fill',
|
|
1326
|
+
source: 'gis-measure-rubber-source',
|
|
1327
|
+
filter: ['==', ['geometry-type'], 'Polygon'],
|
|
1328
|
+
paint: { 'fill-color': '#e8843f', 'fill-opacity': 0.12 },
|
|
1329
|
+
});
|
|
1330
|
+
map.addLayer({
|
|
1331
|
+
id: 'gis-measure-rubber-line',
|
|
1332
|
+
type: 'line',
|
|
1333
|
+
source: 'gis-measure-rubber-source',
|
|
1334
|
+
paint: {
|
|
1335
|
+
'line-color': '#e8843f',
|
|
1336
|
+
'line-width': 1.6,
|
|
1337
|
+
'line-dasharray': [4, 3],
|
|
1338
|
+
},
|
|
1339
|
+
});
|
|
1340
|
+
}
|
|
1341
|
+
return () => {
|
|
1342
|
+
try {
|
|
1343
|
+
for (const layerId of ['gis-measure-rubber-line', 'gis-measure-rubber-fill', 'gis-measure-points', 'gis-measure-line', 'gis-measure-fill']) {
|
|
1344
|
+
if (map.getLayer(layerId)) map.removeLayer(layerId);
|
|
1345
|
+
}
|
|
1346
|
+
if (map.getSource('gis-measure-rubber-source')) map.removeSource('gis-measure-rubber-source');
|
|
1347
|
+
if (map.getSource(measureSourceId)) map.removeSource(measureSourceId);
|
|
1348
|
+
} catch (error) {
|
|
1349
|
+
// The map may already be removed during overlay teardown.
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
}, [ready, measureSourceId]);
|
|
1353
|
+
|
|
1354
|
+
useEffect(() => {
|
|
1355
|
+
const map = mapRef.current;
|
|
1356
|
+
const source = map && map.getSource(measureSourceId);
|
|
1357
|
+
if (!source || !ready) return;
|
|
1358
|
+
source.setData(measurementFeatureCollection(measurePoints, measureMode));
|
|
1359
|
+
for (const layerId of ['gis-measure-fill', 'gis-measure-line', 'gis-measure-points', 'gis-measure-rubber-fill', 'gis-measure-rubber-line']) {
|
|
1360
|
+
if (map.getLayer(layerId)) map.moveLayer(layerId);
|
|
1361
|
+
}
|
|
1362
|
+
}, [measurePoints, measureMode, ready, measureSourceId]);
|
|
1363
|
+
|
|
1364
|
+
useEffect(() => {
|
|
1365
|
+
const map = mapRef.current;
|
|
1366
|
+
const source = map && map.getSource('gis-measure-rubber-source');
|
|
1367
|
+
if (!source || !ready) return undefined;
|
|
1368
|
+
const clear = () => {
|
|
1369
|
+
source.setData({ type: 'FeatureCollection', features: [] });
|
|
1370
|
+
setMeasureHover(null);
|
|
1371
|
+
};
|
|
1372
|
+
if (activeTool !== 'measure' || measureFinished || measurePoints.length === 0) {
|
|
1373
|
+
clear();
|
|
1374
|
+
return undefined;
|
|
1375
|
+
}
|
|
1376
|
+
const last = measurePoints[measurePoints.length - 1];
|
|
1377
|
+
const onMove = (event) => {
|
|
1378
|
+
if (!event || !event.lngLat) return;
|
|
1379
|
+
const next = [event.lngLat.lng, event.lngLat.lat];
|
|
1380
|
+
const ring = measureMode === 'area' && measurePoints.length >= 2
|
|
1381
|
+
? [...measurePoints, next, measurePoints[0]]
|
|
1382
|
+
: null;
|
|
1383
|
+
const features = [{
|
|
1384
|
+
type: 'Feature',
|
|
1385
|
+
properties: {},
|
|
1386
|
+
geometry: { type: 'LineString', coordinates: ring || [last, next] },
|
|
1387
|
+
}];
|
|
1388
|
+
if (ring) {
|
|
1389
|
+
features.push({
|
|
1390
|
+
type: 'Feature',
|
|
1391
|
+
properties: {},
|
|
1392
|
+
geometry: { type: 'Polygon', coordinates: [ring] },
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
source.setData({ type: 'FeatureCollection', features });
|
|
1396
|
+
const now = Date.now();
|
|
1397
|
+
if (now - hoverStampRef.current >= 40) {
|
|
1398
|
+
hoverStampRef.current = now;
|
|
1399
|
+
setMeasureHover(next);
|
|
1400
|
+
}
|
|
1401
|
+
};
|
|
1402
|
+
const onOut = () => clear();
|
|
1403
|
+
map.on('mousemove', onMove);
|
|
1404
|
+
map.on('mouseout', onOut);
|
|
1405
|
+
return () => {
|
|
1406
|
+
map.off('mousemove', onMove);
|
|
1407
|
+
map.off('mouseout', onOut);
|
|
1408
|
+
clear();
|
|
1409
|
+
};
|
|
1410
|
+
}, [activeTool, measureFinished, measureMode, measurePoints, ready]);
|
|
1411
|
+
|
|
1412
|
+
useEffect(() => {
|
|
1413
|
+
const map = mapRef.current;
|
|
1414
|
+
if (!map || !ready) return;
|
|
1415
|
+
for (const layerId of ['gis-measure-fill', 'gis-measure-line', 'gis-measure-points', 'gis-measure-rubber-fill', 'gis-measure-rubber-line']) {
|
|
1416
|
+
if (map.getLayer(layerId)) map.moveLayer(layerId);
|
|
1417
|
+
}
|
|
1418
|
+
}, [layers, ready]);
|
|
1419
|
+
|
|
1420
|
+
useEffect(() => {
|
|
1421
|
+
const map = mapRef.current;
|
|
1422
|
+
if (!map || !ready || drawTarget || editTarget) return undefined;
|
|
1423
|
+
if (activeTool !== 'measure' && activeTool !== 'annotate') return undefined;
|
|
1424
|
+
const onClick = (event) => {
|
|
1425
|
+
const lng = event.lngLat.lng;
|
|
1426
|
+
const lat = event.lngLat.lat;
|
|
1427
|
+
if (activeTool === 'measure') {
|
|
1428
|
+
if (!measureFinished) setMeasurePoints((current) => [...current, [lng, lat]]);
|
|
1429
|
+
} else if (activeTool === 'annotate') {
|
|
1430
|
+
annotationSeqRef.current += 1;
|
|
1431
|
+
const sequence = annotationSeqRef.current;
|
|
1432
|
+
setAnnotations((current) => [...current, {
|
|
1433
|
+
id: 'annotation-' + Date.now() + '-' + sequence,
|
|
1434
|
+
lng,
|
|
1435
|
+
lat,
|
|
1436
|
+
label: t('annotate.default') + ' ' + sequence,
|
|
1437
|
+
}]);
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
map.on('click', onClick);
|
|
1441
|
+
if (activeTool === 'measure') map.doubleClickZoom.disable();
|
|
1442
|
+
return () => {
|
|
1443
|
+
map.off('click', onClick);
|
|
1444
|
+
if (activeTool === 'measure') {
|
|
1445
|
+
try {
|
|
1446
|
+
map.doubleClickZoom.enable();
|
|
1447
|
+
} catch (error) {
|
|
1448
|
+
// The map may already be gone.
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
};
|
|
1452
|
+
}, [activeTool, drawTarget, editTarget, measureFinished, ready, t]);
|
|
1453
|
+
|
|
1454
|
+
useEffect(() => {
|
|
1455
|
+
const map = mapRef.current;
|
|
1456
|
+
if (!map || !ready) return undefined;
|
|
1457
|
+
const canvas = map.getCanvas();
|
|
1458
|
+
const previous = canvas.style.cursor;
|
|
1459
|
+
if (drawTarget) {
|
|
1460
|
+
canvas.style.cursor = 'crosshair';
|
|
1461
|
+
} else if (editTarget) {
|
|
1462
|
+
canvas.style.cursor = 'default';
|
|
1463
|
+
} else if (activeTool === 'measure' || activeTool === 'annotate') {
|
|
1464
|
+
canvas.style.cursor = activeTool === 'measure' && measureFinished ? 'default' : 'crosshair';
|
|
1465
|
+
}
|
|
1466
|
+
return () => {
|
|
1467
|
+
canvas.style.cursor = previous;
|
|
1468
|
+
};
|
|
1469
|
+
}, [activeTool, ready, measureFinished, drawTarget, editTarget]);
|
|
1470
|
+
|
|
1471
|
+
useEffect(() => {
|
|
1472
|
+
const map = mapRef.current;
|
|
1473
|
+
if (!map || !ready || drawTarget || editTarget || activeTool !== 'measure') return undefined;
|
|
1474
|
+
const finish = (event) => {
|
|
1475
|
+
if (event && event.originalEvent && typeof event.originalEvent.preventDefault === 'function') {
|
|
1476
|
+
event.originalEvent.preventDefault();
|
|
1477
|
+
}
|
|
1478
|
+
setMeasureFinished(true);
|
|
1479
|
+
};
|
|
1480
|
+
const preventMenu = (event) => event.preventDefault();
|
|
1481
|
+
const canvas = map.getCanvas();
|
|
1482
|
+
map.on('contextmenu', finish);
|
|
1483
|
+
canvas.addEventListener('contextmenu', preventMenu);
|
|
1484
|
+
return () => {
|
|
1485
|
+
map.off('contextmenu', finish);
|
|
1486
|
+
canvas.removeEventListener('contextmenu', preventMenu);
|
|
1487
|
+
};
|
|
1488
|
+
}, [activeTool, drawTarget, editTarget, ready]);
|
|
1489
|
+
|
|
1490
|
+
useEffect(() => {
|
|
1491
|
+
setDrawPoints([]);
|
|
1492
|
+
}, [drawTarget && drawTarget.layerId]);
|
|
1493
|
+
|
|
1494
|
+
useEffect(() => {
|
|
1495
|
+
const map = mapRef.current;
|
|
1496
|
+
if (!map || !ready) return undefined;
|
|
1497
|
+
if (!map.getSource('gis-draw-source')) {
|
|
1498
|
+
map.addSource('gis-draw-source', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
|
|
1499
|
+
map.addLayer({
|
|
1500
|
+
id: 'gis-draw-fill',
|
|
1501
|
+
type: 'fill',
|
|
1502
|
+
source: 'gis-draw-source',
|
|
1503
|
+
filter: ['==', ['geometry-type'], 'Polygon'],
|
|
1504
|
+
paint: { 'fill-color': '#e8843f', 'fill-opacity': 0.18 },
|
|
1505
|
+
});
|
|
1506
|
+
map.addLayer({
|
|
1507
|
+
id: 'gis-draw-line',
|
|
1508
|
+
type: 'line',
|
|
1509
|
+
source: 'gis-draw-source',
|
|
1510
|
+
filter: ['==', ['geometry-type'], 'LineString'],
|
|
1511
|
+
paint: { 'line-color': '#e8843f', 'line-width': 2, 'line-dasharray': [2, 1] },
|
|
1512
|
+
});
|
|
1513
|
+
map.addLayer({
|
|
1514
|
+
id: 'gis-draw-points',
|
|
1515
|
+
type: 'circle',
|
|
1516
|
+
source: 'gis-draw-source',
|
|
1517
|
+
filter: ['==', ['geometry-type'], 'Point'],
|
|
1518
|
+
paint: {
|
|
1519
|
+
'circle-radius': 4,
|
|
1520
|
+
'circle-color': '#ffffff',
|
|
1521
|
+
'circle-stroke-color': '#e8843f',
|
|
1522
|
+
'circle-stroke-width': 2,
|
|
1523
|
+
},
|
|
1524
|
+
});
|
|
1525
|
+
map.addSource('gis-draw-preview-source', {
|
|
1526
|
+
type: 'geojson',
|
|
1527
|
+
data: { type: 'FeatureCollection', features: [] },
|
|
1528
|
+
});
|
|
1529
|
+
map.addLayer({
|
|
1530
|
+
id: 'gis-draw-preview-fill',
|
|
1531
|
+
type: 'fill',
|
|
1532
|
+
source: 'gis-draw-preview-source',
|
|
1533
|
+
filter: ['==', ['geometry-type'], 'Polygon'],
|
|
1534
|
+
paint: { 'fill-color': '#e8843f', 'fill-opacity': 0.12 },
|
|
1535
|
+
});
|
|
1536
|
+
map.addLayer({
|
|
1537
|
+
id: 'gis-draw-preview-line',
|
|
1538
|
+
type: 'line',
|
|
1539
|
+
source: 'gis-draw-preview-source',
|
|
1540
|
+
filter: ['==', ['geometry-type'], 'LineString'],
|
|
1541
|
+
paint: { 'line-color': '#e8843f', 'line-width': 1.6, 'line-dasharray': [4, 3] },
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
return () => {
|
|
1545
|
+
try {
|
|
1546
|
+
for (const layerId of ['gis-draw-preview-line', 'gis-draw-preview-fill', 'gis-draw-points', 'gis-draw-line', 'gis-draw-fill']) {
|
|
1547
|
+
if (map.getLayer(layerId)) map.removeLayer(layerId);
|
|
1548
|
+
}
|
|
1549
|
+
if (map.getSource('gis-draw-preview-source')) map.removeSource('gis-draw-preview-source');
|
|
1550
|
+
if (map.getSource('gis-draw-source')) map.removeSource('gis-draw-source');
|
|
1551
|
+
} catch (error) {
|
|
1552
|
+
// The map may already be removed.
|
|
1553
|
+
}
|
|
1554
|
+
};
|
|
1555
|
+
}, [ready]);
|
|
1556
|
+
|
|
1557
|
+
useEffect(() => {
|
|
1558
|
+
const map = mapRef.current;
|
|
1559
|
+
const source = map && map.getSource('gis-draw-source');
|
|
1560
|
+
if (!source || !ready) return;
|
|
1561
|
+
source.setData(drawFeatureCollection(drawPoints));
|
|
1562
|
+
for (const layerId of ['gis-draw-fill', 'gis-draw-line', 'gis-draw-points', 'gis-draw-preview-fill', 'gis-draw-preview-line']) {
|
|
1563
|
+
if (map.getLayer(layerId)) map.moveLayer(layerId);
|
|
1564
|
+
}
|
|
1565
|
+
}, [drawPoints, ready]);
|
|
1566
|
+
|
|
1567
|
+
useEffect(() => {
|
|
1568
|
+
const map = mapRef.current;
|
|
1569
|
+
const source = map && map.getSource('gis-draw-preview-source');
|
|
1570
|
+
if (!source || !ready) return undefined;
|
|
1571
|
+
const clear = () => source.setData({ type: 'FeatureCollection', features: [] });
|
|
1572
|
+
if (!drawTarget || drawPoints.length === 0) {
|
|
1573
|
+
clear();
|
|
1574
|
+
return undefined;
|
|
1575
|
+
}
|
|
1576
|
+
const last = drawPoints[drawPoints.length - 1];
|
|
1577
|
+
const onMove = (event) => {
|
|
1578
|
+
if (!event || !event.lngLat) return;
|
|
1579
|
+
const next = [event.lngLat.lng, event.lngLat.lat];
|
|
1580
|
+
const features = [];
|
|
1581
|
+
if (drawPoints.length >= 2) {
|
|
1582
|
+
const ring = [...drawPoints, next, drawPoints[0]];
|
|
1583
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: ring } });
|
|
1584
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [ring] } });
|
|
1585
|
+
} else {
|
|
1586
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: [last, next] } });
|
|
1587
|
+
}
|
|
1588
|
+
source.setData({ type: 'FeatureCollection', features });
|
|
1589
|
+
};
|
|
1590
|
+
const onOut = () => clear();
|
|
1591
|
+
map.on('mousemove', onMove);
|
|
1592
|
+
map.on('mouseout', onOut);
|
|
1593
|
+
return () => {
|
|
1594
|
+
map.off('mousemove', onMove);
|
|
1595
|
+
map.off('mouseout', onOut);
|
|
1596
|
+
clear();
|
|
1597
|
+
};
|
|
1598
|
+
}, [drawTarget, drawPoints, ready]);
|
|
1599
|
+
|
|
1600
|
+
useEffect(() => {
|
|
1601
|
+
const map = mapRef.current;
|
|
1602
|
+
if (!map || !ready) return;
|
|
1603
|
+
for (const layerId of ['gis-draw-fill', 'gis-draw-line', 'gis-draw-points', 'gis-draw-preview-fill', 'gis-draw-preview-line']) {
|
|
1604
|
+
if (map.getLayer(layerId)) map.moveLayer(layerId);
|
|
1605
|
+
}
|
|
1606
|
+
}, [layers, ready]);
|
|
1607
|
+
|
|
1608
|
+
useEffect(() => {
|
|
1609
|
+
const map = mapRef.current;
|
|
1610
|
+
if (!map || !ready || !drawTarget) return undefined;
|
|
1611
|
+
const onClick = (event) => {
|
|
1612
|
+
const lng = event.lngLat.lng;
|
|
1613
|
+
const lat = event.lngLat.lat;
|
|
1614
|
+
setDrawPoints((current) => [...current, [lng, lat]]);
|
|
1615
|
+
};
|
|
1616
|
+
const finish = (event) => {
|
|
1617
|
+
if (event && event.originalEvent && typeof event.originalEvent.preventDefault === 'function') {
|
|
1618
|
+
event.originalEvent.preventDefault();
|
|
1619
|
+
}
|
|
1620
|
+
finishDraw();
|
|
1621
|
+
};
|
|
1622
|
+
const preventMenu = (event) => event.preventDefault();
|
|
1623
|
+
const canvas = map.getCanvas();
|
|
1624
|
+
map.on('click', onClick);
|
|
1625
|
+
map.on('contextmenu', finish);
|
|
1626
|
+
canvas.addEventListener('contextmenu', preventMenu);
|
|
1627
|
+
map.doubleClickZoom.disable();
|
|
1628
|
+
return () => {
|
|
1629
|
+
map.off('click', onClick);
|
|
1630
|
+
map.off('contextmenu', finish);
|
|
1631
|
+
canvas.removeEventListener('contextmenu', preventMenu);
|
|
1632
|
+
try {
|
|
1633
|
+
map.doubleClickZoom.enable();
|
|
1634
|
+
} catch (error) {
|
|
1635
|
+
// The map may already be gone.
|
|
1636
|
+
}
|
|
1637
|
+
};
|
|
1638
|
+
}, [drawTarget, ready, finishDraw]);
|
|
1639
|
+
|
|
1640
|
+
useEffect(() => {
|
|
1641
|
+
if (!drawTarget || typeof window === 'undefined') return undefined;
|
|
1642
|
+
const onKey = (event) => {
|
|
1643
|
+
if (event.key === 'Escape') cancelDraw();
|
|
1644
|
+
};
|
|
1645
|
+
window.addEventListener('keydown', onKey);
|
|
1646
|
+
return () => window.removeEventListener('keydown', onKey);
|
|
1647
|
+
}, [drawTarget, cancelDraw]);
|
|
1648
|
+
|
|
1649
|
+
useEffect(() => {
|
|
1650
|
+
const map = mapRef.current;
|
|
1651
|
+
if (!map || !ready) return undefined;
|
|
1652
|
+
const onContextMenu = (event) => {
|
|
1653
|
+
if (drawTarget || editTarget || activeTool === 'measure') return;
|
|
1654
|
+
const point = event.point;
|
|
1655
|
+
if (!point) return;
|
|
1656
|
+
for (const layer of layers) {
|
|
1657
|
+
if (layer.source !== 'project' || layer.sourceFormat !== 'drawn') continue;
|
|
1658
|
+
const layerIds = [layer.id + '-fill', layer.id + '-line'].filter((id) => map.getLayer(id));
|
|
1659
|
+
if (layerIds.length === 0) continue;
|
|
1660
|
+
const hits = map.queryRenderedFeatures(point, { layers: layerIds });
|
|
1661
|
+
if (!hits.length) continue;
|
|
1662
|
+
if (event.originalEvent && typeof event.originalEvent.preventDefault === 'function') {
|
|
1663
|
+
event.originalEvent.preventDefault();
|
|
1664
|
+
}
|
|
1665
|
+
const properties = hits[0].properties || {};
|
|
1666
|
+
if (onFeatureMenu) {
|
|
1667
|
+
onFeatureMenu(
|
|
1668
|
+
event.originalEvent && event.originalEvent.clientX ? event.originalEvent.clientX : (typeof window !== 'undefined' ? window.innerWidth / 2 : 0),
|
|
1669
|
+
event.originalEvent && event.originalEvent.clientY ? event.originalEvent.clientY : (typeof window !== 'undefined' ? window.innerHeight / 2 : 0),
|
|
1670
|
+
layer,
|
|
1671
|
+
{ name: properties.name, createdAt: properties.created_at },
|
|
1672
|
+
);
|
|
1673
|
+
}
|
|
1674
|
+
return;
|
|
1675
|
+
}
|
|
1676
|
+
};
|
|
1677
|
+
map.on('contextmenu', onContextMenu);
|
|
1678
|
+
return () => map.off('contextmenu', onContextMenu);
|
|
1679
|
+
}, [activeTool, drawTarget, editTarget, layers, ready, onFeatureMenu]);
|
|
1680
|
+
|
|
1681
|
+
useEffect(() => {
|
|
1682
|
+
setEditFeature(null);
|
|
1683
|
+
editFeatureRef.current = null;
|
|
1684
|
+
}, [editTarget && editTarget.layerId]);
|
|
1685
|
+
|
|
1686
|
+
useEffect(() => {
|
|
1687
|
+
const map = mapRef.current;
|
|
1688
|
+
if (!map || !ready) return undefined;
|
|
1689
|
+
if (!map.getSource('gis-edit-source')) {
|
|
1690
|
+
map.addSource('gis-edit-source', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
|
|
1691
|
+
map.addLayer({
|
|
1692
|
+
id: 'gis-edit-fill',
|
|
1693
|
+
type: 'fill',
|
|
1694
|
+
source: 'gis-edit-source',
|
|
1695
|
+
filter: ['==', ['geometry-type'], 'Polygon'],
|
|
1696
|
+
paint: { 'fill-color': '#ffb84d', 'fill-opacity': 0.18 },
|
|
1697
|
+
});
|
|
1698
|
+
map.addLayer({
|
|
1699
|
+
id: 'gis-edit-line',
|
|
1700
|
+
type: 'line',
|
|
1701
|
+
source: 'gis-edit-source',
|
|
1702
|
+
filter: ['==', ['geometry-type'], 'LineString'],
|
|
1703
|
+
paint: { 'line-color': '#ff9f1a', 'line-width': 2.2 },
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
return () => {
|
|
1707
|
+
try {
|
|
1708
|
+
if (map.getLayer('gis-edit-line')) map.removeLayer('gis-edit-line');
|
|
1709
|
+
if (map.getLayer('gis-edit-fill')) map.removeLayer('gis-edit-fill');
|
|
1710
|
+
if (map.getSource('gis-edit-source')) map.removeSource('gis-edit-source');
|
|
1711
|
+
} catch (error) {
|
|
1712
|
+
// The map may already be gone.
|
|
1713
|
+
}
|
|
1714
|
+
};
|
|
1715
|
+
}, [ready]);
|
|
1716
|
+
|
|
1717
|
+
useEffect(() => {
|
|
1718
|
+
const map = mapRef.current;
|
|
1719
|
+
const source = map && map.getSource('gis-edit-source');
|
|
1720
|
+
if (!source || !ready) return;
|
|
1721
|
+
source.setData(editFeature ? editFeatureCollection(editFeature.ring) : { type: 'FeatureCollection', features: [] });
|
|
1722
|
+
for (const layerId of ['gis-edit-fill', 'gis-edit-line']) {
|
|
1723
|
+
if (map.getLayer(layerId)) map.moveLayer(layerId);
|
|
1724
|
+
}
|
|
1725
|
+
}, [editFeature, ready]);
|
|
1726
|
+
|
|
1727
|
+
useEffect(() => {
|
|
1728
|
+
const map = mapRef.current;
|
|
1729
|
+
if (!map || !ready || !editTarget) return undefined;
|
|
1730
|
+
const targetLayer = layers.find((layer) => layer.layerId === editTarget.layerId);
|
|
1731
|
+
if (!targetLayer) return undefined;
|
|
1732
|
+
const onClick = (event) => {
|
|
1733
|
+
const layerIds = [targetLayer.id + '-fill', targetLayer.id + '-line'].filter((id) => map.getLayer(id));
|
|
1734
|
+
const hits = map.queryRenderedFeatures(event.point, { layers: layerIds });
|
|
1735
|
+
if (!hits.length) {
|
|
1736
|
+
setEditFeature(null);
|
|
1737
|
+
return;
|
|
1738
|
+
}
|
|
1739
|
+
const ring = editRingFromGeometry(hits[0].geometry);
|
|
1740
|
+
if (!ring) return;
|
|
1741
|
+
const properties = hits[0].properties || {};
|
|
1742
|
+
setEditFeature({ name: properties.name, createdAt: properties.created_at, ring });
|
|
1743
|
+
};
|
|
1744
|
+
map.on('click', onClick);
|
|
1745
|
+
return () => map.off('click', onClick);
|
|
1746
|
+
}, [editTarget, layers, ready]);
|
|
1747
|
+
|
|
1748
|
+
useEffect(() => {
|
|
1749
|
+
const map = mapRef.current;
|
|
1750
|
+
for (const marker of editMarkersRef.current) marker.remove();
|
|
1751
|
+
editMarkersRef.current = [];
|
|
1752
|
+
if (!map || !ready || !editFeature || !Array.isArray(editFeature.ring)) return undefined;
|
|
1753
|
+
const ring = editFeature.ring;
|
|
1754
|
+
for (let index = 0; index < ring.length - 1; index += 1) {
|
|
1755
|
+
const element = document.createElement('div');
|
|
1756
|
+
element.className = 'gis-vertex-marker';
|
|
1757
|
+
const marker = new maplibregl.Marker({ element, draggable: true, anchor: 'center' })
|
|
1758
|
+
.setLngLat(ring[index])
|
|
1759
|
+
.addTo(map);
|
|
1760
|
+
marker.on('drag', () => {
|
|
1761
|
+
const current = editFeatureRef.current;
|
|
1762
|
+
if (!current) return;
|
|
1763
|
+
const lngLat = marker.getLngLat();
|
|
1764
|
+
const next = current.ring.map((point) => point.slice());
|
|
1765
|
+
next[index] = [lngLat.lng, lngLat.lat];
|
|
1766
|
+
const updated = Object.assign({}, current, { ring: next });
|
|
1767
|
+
editFeatureRef.current = updated;
|
|
1768
|
+
setEditFeature(updated);
|
|
1769
|
+
});
|
|
1770
|
+
marker.on('dragend', () => { saveEditGeometry(); });
|
|
1771
|
+
editMarkersRef.current.push(marker);
|
|
1772
|
+
}
|
|
1773
|
+
return () => {
|
|
1774
|
+
for (const marker of editMarkersRef.current) marker.remove();
|
|
1775
|
+
editMarkersRef.current = [];
|
|
1776
|
+
};
|
|
1777
|
+
}, [editFeature && editFeature.name, editFeature && editFeature.createdAt, editTarget && editTarget.layerId, ready, saveEditGeometry]);
|
|
1778
|
+
|
|
1779
|
+
useEffect(() => {
|
|
1780
|
+
const map = mapRef.current;
|
|
1781
|
+
if (!map || !ready || !editTarget) return undefined;
|
|
1782
|
+
const targetLayer = layers.find((layer) => layer.layerId === editTarget.layerId);
|
|
1783
|
+
if (!targetLayer) return undefined;
|
|
1784
|
+
map.dragPan.disable();
|
|
1785
|
+
const onDown = (event) => {
|
|
1786
|
+
const feature = editFeatureRef.current;
|
|
1787
|
+
if (!feature) return;
|
|
1788
|
+
const layerIds = [targetLayer.id + '-fill'].filter((id) => map.getLayer(id));
|
|
1789
|
+
const hits = map.queryRenderedFeatures(event.point, { layers: layerIds });
|
|
1790
|
+
if (!hits.length) return;
|
|
1791
|
+
const properties = hits[0].properties || {};
|
|
1792
|
+
if (properties.name !== feature.name || properties.created_at !== feature.createdAt) return;
|
|
1793
|
+
editDragRef.current = {
|
|
1794
|
+
start: [event.lngLat.lng, event.lngLat.lat],
|
|
1795
|
+
ring: feature.ring.map((point) => point.slice()),
|
|
1796
|
+
moved: false,
|
|
1797
|
+
};
|
|
1798
|
+
};
|
|
1799
|
+
const onMove = (event) => {
|
|
1800
|
+
const drag = editDragRef.current;
|
|
1801
|
+
const feature = editFeatureRef.current;
|
|
1802
|
+
if (!drag || !feature) return;
|
|
1803
|
+
const dx = event.lngLat.lng - drag.start[0];
|
|
1804
|
+
const dy = event.lngLat.lat - drag.start[1];
|
|
1805
|
+
if (Math.abs(dx) > 1e-10 || Math.abs(dy) > 1e-10) drag.moved = true;
|
|
1806
|
+
const next = drag.ring.map((point) => [point[0] + dx, point[1] + dy]);
|
|
1807
|
+
const updated = Object.assign({}, feature, { ring: next });
|
|
1808
|
+
editFeatureRef.current = updated;
|
|
1809
|
+
setEditFeature(updated);
|
|
1810
|
+
editMarkersRef.current.forEach((marker, index) => {
|
|
1811
|
+
if (next[index]) marker.setLngLat(next[index]);
|
|
1812
|
+
});
|
|
1813
|
+
};
|
|
1814
|
+
const onUp = () => {
|
|
1815
|
+
if (!editDragRef.current) return;
|
|
1816
|
+
const moved = editDragRef.current.moved;
|
|
1817
|
+
editDragRef.current = null;
|
|
1818
|
+
if (moved) saveEditGeometry();
|
|
1819
|
+
};
|
|
1820
|
+
map.on('mousedown', onDown);
|
|
1821
|
+
map.on('mousemove', onMove);
|
|
1822
|
+
map.on('mouseup', onUp);
|
|
1823
|
+
return () => {
|
|
1824
|
+
map.off('mousedown', onDown);
|
|
1825
|
+
map.off('mousemove', onMove);
|
|
1826
|
+
map.off('mouseup', onUp);
|
|
1827
|
+
editDragRef.current = null;
|
|
1828
|
+
try {
|
|
1829
|
+
map.dragPan.enable();
|
|
1830
|
+
} catch (error) {
|
|
1831
|
+
// Map may be gone.
|
|
1832
|
+
}
|
|
1833
|
+
};
|
|
1834
|
+
}, [editTarget, layers, ready, saveEditGeometry]);
|
|
1835
|
+
|
|
1836
|
+
useEffect(() => {
|
|
1837
|
+
if (!editTarget || typeof window === 'undefined') return undefined;
|
|
1838
|
+
const onKey = (event) => {
|
|
1839
|
+
if (event.key === 'Escape') onCancelEdit();
|
|
1840
|
+
};
|
|
1841
|
+
window.addEventListener('keydown', onKey);
|
|
1842
|
+
return () => window.removeEventListener('keydown', onKey);
|
|
1843
|
+
}, [editTarget, onCancelEdit]);
|
|
1844
|
+
|
|
1845
|
+
useEffect(() => {
|
|
1846
|
+
const map = mapRef.current;
|
|
1847
|
+
if (!map || !ready) return undefined;
|
|
1848
|
+
const seen = new Set();
|
|
1849
|
+
annotations.forEach((annotation, index) => {
|
|
1850
|
+
seen.add(annotation.id);
|
|
1851
|
+
let marker = markersRef.current.get(annotation.id);
|
|
1852
|
+
if (!marker) {
|
|
1853
|
+
const element = document.createElement('div');
|
|
1854
|
+
element.className = 'gis-marker';
|
|
1855
|
+
element.addEventListener('click', (event) => event.stopPropagation());
|
|
1856
|
+
marker = new maplibregl.Marker({ element, anchor: 'bottom' })
|
|
1857
|
+
.setLngLat([annotation.lng, annotation.lat])
|
|
1858
|
+
.addTo(map);
|
|
1859
|
+
markersRef.current.set(annotation.id, marker);
|
|
1860
|
+
} else {
|
|
1861
|
+
marker.setLngLat([annotation.lng, annotation.lat]);
|
|
1862
|
+
}
|
|
1863
|
+
const element = marker.getElement();
|
|
1864
|
+
element.textContent = String(index + 1);
|
|
1865
|
+
element.title = annotation.label;
|
|
1866
|
+
element.setAttribute('aria-label', annotation.label);
|
|
1867
|
+
});
|
|
1868
|
+
for (const [id, marker] of markersRef.current) {
|
|
1869
|
+
if (seen.has(id)) continue;
|
|
1870
|
+
marker.remove();
|
|
1871
|
+
markersRef.current.delete(id);
|
|
1872
|
+
}
|
|
1873
|
+
return undefined;
|
|
1874
|
+
}, [annotations, ready]);
|
|
1875
|
+
|
|
1876
|
+
useEffect(() => () => {
|
|
1877
|
+
for (const marker of markersRef.current.values()) {
|
|
1878
|
+
try {
|
|
1879
|
+
marker.remove();
|
|
1880
|
+
} catch (error) {
|
|
1881
|
+
// The map may already be gone.
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
markersRef.current.clear();
|
|
1885
|
+
}, []);
|
|
1886
|
+
|
|
1887
|
+
return h('div', { className: 'gis-map-shell' + (activeTool === 'measure' && !measureFinished ? ' is-measuring' : '') },
|
|
1888
|
+
h('div', { ref: containerRef, className: 'gis-map', role: 'application', 'aria-label': t('map.aria'), 'data-maplibre-version': (maplibregl && maplibregl.version) || 'maplibre' }),
|
|
1889
|
+
!ready && !mapError ? h('div', { className: 'gis-map-loading' }, h('span', { className: 'gis-loading-dot' }), t('map.loading')) : null,
|
|
1890
|
+
mapError ? h('div', { className: 'gis-map-error' }, h(SvgIcon, { name: 'warning', size: 13 }), mapError) : null,
|
|
1891
|
+
ready && !mapError && layers.length === 0 ? h('div', { className: 'gis-empty' },
|
|
1892
|
+
h('div', { className: 'gis-empty-icon' }, h(SvgIcon, { name: 'layers', size: 23 })),
|
|
1893
|
+
h('div', { className: 'gis-empty-title' }, t('map.emptyTitle')),
|
|
1894
|
+
h('div', { className: 'gis-empty-body' }, t('map.emptyBody')),
|
|
1895
|
+
h('div', { className: 'gis-empty-prompt' }, h(SvgIcon, { name: 'chat', size: 12 }), t('map.emptyPrompt')),
|
|
1896
|
+
) : null,
|
|
1897
|
+
h('div', { className: 'gis-map-title-card' },
|
|
1898
|
+
h('span', { className: 'gis-map-title-text' }, t('map.title')),
|
|
1899
|
+
h('span', { className: 'gis-map-title-sub' }, layers.length === 0 ? t('map.subtitleEmpty') : t('map.subtitleActive', { count: layers.length })),
|
|
1900
|
+
),
|
|
1901
|
+
h('div', { className: 'gis-map-actions' },
|
|
1902
|
+
h('div', { className: 'gis-compass', title: 'N', 'aria-hidden': 'true' },
|
|
1903
|
+
h('svg', { width: 22, height: 22, viewBox: '0 0 22 22' },
|
|
1904
|
+
h('circle', { cx: 11, cy: 11, r: 9, fill: 'none', stroke: 'var(--dsw-alias-border-l2)', strokeWidth: 1 }),
|
|
1905
|
+
h('path', { d: 'M11 3.5 13.2 11H8.8L11 3.5Z', fill: 'var(--dsw-alias-brand-primary)' }),
|
|
1906
|
+
h('path', { d: 'M11 18.5 8.8 11h4.4L11 18.5Z', fill: 'var(--dsw-alias-label-secondary)' }),
|
|
1907
|
+
h('text', { x: 11, y: 2.6, textAnchor: 'middle', style: { fontSize: '4.5px', fontWeight: 700, fill: 'var(--dsw-alias-label-secondary)' } }, 'N'),
|
|
1908
|
+
),
|
|
1909
|
+
),
|
|
1910
|
+
h('button', { type: 'button', className: 'gis-icon-btn', title: t('map.zoomIn'), 'aria-label': t('map.zoomIn'), onClick: () => zoomBy(1.25) }, h(SvgIcon, { name: 'plus', size: 16 })),
|
|
1911
|
+
h('button', { type: 'button', className: 'gis-icon-btn', title: t('map.zoomOut'), 'aria-label': t('map.zoomOut'), onClick: () => zoomBy(0.8) }, h(SvgIcon, { name: 'minus', size: 16 })),
|
|
1912
|
+
h('button', { type: 'button', className: 'gis-icon-btn', title: t('map.reset'), 'aria-label': t('map.reset'), onClick: resetView }, h(SvgIcon, { name: 'reset', size: 15 })),
|
|
1913
|
+
),
|
|
1914
|
+
h('div', { className: 'gis-readout' },
|
|
1915
|
+
h('span', { className: 'gis-readout-item' },
|
|
1916
|
+
h('span', { className: 'gis-readout-key' }, t('map.lon')),
|
|
1917
|
+
h('span', { className: 'gis-readout-val' }, position ? position.lng.toFixed(4) + '°' : '—'),
|
|
1918
|
+
),
|
|
1919
|
+
h('span', { className: 'gis-readout-item' },
|
|
1920
|
+
h('span', { className: 'gis-readout-key' }, t('map.lat')),
|
|
1921
|
+
h('span', { className: 'gis-readout-val' }, position ? position.lat.toFixed(4) + '°' : '—'),
|
|
1922
|
+
),
|
|
1923
|
+
h('span', { className: 'gis-readout-item' },
|
|
1924
|
+
h('span', { className: 'gis-readout-key' }, t('map.zoom')),
|
|
1925
|
+
h('span', { className: 'gis-readout-val' }, zoom.toFixed(1)),
|
|
1926
|
+
),
|
|
1927
|
+
),
|
|
1928
|
+
h('div', { className: 'gis-hint' }, t('map.hint')),
|
|
1929
|
+
drawTarget
|
|
1930
|
+
? h(DrawPanel, {
|
|
1931
|
+
t,
|
|
1932
|
+
target: drawTarget,
|
|
1933
|
+
points: drawPoints,
|
|
1934
|
+
saving: drawSaving,
|
|
1935
|
+
onUndo: () => setDrawPoints((current) => current.slice(0, -1)),
|
|
1936
|
+
onFinish: finishDraw,
|
|
1937
|
+
onCancel: cancelDraw,
|
|
1938
|
+
})
|
|
1939
|
+
: editTarget
|
|
1940
|
+
? h(EditPanel, {
|
|
1941
|
+
t,
|
|
1942
|
+
target: editTarget,
|
|
1943
|
+
feature: editFeature,
|
|
1944
|
+
onClear: () => setEditFeature(null),
|
|
1945
|
+
onExit: onCancelEdit,
|
|
1946
|
+
onAppend: () => onAppendLayer && onAppendLayer(editTarget),
|
|
1947
|
+
})
|
|
1948
|
+
: activeTool === 'measure'
|
|
1949
|
+
? h(MeasurePanel, {
|
|
1950
|
+
t,
|
|
1951
|
+
mode: measureMode,
|
|
1952
|
+
points: measurePoints,
|
|
1953
|
+
hover: measureHover,
|
|
1954
|
+
finished: measureFinished,
|
|
1955
|
+
onMode: (nextMode) => {
|
|
1956
|
+
setMeasureMode(nextMode);
|
|
1957
|
+
setMeasurePoints([]);
|
|
1958
|
+
setMeasureFinished(false);
|
|
1959
|
+
},
|
|
1960
|
+
onUndo: () => {
|
|
1961
|
+
setMeasurePoints((current) => current.slice(0, -1));
|
|
1962
|
+
setMeasureFinished(false);
|
|
1963
|
+
},
|
|
1964
|
+
onClear: () => {
|
|
1965
|
+
setMeasurePoints([]);
|
|
1966
|
+
setMeasureFinished(false);
|
|
1967
|
+
},
|
|
1968
|
+
onFinish: () => setMeasureFinished(true),
|
|
1969
|
+
onResume: () => setMeasureFinished(false),
|
|
1970
|
+
})
|
|
1971
|
+
: activeTool === 'annotate'
|
|
1972
|
+
? h(AnnotationPanel, {
|
|
1973
|
+
t,
|
|
1974
|
+
annotations,
|
|
1975
|
+
onRename: (id, label) => setAnnotations((current) => current.map((annotation) => annotation.id === id ? Object.assign({}, annotation, { label }) : annotation)),
|
|
1976
|
+
onDelete: (id) => setAnnotations((current) => current.filter((annotation) => annotation.id !== id)),
|
|
1977
|
+
onClear: () => setAnnotations([]),
|
|
1978
|
+
onFocus: (annotation) => {
|
|
1979
|
+
const map = mapRef.current;
|
|
1980
|
+
if (map) map.flyTo({ center: [annotation.lng, annotation.lat], zoom: Math.max(map.getZoom(), 16), duration: 800 });
|
|
1981
|
+
},
|
|
1982
|
+
})
|
|
1983
|
+
: activeTool === 'layers'
|
|
1984
|
+
? h(LayerPanel, { t, onToggleLayer, onRemoveLayer, onSaveLayer, onReorderLayers, onLocateLayer: locateLayer, onNotify, onLayerMenu, onNewVectorLayer, onEditLayer, onAppendLayer, onRefreshLayers, projectName })
|
|
1985
|
+
: null,
|
|
1986
|
+
);
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
function LayerPanel({ t, onToggleLayer, onRemoveLayer, onSaveLayer, onReorderLayers, onLocateLayer, onNotify, onLayerMenu, onNewVectorLayer, onEditLayer, onAppendLayer, onRefreshLayers, projectName }) {
|
|
1990
|
+
const layers = useLayerStore();
|
|
1991
|
+
const [dragKey, setDragKey] = useState(null);
|
|
1992
|
+
const [dropHint, setDropHint] = useState(null);
|
|
1993
|
+
const credits = [...new Set(layers.map((layer) => layer.attribution).filter(Boolean))].join(' · ');
|
|
1994
|
+
return h('div', { className: 'gis-panel' },
|
|
1995
|
+
h('div', { className: 'gis-panel-head' },
|
|
1996
|
+
h('span', { className: 'gis-panel-title' }, t('layers.title')),
|
|
1997
|
+
h('div', { className: 'gis-panel-head-actions' },
|
|
1998
|
+
h('span', { className: 'gis-panel-count' }, layers.length),
|
|
1999
|
+
onRefreshLayers ? h('button', {
|
|
2000
|
+
type: 'button',
|
|
2001
|
+
className: 'gis-icon-btn gis-icon-btn-sm',
|
|
2002
|
+
title: t('layers.refresh'),
|
|
2003
|
+
'aria-label': t('layers.refresh'),
|
|
2004
|
+
onClick: onRefreshLayers,
|
|
2005
|
+
}, h(SvgIcon, { name: 'refresh', size: 12 })) : null,
|
|
2006
|
+
onNewVectorLayer ? h('button', {
|
|
2007
|
+
type: 'button',
|
|
2008
|
+
className: 'gis-icon-btn gis-icon-btn-sm',
|
|
2009
|
+
title: t('layers.newVector'),
|
|
2010
|
+
'aria-label': t('layers.newVector'),
|
|
2011
|
+
onClick: onNewVectorLayer,
|
|
2012
|
+
}, h(SvgIcon, { name: 'plus', size: 12 })) : null,
|
|
2013
|
+
),
|
|
2014
|
+
),
|
|
2015
|
+
layers.length === 0
|
|
2016
|
+
? h('div', { className: 'gis-layer-empty' },
|
|
2017
|
+
h('span', { className: 'gis-layer-empty-title' }, t('layers.emptyTitle')),
|
|
2018
|
+
h('span', { className: 'gis-layer-empty-body' }, t('layers.emptyBody')),
|
|
2019
|
+
h('span', { className: 'gis-layer-example' }, t('layers.example')),
|
|
2020
|
+
)
|
|
2021
|
+
: h('div', { className: 'gis-layer-list' },
|
|
2022
|
+
layers.map((layer) => h('div', {
|
|
2023
|
+
key: layer.key,
|
|
2024
|
+
className: 'gis-layer-item' + (layer.renderKind === 'unsupported' ? ' is-unsupported' : '')
|
|
2025
|
+
+ (dragKey === layer.key ? ' is-dragging' : '')
|
|
2026
|
+
+ (dropHint && dropHint.key === layer.key ? ' is-drop-' + (dropHint.after ? 'after' : 'before') : ''),
|
|
2027
|
+
draggable: true,
|
|
2028
|
+
title: layer.name,
|
|
2029
|
+
'aria-grabbed': dragKey === layer.key,
|
|
2030
|
+
onContextMenu: (event) => {
|
|
2031
|
+
event.preventDefault();
|
|
2032
|
+
event.stopPropagation();
|
|
2033
|
+
if (onLayerMenu) onLayerMenu(event, layer);
|
|
2034
|
+
},
|
|
2035
|
+
onDragStart: (event) => {
|
|
2036
|
+
setDragKey(layer.key);
|
|
2037
|
+
try {
|
|
2038
|
+
event.dataTransfer.effectAllowed = 'move';
|
|
2039
|
+
event.dataTransfer.setData('text/plain', layer.key);
|
|
2040
|
+
} catch (error) {
|
|
2041
|
+
// Some browsers expose a restricted dataTransfer.
|
|
2042
|
+
}
|
|
2043
|
+
},
|
|
2044
|
+
onDragEnd: () => {
|
|
2045
|
+
setDragKey(null);
|
|
2046
|
+
setDropHint(null);
|
|
2047
|
+
},
|
|
2048
|
+
onDragOver: (event) => {
|
|
2049
|
+
if (!dragKey || dragKey === layer.key) return;
|
|
2050
|
+
event.preventDefault();
|
|
2051
|
+
try {
|
|
2052
|
+
event.dataTransfer.dropEffect = 'move';
|
|
2053
|
+
} catch (error) {
|
|
2054
|
+
// Ignore restricted dataTransfer.
|
|
2055
|
+
}
|
|
2056
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
2057
|
+
const after = event.clientY > rect.top + rect.height / 2;
|
|
2058
|
+
setDropHint({ key: layer.key, after });
|
|
2059
|
+
},
|
|
2060
|
+
onDragLeave: (event) => {
|
|
2061
|
+
if (!event.relatedTarget || !event.currentTarget.contains(event.relatedTarget)) setDropHint(null);
|
|
2062
|
+
},
|
|
2063
|
+
onDrop: (event) => {
|
|
2064
|
+
event.preventDefault();
|
|
2065
|
+
const sourceKey = dragKey || (event.dataTransfer && event.dataTransfer.getData('text/plain'));
|
|
2066
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
2067
|
+
const after = dropHint && dropHint.key === layer.key
|
|
2068
|
+
? dropHint.after
|
|
2069
|
+
: event.clientY > rect.top + rect.height / 2;
|
|
2070
|
+
setDragKey(null);
|
|
2071
|
+
setDropHint(null);
|
|
2072
|
+
if (sourceKey && sourceKey !== layer.key && onReorderLayers) {
|
|
2073
|
+
onReorderLayers(sourceKey, layer.key, after);
|
|
2074
|
+
}
|
|
2075
|
+
},
|
|
2076
|
+
},
|
|
2077
|
+
h('span', { className: 'gis-layer-thumb' }, h(SvgIcon, { name: layer.renderKind === 'unsupported' ? 'file' : 'image', size: 15 })),
|
|
2078
|
+
h('div', { className: 'gis-layer-copy' },
|
|
2079
|
+
h('span', { className: 'gis-layer-name', title: layer.name }, layer.name),
|
|
2080
|
+
h('span', { className: 'gis-layer-meta' }, layer.source === 'project'
|
|
2081
|
+
? datasetKindLabel(layer.datasetKind, t) + (formatLabel(layer.format, layer.sourceFormat, t) ? ' · ' + formatLabel(layer.format, layer.sourceFormat, t) : '') + (layer.renderKind === 'unsupported' ? ' · ' + t('layers.unsupported') : '')
|
|
2082
|
+
: t('layers.session') + ' · ' + t('layers.raster')),
|
|
2083
|
+
layer.style && layer.style.categorical && layer.style.categorical.values
|
|
2084
|
+
? h('div', { className: 'gis-layer-legend' },
|
|
2085
|
+
Object.entries(layer.style.categorical.values).map(([value, item]) => h('span', { key: value, className: 'gis-legend-item' },
|
|
2086
|
+
h('span', { className: 'gis-legend-swatch', style: { background: item.color } }),
|
|
2087
|
+
h('span', { className: 'gis-legend-label' }, item.label || value),
|
|
2088
|
+
)),
|
|
2089
|
+
)
|
|
2090
|
+
: null,
|
|
2091
|
+
),
|
|
2092
|
+
h('div', { className: 'gis-layer-actions' },
|
|
2093
|
+
Array.isArray(layer.bbox) && layer.bbox.length === 4 ? h('button', {
|
|
2094
|
+
type: 'button',
|
|
2095
|
+
className: 'gis-layer-action',
|
|
2096
|
+
title: t('layers.zoomTo'),
|
|
2097
|
+
'aria-label': t('layers.zoomTo'),
|
|
2098
|
+
onClick: () => { if (onLocateLayer) onLocateLayer(layer); },
|
|
2099
|
+
}, h(SvgIcon, { name: 'locate', size: 13 })) : null,
|
|
2100
|
+
layer.source === 'project' && layer.sourceFormat === 'drawn' && onEditLayer ? h('button', {
|
|
2101
|
+
type: 'button',
|
|
2102
|
+
className: 'gis-layer-action',
|
|
2103
|
+
title: t('layers.editVector'),
|
|
2104
|
+
'aria-label': t('layers.editVector'),
|
|
2105
|
+
onClick: () => onEditLayer(layer),
|
|
2106
|
+
}, h(SvgIcon, { name: 'pencil', size: 13 })) : null,
|
|
2107
|
+
layer.source === 'project' && layer.sourceFormat === 'drawn' && onAppendLayer ? h('button', {
|
|
2108
|
+
type: 'button',
|
|
2109
|
+
className: 'gis-layer-action',
|
|
2110
|
+
title: t('layers.appendVector'),
|
|
2111
|
+
'aria-label': t('layers.appendVector'),
|
|
2112
|
+
onClick: () => onAppendLayer({ datasetId: layer.datasetId, layerId: layer.layerId, name: layer.name }),
|
|
2113
|
+
}, h(SvgIcon, { name: 'plus', size: 13 })) : null,
|
|
2114
|
+
layer.source !== 'project' && onSaveLayer ? h('button', {
|
|
2115
|
+
type: 'button',
|
|
2116
|
+
className: 'gis-layer-action',
|
|
2117
|
+
title: t('layers.saveToProject'),
|
|
2118
|
+
'aria-label': t('layers.saveToProject'),
|
|
2119
|
+
onClick: () => onSaveLayer(layer),
|
|
2120
|
+
}, h(SvgIcon, { name: 'database', size: 13 })) : null,
|
|
2121
|
+
h('button', {
|
|
2122
|
+
type: 'button',
|
|
2123
|
+
className: 'gis-layer-action' + (layer.visible ? '' : ' is-off'),
|
|
2124
|
+
title: layer.visible ? t('layers.hide') : t('layers.show'),
|
|
2125
|
+
'aria-label': layer.visible ? t('layers.hide') : t('layers.show'),
|
|
2126
|
+
'aria-pressed': layer.visible,
|
|
2127
|
+
onClick: () => { if (onToggleLayer) onToggleLayer(layer); else toggleLayerVisibility(layer.key); },
|
|
2128
|
+
}, h(SvgIcon, { name: layer.visible ? 'eye' : 'eye-off', size: 13 })),
|
|
2129
|
+
h('button', {
|
|
2130
|
+
type: 'button',
|
|
2131
|
+
className: 'gis-layer-action',
|
|
2132
|
+
title: t('layers.remove'),
|
|
2133
|
+
'aria-label': t('layers.remove'),
|
|
2134
|
+
onClick: () => { if (onRemoveLayer) onRemoveLayer(layer); else removeLayer(layer.key); },
|
|
2135
|
+
}, h(SvgIcon, { name: 'trash', size: 13 })),
|
|
2136
|
+
),
|
|
2137
|
+
)),
|
|
2138
|
+
),
|
|
2139
|
+
h('div', { className: 'gis-panel-foot' }, (projectName ? projectName + ' · ' : '') + t('map.attribution') + (credits ? ' · ' + credits : '')),
|
|
2140
|
+
);
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
const EARTH_RADIUS = 6371008.8;
|
|
2144
|
+
|
|
2145
|
+
function toRadians(value) {
|
|
2146
|
+
return (Number(value) * Math.PI) / 180;
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
function measureDistance(points) {
|
|
2150
|
+
let total = 0;
|
|
2151
|
+
for (let index = 1; index < points.length; index += 1) {
|
|
2152
|
+
const [lng1, lat1] = points[index - 1];
|
|
2153
|
+
const [lng2, lat2] = points[index];
|
|
2154
|
+
const dLat = toRadians(lat2 - lat1);
|
|
2155
|
+
const dLng = toRadians(lng2 - lng1);
|
|
2156
|
+
const a = Math.sin(dLat / 2) ** 2
|
|
2157
|
+
+ Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) * Math.sin(dLng / 2) ** 2;
|
|
2158
|
+
total += 2 * EARTH_RADIUS * Math.asin(Math.min(1, Math.sqrt(a)));
|
|
2159
|
+
}
|
|
2160
|
+
return total;
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
function measureArea(points) {
|
|
2164
|
+
if (points.length < 3) return 0;
|
|
2165
|
+
let total = 0;
|
|
2166
|
+
for (let index = 0; index < points.length; index += 1) {
|
|
2167
|
+
const [lng1, lat1] = points[index];
|
|
2168
|
+
const [lng2, lat2] = points[(index + 1) % points.length];
|
|
2169
|
+
total += toRadians(lng2 - lng1) * (2 + Math.sin(toRadians(lat1)) + Math.sin(toRadians(lat2)));
|
|
2170
|
+
}
|
|
2171
|
+
return Math.abs((total * EARTH_RADIUS * EARTH_RADIUS) / 2);
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
function formatDistance(meters) {
|
|
2175
|
+
if (!Number.isFinite(meters) || meters <= 0) return '—';
|
|
2176
|
+
return meters < 1000 ? meters.toFixed(1) + ' m' : (meters / 1000).toFixed(2) + ' km';
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
function formatArea(squareMeters) {
|
|
2180
|
+
if (!Number.isFinite(squareMeters) || squareMeters <= 0) return '—';
|
|
2181
|
+
return squareMeters < 1000000
|
|
2182
|
+
? squareMeters.toFixed(1) + ' m²'
|
|
2183
|
+
: (squareMeters / 1000000).toFixed(2) + ' km²';
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
function measurementFeatureCollection(points, mode) {
|
|
2187
|
+
const features = [];
|
|
2188
|
+
const closed = mode === 'area' && points.length >= 3 ? [...points, points[0]] : points;
|
|
2189
|
+
if (closed.length >= 2) {
|
|
2190
|
+
features.push({
|
|
2191
|
+
type: 'Feature',
|
|
2192
|
+
properties: {},
|
|
2193
|
+
geometry: { type: 'LineString', coordinates: closed },
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
if (mode === 'area' && closed.length >= 4) {
|
|
2197
|
+
features.push({
|
|
2198
|
+
type: 'Feature',
|
|
2199
|
+
properties: {},
|
|
2200
|
+
geometry: { type: 'Polygon', coordinates: [closed] },
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
for (const point of points) {
|
|
2204
|
+
features.push({
|
|
2205
|
+
type: 'Feature',
|
|
2206
|
+
properties: {},
|
|
2207
|
+
geometry: { type: 'Point', coordinates: point },
|
|
2208
|
+
});
|
|
2209
|
+
}
|
|
2210
|
+
return { type: 'FeatureCollection', features };
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
function drawFeatureCollection(points) {
|
|
2214
|
+
const features = [];
|
|
2215
|
+
const closed = points.length >= 3 ? [...points, points[0]] : points;
|
|
2216
|
+
if (closed.length >= 2) {
|
|
2217
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: closed } });
|
|
2218
|
+
}
|
|
2219
|
+
if (points.length >= 3) {
|
|
2220
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [closed] } });
|
|
2221
|
+
}
|
|
2222
|
+
for (const point of points) {
|
|
2223
|
+
features.push({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: point } });
|
|
2224
|
+
}
|
|
2225
|
+
return { type: 'FeatureCollection', features };
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
function MeasurePanel({ t, mode, points, hover, finished, onMode, onUndo, onClear, onFinish, onResume }) {
|
|
2229
|
+
const previewPoints = hover && !finished ? [...points, hover] : points;
|
|
2230
|
+
const value = mode === 'area'
|
|
2231
|
+
? (previewPoints.length >= 3 ? formatArea(measureArea(previewPoints)) : '—')
|
|
2232
|
+
: (previewPoints.length >= 2 ? formatDistance(measureDistance(previewPoints)) : '—');
|
|
2233
|
+
const canFinish = mode === 'area' ? points.length >= 3 : points.length >= 2;
|
|
2234
|
+
const hint = finished
|
|
2235
|
+
? t('measure.finished')
|
|
2236
|
+
: points.length === 0
|
|
2237
|
+
? t('measure.empty')
|
|
2238
|
+
: t('measure.hint');
|
|
2239
|
+
return h('div', { className: 'gis-panel gis-tool-panel' },
|
|
2240
|
+
h('div', { className: 'gis-panel-head' },
|
|
2241
|
+
h('span', { className: 'gis-panel-title' }, t('tools.measure')),
|
|
2242
|
+
h('div', { className: 'gis-panel-head-actions' },
|
|
2243
|
+
h('span', { className: 'gis-panel-count' }, t('measure.points', { count: points.length })),
|
|
2244
|
+
finished ? h('button', {
|
|
2245
|
+
type: 'button',
|
|
2246
|
+
className: 'gis-icon-btn gis-icon-btn-sm gis-measure-close',
|
|
2247
|
+
title: t('measure.clear'),
|
|
2248
|
+
'aria-label': t('measure.clear'),
|
|
2249
|
+
onClick: onClear,
|
|
2250
|
+
}, h(SvgIcon, { name: 'close', size: 12 })) : null,
|
|
2251
|
+
),
|
|
2252
|
+
),
|
|
2253
|
+
h('div', { className: 'gis-measure-tabs' },
|
|
2254
|
+
h('button', { type: 'button', className: 'gis-tab' + (mode === 'distance' ? ' is-active' : ''), onClick: () => onMode('distance') }, t('measure.distance')),
|
|
2255
|
+
h('button', { type: 'button', className: 'gis-tab' + (mode === 'area' ? ' is-active' : ''), onClick: () => onMode('area') }, t('measure.area')),
|
|
2256
|
+
),
|
|
2257
|
+
h('div', { className: 'gis-measure-value' }, value),
|
|
2258
|
+
h('div', { className: 'gis-tool-hint' + (finished ? ' is-finished' : '') }, hint),
|
|
2259
|
+
h('div', { className: 'gis-tool-actions' },
|
|
2260
|
+
finished
|
|
2261
|
+
? h('button', { type: 'button', className: 'gis-tool-btn', onClick: onResume }, t('measure.resume'))
|
|
2262
|
+
: [
|
|
2263
|
+
h('button', { key: 'undo', type: 'button', className: 'gis-tool-btn', disabled: points.length === 0, onClick: onUndo }, t('measure.undo')),
|
|
2264
|
+
h('button', { key: 'finish', type: 'button', className: 'gis-tool-btn gis-btn-primary', disabled: !canFinish, onClick: onFinish }, t('measure.finish')),
|
|
2265
|
+
],
|
|
2266
|
+
h('button', { type: 'button', className: 'gis-tool-btn', disabled: points.length === 0, onClick: onClear }, t('measure.clear')),
|
|
2267
|
+
),
|
|
2268
|
+
);
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2271
|
+
function EditPanel({ t, target, feature, onClear, onExit, onAppend }) {
|
|
2272
|
+
return h('div', { className: 'gis-panel gis-tool-panel' },
|
|
2273
|
+
h('div', { className: 'gis-panel-head' },
|
|
2274
|
+
h('span', { className: 'gis-panel-title' }, t('layers.editMode')),
|
|
2275
|
+
h('span', { className: 'gis-panel-count' }, feature ? (feature.name || t('layers.polygon')) : ''),
|
|
2276
|
+
),
|
|
2277
|
+
h('div', { className: 'gis-draw-target', title: target ? target.name : '' }, target ? target.name : ''),
|
|
2278
|
+
h('div', { className: 'gis-tool-hint' }, feature ? t('layers.editHint') : t('layers.editSelect')),
|
|
2279
|
+
h('div', { className: 'gis-tool-actions' },
|
|
2280
|
+
feature ? h('button', { type: 'button', className: 'gis-tool-btn', onClick: onClear }, t('layers.editClear')) : null,
|
|
2281
|
+
h('button', { type: 'button', className: 'gis-tool-btn', onClick: onAppend }, t('layers.appendVector')),
|
|
2282
|
+
h('button', { type: 'button', className: 'gis-tool-btn', onClick: onExit }, t('layers.editExit')),
|
|
2283
|
+
),
|
|
2284
|
+
);
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
function DrawPanel({ t, target, points, saving, onUndo, onFinish, onCancel }) {
|
|
2288
|
+
return h('div', { className: 'gis-panel gis-tool-panel' },
|
|
2289
|
+
h('div', { className: 'gis-panel-head' },
|
|
2290
|
+
h('span', { className: 'gis-panel-title' }, t('layers.drawing')),
|
|
2291
|
+
h('span', { className: 'gis-panel-count' }, t('layers.drawPoints', { count: points.length })),
|
|
2292
|
+
),
|
|
2293
|
+
h('div', { className: 'gis-draw-target', title: target ? target.name : '' }, target ? target.name : ''),
|
|
2294
|
+
h('div', { className: 'gis-tool-hint' }, t('layers.drawHint')),
|
|
2295
|
+
h('div', { className: 'gis-tool-actions' },
|
|
2296
|
+
h('button', { type: 'button', className: 'gis-tool-btn', disabled: points.length === 0, onClick: onUndo }, t('layers.drawUndo')),
|
|
2297
|
+
h('button', { type: 'button', className: 'gis-tool-btn gis-btn-primary', disabled: saving || points.length < 3, onClick: onFinish }, t('layers.drawComplete')),
|
|
2298
|
+
h('button', { type: 'button', className: 'gis-tool-btn', onClick: onCancel }, t('layers.drawCancel')),
|
|
2299
|
+
),
|
|
2300
|
+
);
|
|
2301
|
+
}
|
|
2302
|
+
|
|
2303
|
+
function AnnotationPanel({ t, annotations, onRename, onDelete, onClear, onFocus }) {
|
|
2304
|
+
return h('div', { className: 'gis-panel gis-tool-panel' },
|
|
2305
|
+
h('div', { className: 'gis-panel-head' },
|
|
2306
|
+
h('span', { className: 'gis-panel-title' }, t('tools.annotate')),
|
|
2307
|
+
h('span', { className: 'gis-panel-count' }, annotations.length),
|
|
2308
|
+
),
|
|
2309
|
+
annotations.length === 0
|
|
2310
|
+
? h('div', { className: 'gis-layer-empty' }, t('annotate.empty'))
|
|
2311
|
+
: h('div', { className: 'gis-annotation-list' },
|
|
2312
|
+
annotations.map((annotation, index) => h('div', { key: annotation.id, className: 'gis-annotation-row' },
|
|
2313
|
+
h('span', { className: 'gis-annotation-index' }, String(index + 1)),
|
|
2314
|
+
h('input', {
|
|
2315
|
+
className: 'gis-input gis-annotation-input',
|
|
2316
|
+
value: annotation.label,
|
|
2317
|
+
title: t('annotate.name'),
|
|
2318
|
+
'aria-label': t('annotate.name'),
|
|
2319
|
+
onChange: (event) => onRename(annotation.id, event.target.value),
|
|
2320
|
+
}),
|
|
2321
|
+
h('button', { type: 'button', className: 'gis-layer-action', title: t('annotate.locate'), 'aria-label': t('annotate.locate'), onClick: () => onFocus(annotation) }, h(SvgIcon, { name: 'locate', size: 12 })),
|
|
2322
|
+
h('button', { type: 'button', className: 'gis-layer-action', title: t('annotate.delete'), 'aria-label': t('annotate.delete'), onClick: () => onDelete(annotation.id) }, h(SvgIcon, { name: 'trash', size: 12 })),
|
|
2323
|
+
)),
|
|
2324
|
+
),
|
|
2325
|
+
h('div', { className: 'gis-tool-hint' }, t('annotate.hint')),
|
|
2326
|
+
h('div', { className: 'gis-tool-actions' },
|
|
2327
|
+
h('button', { type: 'button', className: 'gis-tool-btn', disabled: annotations.length === 0, onClick: onClear }, t('annotate.clear')),
|
|
2328
|
+
),
|
|
2329
|
+
);
|
|
2330
|
+
}
|
|
2331
|
+
|
|
2332
|
+
function formatBytes(bytes) {
|
|
2333
|
+
const size = Number(bytes);
|
|
2334
|
+
if (!Number.isFinite(size) || size <= 0) return '—';
|
|
2335
|
+
if (size < 1024) return size + ' B';
|
|
2336
|
+
if (size < 1024 * 1024) return (size / 1024).toFixed(1) + ' KB';
|
|
2337
|
+
if (size < 1024 * 1024 * 1024) return (size / (1024 * 1024)).toFixed(1) + ' MB';
|
|
2338
|
+
return (size / (1024 * 1024 * 1024)).toFixed(1) + ' GB';
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
function formatLabel(format, sourceFormat, t) {
|
|
2342
|
+
if (sourceFormat === 'postgis') return t('dataset.pgDatabase');
|
|
2343
|
+
return format || '';
|
|
2344
|
+
}
|
|
2345
|
+
|
|
2346
|
+
function datasetKindLabel(kind, t) {
|
|
2347
|
+
return t('dataset.kind.' + (kind || 'table'));
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
function datasetMeta(dataset, t) {
|
|
2351
|
+
const profile = dataset && dataset.profile ? dataset.profile : {};
|
|
2352
|
+
const parts = [datasetKindLabel(dataset && dataset.kind, t)];
|
|
2353
|
+
if (profile.format || profile.sourceFormat) parts.push(formatLabel(profile.format, profile.sourceFormat, t));
|
|
2354
|
+
if (Number.isFinite(profile.featureCount)) parts.push(t('dataset.features', { count: profile.featureCount }));
|
|
2355
|
+
if (Number.isFinite(profile.rowCountSample)) parts.push(t('dataset.rows', { count: profile.rowCountSample }));
|
|
2356
|
+
if (Number.isFinite(profile.sizeBytes)) parts.push(formatBytes(profile.sizeBytes));
|
|
2357
|
+
if (dataset && dataset.crs) parts.push(t('dataset.crs', { crs: dataset.crs }));
|
|
2358
|
+
if (profile.conversionError) parts.push(t('dataset.conversionFailed'));
|
|
2359
|
+
return parts.join(' · ');
|
|
2360
|
+
}
|
|
2361
|
+
|
|
2362
|
+
function absoluteResourceUrl(value) {
|
|
2363
|
+
const text = String(value || '');
|
|
2364
|
+
if (!text || /^https?:/i.test(text) || text.startsWith('//')) return text;
|
|
2365
|
+
if (text.startsWith('/') && typeof window !== 'undefined' && window.location && window.location.origin) {
|
|
2366
|
+
return window.location.origin + text;
|
|
2367
|
+
}
|
|
2368
|
+
return text;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
function projectLayerToStore(layer, projectId) {
|
|
2372
|
+
const dataset = layer.dataset || {};
|
|
2373
|
+
const profile = dataset.profile || {};
|
|
2374
|
+
const format = dataset.format || profile.format;
|
|
2375
|
+
const bbox = Array.isArray(dataset.bbox) ? dataset.bbox : null;
|
|
2376
|
+
let renderKind = 'unsupported';
|
|
2377
|
+
let tileUrl;
|
|
2378
|
+
let url;
|
|
2379
|
+
if (dataset.kind === 'raster' && format === 'xyz') {
|
|
2380
|
+
renderKind = 'raster-tiles';
|
|
2381
|
+
tileUrl = dataset.uri;
|
|
2382
|
+
} else if (dataset.kind === 'raster' && format === 'cog' && dataset.tilesUrl && profile.renderable !== false) {
|
|
2383
|
+
renderKind = 'raster-tiles';
|
|
2384
|
+
tileUrl = dataset.tilesUrl;
|
|
2385
|
+
} else if (dataset.kind === 'vector' && (format === 'geojson' || format === 'gpkg') && profile.renderable !== false) {
|
|
2386
|
+
renderKind = 'geojson';
|
|
2387
|
+
const contentUrl = dataset.contentUrl || api.contentUrl(dataset.id);
|
|
2388
|
+
const version = [profile.updatedAt || '', profile.featureCount ?? 0, bbox ? bbox.join(',') : '', profile.previewSizeBytes ?? 0].join('|');
|
|
2389
|
+
url = absoluteResourceUrl(contentUrl) + (contentUrl.includes('?') ? '&' : '?') + 'v=' + encodeURIComponent(version);
|
|
2390
|
+
} else if (dataset.kind === 'raster' && format === 'image' && bbox) {
|
|
2391
|
+
renderKind = 'image';
|
|
2392
|
+
url = absoluteResourceUrl(dataset.contentUrl || api.contentUrl(dataset.id));
|
|
2393
|
+
}
|
|
2394
|
+
return {
|
|
2395
|
+
key: 'project:' + layer.id,
|
|
2396
|
+
name: layer.name || profile.name || dataset.uri || '图层',
|
|
2397
|
+
visible: layer.visible !== false,
|
|
2398
|
+
z: Number.isFinite(Number(layer.z)) ? Number(layer.z) : 0,
|
|
2399
|
+
source: 'project',
|
|
2400
|
+
persisted: true,
|
|
2401
|
+
layerId: layer.id,
|
|
2402
|
+
datasetId: layer.datasetId,
|
|
2403
|
+
projectId,
|
|
2404
|
+
renderKind,
|
|
2405
|
+
tileUrl,
|
|
2406
|
+
url,
|
|
2407
|
+
bbox,
|
|
2408
|
+
style: layer.style && typeof layer.style === 'object' ? layer.style : {},
|
|
2409
|
+
datasetKind: dataset.kind,
|
|
2410
|
+
format,
|
|
2411
|
+
sourceFormat: profile.sourceFormat,
|
|
2412
|
+
layerName: profile.layerName || '',
|
|
2413
|
+
fields: Array.isArray(profile.fields) ? profile.fields : [],
|
|
2414
|
+
attribution: profile.attribution || '天地图',
|
|
2415
|
+
tileSize: profile.tileSize || 256,
|
|
2416
|
+
minzoom: profile.minzoom ?? 0,
|
|
2417
|
+
maxzoom: profile.maxzoom ?? 18,
|
|
2418
|
+
};
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
function datasetDisplayName(dataset) {
|
|
2422
|
+
const profile = dataset && dataset.profile ? dataset.profile : {};
|
|
2423
|
+
if (profile.name) return profile.name;
|
|
2424
|
+
if (profile.fileName) return profile.fileName;
|
|
2425
|
+
const uri = String(dataset && dataset.uri ? dataset.uri : '');
|
|
2426
|
+
const clean = uri.split(/[?#]/)[0];
|
|
2427
|
+
const parts = clean.split('/');
|
|
2428
|
+
return parts[parts.length - 1] || uri || '数据集';
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
function NewProjectDialog({ t, onClose, onCreate }) {
|
|
2432
|
+
const [name, setName] = useState('');
|
|
2433
|
+
const [busy, setBusy] = useState(false);
|
|
2434
|
+
const [error, setError] = useState('');
|
|
2435
|
+
const submit = async (event) => {
|
|
2436
|
+
event.preventDefault();
|
|
2437
|
+
const value = name.trim();
|
|
2438
|
+
if (!value || busy) return;
|
|
2439
|
+
setBusy(true);
|
|
2440
|
+
setError('');
|
|
2441
|
+
try {
|
|
2442
|
+
await onCreate(value);
|
|
2443
|
+
onClose();
|
|
2444
|
+
} catch (reason) {
|
|
2445
|
+
setError(reason instanceof Error ? reason.message : String(reason));
|
|
2446
|
+
} finally {
|
|
2447
|
+
setBusy(false);
|
|
2448
|
+
}
|
|
2449
|
+
};
|
|
2450
|
+
return h('div', { className: 'gis-modal', onMouseDown: (event) => { if (event.target === event.currentTarget) onClose(); } },
|
|
2451
|
+
h('form', { className: 'gis-modal-card gis-modal-small', onSubmit: submit },
|
|
2452
|
+
h('div', { className: 'gis-modal-head' },
|
|
2453
|
+
h('div', { className: 'gis-modal-title' }, t('project.new')),
|
|
2454
|
+
h('button', { type: 'button', className: 'gis-icon-btn gis-icon-btn-sm', onClick: onClose, 'aria-label': t('project.cancel') }, h(SvgIcon, { name: 'close', size: 14 })),
|
|
2455
|
+
),
|
|
2456
|
+
h('div', { className: 'gis-modal-body' },
|
|
2457
|
+
h('label', { className: 'gis-field-label' }, t('project.title')),
|
|
2458
|
+
h('input', {
|
|
2459
|
+
className: 'gis-input',
|
|
2460
|
+
value: name,
|
|
2461
|
+
autoFocus: true,
|
|
2462
|
+
placeholder: t('project.namePlaceholder'),
|
|
2463
|
+
onChange: (event) => setName(event.target.value),
|
|
2464
|
+
}),
|
|
2465
|
+
error ? h('div', { className: 'gis-form-error' }, error) : null,
|
|
2466
|
+
),
|
|
2467
|
+
h('div', { className: 'gis-modal-actions' },
|
|
2468
|
+
h('button', { type: 'button', className: 'gis-btn', onClick: onClose }, t('project.cancel')),
|
|
2469
|
+
h('button', { type: 'submit', className: 'gis-btn gis-btn-primary', disabled: busy || !name.trim() }, busy ? t('project.loading') : t('project.create')),
|
|
2470
|
+
),
|
|
2471
|
+
),
|
|
2472
|
+
);
|
|
2473
|
+
}
|
|
2474
|
+
|
|
2475
|
+
function NewVectorLayerDialog({ t, onClose, onCreate }) {
|
|
2476
|
+
const [name, setName] = useState('');
|
|
2477
|
+
const [busy, setBusy] = useState(false);
|
|
2478
|
+
const [error, setError] = useState('');
|
|
2479
|
+
const submit = async (event) => {
|
|
2480
|
+
event.preventDefault();
|
|
2481
|
+
const value = name.trim();
|
|
2482
|
+
if (!value || busy) return;
|
|
2483
|
+
setBusy(true);
|
|
2484
|
+
setError('');
|
|
2485
|
+
try {
|
|
2486
|
+
await onCreate(value);
|
|
2487
|
+
onClose();
|
|
2488
|
+
} catch (reason) {
|
|
2489
|
+
setError(reason instanceof Error ? reason.message : String(reason));
|
|
2490
|
+
} finally {
|
|
2491
|
+
setBusy(false);
|
|
2492
|
+
}
|
|
2493
|
+
};
|
|
2494
|
+
return h('div', { className: 'gis-modal', onMouseDown: (event) => { if (event.target === event.currentTarget) onClose(); } },
|
|
2495
|
+
h('form', { className: 'gis-modal-card gis-modal-small', onSubmit: submit },
|
|
2496
|
+
h('div', { className: 'gis-modal-head' },
|
|
2497
|
+
h('div', { className: 'gis-modal-title' }, t('layers.newVectorTitle')),
|
|
2498
|
+
h('button', { type: 'button', className: 'gis-icon-btn gis-icon-btn-sm', onClick: onClose, 'aria-label': t('project.cancel') }, h(SvgIcon, { name: 'close', size: 14 })),
|
|
2499
|
+
),
|
|
2500
|
+
h('div', { className: 'gis-modal-body' },
|
|
2501
|
+
h('label', { className: 'gis-field-label' }, t('layers.vectorName')),
|
|
2502
|
+
h('input', {
|
|
2503
|
+
className: 'gis-input',
|
|
2504
|
+
value: name,
|
|
2505
|
+
autoFocus: true,
|
|
2506
|
+
placeholder: t('layers.newVectorTitle'),
|
|
2507
|
+
onChange: (event) => setName(event.target.value),
|
|
2508
|
+
}),
|
|
2509
|
+
h('div', { className: 'gis-tool-hint' }, t('layers.vectorHint')),
|
|
2510
|
+
error ? h('div', { className: 'gis-form-error' }, error) : null,
|
|
2511
|
+
),
|
|
2512
|
+
h('div', { className: 'gis-modal-actions' },
|
|
2513
|
+
h('button', { type: 'button', className: 'gis-btn', onClick: onClose }, t('project.cancel')),
|
|
2514
|
+
h('button', { type: 'submit', className: 'gis-btn gis-btn-primary', disabled: busy || !name.trim() }, busy ? t('project.loading') : t('layers.createVector')),
|
|
2515
|
+
),
|
|
2516
|
+
),
|
|
2517
|
+
);
|
|
2518
|
+
}
|
|
2519
|
+
|
|
2520
|
+
function uploadFingerprint(file) {
|
|
2521
|
+
return [file.name, file.size, file.lastModified || 0].join(':');
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
function uploadStorageKey(fingerprint) {
|
|
2525
|
+
return 'gis-workbench.upload.' + fingerprint;
|
|
2526
|
+
}
|
|
2527
|
+
|
|
2528
|
+
function readUploadId(fingerprint) {
|
|
2529
|
+
try {
|
|
2530
|
+
return window.localStorage.getItem(uploadStorageKey(fingerprint)) || '';
|
|
2531
|
+
} catch (error) {
|
|
2532
|
+
return '';
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
|
|
2536
|
+
function writeUploadId(fingerprint, uploadId) {
|
|
2537
|
+
try {
|
|
2538
|
+
if (uploadId) window.localStorage.setItem(uploadStorageKey(fingerprint), uploadId);
|
|
2539
|
+
else window.localStorage.removeItem(uploadStorageKey(fingerprint));
|
|
2540
|
+
} catch (error) {
|
|
2541
|
+
// Resuming after a reload is best effort.
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
const UPLOAD_CHUNK_SIZE = 8 * 1024 * 1024;
|
|
2546
|
+
|
|
2547
|
+
function DatasetManager({ projectId, t, onClose, onChanged, notify }) {
|
|
2548
|
+
const [tab, setTab] = useState('list');
|
|
2549
|
+
const [datasets, setDatasets] = useState([]);
|
|
2550
|
+
const [busy, setBusy] = useState(false);
|
|
2551
|
+
const [message, setMessage] = useState('');
|
|
2552
|
+
const [browse, setBrowse] = useState(null);
|
|
2553
|
+
const [browsePath, setBrowsePath] = useState('');
|
|
2554
|
+
const [selectedFile, setSelectedFile] = useState(null);
|
|
2555
|
+
const [uploadFile, setUploadFile] = useState(null);
|
|
2556
|
+
const [uploadMeta, setUploadMeta] = useState(null);
|
|
2557
|
+
const [uploading, setUploading] = useState(false);
|
|
2558
|
+
const [uploadProgress, setUploadProgress] = useState(0);
|
|
2559
|
+
const [uploadedBytes, setUploadedBytes] = useState(0);
|
|
2560
|
+
const [uploadStatus, setUploadStatus] = useState('idle');
|
|
2561
|
+
const [uploadStatusMessage, setUploadStatusMessage] = useState('');
|
|
2562
|
+
const uploadAbortRef = useRef(null);
|
|
2563
|
+
const uploadPausedRef = useRef(false);
|
|
2564
|
+
const [tileUrl, setTileUrl] = useState(DEFAULT_TILE_URL);
|
|
2565
|
+
const [tileName, setTileName] = useState(DEFAULT_LAYER_NAME);
|
|
2566
|
+
const [databases, setDatabases] = useState([]);
|
|
2567
|
+
const [activeDatabaseId, setActiveDatabaseId] = useState('');
|
|
2568
|
+
const [databaseTables, setDatabaseTables] = useState([]);
|
|
2569
|
+
const [databaseBusy, setDatabaseBusy] = useState(false);
|
|
2570
|
+
const [databaseMessage, setDatabaseMessage] = useState('');
|
|
2571
|
+
const [databaseForm, setDatabaseForm] = useState({
|
|
2572
|
+
name: '',
|
|
2573
|
+
host: '127.0.0.1',
|
|
2574
|
+
port: 5432,
|
|
2575
|
+
dbname: '',
|
|
2576
|
+
username: '',
|
|
2577
|
+
password: '',
|
|
2578
|
+
sslmode: 'prefer',
|
|
2579
|
+
});
|
|
2580
|
+
|
|
2581
|
+
const fail = useCallback((reason) => {
|
|
2582
|
+
const text = reason instanceof Error ? reason.message : String(reason || t('dataset.failed'));
|
|
2583
|
+
setMessage(text);
|
|
2584
|
+
if (notify) notify(text);
|
|
2585
|
+
}, [notify, t]);
|
|
2586
|
+
|
|
2587
|
+
const loadDatasets = useCallback(async () => {
|
|
2588
|
+
if (!projectId) return;
|
|
2589
|
+
try {
|
|
2590
|
+
const rows = await api.listDatasets(projectId);
|
|
2591
|
+
setDatasets(rows);
|
|
2592
|
+
} catch (reason) {
|
|
2593
|
+
fail(reason);
|
|
2594
|
+
}
|
|
2595
|
+
}, [projectId, fail]);
|
|
2596
|
+
|
|
2597
|
+
const loadBrowse = useCallback(async (pathname) => {
|
|
2598
|
+
setBusy(true);
|
|
2599
|
+
try {
|
|
2600
|
+
const result = await api.browse(pathname);
|
|
2601
|
+
setBrowse(result);
|
|
2602
|
+
setBrowsePath(result.path);
|
|
2603
|
+
setSelectedFile(null);
|
|
2604
|
+
} catch (reason) {
|
|
2605
|
+
fail(reason);
|
|
2606
|
+
} finally {
|
|
2607
|
+
setBusy(false);
|
|
2608
|
+
}
|
|
2609
|
+
}, [fail]);
|
|
2610
|
+
|
|
2611
|
+
const loadDatabases = useCallback(async () => {
|
|
2612
|
+
if (!projectId) return;
|
|
2613
|
+
try {
|
|
2614
|
+
const rows = await api.listDatabases(projectId);
|
|
2615
|
+
setDatabases(rows);
|
|
2616
|
+
setActiveDatabaseId((current) => current && rows.some((row) => row.id === current) ? current : (rows[0] ? rows[0].id : ''));
|
|
2617
|
+
} catch (reason) {
|
|
2618
|
+
setDatabaseMessage(reason instanceof Error ? reason.message : t('dataset.dbFailed'));
|
|
2619
|
+
}
|
|
2620
|
+
}, [projectId, t]);
|
|
2621
|
+
|
|
2622
|
+
const loadDatabaseTables = useCallback(async (databaseId) => {
|
|
2623
|
+
if (!databaseId) return;
|
|
2624
|
+
setDatabaseBusy(true);
|
|
2625
|
+
setDatabaseMessage('');
|
|
2626
|
+
try {
|
|
2627
|
+
const tables = await api.listDatabaseTables(databaseId);
|
|
2628
|
+
setDatabaseTables(tables);
|
|
2629
|
+
if (tables.length === 0) setDatabaseMessage(t('dataset.dbNoTables'));
|
|
2630
|
+
} catch (reason) {
|
|
2631
|
+
setDatabaseTables([]);
|
|
2632
|
+
setDatabaseMessage(reason instanceof Error ? reason.message : t('dataset.dbFailed'));
|
|
2633
|
+
} finally {
|
|
2634
|
+
setDatabaseBusy(false);
|
|
2635
|
+
}
|
|
2636
|
+
}, [t]);
|
|
2637
|
+
|
|
2638
|
+
useEffect(() => {
|
|
2639
|
+
loadDatasets();
|
|
2640
|
+
}, [loadDatasets]);
|
|
2641
|
+
|
|
2642
|
+
useEffect(() => {
|
|
2643
|
+
if (tab === 'database') loadDatabases();
|
|
2644
|
+
// Load once when the Database tab is opened.
|
|
2645
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2646
|
+
}, [tab]);
|
|
2647
|
+
|
|
2648
|
+
useEffect(() => {
|
|
2649
|
+
if (tab === 'database' && activeDatabaseId) loadDatabaseTables(activeDatabaseId);
|
|
2650
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2651
|
+
}, [activeDatabaseId]);
|
|
2652
|
+
|
|
2653
|
+
useEffect(() => {
|
|
2654
|
+
if (tab === 'browse' && !browse) loadBrowse(browsePath || undefined);
|
|
2655
|
+
// Load once when the Browse tab is opened; navigation is driven by the path bar.
|
|
2656
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2657
|
+
}, [tab]);
|
|
2658
|
+
|
|
2659
|
+
const addDatasetLayer = async (dataset) => {
|
|
2660
|
+
if (busy) return;
|
|
2661
|
+
setBusy(true);
|
|
2662
|
+
try {
|
|
2663
|
+
await api.createLayer(projectId, { datasetId: dataset.id, name: datasetDisplayName(dataset) });
|
|
2664
|
+
setMessage(t('dataset.layerAdded'));
|
|
2665
|
+
if (onChanged) onChanged();
|
|
2666
|
+
} catch (reason) {
|
|
2667
|
+
fail(reason);
|
|
2668
|
+
} finally {
|
|
2669
|
+
setBusy(false);
|
|
2670
|
+
}
|
|
2671
|
+
};
|
|
2672
|
+
|
|
2673
|
+
const convertToCog = async (dataset) => {
|
|
2674
|
+
if (busy) return;
|
|
2675
|
+
setBusy(true);
|
|
2676
|
+
setMessage('');
|
|
2677
|
+
try {
|
|
2678
|
+
const converted = await api.convertDataset(dataset.id);
|
|
2679
|
+
if (converted && converted.profile && converted.profile.renderable) {
|
|
2680
|
+
const existing = await api.listLayers(projectId);
|
|
2681
|
+
if (!existing.some((layer) => layer.datasetId === converted.id)) {
|
|
2682
|
+
await api.createLayer(projectId, { datasetId: converted.id, name: datasetDisplayName(converted) });
|
|
2683
|
+
setMessage(t('dataset.layerAdded'));
|
|
2684
|
+
} else {
|
|
2685
|
+
setMessage(t('dataset.registered'));
|
|
2686
|
+
}
|
|
2687
|
+
if (onChanged) onChanged();
|
|
2688
|
+
} else {
|
|
2689
|
+
setMessage(t('dataset.conversionFailed'));
|
|
2690
|
+
}
|
|
2691
|
+
await loadDatasets();
|
|
2692
|
+
} catch (reason) {
|
|
2693
|
+
fail(reason);
|
|
2694
|
+
} finally {
|
|
2695
|
+
setBusy(false);
|
|
2696
|
+
}
|
|
2697
|
+
};
|
|
2698
|
+
|
|
2699
|
+
const removeDataset = async (dataset) => {
|
|
2700
|
+
if (busy) return;
|
|
2701
|
+
if (typeof window !== 'undefined' && !window.confirm(t('dataset.deleteConfirm'))) return;
|
|
2702
|
+
setBusy(true);
|
|
2703
|
+
try {
|
|
2704
|
+
await api.deleteDataset(dataset.id);
|
|
2705
|
+
setMessage(t('dataset.deleted'));
|
|
2706
|
+
await loadDatasets();
|
|
2707
|
+
if (onChanged) onChanged();
|
|
2708
|
+
} catch (reason) {
|
|
2709
|
+
fail(reason);
|
|
2710
|
+
} finally {
|
|
2711
|
+
setBusy(false);
|
|
2712
|
+
}
|
|
2713
|
+
};
|
|
2714
|
+
|
|
2715
|
+
const registerSelected = async () => {
|
|
2716
|
+
if (!selectedFile || busy) return;
|
|
2717
|
+
setBusy(true);
|
|
2718
|
+
try {
|
|
2719
|
+
await api.createDataset(projectId, { path: selectedFile.path, addLayer: true });
|
|
2720
|
+
setMessage(t('dataset.registered'));
|
|
2721
|
+
setSelectedFile(null);
|
|
2722
|
+
await loadDatasets();
|
|
2723
|
+
if (onChanged) onChanged();
|
|
2724
|
+
} catch (reason) {
|
|
2725
|
+
fail(reason);
|
|
2726
|
+
} finally {
|
|
2727
|
+
setBusy(false);
|
|
2728
|
+
}
|
|
2729
|
+
};
|
|
2730
|
+
|
|
2731
|
+
useEffect(() => () => {
|
|
2732
|
+
uploadPausedRef.current = true;
|
|
2733
|
+
if (uploadAbortRef.current) uploadAbortRef.current.abort();
|
|
2734
|
+
}, []);
|
|
2735
|
+
|
|
2736
|
+
const chooseUploadFile = (file) => {
|
|
2737
|
+
if (uploading) return;
|
|
2738
|
+
uploadPausedRef.current = false;
|
|
2739
|
+
setUploadFile(file || null);
|
|
2740
|
+
setUploadMeta(null);
|
|
2741
|
+
setUploadProgress(0);
|
|
2742
|
+
setUploadedBytes(0);
|
|
2743
|
+
setUploadStatus('idle');
|
|
2744
|
+
setUploadStatusMessage('');
|
|
2745
|
+
};
|
|
2746
|
+
|
|
2747
|
+
const startUpload = async () => {
|
|
2748
|
+
if (!uploadFile || uploading) return;
|
|
2749
|
+
const file = uploadFile;
|
|
2750
|
+
const fingerprint = uploadFingerprint(file);
|
|
2751
|
+
uploadPausedRef.current = false;
|
|
2752
|
+
setUploading(true);
|
|
2753
|
+
setUploadStatus('uploading');
|
|
2754
|
+
setUploadStatusMessage('');
|
|
2755
|
+
let meta = uploadMeta;
|
|
2756
|
+
try {
|
|
2757
|
+
let candidateId = meta && meta.id ? meta.id : readUploadId(fingerprint);
|
|
2758
|
+
if (candidateId) {
|
|
2759
|
+
try {
|
|
2760
|
+
const fresh = await api.getUpload(candidateId);
|
|
2761
|
+
if (fresh && fresh.status === 'uploading' && fresh.fileName === file.name && Number(fresh.size) === file.size) {
|
|
2762
|
+
meta = fresh;
|
|
2763
|
+
} else {
|
|
2764
|
+
meta = null;
|
|
2765
|
+
}
|
|
2766
|
+
} catch (error) {
|
|
2767
|
+
meta = null;
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
if (!meta) {
|
|
2771
|
+
meta = await api.initUpload(projectId, {
|
|
2772
|
+
fileName: file.name,
|
|
2773
|
+
size: file.size,
|
|
2774
|
+
chunkSize: UPLOAD_CHUNK_SIZE,
|
|
2775
|
+
fingerprint,
|
|
2776
|
+
});
|
|
2777
|
+
writeUploadId(fingerprint, meta.id);
|
|
2778
|
+
}
|
|
2779
|
+
setUploadMeta(meta);
|
|
2780
|
+
const chunkSize = Number(meta.chunkSize) || UPLOAD_CHUNK_SIZE;
|
|
2781
|
+
const received = new Set(Array.isArray(meta.receivedChunks) ? meta.receivedChunks : []);
|
|
2782
|
+
let uploaded = 0;
|
|
2783
|
+
for (let index = 0; index < meta.totalChunks; index += 1) {
|
|
2784
|
+
if (!received.has(index)) continue;
|
|
2785
|
+
const start = index * chunkSize;
|
|
2786
|
+
uploaded += Math.min(chunkSize, file.size - start);
|
|
2787
|
+
}
|
|
2788
|
+
setUploadedBytes(uploaded);
|
|
2789
|
+
setUploadProgress(file.size > 0 ? Math.min(99, Math.round((uploaded / file.size) * 100)) : 0);
|
|
2790
|
+
for (let index = 0; index < meta.totalChunks; index += 1) {
|
|
2791
|
+
if (uploadPausedRef.current) break;
|
|
2792
|
+
if (received.has(index)) continue;
|
|
2793
|
+
const start = index * chunkSize;
|
|
2794
|
+
const end = Math.min(file.size, start + chunkSize);
|
|
2795
|
+
const blob = file.slice(start, end);
|
|
2796
|
+
const controller = new AbortController();
|
|
2797
|
+
uploadAbortRef.current = controller;
|
|
2798
|
+
const onProgress = (loaded) => {
|
|
2799
|
+
if (file.size <= 0) {
|
|
2800
|
+
setUploadProgress(99);
|
|
2801
|
+
return;
|
|
2802
|
+
}
|
|
2803
|
+
setUploadProgress(Math.min(99, Math.round(((uploaded + loaded) / file.size) * 100)));
|
|
2804
|
+
};
|
|
2805
|
+
await api.uploadChunk(meta.id, index, blob, onProgress, controller.signal);
|
|
2806
|
+
received.add(index);
|
|
2807
|
+
uploaded += blob.size;
|
|
2808
|
+
setUploadedBytes(uploaded);
|
|
2809
|
+
setUploadProgress(file.size > 0 ? Math.min(99, Math.round((uploaded / file.size) * 100)) : 99);
|
|
2810
|
+
meta = Object.assign({}, meta, { receivedChunks: [...received].sort((left, right) => left - right) });
|
|
2811
|
+
setUploadMeta(meta);
|
|
2812
|
+
}
|
|
2813
|
+
uploadAbortRef.current = null;
|
|
2814
|
+
if (uploadPausedRef.current) {
|
|
2815
|
+
setUploadStatus('paused');
|
|
2816
|
+
setUploading(false);
|
|
2817
|
+
return;
|
|
2818
|
+
}
|
|
2819
|
+
setUploadStatus('completing');
|
|
2820
|
+
const completed = await api.completeUpload(meta.id, { addLayer: true });
|
|
2821
|
+
const importedCount = Array.isArray(completed && completed.datasets) ? completed.datasets.length : 1;
|
|
2822
|
+
writeUploadId(fingerprint, '');
|
|
2823
|
+
setUploadMeta(null);
|
|
2824
|
+
setUploadFile(null);
|
|
2825
|
+
setUploadStatus('done');
|
|
2826
|
+
setUploadProgress(100);
|
|
2827
|
+
setUploadedBytes(file.size);
|
|
2828
|
+
setMessage(importedCount > 1 ? t('dataset.uploadSuccessCount', { count: importedCount }) : t('dataset.uploadSuccess'));
|
|
2829
|
+
await loadDatasets();
|
|
2830
|
+
if (onChanged) onChanged();
|
|
2831
|
+
} catch (reason) {
|
|
2832
|
+
if (uploadPausedRef.current || (reason && reason.name === 'AbortError')) {
|
|
2833
|
+
setUploadStatus('paused');
|
|
2834
|
+
setUploading(false);
|
|
2835
|
+
return;
|
|
2836
|
+
}
|
|
2837
|
+
setUploadStatus('error');
|
|
2838
|
+
setUploadStatusMessage(reason instanceof Error ? reason.message : t('dataset.uploadFailed'));
|
|
2839
|
+
fail(reason);
|
|
2840
|
+
} finally {
|
|
2841
|
+
uploadAbortRef.current = null;
|
|
2842
|
+
setUploading(false);
|
|
2843
|
+
}
|
|
2844
|
+
};
|
|
2845
|
+
|
|
2846
|
+
const pauseUpload = () => {
|
|
2847
|
+
uploadPausedRef.current = true;
|
|
2848
|
+
setUploadStatus('pausing');
|
|
2849
|
+
if (uploadAbortRef.current) uploadAbortRef.current.abort();
|
|
2850
|
+
};
|
|
2851
|
+
|
|
2852
|
+
const addTile = async () => {
|
|
2853
|
+
if (!tileUrl.trim() || busy) return;
|
|
2854
|
+
setBusy(true);
|
|
2855
|
+
try {
|
|
2856
|
+
await api.createTileDataset(projectId, {
|
|
2857
|
+
name: tileName.trim() || DEFAULT_LAYER_NAME,
|
|
2858
|
+
tileUrl: tileUrl.trim(),
|
|
2859
|
+
addLayer: true,
|
|
2860
|
+
});
|
|
2861
|
+
setMessage(t('dataset.registered'));
|
|
2862
|
+
await loadDatasets();
|
|
2863
|
+
if (onChanged) onChanged();
|
|
2864
|
+
} catch (reason) {
|
|
2865
|
+
fail(reason);
|
|
2866
|
+
} finally {
|
|
2867
|
+
setBusy(false);
|
|
2868
|
+
}
|
|
2869
|
+
};
|
|
2870
|
+
|
|
2871
|
+
const registerDatabase = async () => {
|
|
2872
|
+
if (databaseBusy) return;
|
|
2873
|
+
if (!databaseForm.host.trim() || !databaseForm.dbname.trim() || !databaseForm.username.trim()) {
|
|
2874
|
+
setDatabaseMessage(t('dataset.dbFailed'));
|
|
2875
|
+
return;
|
|
2876
|
+
}
|
|
2877
|
+
setDatabaseBusy(true);
|
|
2878
|
+
setDatabaseMessage(t('dataset.dbConnecting'));
|
|
2879
|
+
try {
|
|
2880
|
+
const database = await api.createDatabase(projectId, Object.assign({}, databaseForm, {
|
|
2881
|
+
name: databaseForm.name.trim() || databaseForm.dbname.trim(),
|
|
2882
|
+
}));
|
|
2883
|
+
setDatabases((current) => [...current, database]);
|
|
2884
|
+
setActiveDatabaseId(database.id);
|
|
2885
|
+
setDatabaseForm((current) => Object.assign({}, current, { name: database.name }));
|
|
2886
|
+
await loadDatabaseTables(database.id);
|
|
2887
|
+
setDatabaseMessage(t('dataset.dbRegistered'));
|
|
2888
|
+
} catch (reason) {
|
|
2889
|
+
setDatabaseMessage(reason instanceof Error ? reason.message : t('dataset.dbFailed'));
|
|
2890
|
+
} finally {
|
|
2891
|
+
setDatabaseBusy(false);
|
|
2892
|
+
}
|
|
2893
|
+
};
|
|
2894
|
+
|
|
2895
|
+
const deleteDatabaseRegistration = async (database) => {
|
|
2896
|
+
if (databaseBusy || !database) return;
|
|
2897
|
+
if (typeof window !== 'undefined' && typeof window.confirm === 'function' && !window.confirm(t('dataset.dbDelete') + '?')) return;
|
|
2898
|
+
setDatabaseBusy(true);
|
|
2899
|
+
try {
|
|
2900
|
+
await api.deleteDatabase(database.id);
|
|
2901
|
+
const remaining = databases.filter((item) => item.id !== database.id);
|
|
2902
|
+
setDatabases(remaining);
|
|
2903
|
+
setActiveDatabaseId(remaining[0] ? remaining[0].id : '');
|
|
2904
|
+
setDatabaseTables([]);
|
|
2905
|
+
} catch (reason) {
|
|
2906
|
+
setDatabaseMessage(reason instanceof Error ? reason.message : t('dataset.dbFailed'));
|
|
2907
|
+
} finally {
|
|
2908
|
+
setDatabaseBusy(false);
|
|
2909
|
+
}
|
|
2910
|
+
};
|
|
2911
|
+
|
|
2912
|
+
const importDatabaseTable = async (table) => {
|
|
2913
|
+
if (databaseBusy || !activeDatabaseId) return;
|
|
2914
|
+
setDatabaseBusy(true);
|
|
2915
|
+
setDatabaseMessage('');
|
|
2916
|
+
try {
|
|
2917
|
+
await api.importDatabaseTable(activeDatabaseId, {
|
|
2918
|
+
schema: table.schema,
|
|
2919
|
+
table: table.table,
|
|
2920
|
+
addLayer: true,
|
|
2921
|
+
});
|
|
2922
|
+
setDatabaseMessage(t('dataset.dbImported'));
|
|
2923
|
+
await loadDatasets();
|
|
2924
|
+
if (onChanged) onChanged();
|
|
2925
|
+
} catch (reason) {
|
|
2926
|
+
fail(reason);
|
|
2927
|
+
} finally {
|
|
2928
|
+
setDatabaseBusy(false);
|
|
2929
|
+
}
|
|
2930
|
+
};
|
|
2931
|
+
|
|
2932
|
+
const tabs = [
|
|
2933
|
+
['list', t('dataset.tabList')],
|
|
2934
|
+
['browse', t('dataset.tabBrowse')],
|
|
2935
|
+
['upload', t('dataset.tabUpload')],
|
|
2936
|
+
['database', t('dataset.tabDatabase')],
|
|
2937
|
+
['url', t('dataset.tabUrl')],
|
|
2938
|
+
];
|
|
2939
|
+
|
|
2940
|
+
let body;
|
|
2941
|
+
if (tab === 'list') {
|
|
2942
|
+
body = h('div', { className: 'gis-dataset-list' },
|
|
2943
|
+
datasets.length === 0 ? h('div', { className: 'gis-layer-empty' }, t('dataset.empty')) : null,
|
|
2944
|
+
datasets.map((dataset) => h('div', { key: dataset.id, className: 'gis-dataset-card' },
|
|
2945
|
+
h('div', { className: 'gis-dataset-icon' }, h(SvgIcon, { name: dataset.kind === 'raster' ? 'image' : dataset.kind === 'vector' ? 'map' : 'database', size: 16 })),
|
|
2946
|
+
h('div', { className: 'gis-dataset-copy' },
|
|
2947
|
+
h('div', { className: 'gis-dataset-name', title: datasetDisplayName(dataset) }, datasetDisplayName(dataset)),
|
|
2948
|
+
h('div', { className: 'gis-dataset-meta' }, datasetMeta(dataset, t)),
|
|
2949
|
+
h('div', { className: 'gis-dataset-uri', title: dataset.uri }, dataset.uri),
|
|
2950
|
+
),
|
|
2951
|
+
h('div', { className: 'gis-dataset-actions' },
|
|
2952
|
+
dataset.kind === 'raster' && dataset.profile && (dataset.profile.format === 'geotiff' || dataset.profile.conversionError || (dataset.profile.format === 'cog' && dataset.profile.renderable === false))
|
|
2953
|
+
? h('button', { type: 'button', className: 'gis-tool-btn is-primary', disabled: busy, onClick: () => convertToCog(dataset) }, t('dataset.convertCog'))
|
|
2954
|
+
: h('button', { type: 'button', className: 'gis-tool-btn is-primary', disabled: busy || (dataset.profile && dataset.profile.renderable === false), onClick: () => addDatasetLayer(dataset) }, t('dataset.addLayer')),
|
|
2955
|
+
h('button', { type: 'button', className: 'gis-link-button', disabled: busy, onClick: () => removeDataset(dataset) }, t('dataset.delete')),
|
|
2956
|
+
),
|
|
2957
|
+
)),
|
|
2958
|
+
);
|
|
2959
|
+
} else if (tab === 'browse') {
|
|
2960
|
+
body = h('div', { className: 'gis-browse' },
|
|
2961
|
+
h('div', { className: 'gis-browse-bar' },
|
|
2962
|
+
h('button', { type: 'button', className: 'gis-icon-btn gis-icon-btn-sm', title: t('dataset.up'), disabled: !browse || !browse.parent || busy, onClick: () => browse && browse.parent && loadBrowse(browse.parent) }, h(SvgIcon, { name: 'chevron-left', size: 14 })),
|
|
2963
|
+
h('input', { className: 'gis-input gis-input-mono', value: browsePath, onChange: (event) => setBrowsePath(event.target.value), onKeyDown: (event) => { if (event.key === 'Enter') loadBrowse(browsePath); } }),
|
|
2964
|
+
h('button', { type: 'button', className: 'gis-btn', disabled: busy, onClick: () => loadBrowse(browsePath) }, t('dataset.go')),
|
|
2965
|
+
),
|
|
2966
|
+
browse && browse.roots && browse.roots.length > 1 ? h('div', { className: 'gis-browse-roots' },
|
|
2967
|
+
browse.roots.map((root) => h('button', {
|
|
2968
|
+
key: root,
|
|
2969
|
+
type: 'button',
|
|
2970
|
+
className: 'gis-root-chip' + (browse.path === root ? ' is-active' : ''),
|
|
2971
|
+
onClick: () => loadBrowse(root),
|
|
2972
|
+
title: root,
|
|
2973
|
+
}, root)),
|
|
2974
|
+
) : null,
|
|
2975
|
+
h('div', { className: 'gis-browse-list' },
|
|
2976
|
+
!browse && busy ? h('div', { className: 'gis-empty-note' }, t('dataset.loading')) : null,
|
|
2977
|
+
browse ? browse.entries.map((entry) => h('button', {
|
|
2978
|
+
key: entry.path,
|
|
2979
|
+
type: 'button',
|
|
2980
|
+
className: 'gis-browse-row' + (selectedFile && selectedFile.path === entry.path ? ' is-selected' : '') + (entry.type === 'file' && !entry.supported ? ' is-disabled' : ''),
|
|
2981
|
+
disabled: busy || (entry.type === 'file' && !entry.supported),
|
|
2982
|
+
onClick: () => {
|
|
2983
|
+
if (entry.type === 'directory') loadBrowse(entry.path);
|
|
2984
|
+
else setSelectedFile(entry);
|
|
2985
|
+
},
|
|
2986
|
+
},
|
|
2987
|
+
h('span', { className: 'gis-browse-icon' }, h(SvgIcon, { name: entry.type === 'directory' ? 'folder' : 'file', size: 14 })),
|
|
2988
|
+
h('span', { className: 'gis-browse-name' }, entry.name),
|
|
2989
|
+
h('span', { className: 'gis-browse-meta' }, entry.type === 'directory' ? '' : (entry.kindHint || 'file') + ' · ' + formatBytes(entry.size)),
|
|
2990
|
+
)) : null,
|
|
2991
|
+
),
|
|
2992
|
+
h('div', { className: 'gis-browse-foot' },
|
|
2993
|
+
h('span', { className: 'gis-selected-path', title: selectedFile ? selectedFile.path : '' }, selectedFile ? t('dataset.selected') + ': ' + selectedFile.name : t('dataset.selectFile')),
|
|
2994
|
+
h('button', { type: 'button', className: 'gis-btn gis-btn-primary', disabled: !selectedFile || busy, onClick: registerSelected }, busy ? t('dataset.loading') : t('dataset.register')),
|
|
2995
|
+
),
|
|
2996
|
+
);
|
|
2997
|
+
} else if (tab === 'upload') {
|
|
2998
|
+
const statusText = uploadStatus === 'uploading'
|
|
2999
|
+
? t('dataset.uploading')
|
|
3000
|
+
: uploadStatus === 'pausing'
|
|
3001
|
+
? t('dataset.uploading')
|
|
3002
|
+
: uploadStatus === 'paused'
|
|
3003
|
+
? t('dataset.paused')
|
|
3004
|
+
: uploadStatus === 'completing'
|
|
3005
|
+
? t('dataset.completing')
|
|
3006
|
+
: uploadStatus === 'done'
|
|
3007
|
+
? t('dataset.uploadSuccess')
|
|
3008
|
+
: uploadStatus === 'error'
|
|
3009
|
+
? (uploadStatusMessage || t('dataset.uploadFailed'))
|
|
3010
|
+
: t('dataset.uploadResumable');
|
|
3011
|
+
body = h('div', { className: 'gis-upload' },
|
|
3012
|
+
h('div', { className: 'gis-layer-empty' },
|
|
3013
|
+
h('div', { className: 'gis-layer-empty-title' }, t('dataset.upload')),
|
|
3014
|
+
h('div', { className: 'gis-layer-empty-body' }, t('dataset.uploadHint')),
|
|
3015
|
+
h('div', { className: 'gis-upload-hint' }, t('dataset.uploadResumable')),
|
|
3016
|
+
),
|
|
3017
|
+
h('input', {
|
|
3018
|
+
type: 'file',
|
|
3019
|
+
className: 'gis-file-input',
|
|
3020
|
+
disabled: uploading,
|
|
3021
|
+
onChange: (event) => chooseUploadFile(event.target.files && event.target.files[0] ? event.target.files[0] : null),
|
|
3022
|
+
}),
|
|
3023
|
+
uploadFile || uploadProgress > 0 ? h('div', { className: 'gis-upload-progress' },
|
|
3024
|
+
h('div', { className: 'gis-progress-track' },
|
|
3025
|
+
h('div', { className: 'gis-progress-fill', style: { width: uploadProgress + '%' } }),
|
|
3026
|
+
),
|
|
3027
|
+
h('div', { className: 'gis-progress-meta' },
|
|
3028
|
+
t('dataset.uploadProgress', {
|
|
3029
|
+
percent: uploadProgress,
|
|
3030
|
+
uploaded: formatBytes(uploadedBytes),
|
|
3031
|
+
total: formatBytes(uploadFile ? uploadFile.size : uploadedBytes),
|
|
3032
|
+
}),
|
|
3033
|
+
),
|
|
3034
|
+
) : null,
|
|
3035
|
+
h('div', { className: 'gis-upload-status' + (uploadStatus === 'error' ? ' is-error' : '') }, statusText),
|
|
3036
|
+
h('div', { className: 'gis-browse-foot' },
|
|
3037
|
+
h('span', { className: 'gis-selected-path' }, uploadFile ? uploadFile.name : t('dataset.selectFile')),
|
|
3038
|
+
uploading || uploadStatus === 'pausing'
|
|
3039
|
+
? h('button', { type: 'button', className: 'gis-btn', disabled: uploadStatus === 'pausing', onClick: pauseUpload }, t('dataset.pause'))
|
|
3040
|
+
: uploadStatus === 'done'
|
|
3041
|
+
? null
|
|
3042
|
+
: h('button', {
|
|
3043
|
+
type: 'button',
|
|
3044
|
+
className: 'gis-btn gis-btn-primary',
|
|
3045
|
+
disabled: !uploadFile || uploadStatus === 'completing',
|
|
3046
|
+
onClick: startUpload,
|
|
3047
|
+
}, uploadStatus === 'paused' ? t('dataset.resume') : t('dataset.upload')),
|
|
3048
|
+
),
|
|
3049
|
+
);
|
|
3050
|
+
} else if (tab === 'database') {
|
|
3051
|
+
const field = (key, label, options = {}) => h('div', { className: 'gis-db-field' },
|
|
3052
|
+
h('label', { className: 'gis-field-label' }, label),
|
|
3053
|
+
h('input', Object.assign({}, options, {
|
|
3054
|
+
className: 'gis-input' + (options.mono ? ' gis-input-mono' : ''),
|
|
3055
|
+
value: databaseForm[key],
|
|
3056
|
+
disabled: databaseBusy,
|
|
3057
|
+
onChange: (event) => setDatabaseForm((current) => Object.assign({}, current, { [key]: event.target.value })),
|
|
3058
|
+
})),
|
|
3059
|
+
);
|
|
3060
|
+
body = h('div', { className: 'gis-database' },
|
|
3061
|
+
h('div', { className: 'gis-db-heading' },
|
|
3062
|
+
h('div', { className: 'gis-db-title' }, t('dataset.dbTitle')),
|
|
3063
|
+
h('div', { className: 'gis-tool-hint' }, t('dataset.dbHint')),
|
|
3064
|
+
),
|
|
3065
|
+
databases.length > 0 ? h('div', { className: 'gis-db-list' },
|
|
3066
|
+
h('div', { className: 'gis-field-label' }, t('dataset.dbSelect')),
|
|
3067
|
+
databases.map((database) => h('div', { key: database.id, className: 'gis-db-item' + (database.id === activeDatabaseId ? ' is-active' : '') },
|
|
3068
|
+
h('button', {
|
|
3069
|
+
type: 'button',
|
|
3070
|
+
className: 'gis-db-select',
|
|
3071
|
+
onClick: () => setActiveDatabaseId(database.id),
|
|
3072
|
+
title: database.host + ':' + database.port + '/' + database.dbname,
|
|
3073
|
+
}, h(SvgIcon, { name: 'database', size: 13 }), database.name),
|
|
3074
|
+
h('button', {
|
|
3075
|
+
type: 'button',
|
|
3076
|
+
className: 'gis-link-button',
|
|
3077
|
+
disabled: databaseBusy,
|
|
3078
|
+
onClick: () => deleteDatabaseRegistration(database),
|
|
3079
|
+
}, t('dataset.dbDelete')),
|
|
3080
|
+
)),
|
|
3081
|
+
) : null,
|
|
3082
|
+
h('div', { className: 'gis-db-form' },
|
|
3083
|
+
field('name', t('dataset.dbName'), { placeholder: 'gis' }),
|
|
3084
|
+
field('host', t('dataset.dbHost'), { placeholder: '127.0.0.1' }),
|
|
3085
|
+
field('port', t('dataset.dbPort'), { type: 'number', min: 1, max: 65535 }),
|
|
3086
|
+
field('dbname', t('dataset.dbDatabase'), { placeholder: 'gis' }),
|
|
3087
|
+
field('username', t('dataset.dbUser'), { placeholder: 'postgres' }),
|
|
3088
|
+
field('password', t('dataset.dbPassword'), { type: 'password', autoComplete: 'off' }),
|
|
3089
|
+
field('sslmode', t('dataset.dbSsl'), { placeholder: 'prefer' }),
|
|
3090
|
+
),
|
|
3091
|
+
h('div', { className: 'gis-browse-foot' },
|
|
3092
|
+
h('span', { className: 'gis-db-status' }, databaseBusy ? t('dataset.dbConnecting') : databaseMessage),
|
|
3093
|
+
h('button', { type: 'button', className: 'gis-btn gis-btn-primary', disabled: databaseBusy, onClick: registerDatabase }, t('dataset.dbRegister')),
|
|
3094
|
+
),
|
|
3095
|
+
activeDatabaseId && databaseTables.length > 0 ? h('div', { className: 'gis-db-tables' },
|
|
3096
|
+
h('div', { className: 'gis-field-label' }, t('dataset.dbTables')),
|
|
3097
|
+
databaseTables.map((table) => h('div', { key: table.schema + '.' + table.table, className: 'gis-db-table' },
|
|
3098
|
+
h('div', { className: 'gis-db-table-copy' },
|
|
3099
|
+
h('div', { className: 'gis-db-table-name', title: table.schema + '.' + table.table }, table.schema + '.' + table.table),
|
|
3100
|
+
h('div', { className: 'gis-db-table-meta' }, (table.geometryType || 'geometry') + ' · EPSG:' + (table.srid || '?') + ' · ' + t('dataset.dbColumns', { count: table.columns ? table.columns.length : 0 })),
|
|
3101
|
+
),
|
|
3102
|
+
h('button', {
|
|
3103
|
+
type: 'button',
|
|
3104
|
+
className: 'gis-tool-btn is-primary',
|
|
3105
|
+
disabled: databaseBusy,
|
|
3106
|
+
onClick: () => importDatabaseTable(table),
|
|
3107
|
+
}, t('dataset.dbAddTable')),
|
|
3108
|
+
)),
|
|
3109
|
+
) : null,
|
|
3110
|
+
);
|
|
3111
|
+
} else {
|
|
3112
|
+
body = h('div', { className: 'gis-upload' },
|
|
3113
|
+
h('label', { className: 'gis-field-label' }, t('dataset.urlName')),
|
|
3114
|
+
h('input', { className: 'gis-input', value: tileName, onChange: (event) => setTileName(event.target.value) }),
|
|
3115
|
+
h('label', { className: 'gis-field-label' }, t('dataset.tabUrl')),
|
|
3116
|
+
h('input', { className: 'gis-input gis-input-mono', value: tileUrl, onChange: (event) => setTileUrl(event.target.value), placeholder: t('dataset.urlPlaceholder') }),
|
|
3117
|
+
h('div', { className: 'gis-browse-foot' },
|
|
3118
|
+
h('span', { className: 'gis-selected-path' }, ''),
|
|
3119
|
+
h('button', { type: 'button', className: 'gis-btn gis-btn-primary', disabled: !tileUrl.trim() || busy, onClick: addTile }, busy ? t('dataset.loading') : t('dataset.addUrl')),
|
|
3120
|
+
),
|
|
3121
|
+
);
|
|
3122
|
+
}
|
|
3123
|
+
|
|
3124
|
+
return h('div', { className: 'gis-modal', onMouseDown: (event) => { if (event.target === event.currentTarget) onClose(); } },
|
|
3125
|
+
h('div', { className: 'gis-modal-card gis-data-modal' },
|
|
3126
|
+
h('div', { className: 'gis-modal-head' },
|
|
3127
|
+
h('div', { className: 'gis-modal-title' }, t('dataset.title')),
|
|
3128
|
+
h('button', { type: 'button', className: 'gis-icon-btn gis-icon-btn-sm', onClick: onClose, 'aria-label': t('project.cancel') }, h(SvgIcon, { name: 'close', size: 14 })),
|
|
3129
|
+
),
|
|
3130
|
+
h('div', { className: 'gis-tabs' },
|
|
3131
|
+
tabs.map(([id, label]) => h('button', {
|
|
3132
|
+
key: id,
|
|
3133
|
+
type: 'button',
|
|
3134
|
+
className: 'gis-tab' + (tab === id ? ' is-active' : ''),
|
|
3135
|
+
onClick: () => setTab(id),
|
|
3136
|
+
}, label)),
|
|
3137
|
+
),
|
|
3138
|
+
message ? h('div', { className: 'gis-modal-message' }, message) : null,
|
|
3139
|
+
h('div', { className: 'gis-modal-body' }, body),
|
|
3140
|
+
),
|
|
3141
|
+
);
|
|
3142
|
+
}
|
|
3143
|
+
|
|
3144
|
+
const TOOL_DEFS = [
|
|
3145
|
+
{ id: 'layers', key: 'tools.layers', icon: 'layers' },
|
|
3146
|
+
{ id: 'measure', key: 'tools.measure', icon: 'ruler' },
|
|
3147
|
+
{ id: 'annotate', key: 'tools.annotate', icon: 'pin' },
|
|
3148
|
+
{ id: 'analyze', key: 'tools.analyze', icon: 'chart' },
|
|
3149
|
+
{ id: 'data', key: 'tools.data', icon: 'database' },
|
|
3150
|
+
];
|
|
3151
|
+
|
|
3152
|
+
function Workbench({ runtime, t }) {
|
|
3153
|
+
const scheme = useScheme(runtime);
|
|
3154
|
+
const layers = useLayerStore();
|
|
3155
|
+
const [activeTool, setActiveToolState] = useState('layers');
|
|
3156
|
+
const [toast, setToast] = useState(null);
|
|
3157
|
+
const [projects, setProjects] = useState([]);
|
|
3158
|
+
const [projectLoading, setProjectLoading] = useState(true);
|
|
3159
|
+
const [projectId, setProjectId] = useState(() => {
|
|
3160
|
+
try {
|
|
3161
|
+
return window.localStorage.getItem('gis-workbench.project') || '';
|
|
3162
|
+
} catch (error) {
|
|
3163
|
+
return '';
|
|
3164
|
+
}
|
|
3165
|
+
});
|
|
3166
|
+
const [layerTick, setLayerTick] = useState(0);
|
|
3167
|
+
const [dataOpen, setDataOpen] = useState(false);
|
|
3168
|
+
const [newProjectOpen, setNewProjectOpen] = useState(false);
|
|
3169
|
+
const [newVectorOpen, setNewVectorOpen] = useState(false);
|
|
3170
|
+
const [drawTarget, setDrawTarget] = useState(null);
|
|
3171
|
+
const [dirty, setDirty] = useState(false);
|
|
3172
|
+
const [saving, setSaving] = useState(false);
|
|
3173
|
+
const [layerMenu, setLayerMenu] = useState(null);
|
|
3174
|
+
const [featureMenu, setFeatureMenu] = useState(null);
|
|
3175
|
+
const loadedProjectRef = useRef('');
|
|
3176
|
+
const [editTarget, setEditTarget] = useState(null);
|
|
3177
|
+
const timerRef = useRef(null);
|
|
3178
|
+
|
|
3179
|
+
useEffect(() => () => {
|
|
3180
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
3181
|
+
}, []);
|
|
3182
|
+
|
|
3183
|
+
const showToast = useCallback((message) => {
|
|
3184
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
3185
|
+
setToast(message);
|
|
3186
|
+
timerRef.current = setTimeout(() => {
|
|
3187
|
+
timerRef.current = null;
|
|
3188
|
+
setToast(null);
|
|
3189
|
+
}, 2600);
|
|
3190
|
+
}, []);
|
|
3191
|
+
|
|
3192
|
+
const chooseTool = useCallback((toolId) => {
|
|
3193
|
+
if (toolId === 'layers') {
|
|
3194
|
+
setActiveToolState((current) => (current === 'layers' ? 'none' : 'layers'));
|
|
3195
|
+
return;
|
|
3196
|
+
}
|
|
3197
|
+
setActiveToolState(toolId);
|
|
3198
|
+
if (toolId === 'analyze') showToast(t('tools.pending'));
|
|
3199
|
+
}, [showToast, t]);
|
|
3200
|
+
|
|
3201
|
+
const refreshLayers = useCallback(() => {
|
|
3202
|
+
setLayerTick((value) => value + 1);
|
|
3203
|
+
}, []);
|
|
3204
|
+
|
|
3205
|
+
useEffect(() => {
|
|
3206
|
+
let alive = true;
|
|
3207
|
+
api.getBootstrap().then((data) => {
|
|
3208
|
+
if (!alive) return;
|
|
3209
|
+
const list = Array.isArray(data.projects) ? data.projects : [];
|
|
3210
|
+
setProjects(list);
|
|
3211
|
+
let stored = '';
|
|
3212
|
+
try {
|
|
3213
|
+
stored = window.localStorage.getItem('gis-workbench.project') || '';
|
|
3214
|
+
} catch (error) {
|
|
3215
|
+
stored = '';
|
|
3216
|
+
}
|
|
3217
|
+
const next = list.some((project) => project.id === stored)
|
|
3218
|
+
? stored
|
|
3219
|
+
: (data.defaultProjectId || (list[0] && list[0].id) || '');
|
|
3220
|
+
setProjectId(next);
|
|
3221
|
+
setProjectLoading(false);
|
|
3222
|
+
}).catch((error) => {
|
|
3223
|
+
if (!alive) return;
|
|
3224
|
+
setProjectLoading(false);
|
|
3225
|
+
console.warn('[gis-workbench] could not load projects', error);
|
|
3226
|
+
showToast(error instanceof Error ? error.message : t('project.failed'));
|
|
3227
|
+
});
|
|
3228
|
+
return () => {
|
|
3229
|
+
alive = false;
|
|
3230
|
+
};
|
|
3231
|
+
}, [showToast, t]);
|
|
3232
|
+
|
|
3233
|
+
useEffect(() => {
|
|
3234
|
+
if (!dirty || typeof window === 'undefined') return undefined;
|
|
3235
|
+
const warn = (event) => {
|
|
3236
|
+
event.preventDefault();
|
|
3237
|
+
event.returnValue = '';
|
|
3238
|
+
};
|
|
3239
|
+
window.addEventListener('beforeunload', warn);
|
|
3240
|
+
return () => {
|
|
3241
|
+
window.removeEventListener('beforeunload', warn);
|
|
3242
|
+
};
|
|
3243
|
+
}, [dirty]);
|
|
3244
|
+
|
|
3245
|
+
useEffect(() => {
|
|
3246
|
+
try {
|
|
3247
|
+
if (projectId) window.localStorage.setItem('gis-workbench.project', projectId);
|
|
3248
|
+
} catch (error) {
|
|
3249
|
+
// Persisting the selected project is best effort.
|
|
3250
|
+
}
|
|
3251
|
+
}, [projectId]);
|
|
3252
|
+
|
|
3253
|
+
useEffect(() => {
|
|
3254
|
+
let alive = true;
|
|
3255
|
+
if (!projectId) {
|
|
3256
|
+
replaceProjectLayers(null, []);
|
|
3257
|
+
loadedProjectRef.current = '';
|
|
3258
|
+
return undefined;
|
|
3259
|
+
}
|
|
3260
|
+
if (loadedProjectRef.current !== projectId) {
|
|
3261
|
+
loadedProjectRef.current = projectId;
|
|
3262
|
+
replaceProjectLayers(projectId, []);
|
|
3263
|
+
}
|
|
3264
|
+
api.listLayers(projectId).then((rows) => {
|
|
3265
|
+
if (!alive) return;
|
|
3266
|
+
replaceProjectLayers(projectId, (rows || []).map((layer) => projectLayerToStore(layer, projectId)));
|
|
3267
|
+
}).catch((error) => {
|
|
3268
|
+
if (!alive) return;
|
|
3269
|
+
console.warn('[gis-workbench] could not load project layers', error);
|
|
3270
|
+
showToast(error instanceof Error ? error.message : t('project.failed'));
|
|
3271
|
+
});
|
|
3272
|
+
return () => {
|
|
3273
|
+
alive = false;
|
|
3274
|
+
};
|
|
3275
|
+
}, [projectId, layerTick, showToast, t]);
|
|
3276
|
+
|
|
3277
|
+
const handleToggleLayer = useCallback((layer) => {
|
|
3278
|
+
toggleLayerVisibility(layer.key);
|
|
3279
|
+
if (layer.source === 'project' || layer.persisted) setDirty(true);
|
|
3280
|
+
}, []);
|
|
3281
|
+
|
|
3282
|
+
const handleRemoveLayer = useCallback((layer) => {
|
|
3283
|
+
removeLayer(layer.key);
|
|
3284
|
+
if (layer.source === 'project' || layer.persisted) setDirty(true);
|
|
3285
|
+
}, []);
|
|
3286
|
+
|
|
3287
|
+
const saveProject = useCallback(async (overrideLayers) => {
|
|
3288
|
+
if (!projectId || saving) return false;
|
|
3289
|
+
setSaving(true);
|
|
3290
|
+
try {
|
|
3291
|
+
const sourceLayers = Array.isArray(overrideLayers) ? overrideLayers : layers;
|
|
3292
|
+
const projectLayers = sourceLayers
|
|
3293
|
+
.filter((layer) => layer.source === 'project')
|
|
3294
|
+
.map((layer) => ({
|
|
3295
|
+
id: layer.layerId,
|
|
3296
|
+
datasetId: layer.datasetId,
|
|
3297
|
+
name: layer.name,
|
|
3298
|
+
visible: layer.visible,
|
|
3299
|
+
z: layer.z,
|
|
3300
|
+
style: layer.style || {},
|
|
3301
|
+
}));
|
|
3302
|
+
await api.saveProjectState(projectId, { layers: projectLayers });
|
|
3303
|
+
setDirty(false);
|
|
3304
|
+
refreshLayers();
|
|
3305
|
+
showToast(t('project.saved'));
|
|
3306
|
+
return true;
|
|
3307
|
+
} catch (error) {
|
|
3308
|
+
showToast((error instanceof Error ? error.message : t('project.saveFailed')));
|
|
3309
|
+
return false;
|
|
3310
|
+
} finally {
|
|
3311
|
+
setSaving(false);
|
|
3312
|
+
}
|
|
3313
|
+
}, [projectId, saving, layers, refreshLayers, showToast, t]);
|
|
3314
|
+
|
|
3315
|
+
const handleReorderLayers = useCallback((sourceKey, targetKey, after) => {
|
|
3316
|
+
const source = layers.find((layer) => layer.key === sourceKey);
|
|
3317
|
+
const target = layers.find((layer) => layer.key === targetKey);
|
|
3318
|
+
if (!reorderLayers(sourceKey, targetKey, after)) return;
|
|
3319
|
+
if ((source && (source.source === 'project' || source.persisted)) || (target && (target.source === 'project' || target.persisted))) {
|
|
3320
|
+
setDirty(true);
|
|
3321
|
+
void saveProject(getLayers());
|
|
3322
|
+
}
|
|
3323
|
+
}, [layers, saveProject]);
|
|
3324
|
+
|
|
3325
|
+
const openNewVectorLayer = useCallback(async () => {
|
|
3326
|
+
if (!projectId) {
|
|
3327
|
+
showToast(t('dataset.noProject'));
|
|
3328
|
+
return;
|
|
3329
|
+
}
|
|
3330
|
+
if (dirty && !(await saveProject())) return;
|
|
3331
|
+
setNewVectorOpen(true);
|
|
3332
|
+
}, [projectId, dirty, saveProject, showToast, t]);
|
|
3333
|
+
|
|
3334
|
+
const createVectorLayer = useCallback(async (name) => {
|
|
3335
|
+
if (!projectId) throw new Error(t('dataset.noProject'));
|
|
3336
|
+
const result = await api.createVectorLayer(projectId, { name });
|
|
3337
|
+
setEditTarget(null);
|
|
3338
|
+
setDrawTarget({
|
|
3339
|
+
datasetId: result.dataset.id,
|
|
3340
|
+
layerId: result.layer && result.layer.id,
|
|
3341
|
+
name: result.dataset.profile && result.dataset.profile.name ? result.dataset.profile.name : name,
|
|
3342
|
+
});
|
|
3343
|
+
setActiveToolState('layers');
|
|
3344
|
+
refreshLayers();
|
|
3345
|
+
showToast(t('layers.newVector'));
|
|
3346
|
+
}, [projectId, refreshLayers, showToast, t]);
|
|
3347
|
+
|
|
3348
|
+
const savePolygon = useCallback(async (target, geometry) => {
|
|
3349
|
+
if (!target) throw new Error(t('layers.drawFailed'));
|
|
3350
|
+
const dataset = await api.addVectorFeature(target.datasetId, { geometry });
|
|
3351
|
+
setDrawTarget(null);
|
|
3352
|
+
refreshLayers();
|
|
3353
|
+
showToast(t('layers.drawSaved'));
|
|
3354
|
+
return dataset;
|
|
3355
|
+
}, [refreshLayers, showToast, t]);
|
|
3356
|
+
|
|
3357
|
+
const cancelDraw = useCallback(() => {
|
|
3358
|
+
setDrawTarget(null);
|
|
3359
|
+
}, []);
|
|
3360
|
+
|
|
3361
|
+
const editVectorLayer = useCallback(async (layer) => {
|
|
3362
|
+
if (!layer || !layer.datasetId) return;
|
|
3363
|
+
if (dirty && !(await saveProject())) return;
|
|
3364
|
+
setDrawTarget(null);
|
|
3365
|
+
setEditTarget({ datasetId: layer.datasetId, layerId: layer.layerId, name: layer.name });
|
|
3366
|
+
setActiveToolState('layers');
|
|
3367
|
+
}, [dirty, saveProject]);
|
|
3368
|
+
|
|
3369
|
+
const appendVectorLayer = useCallback(async (target) => {
|
|
3370
|
+
if (!target || !target.datasetId) return;
|
|
3371
|
+
if (dirty && !(await saveProject())) return;
|
|
3372
|
+
setEditTarget(null);
|
|
3373
|
+
setDrawTarget({ datasetId: target.datasetId, layerId: target.layerId, name: target.name });
|
|
3374
|
+
setActiveToolState('layers');
|
|
3375
|
+
}, [dirty, saveProject]);
|
|
3376
|
+
|
|
3377
|
+
const updatePolygon = useCallback(async (target, feature, ring) => {
|
|
3378
|
+
if (!target || !feature) throw new Error(t('layers.featureUpdateFailed'));
|
|
3379
|
+
const dataset = await api.updateVectorFeature(target.datasetId, {
|
|
3380
|
+
name: feature.name,
|
|
3381
|
+
createdAt: feature.createdAt,
|
|
3382
|
+
geometry: { type: 'Polygon', coordinates: [ring] },
|
|
3383
|
+
});
|
|
3384
|
+
refreshLayers();
|
|
3385
|
+
showToast(t('layers.featureUpdated'));
|
|
3386
|
+
return dataset;
|
|
3387
|
+
}, [refreshLayers, showToast, t]);
|
|
3388
|
+
|
|
3389
|
+
const cancelEdit = useCallback(() => {
|
|
3390
|
+
setEditTarget(null);
|
|
3391
|
+
}, []);
|
|
3392
|
+
|
|
3393
|
+
const changeProject = useCallback(async (next) => {
|
|
3394
|
+
if (!next || next === projectId) return;
|
|
3395
|
+
if (dirty && !(await saveProject())) return;
|
|
3396
|
+
setDrawTarget(null);
|
|
3397
|
+
setEditTarget(null);
|
|
3398
|
+
setProjectId(next);
|
|
3399
|
+
setDirty(false);
|
|
3400
|
+
}, [projectId, dirty, saveProject]);
|
|
3401
|
+
|
|
3402
|
+
const openDataManager = useCallback(async () => {
|
|
3403
|
+
if (!projectId) {
|
|
3404
|
+
showToast(t('dataset.noProject'));
|
|
3405
|
+
return;
|
|
3406
|
+
}
|
|
3407
|
+
if (dirty && !(await saveProject())) return;
|
|
3408
|
+
setDataOpen(true);
|
|
3409
|
+
}, [projectId, dirty, saveProject, showToast, t]);
|
|
3410
|
+
|
|
3411
|
+
const handleExit = useCallback(async () => {
|
|
3412
|
+
if (dirty && !(await saveProject())) return;
|
|
3413
|
+
exitWorkbench(runtime);
|
|
3414
|
+
}, [dirty, saveProject, runtime]);
|
|
3415
|
+
|
|
3416
|
+
const handleSaveLayer = useCallback(async (layer) => {
|
|
3417
|
+
if (!projectId) {
|
|
3418
|
+
showToast(t('dataset.noProject'));
|
|
3419
|
+
return;
|
|
3420
|
+
}
|
|
3421
|
+
if (!layer.tileUrl) {
|
|
3422
|
+
showToast(t('dataset.failed'));
|
|
3423
|
+
return;
|
|
3424
|
+
}
|
|
3425
|
+
if (dirty && !(await saveProject())) return;
|
|
3426
|
+
try {
|
|
3427
|
+
await api.createTileDataset(projectId, {
|
|
3428
|
+
name: layer.name,
|
|
3429
|
+
tileUrl: layer.tileUrl,
|
|
3430
|
+
tileSize: layer.tileSize,
|
|
3431
|
+
attribution: layer.attribution,
|
|
3432
|
+
addLayer: true,
|
|
3433
|
+
});
|
|
3434
|
+
removeLayer(layer.key);
|
|
3435
|
+
refreshLayers();
|
|
3436
|
+
showToast(t('layers.added'));
|
|
3437
|
+
} catch (error) {
|
|
3438
|
+
showToast(error instanceof Error ? error.message : t('dataset.failed'));
|
|
3439
|
+
}
|
|
3440
|
+
}, [projectId, dirty, saveProject, refreshLayers, showToast, t]);
|
|
3441
|
+
|
|
3442
|
+
const createProject = useCallback(async (name) => {
|
|
3443
|
+
if (dirty && !(await saveProject())) throw new Error(t('project.saveFailed'));
|
|
3444
|
+
const project = await api.createProject({ name });
|
|
3445
|
+
setProjects((current) => [...current, project]);
|
|
3446
|
+
setDrawTarget(null);
|
|
3447
|
+
setEditTarget(null);
|
|
3448
|
+
setProjectId(project.id);
|
|
3449
|
+
showToast(t('project.created'));
|
|
3450
|
+
}, [dirty, saveProject, showToast, t]);
|
|
3451
|
+
|
|
3452
|
+
const activeProject = projects.find((project) => project.id === projectId);
|
|
3453
|
+
const projectName = activeProject ? activeProject.name : '';
|
|
3454
|
+
|
|
3455
|
+
useEffect(() => {
|
|
3456
|
+
if ((!layerMenu && !featureMenu) || typeof window === 'undefined') return undefined;
|
|
3457
|
+
const onKey = (event) => {
|
|
3458
|
+
if (event.key === 'Escape') {
|
|
3459
|
+
setLayerMenu(null);
|
|
3460
|
+
setFeatureMenu(null);
|
|
3461
|
+
}
|
|
3462
|
+
};
|
|
3463
|
+
window.addEventListener('keydown', onKey);
|
|
3464
|
+
return () => window.removeEventListener('keydown', onKey);
|
|
3465
|
+
}, [layerMenu, featureMenu]);
|
|
3466
|
+
|
|
3467
|
+
const handleChat = () => {
|
|
3468
|
+
if (!toggleChatPanel(runtime)) showToast(t('chat.unavailable'));
|
|
3469
|
+
};
|
|
3470
|
+
|
|
3471
|
+
return h('div', { className: 'gis-workbench' + (scheme === 'dark' ? ' gis-dark' : '') },
|
|
3472
|
+
h(GisStyle, { withMap: true }),
|
|
3473
|
+
h('header', { className: 'gis-topbar' },
|
|
3474
|
+
h('div', { className: 'gis-brand' },
|
|
3475
|
+
h('span', { className: 'gis-brand-mark' }, h(SvgIcon, { name: 'map', size: 19 })),
|
|
3476
|
+
h('div', { className: 'gis-brand-text' },
|
|
3477
|
+
h('span', { className: 'gis-brand-title' }, t('mode.workbench')),
|
|
3478
|
+
h('span', { className: 'gis-brand-sub' }, t('brand.sub')),
|
|
3479
|
+
),
|
|
3480
|
+
),
|
|
3481
|
+
h('div', { className: 'gis-topbar-center' },
|
|
3482
|
+
h('div', { className: 'gis-project-control' },
|
|
3483
|
+
h(SvgIcon, { name: 'folder', size: 14 }),
|
|
3484
|
+
h('select', {
|
|
3485
|
+
className: 'gis-project-select',
|
|
3486
|
+
value: projectId,
|
|
3487
|
+
disabled: projectLoading,
|
|
3488
|
+
title: t('project.select'),
|
|
3489
|
+
onChange: (event) => changeProject(event.target.value),
|
|
3490
|
+
},
|
|
3491
|
+
projectLoading
|
|
3492
|
+
? h('option', { value: '' }, t('project.loading'))
|
|
3493
|
+
: projects.length > 0
|
|
3494
|
+
? projects.map((project) => h('option', { key: project.id, value: project.id }, project.name))
|
|
3495
|
+
: h('option', { value: '' }, t('project.none')),
|
|
3496
|
+
),
|
|
3497
|
+
h('button', {
|
|
3498
|
+
type: 'button',
|
|
3499
|
+
className: 'gis-icon-btn gis-icon-btn-sm',
|
|
3500
|
+
title: t('project.new'),
|
|
3501
|
+
'aria-label': t('project.new'),
|
|
3502
|
+
onClick: () => setNewProjectOpen(true),
|
|
3503
|
+
}, h(SvgIcon, { name: 'plus', size: 13 })),
|
|
3504
|
+
),
|
|
3505
|
+
h('span', { className: 'gis-mode-chip' },
|
|
3506
|
+
h('span', { className: 'gis-mode-dot' }),
|
|
3507
|
+
t('mode.badge'),
|
|
3508
|
+
),
|
|
3509
|
+
h('span', { className: 'gis-topbar-meta' }, t('mode.framework') + ' · ' + t('topbar.stats', { layers: layers.length })),
|
|
3510
|
+
),
|
|
3511
|
+
h('div', { className: 'gis-topbar-actions' },
|
|
3512
|
+
h('button', {
|
|
3513
|
+
type: 'button',
|
|
3514
|
+
className: 'gis-btn' + (dirty ? ' gis-save-button is-dirty' : ' gis-save-button'),
|
|
3515
|
+
onClick: saveProject,
|
|
3516
|
+
disabled: !projectId || saving,
|
|
3517
|
+
title: dirty ? t('project.unsaved') : t('project.save'),
|
|
3518
|
+
'aria-label': t('project.save'),
|
|
3519
|
+
},
|
|
3520
|
+
h(SvgIcon, { name: 'save', size: 15 }),
|
|
3521
|
+
h('span', { className: 'gis-btn-label' }, saving ? t('project.saving') : t('project.save')),
|
|
3522
|
+
dirty ? h('span', { className: 'gis-save-dot' }) : null,
|
|
3523
|
+
),
|
|
3524
|
+
h('button', {
|
|
3525
|
+
type: 'button',
|
|
3526
|
+
className: 'gis-btn',
|
|
3527
|
+
onClick: handleChat,
|
|
3528
|
+
title: t('chat.toggle'),
|
|
3529
|
+
'aria-label': t('chat.toggle'),
|
|
3530
|
+
},
|
|
3531
|
+
h(SvgIcon, { name: 'chat', size: 15 }),
|
|
3532
|
+
h('span', { className: 'gis-btn-label' }, t('chat.toggle')),
|
|
3533
|
+
),
|
|
3534
|
+
h('button', {
|
|
3535
|
+
type: 'button',
|
|
3536
|
+
className: 'gis-btn gis-btn-primary',
|
|
3537
|
+
onClick: handleExit,
|
|
3538
|
+
title: t('mode.exit'),
|
|
3539
|
+
'aria-label': t('mode.exit'),
|
|
3540
|
+
},
|
|
3541
|
+
h(SvgIcon, { name: 'layout', size: 15 }),
|
|
3542
|
+
h('span', { className: 'gis-btn-label' }, t('mode.original')),
|
|
3543
|
+
),
|
|
3544
|
+
),
|
|
3545
|
+
),
|
|
3546
|
+
h('div', { className: 'gis-main' },
|
|
3547
|
+
h('aside', { className: 'gis-rail', 'aria-label': t('tools.title') },
|
|
3548
|
+
TOOL_DEFS.map((tool) => h('button', {
|
|
3549
|
+
key: tool.id,
|
|
3550
|
+
type: 'button',
|
|
3551
|
+
className: 'gis-rail-btn' + (tool.id === 'data' ? (dataOpen ? ' is-active' : '') : (activeTool === tool.id ? ' is-active' : '')),
|
|
3552
|
+
title: t(tool.key),
|
|
3553
|
+
'aria-label': t(tool.key),
|
|
3554
|
+
'aria-pressed': tool.id === 'data' ? dataOpen : activeTool === tool.id,
|
|
3555
|
+
onClick: () => {
|
|
3556
|
+
if (tool.id === 'data') openDataManager();
|
|
3557
|
+
else chooseTool(tool.id);
|
|
3558
|
+
},
|
|
3559
|
+
}, h(SvgIcon, { name: tool.icon, size: 18 }))),
|
|
3560
|
+
h('div', { className: 'gis-rail-divider' }),
|
|
3561
|
+
h('div', { className: 'gis-rail-foot', title: t('mode.framework') },
|
|
3562
|
+
h('span', { className: 'gis-rail-dot' }),
|
|
3563
|
+
h('span', { className: 'gis-rail-version' }, 'v0.2'),
|
|
3564
|
+
),
|
|
3565
|
+
),
|
|
3566
|
+
h('div', { className: 'gis-stage' },
|
|
3567
|
+
h(MapCanvas, {
|
|
3568
|
+
runtime,
|
|
3569
|
+
t,
|
|
3570
|
+
activeTool,
|
|
3571
|
+
onToggleLayer: handleToggleLayer,
|
|
3572
|
+
onRemoveLayer: handleRemoveLayer,
|
|
3573
|
+
onSaveLayer: handleSaveLayer,
|
|
3574
|
+
onReorderLayers: handleReorderLayers,
|
|
3575
|
+
onNotify: showToast,
|
|
3576
|
+
drawTarget,
|
|
3577
|
+
onSavePolygon: savePolygon,
|
|
3578
|
+
onCancelDraw: cancelDraw,
|
|
3579
|
+
onNewVectorLayer: openNewVectorLayer,
|
|
3580
|
+
onEditLayer: editVectorLayer,
|
|
3581
|
+
onAppendLayer: appendVectorLayer,
|
|
3582
|
+
onRefreshLayers: refreshLayers,
|
|
3583
|
+
editTarget,
|
|
3584
|
+
onUpdateFeature: updatePolygon,
|
|
3585
|
+
onCancelEdit: cancelEdit,
|
|
3586
|
+
onLayerMenu: (event, layer) => { setFeatureMenu(null); setLayerMenu({ x: event.clientX, y: event.clientY, layer }); },
|
|
3587
|
+
onFeatureMenu: (x, y, layer, feature) => { setLayerMenu(null); setFeatureMenu({ x, y, layer, feature }); },
|
|
3588
|
+
projectName,
|
|
3589
|
+
}),
|
|
3590
|
+
toast ? h('div', { className: 'gis-toast', role: 'status' }, toast) : null,
|
|
3591
|
+
),
|
|
3592
|
+
),
|
|
3593
|
+
newProjectOpen
|
|
3594
|
+
? h(NewProjectDialog, { t, onClose: () => setNewProjectOpen(false), onCreate: createProject })
|
|
3595
|
+
: null,
|
|
3596
|
+
newVectorOpen
|
|
3597
|
+
? h(NewVectorLayerDialog, { t, onClose: () => setNewVectorOpen(false), onCreate: createVectorLayer })
|
|
3598
|
+
: null,
|
|
3599
|
+
dataOpen && projectId
|
|
3600
|
+
? h(DatasetManager, {
|
|
3601
|
+
projectId,
|
|
3602
|
+
t,
|
|
3603
|
+
onClose: () => setDataOpen(false),
|
|
3604
|
+
onChanged: refreshLayers,
|
|
3605
|
+
notify: showToast,
|
|
3606
|
+
})
|
|
3607
|
+
: null,
|
|
3608
|
+
layerMenu
|
|
3609
|
+
? h(React.Fragment, null,
|
|
3610
|
+
h('div', {
|
|
3611
|
+
className: 'gis-context-backdrop',
|
|
3612
|
+
onClick: () => setLayerMenu(null),
|
|
3613
|
+
onContextMenu: (event) => { event.preventDefault(); setLayerMenu(null); },
|
|
3614
|
+
}),
|
|
3615
|
+
h('div', {
|
|
3616
|
+
className: 'gis-context-menu',
|
|
3617
|
+
style: {
|
|
3618
|
+
left: Math.max(8, Math.min(layerMenu.x, (typeof window !== 'undefined' ? window.innerWidth : 1200) - 252)) + 'px',
|
|
3619
|
+
top: Math.max(8, Math.min(layerMenu.y, (typeof window !== 'undefined' ? window.innerHeight : 800) - 96)) + 'px',
|
|
3620
|
+
},
|
|
3621
|
+
onClick: (event) => event.stopPropagation(),
|
|
3622
|
+
},
|
|
3623
|
+
h('div', { className: 'gis-context-title', title: layerMenu.layer.name }, layerMenu.layer.name),
|
|
3624
|
+
h('button', {
|
|
3625
|
+
type: 'button',
|
|
3626
|
+
className: 'gis-context-action',
|
|
3627
|
+
onClick: async () => {
|
|
3628
|
+
const layer = layerMenu.layer;
|
|
3629
|
+
setLayerMenu(null);
|
|
3630
|
+
const ok = await appendToConversation(runtime, buildLayerPrompt(layer, projectName));
|
|
3631
|
+
showToast(ok ? t('layers.addedToChat') : t('layers.chatUnavailable'));
|
|
3632
|
+
},
|
|
3633
|
+
}, h(SvgIcon, { name: 'chat', size: 13 }), t('layers.addToChat')),
|
|
3634
|
+
),
|
|
3635
|
+
)
|
|
3636
|
+
: null,
|
|
3637
|
+
featureMenu
|
|
3638
|
+
? h(React.Fragment, null,
|
|
3639
|
+
h('div', {
|
|
3640
|
+
className: 'gis-context-backdrop',
|
|
3641
|
+
onClick: () => setFeatureMenu(null),
|
|
3642
|
+
onContextMenu: (event) => { event.preventDefault(); setFeatureMenu(null); },
|
|
3643
|
+
}),
|
|
3644
|
+
h('div', {
|
|
3645
|
+
className: 'gis-context-menu',
|
|
3646
|
+
style: {
|
|
3647
|
+
left: Math.max(8, Math.min(featureMenu.x, (typeof window !== 'undefined' ? window.innerWidth : 1200) - 252)) + 'px',
|
|
3648
|
+
top: Math.max(8, Math.min(featureMenu.y, (typeof window !== 'undefined' ? window.innerHeight : 800) - 110)) + 'px',
|
|
3649
|
+
},
|
|
3650
|
+
onClick: (event) => event.stopPropagation(),
|
|
3651
|
+
},
|
|
3652
|
+
h('div', { className: 'gis-context-title' }, featureMenu.feature.name || t('layers.polygon')),
|
|
3653
|
+
h('div', { className: 'gis-context-subtitle' }, featureMenu.layer.name),
|
|
3654
|
+
h('button', {
|
|
3655
|
+
type: 'button',
|
|
3656
|
+
className: 'gis-context-action is-danger',
|
|
3657
|
+
onClick: async () => {
|
|
3658
|
+
const item = featureMenu;
|
|
3659
|
+
setFeatureMenu(null);
|
|
3660
|
+
try {
|
|
3661
|
+
await api.deleteVectorFeature(item.layer.datasetId, {
|
|
3662
|
+
name: item.feature.name,
|
|
3663
|
+
createdAt: item.feature.createdAt,
|
|
3664
|
+
});
|
|
3665
|
+
refreshLayers();
|
|
3666
|
+
showToast(t('layers.featureDeleted'));
|
|
3667
|
+
} catch (error) {
|
|
3668
|
+
showToast(error instanceof Error ? error.message : t('layers.featureDeleteFailed'));
|
|
3669
|
+
}
|
|
3670
|
+
},
|
|
3671
|
+
}, h(SvgIcon, { name: 'trash', size: 13 }), t('layers.deleteFeature')),
|
|
3672
|
+
),
|
|
3673
|
+
)
|
|
3674
|
+
: null,
|
|
3675
|
+
);
|
|
3676
|
+
}
|
|
3677
|
+
|
|
3678
|
+
function WorkbenchOverlay({ runtime, t }) {
|
|
3679
|
+
const open = useWorkbenchOpen();
|
|
3680
|
+
const [right, setRight] = useState(0);
|
|
3681
|
+
|
|
3682
|
+
useEffect(() => {
|
|
3683
|
+
if (!open || typeof document === 'undefined') return undefined;
|
|
3684
|
+
const column = document.querySelector('[data-rightbar-col]');
|
|
3685
|
+
if (!column) {
|
|
3686
|
+
setRight(0);
|
|
3687
|
+
return undefined;
|
|
3688
|
+
}
|
|
3689
|
+
const measure = () => setRight(Math.max(0, Math.round(column.getBoundingClientRect().width)));
|
|
3690
|
+
measure();
|
|
3691
|
+
let observer;
|
|
3692
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
3693
|
+
observer = new ResizeObserver(measure);
|
|
3694
|
+
observer.observe(column);
|
|
3695
|
+
}
|
|
3696
|
+
window.addEventListener('resize', measure);
|
|
3697
|
+
return () => {
|
|
3698
|
+
if (observer) observer.disconnect();
|
|
3699
|
+
window.removeEventListener('resize', measure);
|
|
3700
|
+
};
|
|
3701
|
+
}, [open]);
|
|
3702
|
+
|
|
3703
|
+
if (!open) return null;
|
|
3704
|
+
return h('div', {
|
|
3705
|
+
className: 'gis-overlay',
|
|
3706
|
+
style: { right: right + 'px' },
|
|
3707
|
+
role: 'region',
|
|
3708
|
+
'aria-label': t('mode.workbench'),
|
|
3709
|
+
}, h(Workbench, { runtime, t }));
|
|
3710
|
+
}
|
|
3711
|
+
|
|
3712
|
+
function GisFixedChatView(props) {
|
|
3713
|
+
return props.renderSlot('conversation.session', { view: 'chat' });
|
|
3714
|
+
}
|
|
3715
|
+
|
|
3716
|
+
function GisChatTab({ runtime, t, sessionId, useSession, useConversation, useSessions, renderFactorySlot }) {
|
|
3717
|
+
const session = useSession ? useSession((value) => value) : undefined;
|
|
3718
|
+
const conversation = useConversation ? useConversation((value) => value) : undefined;
|
|
3719
|
+
const summaryBlank = useSessions ? useSessions((state) => (sessionId && state.byId[sessionId] ? state.byId[sessionId].blank : undefined)) : undefined;
|
|
3720
|
+
|
|
3721
|
+
const collapse = () => {
|
|
3722
|
+
try {
|
|
3723
|
+
if (runtime.sidebarRight.isExpanded()) runtime.sidebarRight.toggleExpanded();
|
|
3724
|
+
} catch (error) {
|
|
3725
|
+
console.warn('[gis-workbench] could not collapse the conversation panel', error);
|
|
3726
|
+
}
|
|
3727
|
+
};
|
|
3728
|
+
|
|
3729
|
+
const top = h('div', { className: 'gis-chat-top' },
|
|
3730
|
+
h('span', { className: 'gis-chat-status-dot' }),
|
|
3731
|
+
h('div', { className: 'gis-chat-meta' },
|
|
3732
|
+
h('span', { className: 'gis-chat-title' }, t('chat.title')),
|
|
3733
|
+
h('span', { className: 'gis-chat-sub' }, t('chat.subtitle')),
|
|
3734
|
+
),
|
|
3735
|
+
h('button', {
|
|
3736
|
+
type: 'button',
|
|
3737
|
+
className: 'gis-chat-collapse',
|
|
3738
|
+
title: t('chat.collapse'),
|
|
3739
|
+
'aria-label': t('chat.collapse'),
|
|
3740
|
+
onClick: collapse,
|
|
3741
|
+
}, h(SvgIcon, { name: 'chevron-right', size: 15 })),
|
|
3742
|
+
);
|
|
3743
|
+
|
|
3744
|
+
if (!session || !renderFactorySlot) {
|
|
3745
|
+
return h('div', { className: 'gis-chat-wrap' }, h(GisStyle, null), top, h('div', { className: 'gis-chat-empty' }, t('chat.noSession')));
|
|
3746
|
+
}
|
|
3747
|
+
|
|
3748
|
+
const active = conversation && conversation.activeTargets && conversation.activeTargets.size > 0;
|
|
3749
|
+
const phase = active || (!session.blank && !session.awaitingFirstTurn) || session.running
|
|
3750
|
+
? 'active'
|
|
3751
|
+
: session.promptAttempted
|
|
3752
|
+
? 'engaging'
|
|
3753
|
+
: 'blank';
|
|
3754
|
+
const settling = phase === 'blank' && session.openState === 'loading' && summaryBlank !== true;
|
|
3755
|
+
const hero = phase === 'blank' && (session.openState === 'open' || summaryBlank === true);
|
|
3756
|
+
|
|
3757
|
+
let content = null;
|
|
3758
|
+
try {
|
|
3759
|
+
content = renderFactorySlot('conversation.content', {
|
|
3760
|
+
variant: 'embedded',
|
|
3761
|
+
phase: settling ? 'settling' : hero ? 'hero' : 'active',
|
|
3762
|
+
hero,
|
|
3763
|
+
}, {
|
|
3764
|
+
slots: { views: GisFixedChatView },
|
|
3765
|
+
});
|
|
3766
|
+
} catch (error) {
|
|
3767
|
+
console.error('[gis-workbench] could not render the embedded conversation', error);
|
|
3768
|
+
}
|
|
3769
|
+
|
|
3770
|
+
return h('div', { className: 'gis-chat-wrap' },
|
|
3771
|
+
h(GisStyle, null),
|
|
3772
|
+
top,
|
|
3773
|
+
h('div', { className: 'gis-chat-body' },
|
|
3774
|
+
content
|
|
3775
|
+
? h(ChatBoundary, { fallback: t('chat.unavailable') }, content)
|
|
3776
|
+
: h('div', { className: 'gis-chat-empty' }, t('chat.unavailable')),
|
|
3777
|
+
),
|
|
3778
|
+
);
|
|
3779
|
+
}
|
|
3780
|
+
|
|
3781
|
+
function WorkbenchLauncher({ runtime, t, sessionId }) {
|
|
3782
|
+
return h(React.Fragment, null,
|
|
3783
|
+
h(GisStyle, null),
|
|
3784
|
+
h('button', {
|
|
3785
|
+
type: 'button',
|
|
3786
|
+
className: 'gis-launcher',
|
|
3787
|
+
title: t('mode.enter'),
|
|
3788
|
+
'aria-label': t('mode.enter'),
|
|
3789
|
+
onClick: () => enterWorkbench(runtime, sessionId),
|
|
3790
|
+
},
|
|
3791
|
+
h(SvgIcon, { name: 'map', size: 13 }),
|
|
3792
|
+
h('span', null, 'GIS'),
|
|
3793
|
+
),
|
|
3794
|
+
);
|
|
3795
|
+
}
|
|
3796
|
+
|
|
3797
|
+
function argText(block, key) {
|
|
3798
|
+
try {
|
|
3799
|
+
return block && block.args && block.args.text ? block.args.text(key) : undefined;
|
|
3800
|
+
} catch (error) {
|
|
3801
|
+
return undefined;
|
|
3802
|
+
}
|
|
3803
|
+
}
|
|
3804
|
+
|
|
3805
|
+
function argNumber(block, key, fallback) {
|
|
3806
|
+
try {
|
|
3807
|
+
const value = block && block.args && block.args.value ? block.args.value(key) : undefined;
|
|
3808
|
+
const number = typeof value === 'number' ? value : Number(value);
|
|
3809
|
+
return Number.isFinite(number) ? number : fallback;
|
|
3810
|
+
} catch (error) {
|
|
3811
|
+
return fallback;
|
|
3812
|
+
}
|
|
3813
|
+
}
|
|
3814
|
+
|
|
3815
|
+
function argValue(block, key) {
|
|
3816
|
+
try {
|
|
3817
|
+
return block && block.args && block.args.value ? block.args.value(key) : undefined;
|
|
3818
|
+
} catch (error) {
|
|
3819
|
+
return undefined;
|
|
3820
|
+
}
|
|
3821
|
+
}
|
|
3822
|
+
|
|
3823
|
+
function StyleToolView({ runtime, t, phase, block, callId, sessionId }) {
|
|
3824
|
+
const layerId = argText(block, 'layerId') || '';
|
|
3825
|
+
const stylePatch = argValue(block, 'stylePatch');
|
|
3826
|
+
const isResult = phase === 'result';
|
|
3827
|
+
const isError = isResult && Boolean(block && block.isError);
|
|
3828
|
+
const shouldApply = isResult && !isError && layerId && stylePatch && typeof stylePatch === 'object';
|
|
3829
|
+
const [applied, setApplied] = React.useState(false);
|
|
3830
|
+
|
|
3831
|
+
useEffect(() => {
|
|
3832
|
+
if (!shouldApply) return;
|
|
3833
|
+
const changed = updateProjectLayerStyle(layerId, stylePatch);
|
|
3834
|
+
setApplied(changed);
|
|
3835
|
+
}, [shouldApply, layerId, JSON.stringify(stylePatch)]);
|
|
3836
|
+
|
|
3837
|
+
let stateText = t('gis.style.updating');
|
|
3838
|
+
let stateClass = '';
|
|
3839
|
+
if (isError) {
|
|
3840
|
+
stateText = t('gis.style.failed');
|
|
3841
|
+
stateClass = ' is-error';
|
|
3842
|
+
} else if (isResult) {
|
|
3843
|
+
stateText = applied ? t('gis.style.updated') : t('gis.style.savedOnly');
|
|
3844
|
+
stateClass = ' is-ok';
|
|
3845
|
+
}
|
|
3846
|
+
const errorText = isError
|
|
3847
|
+
? (block && block.error && (block.error.reason || block.error.code || block.error.name)) || t('gis.style.errorFallback')
|
|
3848
|
+
: null;
|
|
3849
|
+
const summary = stylePatch && typeof stylePatch === 'object' ? JSON.stringify(stylePatch) : '';
|
|
3850
|
+
|
|
3851
|
+
return h('div', { className: 'gis-tool-card' },
|
|
3852
|
+
h(GisStyle, null),
|
|
3853
|
+
h('div', { className: 'gis-tool-head' },
|
|
3854
|
+
h('div', { className: 'gis-tool-icon' }, h(SvgIcon, { name: 'layers', size: 15 })),
|
|
3855
|
+
h('div', { className: 'gis-tool-copy' },
|
|
3856
|
+
h('div', { className: 'gis-tool-title' }, t('gis.style.title')),
|
|
3857
|
+
h('div', { className: 'gis-tool-sub' }, layerId || ''),
|
|
3858
|
+
),
|
|
3859
|
+
h('span', { className: 'gis-tool-state' + stateClass }, stateText),
|
|
3860
|
+
),
|
|
3861
|
+
h('div', { className: 'gis-tool-body' },
|
|
3862
|
+
summary ? h('div', { className: 'gis-tool-url', title: summary }, summary.slice(0, 220)) : null,
|
|
3863
|
+
errorText ? h('div', { className: 'gis-tool-error' }, errorText) : null,
|
|
3864
|
+
!isResult ? h('div', { className: 'gis-tool-sub' }, t('gis.style.pendingHint')) : null,
|
|
3865
|
+
),
|
|
3866
|
+
);
|
|
3867
|
+
}
|
|
3868
|
+
|
|
3869
|
+
function AddLayerToolView({ runtime, t, phase, block, callId, sessionId }) {
|
|
3870
|
+
const layers = useLayerStore();
|
|
3871
|
+
const name = (argText(block, 'name') || t('gis.tool.defaultName')).trim() || t('gis.tool.defaultName');
|
|
3872
|
+
const rawTileUrl = argText(block, 'tileUrl') || DEFAULT_TILE_URL;
|
|
3873
|
+
const tileUrl = String(rawTileUrl).trim() || DEFAULT_TILE_URL;
|
|
3874
|
+
const tileSize = argNumber(block, 'tileSize', DEFAULT_TILE_SIZE) === 512 ? 512 : DEFAULT_TILE_SIZE;
|
|
3875
|
+
const minzoom = clamp(argNumber(block, 'minzoom', 0), 0, 24);
|
|
3876
|
+
const maxzoom = Math.max(minzoom, clamp(argNumber(block, 'maxzoom', 18), 0, 24));
|
|
3877
|
+
const attribution = argText(block, 'attribution') || DEFAULT_ATTRIBUTION;
|
|
3878
|
+
const isResult = phase === 'result';
|
|
3879
|
+
const isError = isResult && Boolean(block && block.isError);
|
|
3880
|
+
const shouldAdd = isResult && !isError;
|
|
3881
|
+
const added = layers.find((layer) => layer.tileUrl === tileUrl);
|
|
3882
|
+
const showAdded = shouldAdd && Boolean(added);
|
|
3883
|
+
|
|
3884
|
+
useEffect(() => {
|
|
3885
|
+
if (!shouldAdd) return;
|
|
3886
|
+
addLayer({
|
|
3887
|
+
key: 'tool:' + callId,
|
|
3888
|
+
name,
|
|
3889
|
+
tileUrl,
|
|
3890
|
+
tileSize,
|
|
3891
|
+
minzoom,
|
|
3892
|
+
maxzoom,
|
|
3893
|
+
attribution,
|
|
3894
|
+
source: 'conversation',
|
|
3895
|
+
});
|
|
3896
|
+
}, [shouldAdd, callId, name, tileUrl, tileSize, minzoom, maxzoom, attribution]);
|
|
3897
|
+
|
|
3898
|
+
let stateText = t('gis.tool.preparing');
|
|
3899
|
+
let stateClass = '';
|
|
3900
|
+
if (phase === 'start') stateText = t('gis.tool.adding');
|
|
3901
|
+
if (isError) {
|
|
3902
|
+
stateText = t('gis.tool.failed');
|
|
3903
|
+
stateClass = ' is-error';
|
|
3904
|
+
} else if (isResult) {
|
|
3905
|
+
stateText = t('gis.tool.added');
|
|
3906
|
+
stateClass = ' is-ok';
|
|
3907
|
+
}
|
|
3908
|
+
|
|
3909
|
+
const errorText = isError
|
|
3910
|
+
? (block && block.error && (block.error.reason || block.error.code || block.error.name)) || t('gis.tool.errorFallback')
|
|
3911
|
+
: null;
|
|
3912
|
+
|
|
3913
|
+
return h('div', { className: 'gis-tool-card' },
|
|
3914
|
+
h(GisStyle, null),
|
|
3915
|
+
h('div', { className: 'gis-tool-head' },
|
|
3916
|
+
h('div', { className: 'gis-tool-icon' }, h(SvgIcon, { name: 'layers', size: 15 })),
|
|
3917
|
+
h('div', { className: 'gis-tool-copy' },
|
|
3918
|
+
h('div', { className: 'gis-tool-title' }, t('gis.tool.title')),
|
|
3919
|
+
h('div', { className: 'gis-tool-sub' }, name),
|
|
3920
|
+
),
|
|
3921
|
+
h('span', { className: 'gis-tool-state' + stateClass }, stateText),
|
|
3922
|
+
),
|
|
3923
|
+
h('div', { className: 'gis-tool-body' },
|
|
3924
|
+
h('div', { className: 'gis-tool-url', title: tileUrl }, tileUrl),
|
|
3925
|
+
errorText ? h('div', { className: 'gis-tool-error' }, errorText) : null,
|
|
3926
|
+
h('div', { className: 'gis-tool-actions' },
|
|
3927
|
+
showAdded ? h('button', {
|
|
3928
|
+
type: 'button',
|
|
3929
|
+
className: 'gis-tool-btn is-primary',
|
|
3930
|
+
onClick: () => enterWorkbench(runtime, sessionId),
|
|
3931
|
+
}, h(SvgIcon, { name: 'map', size: 12 }), t('gis.tool.viewMap')) : null,
|
|
3932
|
+
!isResult ? h('span', { className: 'gis-tool-sub' }, t('gis.tool.pendingHint')) : null,
|
|
3933
|
+
),
|
|
3934
|
+
),
|
|
3935
|
+
);
|
|
3936
|
+
}
|
|
3937
|
+
|
|
3938
|
+
/** Client-side fallback so a place-only call still moves the camera. */
|
|
3939
|
+
const PLACE_COORDS = {
|
|
3940
|
+
hangzhou: [120.1551, 30.2741], '杭州': [120.1551, 30.2741],
|
|
3941
|
+
beijing: [116.4074, 39.9042], '北京': [116.4074, 39.9042],
|
|
3942
|
+
shanghai: [121.4737, 31.2304], '上海': [121.4737, 31.2304],
|
|
3943
|
+
shenzhen: [114.0579, 22.5431], '深圳': [114.0579, 22.5431],
|
|
3944
|
+
guangzhou: [113.2644, 23.1291], '广州': [113.2644, 23.1291],
|
|
3945
|
+
chengdu: [104.0665, 30.5723], '成都': [104.0665, 30.5723],
|
|
3946
|
+
xian: [108.9398, 34.3416], '西安': [108.9398, 34.3416],
|
|
3947
|
+
china: [104.2, 35.5], '中国': [104.2, 35.5],
|
|
3948
|
+
};
|
|
3949
|
+
|
|
3950
|
+
function readViewArgs(block) {
|
|
3951
|
+
let center = null;
|
|
3952
|
+
try {
|
|
3953
|
+
const raw = block && block.args && block.args.value ? block.args.value('center') : undefined;
|
|
3954
|
+
if (Array.isArray(raw) && raw.length >= 2) center = [Number(raw[0]), Number(raw[1])];
|
|
3955
|
+
} catch (error) {
|
|
3956
|
+
center = null;
|
|
3957
|
+
}
|
|
3958
|
+
const place = (argText(block, 'place') || '').trim().toLowerCase();
|
|
3959
|
+
if ((!center || !Number.isFinite(center[0]) || !Number.isFinite(center[1])) && place) {
|
|
3960
|
+
const preset = PLACE_COORDS[place];
|
|
3961
|
+
if (preset) center = preset.slice();
|
|
3962
|
+
}
|
|
3963
|
+
const label = (argText(block, 'label') || argText(block, 'place') || '').trim();
|
|
3964
|
+
return { center, zoom: argNumber(block, 'zoom', 11), label };
|
|
3965
|
+
}
|
|
3966
|
+
|
|
3967
|
+
function SetViewToolView({ runtime, t, phase, block, callId, sessionId }) {
|
|
3968
|
+
const { center, zoom, label } = readViewArgs(block);
|
|
3969
|
+
const isResult = phase === 'result';
|
|
3970
|
+
const isError = isResult && Boolean(block && block.isError);
|
|
3971
|
+
const shouldApply = isResult && !isError && Array.isArray(center);
|
|
3972
|
+
const valid = Array.isArray(center) && Number.isFinite(center[0]) && Number.isFinite(center[1]);
|
|
3973
|
+
|
|
3974
|
+
useEffect(() => {
|
|
3975
|
+
if (!shouldApply) return;
|
|
3976
|
+
setView({ key: 'tool:' + callId, center, zoom, label });
|
|
3977
|
+
}, [shouldApply, callId, zoom, label, center ? center[0] : null, center ? center[1] : null]);
|
|
3978
|
+
|
|
3979
|
+
let stateText = t('gis.view.locating');
|
|
3980
|
+
let stateClass = '';
|
|
3981
|
+
if (isError) {
|
|
3982
|
+
stateText = t('gis.view.failed');
|
|
3983
|
+
stateClass = ' is-error';
|
|
3984
|
+
} else if (isResult) {
|
|
3985
|
+
stateText = t('gis.view.located');
|
|
3986
|
+
stateClass = ' is-ok';
|
|
3987
|
+
}
|
|
3988
|
+
|
|
3989
|
+
const coords = valid
|
|
3990
|
+
? center[1].toFixed(4) + '°N, ' + center[0].toFixed(4) + '°E'
|
|
3991
|
+
: t('gis.view.errorFallback');
|
|
3992
|
+
|
|
3993
|
+
return h('div', { className: 'gis-tool-card' },
|
|
3994
|
+
h(GisStyle, null),
|
|
3995
|
+
h('div', { className: 'gis-tool-head' },
|
|
3996
|
+
h('div', { className: 'gis-tool-icon' }, h(SvgIcon, { name: 'map', size: 15 })),
|
|
3997
|
+
h('div', { className: 'gis-tool-copy' },
|
|
3998
|
+
h('div', { className: 'gis-tool-title' }, t('gis.view.title')),
|
|
3999
|
+
h('div', { className: 'gis-tool-sub' }, label || t('gis.view.defaultLabel')),
|
|
4000
|
+
),
|
|
4001
|
+
h('span', { className: 'gis-tool-state' + stateClass }, stateText),
|
|
4002
|
+
),
|
|
4003
|
+
h('div', { className: 'gis-tool-body' },
|
|
4004
|
+
h('div', { className: 'gis-tool-url' }, coords + ' · z' + Number(zoom).toFixed(1)),
|
|
4005
|
+
h('div', { className: 'gis-tool-actions' },
|
|
4006
|
+
isResult ? h('button', {
|
|
4007
|
+
type: 'button',
|
|
4008
|
+
className: 'gis-tool-btn is-primary',
|
|
4009
|
+
onClick: () => enterWorkbench(runtime, sessionId),
|
|
4010
|
+
}, h(SvgIcon, { name: 'map', size: 12 }), t('gis.tool.viewMap')) : null,
|
|
4011
|
+
!isResult ? h('span', { className: 'gis-tool-sub' }, t('gis.view.pendingHint')) : null,
|
|
4012
|
+
),
|
|
4013
|
+
),
|
|
4014
|
+
);
|
|
4015
|
+
}
|
|
4016
|
+
|
|
4017
|
+
function makeStyleToolView(runtime, t) {
|
|
4018
|
+
return function GisStyleToolView(props) {
|
|
4019
|
+
return h(StyleToolView, Object.assign({}, props, { runtime, t: props.t || t }));
|
|
4020
|
+
};
|
|
4021
|
+
}
|
|
4022
|
+
|
|
4023
|
+
function makeViewToolView(runtime, t) {
|
|
4024
|
+
return function GisSetViewToolView(props) {
|
|
4025
|
+
return h(SetViewToolView, Object.assign({}, props, { runtime, t: props.t || t }));
|
|
4026
|
+
};
|
|
4027
|
+
}
|
|
4028
|
+
|
|
4029
|
+
function makeOverlay(runtime, t) {
|
|
4030
|
+
return function GisWorkbenchOverlay(props) {
|
|
4031
|
+
return h(WorkbenchOverlay, { runtime, t: props.t || t });
|
|
4032
|
+
};
|
|
4033
|
+
}
|
|
4034
|
+
|
|
4035
|
+
function makeChatTab(runtime, t) {
|
|
4036
|
+
return function GisChatTabEntry(props) {
|
|
4037
|
+
return h(GisChatTab, Object.assign({}, props, { runtime, t: props.t || t }));
|
|
4038
|
+
};
|
|
4039
|
+
}
|
|
4040
|
+
|
|
4041
|
+
function makeLauncher(runtime, t) {
|
|
4042
|
+
return function GisWorkbenchLauncher(props) {
|
|
4043
|
+
return h(WorkbenchLauncher, Object.assign({}, props, { runtime, t: props.t || t }));
|
|
4044
|
+
};
|
|
4045
|
+
}
|
|
4046
|
+
|
|
4047
|
+
function makeToolView(runtime, t) {
|
|
4048
|
+
return function GisAddLayerToolView(props) {
|
|
4049
|
+
return h(AddLayerToolView, Object.assign({}, props, { runtime, t: props.t || t }));
|
|
4050
|
+
};
|
|
4051
|
+
}
|
|
4052
|
+
|
|
4053
|
+
let priorRightbar = null;
|
|
4054
|
+
const mapSessions = new Map();
|
|
4055
|
+
let activeAnchorSessionId;
|
|
4056
|
+
let activeMapSessionId;
|
|
4057
|
+
let workbenchEpoch = 0;
|
|
4058
|
+
let chatTabTitle = '地图对话';
|
|
4059
|
+
const composerActions = new Map();
|
|
4060
|
+
|
|
4061
|
+
function buildLayerPrompt(layer, projectName) {
|
|
4062
|
+
const lines = ['图层:' + (layer.name || '未命名图层')];
|
|
4063
|
+
if (layer.layerId) lines.push('layerId:' + layer.layerId);
|
|
4064
|
+
else if (layer.key) lines.push('layerKey:' + layer.key);
|
|
4065
|
+
if (layer.projectId) lines.push('projectId:' + layer.projectId);
|
|
4066
|
+
if (projectName) lines.push('项目:' + projectName);
|
|
4067
|
+
const promptFormat = layer.sourceFormat === 'postgis' ? 'pg数据库' : layer.format;
|
|
4068
|
+
if (layer.datasetKind) lines.push('类型:' + layer.datasetKind + (promptFormat ? ' / ' + promptFormat : ''));
|
|
4069
|
+
if (Number.isFinite(Number(layer.featureCount))) lines.push('要素数:' + layer.featureCount);
|
|
4070
|
+
if (Array.isArray(layer.fields) && layer.fields.length > 0) lines.push('字段:' + layer.fields.join(', '));
|
|
4071
|
+
if (Array.isArray(layer.bbox) && layer.bbox.length === 4) {
|
|
4072
|
+
lines.push('范围:' + layer.bbox.map((value) => Number(value).toFixed(6)).join(', '));
|
|
4073
|
+
}
|
|
4074
|
+
if (layer.style && typeof layer.style === 'object' && Object.keys(layer.style).length > 0) {
|
|
4075
|
+
const text = JSON.stringify(layer.style);
|
|
4076
|
+
if (text.length <= 320) lines.push('当前样式:' + text);
|
|
4077
|
+
}
|
|
4078
|
+
lines.push('', '请基于该图层:');
|
|
4079
|
+
return lines.join('\n');
|
|
4080
|
+
}
|
|
4081
|
+
|
|
4082
|
+
function appendChatDraft(sessionId, text) {
|
|
4083
|
+
const entry = composerActions.get(sessionId);
|
|
4084
|
+
if (!entry) return false;
|
|
4085
|
+
const current = String(entry.draft || '');
|
|
4086
|
+
const next = current.trim()
|
|
4087
|
+
? current.replace(/\s+$/, '') + '\n\n' + text
|
|
4088
|
+
: text;
|
|
4089
|
+
entry.inputActions.setDraft(next);
|
|
4090
|
+
return true;
|
|
4091
|
+
}
|
|
4092
|
+
|
|
4093
|
+
async function appendToConversation(runtime, text) {
|
|
4094
|
+
const sessionId = activeMapSessionId || activeAnchorSessionId;
|
|
4095
|
+
if (!sessionId) return false;
|
|
4096
|
+
try {
|
|
4097
|
+
openChat(runtime, sessionId);
|
|
4098
|
+
} catch (error) {
|
|
4099
|
+
// Retry below; the composer may mount shortly.
|
|
4100
|
+
}
|
|
4101
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
4102
|
+
if (appendChatDraft(sessionId, text)) return true;
|
|
4103
|
+
await new Promise((resolve) => setTimeout(resolve, 120));
|
|
4104
|
+
}
|
|
4105
|
+
return false;
|
|
4106
|
+
}
|
|
4107
|
+
|
|
4108
|
+
function ComposerDraftBridge({ sessionId, inputActions, useInput }) {
|
|
4109
|
+
const draft = useInput((state) => state.draft);
|
|
4110
|
+
useEffect(() => {
|
|
4111
|
+
if (!sessionId || !inputActions) return undefined;
|
|
4112
|
+
composerActions.set(sessionId, { inputActions, draft });
|
|
4113
|
+
return undefined;
|
|
4114
|
+
}, [sessionId, inputActions, draft]);
|
|
4115
|
+
return null;
|
|
4116
|
+
}
|
|
4117
|
+
|
|
4118
|
+
function makeComposerBridge(runtime, t) {
|
|
4119
|
+
return function GisComposerBridge(props) {
|
|
4120
|
+
return h(ComposerDraftBridge, Object.assign({}, props, { runtime, t: props.t || t }));
|
|
4121
|
+
};
|
|
4122
|
+
}
|
|
4123
|
+
|
|
4124
|
+
|
|
4125
|
+
function ensureChatTabType(runtime) {
|
|
4126
|
+
const tabs = runtime.get('sidebarRightTabs');
|
|
4127
|
+
if (!tabs || typeof tabs.register !== 'function') return false;
|
|
4128
|
+
try {
|
|
4129
|
+
if (typeof tabs.get === 'function' && tabs.get(CHAT_KIND)) return true;
|
|
4130
|
+
} catch (error) {
|
|
4131
|
+
// Fall through and try to register.
|
|
4132
|
+
}
|
|
4133
|
+
try {
|
|
4134
|
+
const dispose = tabs.register({
|
|
4135
|
+
id: CHAT_ID,
|
|
4136
|
+
kind: CHAT_KIND,
|
|
4137
|
+
title: () => chatTabTitle,
|
|
4138
|
+
});
|
|
4139
|
+
if (typeof runtime.effect === 'function') runtime.effect(() => dispose);
|
|
4140
|
+
return true;
|
|
4141
|
+
} catch (error) {
|
|
4142
|
+
console.error('[gis-workbench] could not register the conversation tab type on demand', error);
|
|
4143
|
+
return false;
|
|
4144
|
+
}
|
|
4145
|
+
}
|
|
4146
|
+
|
|
4147
|
+
function openChat(runtime, targetSessionId) {
|
|
4148
|
+
ensureChatTabType(runtime);
|
|
4149
|
+
try {
|
|
4150
|
+
const sidebar = runtime.sidebarRight;
|
|
4151
|
+
const mounted = sidebar.mounted && sidebar.mounted.getSnapshot ? sidebar.mounted.getSnapshot() : undefined;
|
|
4152
|
+
if (targetSessionId && mounted !== targetSessionId && typeof sidebar.openTabIn === 'function') {
|
|
4153
|
+
sidebar.openTabIn(targetSessionId, CHAT_KIND);
|
|
4154
|
+
return true;
|
|
4155
|
+
}
|
|
4156
|
+
const tabs = sidebar.openTabs && sidebar.openTabs.getSnapshot ? sidebar.openTabs.getSnapshot() : [];
|
|
4157
|
+
const existing = tabs.find((tab) => tab.kind === CHAT_KIND);
|
|
4158
|
+
if (existing) {
|
|
4159
|
+
if (!sidebar.isExpanded()) sidebar.toggleExpanded();
|
|
4160
|
+
try {
|
|
4161
|
+
sidebar.focus(existing.id);
|
|
4162
|
+
} catch (error) {
|
|
4163
|
+
// Focusing is best effort.
|
|
4164
|
+
}
|
|
4165
|
+
return true;
|
|
4166
|
+
}
|
|
4167
|
+
sidebar.openTab(CHAT_KIND);
|
|
4168
|
+
if (!sidebar.isExpanded()) sidebar.toggleExpanded();
|
|
4169
|
+
return true;
|
|
4170
|
+
} catch (error) {
|
|
4171
|
+
console.warn('[gis-workbench] could not open the conversation panel', error);
|
|
4172
|
+
return false;
|
|
4173
|
+
}
|
|
4174
|
+
}
|
|
4175
|
+
|
|
4176
|
+
function activateMapChat(runtime, mapSessionId) {
|
|
4177
|
+
if (!mapSessionId) return;
|
|
4178
|
+
const attempt = () => {
|
|
4179
|
+
if (!workbenchOpen || activeMapSessionId !== mapSessionId) return;
|
|
4180
|
+
try {
|
|
4181
|
+
const sidebar = runtime.sidebarRight;
|
|
4182
|
+
if (typeof sidebar.openTabIn === 'function') sidebar.openTabIn(mapSessionId, CHAT_KIND);
|
|
4183
|
+
} catch (error) {
|
|
4184
|
+
console.warn('[gis-workbench] could not target the map session tab', error);
|
|
4185
|
+
}
|
|
4186
|
+
openChat(runtime, mapSessionId);
|
|
4187
|
+
};
|
|
4188
|
+
attempt();
|
|
4189
|
+
setTimeout(attempt, 120);
|
|
4190
|
+
setTimeout(attempt, 320);
|
|
4191
|
+
setTimeout(attempt, 760);
|
|
4192
|
+
setTimeout(attempt, 1500);
|
|
4193
|
+
setTimeout(attempt, 3000);
|
|
4194
|
+
}
|
|
4195
|
+
|
|
4196
|
+
function closeChat(runtime) {
|
|
4197
|
+
try {
|
|
4198
|
+
const sidebar = runtime.sidebarRight;
|
|
4199
|
+
const tabs = sidebar.openTabs && sidebar.openTabs.getSnapshot ? sidebar.openTabs.getSnapshot() : [];
|
|
4200
|
+
for (const tab of tabs) {
|
|
4201
|
+
if (tab.kind !== CHAT_KIND) continue;
|
|
4202
|
+
try {
|
|
4203
|
+
sidebar.close(tab.id);
|
|
4204
|
+
} catch (error) {
|
|
4205
|
+
console.warn('[gis-workbench] could not close the conversation tab', error);
|
|
4206
|
+
}
|
|
4207
|
+
}
|
|
4208
|
+
} catch (error) {
|
|
4209
|
+
console.warn('[gis-workbench] could not inspect the conversation tabs', error);
|
|
4210
|
+
}
|
|
4211
|
+
}
|
|
4212
|
+
|
|
4213
|
+
function toggleChatPanel(runtime) {
|
|
4214
|
+
if (!activeMapSessionId) return false;
|
|
4215
|
+
ensureChatTabType(runtime);
|
|
4216
|
+
try {
|
|
4217
|
+
const sidebar = runtime.sidebarRight;
|
|
4218
|
+
const tabs = sidebar.openTabs && sidebar.openTabs.getSnapshot ? sidebar.openTabs.getSnapshot() : [];
|
|
4219
|
+
const existing = tabs.find((tab) => tab.kind === CHAT_KIND);
|
|
4220
|
+
if (existing) {
|
|
4221
|
+
const active = sidebar.active();
|
|
4222
|
+
if (sidebar.isExpanded()) {
|
|
4223
|
+
if (active && active.id === existing.id) sidebar.toggleExpanded();
|
|
4224
|
+
else sidebar.focus(existing.id);
|
|
4225
|
+
} else {
|
|
4226
|
+
sidebar.toggleExpanded();
|
|
4227
|
+
sidebar.focus(existing.id);
|
|
4228
|
+
}
|
|
4229
|
+
return true;
|
|
4230
|
+
}
|
|
4231
|
+
return openChat(runtime, activeMapSessionId);
|
|
4232
|
+
} catch (error) {
|
|
4233
|
+
console.warn('[gis-workbench] could not toggle the conversation panel', error);
|
|
4234
|
+
return false;
|
|
4235
|
+
}
|
|
4236
|
+
}
|
|
4237
|
+
|
|
4238
|
+
function workspaceIdOf(runtime, sessionId) {
|
|
4239
|
+
try {
|
|
4240
|
+
const workspaces = runtime.get('workspaces');
|
|
4241
|
+
const snapshot = workspaces && workspaces.list && workspaces.list.getSnapshot ? workspaces.list.getSnapshot() : undefined;
|
|
4242
|
+
const items = snapshot && Array.isArray(snapshot.items) ? snapshot.items : [];
|
|
4243
|
+
const workspace = items.find((item) => Array.isArray(item.sessionIds) && item.sessionIds.includes(sessionId));
|
|
4244
|
+
return workspace ? workspace.workspaceId : undefined;
|
|
4245
|
+
} catch (error) {
|
|
4246
|
+
return undefined;
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
|
|
4250
|
+
async function ensureMapSession(runtime, anchorSessionId) {
|
|
4251
|
+
const uiWorkspace = runtime.get('uiWorkspace');
|
|
4252
|
+
const sessions = runtime.get('sessions');
|
|
4253
|
+
const workspaceId = workspaceIdOf(runtime, anchorSessionId);
|
|
4254
|
+
if (!uiWorkspace || typeof uiWorkspace.connectWorkspace !== 'function' || !sessions || !workspaceId) {
|
|
4255
|
+
return anchorSessionId;
|
|
4256
|
+
}
|
|
4257
|
+
const cached = mapSessions.get(workspaceId);
|
|
4258
|
+
if (cached) {
|
|
4259
|
+
try {
|
|
4260
|
+
if (sessions.binding(cached)) return cached;
|
|
4261
|
+
} catch (error) {
|
|
4262
|
+
// The previous map session is gone; create another.
|
|
4263
|
+
}
|
|
4264
|
+
}
|
|
4265
|
+
const created = await uiWorkspace.connectWorkspace(workspaceId);
|
|
4266
|
+
if (created && created === anchorSessionId && typeof sessions.fork === 'function') {
|
|
4267
|
+
try {
|
|
4268
|
+
const forked = await sessions.fork({ sessionId: anchorSessionId, increaseTitle: false });
|
|
4269
|
+
if (forked) {
|
|
4270
|
+
mapSessions.set(workspaceId, forked);
|
|
4271
|
+
tagMapSession(sessions, forked);
|
|
4272
|
+
return forked;
|
|
4273
|
+
}
|
|
4274
|
+
} catch (error) {
|
|
4275
|
+
console.warn('[gis-workbench] could not fork a separate map session', error);
|
|
4276
|
+
}
|
|
4277
|
+
}
|
|
4278
|
+
if (created) {
|
|
4279
|
+
mapSessions.set(workspaceId, created);
|
|
4280
|
+
tagMapSession(sessions, created);
|
|
4281
|
+
return created;
|
|
4282
|
+
}
|
|
4283
|
+
return anchorSessionId;
|
|
4284
|
+
}
|
|
4285
|
+
|
|
4286
|
+
function tagMapSession(sessions, sessionId) {
|
|
4287
|
+
try {
|
|
4288
|
+
const session = sessions.binding(sessionId) && sessions.binding(sessionId).session;
|
|
4289
|
+
if (session && typeof session.rename === 'function') {
|
|
4290
|
+
session.rename('地图会话').catch((error) => {
|
|
4291
|
+
console.warn('[gis-workbench] could not name the map session', error);
|
|
4292
|
+
});
|
|
4293
|
+
}
|
|
4294
|
+
} catch (error) {
|
|
4295
|
+
console.warn('[gis-workbench] could not name the map session', error);
|
|
4296
|
+
}
|
|
4297
|
+
}
|
|
4298
|
+
|
|
4299
|
+
function waitForMounted(runtime, sessionId, timeoutMs) {
|
|
4300
|
+
return new Promise((resolve) => {
|
|
4301
|
+
const mounted = runtime.sidebarRight && runtime.sidebarRight.mounted;
|
|
4302
|
+
if (!mounted || typeof mounted.getSnapshot !== 'function') {
|
|
4303
|
+
setTimeout(() => resolve(false), 120);
|
|
4304
|
+
return;
|
|
4305
|
+
}
|
|
4306
|
+
if (mounted.getSnapshot() === sessionId) {
|
|
4307
|
+
resolve(true);
|
|
4308
|
+
return;
|
|
4309
|
+
}
|
|
4310
|
+
let done = false;
|
|
4311
|
+
let timer;
|
|
4312
|
+
let unsubscribe = () => {};
|
|
4313
|
+
const finish = (value) => {
|
|
4314
|
+
if (done) return;
|
|
4315
|
+
done = true;
|
|
4316
|
+
clearTimeout(timer);
|
|
4317
|
+
unsubscribe();
|
|
4318
|
+
resolve(value);
|
|
4319
|
+
};
|
|
4320
|
+
if (typeof mounted.subscribe === 'function') {
|
|
4321
|
+
unsubscribe = mounted.subscribe(() => {
|
|
4322
|
+
if (mounted.getSnapshot() === sessionId) finish(true);
|
|
4323
|
+
});
|
|
4324
|
+
}
|
|
4325
|
+
timer = setTimeout(() => finish(false), timeoutMs);
|
|
4326
|
+
});
|
|
4327
|
+
}
|
|
4328
|
+
|
|
4329
|
+
function restoreRightbar(runtime, prior) {
|
|
4330
|
+
try {
|
|
4331
|
+
const sidebar = runtime.sidebarRight;
|
|
4332
|
+
if (prior && prior.expanded) {
|
|
4333
|
+
if (!sidebar.isExpanded()) sidebar.toggleExpanded();
|
|
4334
|
+
if (prior.activeId) {
|
|
4335
|
+
try {
|
|
4336
|
+
sidebar.focus(prior.activeId);
|
|
4337
|
+
} catch (error) {
|
|
4338
|
+
// The previous tab may be gone.
|
|
4339
|
+
}
|
|
4340
|
+
}
|
|
4341
|
+
} else if (sidebar.isExpanded()) {
|
|
4342
|
+
sidebar.toggleExpanded();
|
|
4343
|
+
}
|
|
4344
|
+
} catch (error) {
|
|
4345
|
+
console.warn('[gis-workbench] could not restore the conversation panel state', error);
|
|
4346
|
+
}
|
|
4347
|
+
}
|
|
4348
|
+
|
|
4349
|
+
async function enterWorkbench(runtime, sessionId) {
|
|
4350
|
+
if (workbenchOpen) {
|
|
4351
|
+
if (activeMapSessionId) openChat(runtime, activeMapSessionId);
|
|
4352
|
+
return true;
|
|
4353
|
+
}
|
|
4354
|
+
try {
|
|
4355
|
+
const sidebar = runtime.sidebarRight;
|
|
4356
|
+
const active = sidebar.active();
|
|
4357
|
+
priorRightbar = {
|
|
4358
|
+
expanded: sidebar.isExpanded(),
|
|
4359
|
+
activeId: active ? active.id : null,
|
|
4360
|
+
};
|
|
4361
|
+
} catch (error) {
|
|
4362
|
+
priorRightbar = null;
|
|
4363
|
+
}
|
|
4364
|
+
activeAnchorSessionId = sessionId;
|
|
4365
|
+
activeMapSessionId = sessionId || undefined;
|
|
4366
|
+
setWorkbenchOpen(true);
|
|
4367
|
+
++workbenchEpoch;
|
|
4368
|
+
if (!sessionId) {
|
|
4369
|
+
openChat(runtime);
|
|
4370
|
+
return true;
|
|
4371
|
+
}
|
|
4372
|
+
activateMapChat(runtime, sessionId);
|
|
4373
|
+
return true;
|
|
4374
|
+
}
|
|
4375
|
+
|
|
4376
|
+
function exitWorkbench(runtime) {
|
|
4377
|
+
workbenchEpoch += 1;
|
|
4378
|
+
setWorkbenchOpen(false);
|
|
4379
|
+
const anchor = activeAnchorSessionId;
|
|
4380
|
+
const map = activeMapSessionId;
|
|
4381
|
+
const prior = priorRightbar;
|
|
4382
|
+
priorRightbar = null;
|
|
4383
|
+
activeAnchorSessionId = undefined;
|
|
4384
|
+
activeMapSessionId = undefined;
|
|
4385
|
+
closeChat(runtime);
|
|
4386
|
+
restoreRightbar(runtime, prior);
|
|
4387
|
+
}
|
|
4388
|
+
|
|
4389
|
+
export function createPlugin(ReactRuntime) {
|
|
4390
|
+
React = ReactRuntime;
|
|
4391
|
+
h = React.createElement;
|
|
4392
|
+
useState = React.useState;
|
|
4393
|
+
useEffect = React.useEffect;
|
|
4394
|
+
useRef = React.useRef;
|
|
4395
|
+
useCallback = React.useCallback;
|
|
4396
|
+
useMemo = React.useMemo;
|
|
4397
|
+
|
|
4398
|
+
ChatBoundary = class ChatBoundary extends React.Component {
|
|
4399
|
+
constructor(props) {
|
|
4400
|
+
super(props);
|
|
4401
|
+
this.state = { failed: false };
|
|
4402
|
+
}
|
|
4403
|
+
|
|
4404
|
+
static getDerivedStateFromError() {
|
|
4405
|
+
return { failed: true };
|
|
4406
|
+
}
|
|
4407
|
+
|
|
4408
|
+
componentDidCatch(error) {
|
|
4409
|
+
console.error('[gis-workbench] embedded conversation failed', error);
|
|
4410
|
+
}
|
|
4411
|
+
|
|
4412
|
+
render() {
|
|
4413
|
+
if (this.state.failed) {
|
|
4414
|
+
return h('div', { className: 'gis-chat-empty' }, this.props.fallback);
|
|
4415
|
+
}
|
|
4416
|
+
return this.props.children;
|
|
4417
|
+
}
|
|
4418
|
+
};
|
|
4419
|
+
|
|
4420
|
+
function apply(ctx) {
|
|
4421
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'gis-workbench: dictionaries');
|
|
4422
|
+
const t = ctx.locale.bind(NS);
|
|
4423
|
+
chatTabTitle = t('chat.tab');
|
|
4424
|
+
|
|
4425
|
+
try {
|
|
4426
|
+
const layout = ctx.get('layout');
|
|
4427
|
+
const active = layout && layout.panelInfo && layout.panelInfo.getSnapshot ? layout.panelInfo.getSnapshot() : undefined;
|
|
4428
|
+
if (active && active.activePanelId === 'gis-workbench') layout.selectPanel(null);
|
|
4429
|
+
} catch (error) {
|
|
4430
|
+
console.warn('[gis-workbench] could not clear the legacy workbench panel selection', error);
|
|
4431
|
+
}
|
|
4432
|
+
|
|
4433
|
+
const tabTypes = ctx.get('sidebarRightTabs');
|
|
4434
|
+
if (tabTypes && typeof tabTypes.register === 'function') {
|
|
4435
|
+
ctx.effect(() => {
|
|
4436
|
+
try {
|
|
4437
|
+
return tabTypes.register({
|
|
4438
|
+
id: CHAT_ID,
|
|
4439
|
+
kind: CHAT_KIND,
|
|
4440
|
+
title: () => t('chat.tab'),
|
|
4441
|
+
});
|
|
4442
|
+
} catch (error) {
|
|
4443
|
+
console.error('[gis-workbench] could not register the conversation tab type', error);
|
|
4444
|
+
return undefined;
|
|
4445
|
+
}
|
|
4446
|
+
}, 'gis-workbench: conversation tab type');
|
|
4447
|
+
} else {
|
|
4448
|
+
console.warn('[gis-workbench] the sidebar tab registry is unavailable; the map panel still works.');
|
|
4449
|
+
}
|
|
4450
|
+
|
|
4451
|
+
ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
4452
|
+
name: 'shell.overlay',
|
|
4453
|
+
id: 'gis-workbench-overlay',
|
|
4454
|
+
order: 50,
|
|
4455
|
+
locale: NS,
|
|
4456
|
+
}, makeOverlay(ctx, t)));
|
|
4457
|
+
|
|
4458
|
+
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register({
|
|
4459
|
+
name: 'sidebar.right.pane.tab',
|
|
4460
|
+
key: CHAT_ID,
|
|
4461
|
+
locale: NS,
|
|
4462
|
+
}, makeChatTab(ctx, t)));
|
|
4463
|
+
|
|
4464
|
+
ctx.slots.inject('conversation.input.dock', () => ctx.slots.register({
|
|
4465
|
+
name: 'conversation.input.dock',
|
|
4466
|
+
id: 'gis-composer-bridge',
|
|
4467
|
+
order: 30,
|
|
4468
|
+
locale: NS,
|
|
4469
|
+
}, makeComposerBridge(ctx, t)));
|
|
4470
|
+
|
|
4471
|
+
ctx.slots.inject('conversation.session.header.utilities', () => ctx.slots.register({
|
|
4472
|
+
name: 'conversation.session.header.utilities',
|
|
4473
|
+
id: 'gis-workbench-launcher',
|
|
4474
|
+
order: 40,
|
|
4475
|
+
locale: NS,
|
|
4476
|
+
}, makeLauncher(ctx, t)));
|
|
4477
|
+
|
|
4478
|
+
ctx.slots.inject('tool.call.toolview', () => ctx.slots.register({
|
|
4479
|
+
name: 'tool.call.toolview',
|
|
4480
|
+
key: TOOL_VIEW_KEY,
|
|
4481
|
+
locale: NS,
|
|
4482
|
+
}, makeToolView(ctx, t)));
|
|
4483
|
+
|
|
4484
|
+
ctx.slots.inject('tool.call.toolview', () => ctx.slots.register({
|
|
4485
|
+
name: 'tool.call.toolview',
|
|
4486
|
+
key: VIEW_TOOL_NAME,
|
|
4487
|
+
locale: NS,
|
|
4488
|
+
}, makeViewToolView(ctx, t)));
|
|
4489
|
+
|
|
4490
|
+
ctx.slots.inject('tool.call.toolview', () => ctx.slots.register({
|
|
4491
|
+
name: 'tool.call.toolview',
|
|
4492
|
+
key: UPDATE_STYLE_TOOL_NAME,
|
|
4493
|
+
locale: NS,
|
|
4494
|
+
}, makeStyleToolView(ctx, t)));
|
|
4495
|
+
}
|
|
4496
|
+
|
|
4497
|
+
return {
|
|
4498
|
+
inject: ['slots', 'locale', 'sidebarRight', 'theme'],
|
|
4499
|
+
apply,
|
|
4500
|
+
};
|
|
4501
|
+
}
|
|
4502
|
+
/** Exported so a headless harness can mount the map without the host shell. */
|
|
4503
|
+
export { MapCanvas };
|