@file-viewer/renderer-geo 2.0.11
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/LICENSE +160 -0
- package/README.en.md +37 -0
- package/README.md +37 -0
- package/dist/geo.d.ts +2 -0
- package/dist/geo.js +284 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +17 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work by
|
|
47
|
+
the copyright owner or by an individual or Legal Entity authorized to submit on
|
|
48
|
+
behalf of the copyright owner. For the purposes of this definition, "submitted"
|
|
49
|
+
means any form of electronic, verbal, or written communication sent to the
|
|
50
|
+
Licensor or its representatives, including but not limited to communication on
|
|
51
|
+
electronic mailing lists, source code control systems, and issue tracking systems
|
|
52
|
+
that are managed by, or on behalf of, the Licensor for the purpose of discussing
|
|
53
|
+
and improving the Work, but excluding communication that is conspicuously marked
|
|
54
|
+
or otherwise designated in writing by the copyright owner as "Not a
|
|
55
|
+
Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work to
|
|
75
|
+
which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
90
|
+
You changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the Derivative
|
|
102
|
+
Works; within the Source form or documentation, if provided along with the
|
|
103
|
+
Derivative Works; or, within a display generated by the Derivative Works, if and
|
|
104
|
+
wherever such third-party notices normally appear. The contents of the NOTICE
|
|
105
|
+
file are for informational purposes only and do not modify the License. You may
|
|
106
|
+
add Your own attribution notices within Derivative Works that You distribute,
|
|
107
|
+
alongside or as an addendum to the NOTICE text from the Work, provided that such
|
|
108
|
+
additional attribution notices cannot be construed as modifying the License.
|
|
109
|
+
|
|
110
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
111
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
112
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
113
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
114
|
+
with the conditions stated in this License.
|
|
115
|
+
|
|
116
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
117
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
118
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
119
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
120
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
121
|
+
executed with Licensor regarding such Contributions.
|
|
122
|
+
|
|
123
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
124
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
125
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
126
|
+
reproducing the content of the NOTICE file.
|
|
127
|
+
|
|
128
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
129
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
130
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
131
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
132
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
133
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
134
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
135
|
+
associated with Your exercise of permissions under this License.
|
|
136
|
+
|
|
137
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
138
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
139
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
140
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
141
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
142
|
+
arising as a result of this License or out of the use or inability to use the
|
|
143
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
144
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
145
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
146
|
+
damages.
|
|
147
|
+
|
|
148
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
149
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
150
|
+
acceptance of support, warranty, indemnity, or other liability obligations and/or
|
|
151
|
+
rights consistent with this License. However, in accepting such obligations, You
|
|
152
|
+
may act only on Your own behalf and on Your sole responsibility, not on behalf of
|
|
153
|
+
any other Contributor, and only if You agree to indemnify, defend, and hold each
|
|
154
|
+
Contributor harmless for any liability incurred by, or claims asserted against,
|
|
155
|
+
such Contributor by reason of your accepting any such warranty or additional
|
|
156
|
+
liability.
|
|
157
|
+
|
|
158
|
+
END OF TERMS AND CONDITIONS
|
|
159
|
+
|
|
160
|
+
Copyright 2026 Flyfish Viewer
|
package/README.en.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @file-viewer/renderer-geo
|
|
2
|
+
|
|
3
|
+
Standalone geospatial renderer package for Flyfish File Viewer. It previews GeoJSON, KML, GPX, and Shapefile data in the browser while keeping `@tmcw/togeojson` and `shpjs` lazy-loaded for geospatial files only.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { geoRenderer } from '@file-viewer/renderer-geo'
|
|
9
|
+
|
|
10
|
+
const options = {
|
|
11
|
+
builtinRenderers: 'none',
|
|
12
|
+
renderers: [geoRenderer],
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
You can also compose it through the full preset:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { allRenderers } from '@file-viewer/preset-all'
|
|
20
|
+
|
|
21
|
+
const options = {
|
|
22
|
+
builtinRenderers: 'none',
|
|
23
|
+
renderers: allRenderers,
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Capabilities
|
|
28
|
+
|
|
29
|
+
- Reads GeoJSON `FeatureCollection`, `Feature`, and standalone geometry objects.
|
|
30
|
+
- Loads `@tmcw/togeojson` only for KML / GPX and normalizes them into the shared GeoJSON pipeline.
|
|
31
|
+
- Loads `shpjs` only for SHP / Shapefile previews, including common ZIP or binary Shapefile payloads.
|
|
32
|
+
- Does not depend on online map tiles. The default output is an offline SVG vector map, which is suitable for intranet deployment and attachment review.
|
|
33
|
+
- Cleans up DOM resources on unmount and remains compatible with core lifecycle, HTML export, and zoom orchestration.
|
|
34
|
+
|
|
35
|
+
## Migration Note
|
|
36
|
+
|
|
37
|
+
`@file-viewer/core` no longer bundles the geo renderer and no longer installs `@tmcw/togeojson` or `shpjs` by default. Install this renderer explicitly, or use `@file-viewer/preset-all`, when GeoJSON / KML / GPX / SHP preview is required.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @file-viewer/renderer-geo
|
|
2
|
+
|
|
3
|
+
Flyfish File Viewer 的独立地理数据 renderer 包。它负责 GeoJSON、KML、GPX 和 Shapefile 的浏览器端预览,并让 `@tmcw/togeojson`、`shpjs` 只在命中地理数据格式时加载。
|
|
4
|
+
|
|
5
|
+
## 使用
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { geoRenderer } from '@file-viewer/renderer-geo'
|
|
9
|
+
|
|
10
|
+
const options = {
|
|
11
|
+
builtinRenderers: 'none',
|
|
12
|
+
renderers: [geoRenderer],
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
也可以通过全量 preset 自动装配:
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { allRenderers } from '@file-viewer/preset-all'
|
|
20
|
+
|
|
21
|
+
const options = {
|
|
22
|
+
builtinRenderers: 'none',
|
|
23
|
+
renderers: allRenderers,
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 能力
|
|
28
|
+
|
|
29
|
+
- GeoJSON 直接读取 `FeatureCollection`、`Feature` 或单个 geometry。
|
|
30
|
+
- KML / GPX 只在命中格式时按需加载 `@tmcw/togeojson`,转换为统一 GeoJSON 管线。
|
|
31
|
+
- SHP / Shapefile 只在命中格式时按需加载 `shpjs`,支持常见 ZIP 或二进制 Shapefile 数据转 GeoJSON。
|
|
32
|
+
- 渲染层不依赖在线地图瓦片,默认输出离线 SVG 矢量预览,适合内网部署和附件快速审阅。
|
|
33
|
+
- 卸载时清理 DOM 资源,和 core 的生命周期、导出 HTML、缩放能力保持一致。
|
|
34
|
+
|
|
35
|
+
## 迁移说明
|
|
36
|
+
|
|
37
|
+
`@file-viewer/core` 已不再内置 geo renderer,也不会默认安装 `@tmcw/togeojson` 和 `shpjs`。需要 GeoJSON / KML / GPX / SHP 预览时,请显式安装本包,或直接使用 `@file-viewer/preset-all` 聚合能力。
|
package/dist/geo.d.ts
ADDED
package/dist/geo.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { readFileViewerText, } from '@file-viewer/core';
|
|
2
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
3
|
+
const GEO_WIDTH = 960;
|
|
4
|
+
const GEO_HEIGHT = 620;
|
|
5
|
+
const GEO_PADDING = 36;
|
|
6
|
+
const geoStyle = `
|
|
7
|
+
.geo-viewer{min-height:100%;display:grid;grid-template-columns:minmax(240px,320px) minmax(0,1fr);background:#eef1f4;color:#132235}
|
|
8
|
+
.geo-panel{padding:24px;border-right:1px solid rgba(15,23,42,.08);background:#fff;box-sizing:border-box}
|
|
9
|
+
.geo-panel>span{color:#0f766e;font-size:12px;font-weight:800}.geo-panel h2{margin:8px 0 22px;font-size:24px}
|
|
10
|
+
.geo-panel dl{display:grid;gap:12px;margin:0}.geo-panel dt,.geo-counts strong{color:#64748b;font-size:12px;font-weight:700}.geo-panel dd{margin:4px 0 0;word-break:break-all;font-size:14px}
|
|
11
|
+
.geo-counts{margin-top:24px}.geo-counts p{margin:8px 0 0;font-size:14px}
|
|
12
|
+
.geo-map{padding:28px;overflow:auto}.geo-map svg{display:block;width:min(100%,1200px);min-width:640px;height:auto;margin:0 auto;overflow:visible}
|
|
13
|
+
.geo-map rect{fill:#f8fafc;stroke:rgba(15,23,42,.08)}
|
|
14
|
+
.geo-map path{fill:none;stroke:#0f766e;stroke-width:2.2;vector-effect:non-scaling-stroke}.geo-map .geo-polygon{fill:rgba(45,212,191,.18)}
|
|
15
|
+
.geo-map circle{fill:#2563eb;stroke:#fff;stroke-width:1.5;vector-effect:non-scaling-stroke}
|
|
16
|
+
.geo-state{display:flex;align-items:center;justify-content:center;min-height:280px;border-radius:8px;background:rgba(255,255,255,.82);color:#64748b}
|
|
17
|
+
.geo-state.error{color:#b42318}
|
|
18
|
+
.file-viewer[data-viewer-theme='dark'] .geo-viewer{background:#101820;color:#e5edf6}.file-viewer[data-viewer-theme='dark'] .geo-panel{background:#111827;border-color:rgba(148,163,184,.18)}.file-viewer[data-viewer-theme='dark'] .geo-panel dt,.file-viewer[data-viewer-theme='dark'] .geo-counts strong{color:#94a3b8}.file-viewer[data-viewer-theme='dark'] .geo-map rect{fill:#111827;stroke:rgba(148,163,184,.18)}
|
|
19
|
+
@media (prefers-color-scheme:dark){.file-viewer[data-viewer-theme='system'] .geo-viewer{background:#101820;color:#e5edf6}.file-viewer[data-viewer-theme='system'] .geo-panel{background:#111827;border-color:rgba(148,163,184,.18)}.file-viewer[data-viewer-theme='system'] .geo-panel dt,.file-viewer[data-viewer-theme='system'] .geo-counts strong{color:#94a3b8}.file-viewer[data-viewer-theme='system'] .geo-map rect{fill:#111827;stroke:rgba(148,163,184,.18)}}
|
|
20
|
+
@media (max-width:860px){.geo-viewer{grid-template-columns:1fr}.geo-panel{border-right:0;border-bottom:1px solid rgba(15,23,42,.08)}}
|
|
21
|
+
`;
|
|
22
|
+
const createStyle = () => {
|
|
23
|
+
const style = document.createElement('style');
|
|
24
|
+
style.textContent = geoStyle;
|
|
25
|
+
return style;
|
|
26
|
+
};
|
|
27
|
+
const createSvgElement = (tag) => {
|
|
28
|
+
return document.createElementNS(SVG_NS, tag);
|
|
29
|
+
};
|
|
30
|
+
const normalizeGeoJson = (value) => {
|
|
31
|
+
const candidate = value;
|
|
32
|
+
if ((candidate === null || candidate === void 0 ? void 0 : candidate.type) === 'FeatureCollection' && Array.isArray(candidate.features)) {
|
|
33
|
+
return candidate;
|
|
34
|
+
}
|
|
35
|
+
if ((candidate === null || candidate === void 0 ? void 0 : candidate.type) === 'Feature') {
|
|
36
|
+
return { type: 'FeatureCollection', features: [candidate] };
|
|
37
|
+
}
|
|
38
|
+
if ((candidate === null || candidate === void 0 ? void 0 : candidate.type) && candidate.coordinates) {
|
|
39
|
+
return {
|
|
40
|
+
type: 'FeatureCollection',
|
|
41
|
+
features: [{ type: 'Feature', geometry: candidate, properties: {} }],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
throw new Error('无法识别的 GeoJSON 数据');
|
|
45
|
+
};
|
|
46
|
+
const parseXml = (text) => {
|
|
47
|
+
const doc = new DOMParser().parseFromString(text, 'application/xml');
|
|
48
|
+
const error = doc.querySelector('parsererror');
|
|
49
|
+
if (error) {
|
|
50
|
+
throw new Error(error.textContent || 'XML 解析失败');
|
|
51
|
+
}
|
|
52
|
+
return doc;
|
|
53
|
+
};
|
|
54
|
+
const normalizeGeoType = (type) => {
|
|
55
|
+
const normalized = (type || 'geojson').trim().toLowerCase();
|
|
56
|
+
if (normalized === 'json') {
|
|
57
|
+
return 'geojson';
|
|
58
|
+
}
|
|
59
|
+
if (normalized === 'shapefile') {
|
|
60
|
+
return 'shp';
|
|
61
|
+
}
|
|
62
|
+
return normalized;
|
|
63
|
+
};
|
|
64
|
+
const parseGeo = async (buffer, type) => {
|
|
65
|
+
if (type === 'geojson') {
|
|
66
|
+
return normalizeGeoJson(JSON.parse(await readFileViewerText(buffer)));
|
|
67
|
+
}
|
|
68
|
+
if (type === 'kml' || type === 'gpx') {
|
|
69
|
+
const toGeoJSON = await import('@tmcw/togeojson');
|
|
70
|
+
const doc = parseXml(await readFileViewerText(buffer));
|
|
71
|
+
return normalizeGeoJson(type === 'kml' ? toGeoJSON.kml(doc) : toGeoJSON.gpx(doc));
|
|
72
|
+
}
|
|
73
|
+
if (type === 'shp') {
|
|
74
|
+
const { default: shp } = await import('shpjs');
|
|
75
|
+
return normalizeGeoJson(await shp(buffer));
|
|
76
|
+
}
|
|
77
|
+
throw new Error(`不支持 .${type} 地理格式`);
|
|
78
|
+
};
|
|
79
|
+
const walkPositions = (geometry, visit) => {
|
|
80
|
+
if (!geometry) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
switch (geometry.type) {
|
|
84
|
+
case 'Point':
|
|
85
|
+
visit(geometry.coordinates);
|
|
86
|
+
break;
|
|
87
|
+
case 'MultiPoint':
|
|
88
|
+
case 'LineString':
|
|
89
|
+
geometry.coordinates.forEach(visit);
|
|
90
|
+
break;
|
|
91
|
+
case 'MultiLineString':
|
|
92
|
+
case 'Polygon':
|
|
93
|
+
geometry.coordinates.forEach(line => line.forEach(visit));
|
|
94
|
+
break;
|
|
95
|
+
case 'MultiPolygon':
|
|
96
|
+
geometry.coordinates.forEach(polygon => polygon.forEach(line => line.forEach(visit)));
|
|
97
|
+
break;
|
|
98
|
+
case 'GeometryCollection':
|
|
99
|
+
geometry.geometries.forEach(item => walkPositions(item, visit));
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const collectBounds = (features) => {
|
|
104
|
+
let minX = Infinity;
|
|
105
|
+
let minY = Infinity;
|
|
106
|
+
let maxX = -Infinity;
|
|
107
|
+
let maxY = -Infinity;
|
|
108
|
+
features.forEach(feature => {
|
|
109
|
+
walkPositions(feature.geometry, position => {
|
|
110
|
+
const [x, y] = position;
|
|
111
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
minX = Math.min(minX, x);
|
|
115
|
+
minY = Math.min(minY, y);
|
|
116
|
+
maxX = Math.max(maxX, x);
|
|
117
|
+
maxY = Math.max(maxY, y);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minY)) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
return { minX, minY, maxX, maxY };
|
|
124
|
+
};
|
|
125
|
+
const projectPosition = (position, bounds) => {
|
|
126
|
+
if (!bounds) {
|
|
127
|
+
return [GEO_WIDTH / 2, GEO_HEIGHT / 2];
|
|
128
|
+
}
|
|
129
|
+
const xRange = Math.max(1e-9, bounds.maxX - bounds.minX);
|
|
130
|
+
const yRange = Math.max(1e-9, bounds.maxY - bounds.minY);
|
|
131
|
+
const scale = Math.min((GEO_WIDTH - GEO_PADDING * 2) / xRange, (GEO_HEIGHT - GEO_PADDING * 2) / yRange);
|
|
132
|
+
const xOffset = (GEO_WIDTH - xRange * scale) / 2;
|
|
133
|
+
const yOffset = (GEO_HEIGHT - yRange * scale) / 2;
|
|
134
|
+
const x = xOffset + (position[0] - bounds.minX) * scale;
|
|
135
|
+
const y = GEO_HEIGHT - (yOffset + (position[1] - bounds.minY) * scale);
|
|
136
|
+
return [Number(x.toFixed(2)), Number(y.toFixed(2))];
|
|
137
|
+
};
|
|
138
|
+
const pathLine = (positions, bounds, close = false) => {
|
|
139
|
+
const points = positions
|
|
140
|
+
.filter(position => Number.isFinite(position[0]) && Number.isFinite(position[1]))
|
|
141
|
+
.map(position => projectPosition(position, bounds));
|
|
142
|
+
if (!points.length) {
|
|
143
|
+
return '';
|
|
144
|
+
}
|
|
145
|
+
const [first, ...rest] = points;
|
|
146
|
+
const body = [`M${first[0]} ${first[1]}`, ...rest.map(point => `L${point[0]} ${point[1]}`)];
|
|
147
|
+
if (close) {
|
|
148
|
+
body.push('Z');
|
|
149
|
+
}
|
|
150
|
+
return body.join(' ');
|
|
151
|
+
};
|
|
152
|
+
const appendGeometry = (parent, geometry, bounds) => {
|
|
153
|
+
if (!geometry) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
switch (geometry.type) {
|
|
157
|
+
case 'Point': {
|
|
158
|
+
const [x, y] = projectPosition(geometry.coordinates, bounds);
|
|
159
|
+
const circle = createSvgElement('circle');
|
|
160
|
+
circle.setAttribute('cx', String(x));
|
|
161
|
+
circle.setAttribute('cy', String(y));
|
|
162
|
+
circle.setAttribute('r', '4');
|
|
163
|
+
parent.appendChild(circle);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case 'MultiPoint':
|
|
167
|
+
geometry.coordinates.forEach(position => appendGeometry(parent, { type: 'Point', coordinates: position }, bounds));
|
|
168
|
+
break;
|
|
169
|
+
case 'LineString': {
|
|
170
|
+
const path = createSvgElement('path');
|
|
171
|
+
path.setAttribute('d', pathLine(geometry.coordinates, bounds));
|
|
172
|
+
parent.appendChild(path);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'MultiLineString':
|
|
176
|
+
geometry.coordinates.forEach(line => appendGeometry(parent, { type: 'LineString', coordinates: line }, bounds));
|
|
177
|
+
break;
|
|
178
|
+
case 'Polygon':
|
|
179
|
+
geometry.coordinates.forEach(line => {
|
|
180
|
+
const path = createSvgElement('path');
|
|
181
|
+
path.classList.add('geo-polygon');
|
|
182
|
+
path.setAttribute('d', pathLine(line, bounds, true));
|
|
183
|
+
parent.appendChild(path);
|
|
184
|
+
});
|
|
185
|
+
break;
|
|
186
|
+
case 'MultiPolygon':
|
|
187
|
+
geometry.coordinates.forEach(polygon => {
|
|
188
|
+
appendGeometry(parent, { type: 'Polygon', coordinates: polygon }, bounds);
|
|
189
|
+
});
|
|
190
|
+
break;
|
|
191
|
+
case 'GeometryCollection':
|
|
192
|
+
geometry.geometries.forEach(item => appendGeometry(parent, item, bounds));
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const buildGeometryCounts = (features) => {
|
|
197
|
+
const counts = new Map();
|
|
198
|
+
features.forEach(feature => {
|
|
199
|
+
var _a;
|
|
200
|
+
const key = ((_a = feature.geometry) === null || _a === void 0 ? void 0 : _a.type) || 'Null';
|
|
201
|
+
counts.set(key, (counts.get(key) || 0) + 1);
|
|
202
|
+
});
|
|
203
|
+
return [...counts.entries()];
|
|
204
|
+
};
|
|
205
|
+
const formatBounds = (bounds) => {
|
|
206
|
+
if (!bounds) {
|
|
207
|
+
return '-';
|
|
208
|
+
}
|
|
209
|
+
return `${bounds.minX.toFixed(5)}, ${bounds.minY.toFixed(5)} -> ${bounds.maxX.toFixed(5)}, ${bounds.maxY.toFixed(5)}`;
|
|
210
|
+
};
|
|
211
|
+
const appendDescriptionItem = (list, label, value) => {
|
|
212
|
+
const row = document.createElement('div');
|
|
213
|
+
const term = document.createElement('dt');
|
|
214
|
+
term.textContent = label;
|
|
215
|
+
const detail = document.createElement('dd');
|
|
216
|
+
detail.textContent = String(value);
|
|
217
|
+
row.append(term, detail);
|
|
218
|
+
list.appendChild(row);
|
|
219
|
+
};
|
|
220
|
+
const renderCollection = (collection, type) => {
|
|
221
|
+
const root = document.createElement('div');
|
|
222
|
+
root.className = 'geo-viewer';
|
|
223
|
+
const features = collection.features || [];
|
|
224
|
+
const bounds = collectBounds(features);
|
|
225
|
+
const panel = document.createElement('aside');
|
|
226
|
+
panel.className = 'geo-panel';
|
|
227
|
+
const badge = document.createElement('span');
|
|
228
|
+
badge.textContent = type.toUpperCase();
|
|
229
|
+
const heading = document.createElement('h2');
|
|
230
|
+
heading.textContent = '地理数据预览';
|
|
231
|
+
const description = document.createElement('dl');
|
|
232
|
+
appendDescriptionItem(description, '要素数', features.length);
|
|
233
|
+
appendDescriptionItem(description, '范围', formatBounds(bounds));
|
|
234
|
+
const counts = document.createElement('div');
|
|
235
|
+
counts.className = 'geo-counts';
|
|
236
|
+
const countsHeading = document.createElement('strong');
|
|
237
|
+
countsHeading.textContent = '几何类型';
|
|
238
|
+
counts.appendChild(countsHeading);
|
|
239
|
+
buildGeometryCounts(features).forEach(([name, count]) => {
|
|
240
|
+
const row = document.createElement('p');
|
|
241
|
+
row.textContent = `${name}: ${count}`;
|
|
242
|
+
counts.appendChild(row);
|
|
243
|
+
});
|
|
244
|
+
panel.append(badge, heading, description, counts);
|
|
245
|
+
const map = document.createElement('main');
|
|
246
|
+
map.className = 'geo-map';
|
|
247
|
+
const svg = createSvgElement('svg');
|
|
248
|
+
svg.setAttribute('viewBox', `0 0 ${GEO_WIDTH} ${GEO_HEIGHT}`);
|
|
249
|
+
svg.setAttribute('role', 'img');
|
|
250
|
+
svg.setAttribute('aria-label', '地理数据 SVG 预览');
|
|
251
|
+
const rect = createSvgElement('rect');
|
|
252
|
+
rect.setAttribute('width', String(GEO_WIDTH));
|
|
253
|
+
rect.setAttribute('height', String(GEO_HEIGHT));
|
|
254
|
+
rect.setAttribute('rx', '8');
|
|
255
|
+
const group = createSvgElement('g');
|
|
256
|
+
features.forEach(feature => appendGeometry(group, feature.geometry, bounds));
|
|
257
|
+
svg.append(rect, group);
|
|
258
|
+
map.appendChild(svg);
|
|
259
|
+
root.append(panel, map);
|
|
260
|
+
return root;
|
|
261
|
+
};
|
|
262
|
+
export default async function renderGeo(buffer, target, type) {
|
|
263
|
+
const normalizedType = normalizeGeoType(type);
|
|
264
|
+
const style = createStyle();
|
|
265
|
+
const state = document.createElement('div');
|
|
266
|
+
state.className = 'geo-state';
|
|
267
|
+
state.textContent = '正在解析地理数据...';
|
|
268
|
+
target.replaceChildren(style, state);
|
|
269
|
+
try {
|
|
270
|
+
const collection = await parseGeo(buffer, normalizedType);
|
|
271
|
+
target.replaceChildren(style, renderCollection(collection, normalizedType));
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
console.error(error);
|
|
275
|
+
state.classList.add('error');
|
|
276
|
+
state.textContent = error instanceof Error ? error.message : String(error);
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
$el: target,
|
|
280
|
+
unmount() {
|
|
281
|
+
target.replaceChildren();
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type FileRenderHandler, type FileViewerRenderedInstance, type FileViewerRendererPlugin, type RendererDefinition } from '@file-viewer/core';
|
|
2
|
+
export declare const geoRendererDefinition: RendererDefinition;
|
|
3
|
+
export declare const renderFileViewerGeo: FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>;
|
|
4
|
+
export declare const geoRenderer: FileViewerRendererPlugin<FileRenderHandler<FileViewerRenderedInstance, HTMLDivElement>>;
|
|
5
|
+
export default geoRenderer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DEFAULT_RENDERER_DEFINITIONS, } from '@file-viewer/core';
|
|
2
|
+
const geoDefinition = DEFAULT_RENDERER_DEFINITIONS.find(definition => definition.id === 'geo');
|
|
3
|
+
if (!geoDefinition) {
|
|
4
|
+
throw new Error('@file-viewer/renderer-geo could not locate the core geospatial renderer definition.');
|
|
5
|
+
}
|
|
6
|
+
export const geoRendererDefinition = geoDefinition;
|
|
7
|
+
export const renderFileViewerGeo = (buffer, target, type) => import('./geo.js').then(({ default: renderGeo }) => renderGeo(buffer, target, type));
|
|
8
|
+
export const geoRenderer = {
|
|
9
|
+
id: 'file-viewer-renderer-geo',
|
|
10
|
+
label: 'Flyfish File Viewer geospatial renderer',
|
|
11
|
+
definitions: [geoRendererDefinition],
|
|
12
|
+
handlers: [{
|
|
13
|
+
rendererId: geoRendererDefinition.id,
|
|
14
|
+
handler: renderFileViewerGeo,
|
|
15
|
+
}],
|
|
16
|
+
};
|
|
17
|
+
export default geoRenderer;
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@file-viewer/renderer-geo",
|
|
3
|
+
"version": "2.0.11",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Standalone geospatial renderer plugin for Flyfish File Viewer with GeoJSON, KML, GPX, and SHP parsing, SVG map preview, and offline-friendly lazy loading.",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"file-viewer",
|
|
9
|
+
"renderer",
|
|
10
|
+
"geojson",
|
|
11
|
+
"kml",
|
|
12
|
+
"gpx",
|
|
13
|
+
"shapefile",
|
|
14
|
+
"document-preview",
|
|
15
|
+
"document-viewer",
|
|
16
|
+
"file-preview",
|
|
17
|
+
"self-hosted"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"registry": "https://registry.npmjs.org/"
|
|
22
|
+
},
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Wangyu",
|
|
25
|
+
"email": "wybaby168@gmail.com"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/flyfish-dev/file-viewer-renderer-geo.git",
|
|
30
|
+
"directory": "packages/renderers/geo"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://doc.file-viewer.app/guide/on-demand-renderers",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/flyfish-dev/file-viewer-renderer-geo/issues"
|
|
35
|
+
},
|
|
36
|
+
"funding": {
|
|
37
|
+
"type": "individual",
|
|
38
|
+
"url": "https://dev.flyfish.group/shop"
|
|
39
|
+
},
|
|
40
|
+
"main": "./dist/index.js",
|
|
41
|
+
"module": "./dist/index.js",
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"import": "./dist/index.js",
|
|
47
|
+
"default": "./dist/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./package.json": "./package.json"
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"README.md",
|
|
54
|
+
"README.en.md",
|
|
55
|
+
"LICENSE"
|
|
56
|
+
],
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"@file-viewer/core": "^2.0.11",
|
|
59
|
+
"@tmcw/togeojson": "^7.1.2",
|
|
60
|
+
"shpjs": "^6.2.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"typescript": "^6.0.3"
|
|
64
|
+
},
|
|
65
|
+
"license": "Apache-2.0",
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsc -b tsconfig.json",
|
|
68
|
+
"type-check": "tsc -b tsconfig.json"
|
|
69
|
+
}
|
|
70
|
+
}
|