maplibre-draw-polygon 0.0.1
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 +201 -0
- package/README.md +20 -0
- package/dist/PolygonControls.svelte +51 -0
- package/dist/PolygonControls.svelte.d.ts +7 -0
- package/dist/PolygonToolLayer.svelte +40 -0
- package/dist/PolygonToolLayer.svelte.d.ts +18 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/polygon_tool.d.ts +39 -0
- package/dist/polygon_tool.js +343 -0
- package/dist/stores.d.ts +4 -0
- package/dist/stores.js +5 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.js +36 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# maplibre-draw-polygon
|
|
2
|
+
|
|
3
|
+
A plugin to draw freehand polygons with MapLibre GL JS
|
|
4
|
+
|
|
5
|
+
TODO before publishing to NPM:
|
|
6
|
+
|
|
7
|
+
- example page
|
|
8
|
+
- clean up unused sveltekit boilerplate
|
|
9
|
+
- use peer dependencies?
|
|
10
|
+
- where should ctrl+z support live?
|
|
11
|
+
- layer IDs and zorder configurable?
|
|
12
|
+
- docs
|
|
13
|
+
|
|
14
|
+
Used in:
|
|
15
|
+
|
|
16
|
+
- osm2streets Street Explorer
|
|
17
|
+
- Severance Snape
|
|
18
|
+
- the LTN tool
|
|
19
|
+
- od2net
|
|
20
|
+
- TODO: ATIP, inspectorate tools
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { PolygonTool } from "./polygon_tool.js";
|
|
3
|
+
import { undoLength } from "./stores.js";
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
polygonTool: PolygonTool;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let { polygonTool }: Props = $props();
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<div style="display: flex; justify-content: space-between;">
|
|
13
|
+
<button onclick={() => polygonTool.finish()}>Finish</button>
|
|
14
|
+
<button onclick={() => polygonTool.cancel()}>Cancel</button>
|
|
15
|
+
<button disabled={$undoLength == 0} onclick={() => polygonTool.undo()}>
|
|
16
|
+
{#if $undoLength == 0}
|
|
17
|
+
Undo
|
|
18
|
+
{:else}
|
|
19
|
+
Undo ({$undoLength})
|
|
20
|
+
{/if}
|
|
21
|
+
</button>
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
<ul>
|
|
25
|
+
<li>
|
|
26
|
+
<b>Click</b>
|
|
27
|
+
the map to add a vertex
|
|
28
|
+
</li>
|
|
29
|
+
<li>
|
|
30
|
+
<b>Click</b>
|
|
31
|
+
a vertex to delete it
|
|
32
|
+
</li>
|
|
33
|
+
<li>
|
|
34
|
+
<b>Drag</b>
|
|
35
|
+
a vertex or the polygon to move it
|
|
36
|
+
</li>
|
|
37
|
+
<li>
|
|
38
|
+
Press <b>Control+Z</b>
|
|
39
|
+
to undo your last change
|
|
40
|
+
</li>
|
|
41
|
+
<li>
|
|
42
|
+
Press <b>Enter</b>
|
|
43
|
+
or
|
|
44
|
+
<b>double click</b>
|
|
45
|
+
to finish
|
|
46
|
+
</li>
|
|
47
|
+
<li>
|
|
48
|
+
Press <b>Escape</b>
|
|
49
|
+
to cancel
|
|
50
|
+
</li>
|
|
51
|
+
</ul>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PolygonTool } from "./polygon_tool.js";
|
|
2
|
+
interface Props {
|
|
3
|
+
polygonTool: PolygonTool;
|
|
4
|
+
}
|
|
5
|
+
declare const PolygonControls: import("svelte").Component<Props, {}, "">;
|
|
6
|
+
type PolygonControls = ReturnType<typeof PolygonControls>;
|
|
7
|
+
export default PolygonControls;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { CircleLayer, FillLayer, GeoJSON, LineLayer } from "svelte-maplibre";
|
|
3
|
+
import { isLine, isPoint, isPolygon } from "./utils.js";
|
|
4
|
+
import { polygonToolGj } from "./stores.js";
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<GeoJSON data={$polygonToolGj}>
|
|
8
|
+
<FillLayer
|
|
9
|
+
id="edit-polygon-fill"
|
|
10
|
+
filter={isPolygon}
|
|
11
|
+
paint={{
|
|
12
|
+
"fill-color": "red",
|
|
13
|
+
"fill-opacity": [
|
|
14
|
+
"case",
|
|
15
|
+
["boolean", ["get", "hover"], "false"],
|
|
16
|
+
1.0,
|
|
17
|
+
0.5,
|
|
18
|
+
],
|
|
19
|
+
}}
|
|
20
|
+
/>
|
|
21
|
+
<LineLayer
|
|
22
|
+
id="edit-polygon-lines"
|
|
23
|
+
filter={isLine}
|
|
24
|
+
paint={{
|
|
25
|
+
// TODO Dashed
|
|
26
|
+
"line-color": "black",
|
|
27
|
+
"line-width": 8,
|
|
28
|
+
"line-opacity": 0.5,
|
|
29
|
+
}}
|
|
30
|
+
/>
|
|
31
|
+
<CircleLayer
|
|
32
|
+
id="edit-polygon-vertices"
|
|
33
|
+
filter={isPoint}
|
|
34
|
+
paint={{
|
|
35
|
+
"circle-color": "black",
|
|
36
|
+
"circle-opacity": ["case", ["has", "hovered"], 1.0, 0.5],
|
|
37
|
+
"circle-radius": 10,
|
|
38
|
+
}}
|
|
39
|
+
/>
|
|
40
|
+
</GeoJSON>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
2
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
3
|
+
$$bindings?: Bindings;
|
|
4
|
+
} & Exports;
|
|
5
|
+
(internal: unknown, props: {
|
|
6
|
+
$$events?: Events;
|
|
7
|
+
$$slots?: Slots;
|
|
8
|
+
}): Exports & {
|
|
9
|
+
$set?: any;
|
|
10
|
+
$on?: any;
|
|
11
|
+
};
|
|
12
|
+
z_$$bindings?: Bindings;
|
|
13
|
+
}
|
|
14
|
+
declare const PolygonToolLayer: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
|
15
|
+
[evt: string]: CustomEvent<any>;
|
|
16
|
+
}, {}, {}, string>;
|
|
17
|
+
type PolygonToolLayer = InstanceType<typeof PolygonToolLayer>;
|
|
18
|
+
export default PolygonToolLayer;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Feature, Point, Polygon, Position } from "geojson";
|
|
2
|
+
import type { Map, MapMouseEvent } from "maplibre-gl";
|
|
3
|
+
import { type FeatureWithProps } from "./utils.js";
|
|
4
|
+
export declare class PolygonTool {
|
|
5
|
+
map: Map;
|
|
6
|
+
active: boolean;
|
|
7
|
+
eventListenersSuccess: ((f: FeatureWithProps<Polygon>) => void)[];
|
|
8
|
+
eventListenersUpdated: ((f: FeatureWithProps<Polygon>) => void)[];
|
|
9
|
+
eventListenersFailure: (() => void)[];
|
|
10
|
+
points: Position[];
|
|
11
|
+
cursor: Feature<Point> | null;
|
|
12
|
+
hover: "polygon" | number | null;
|
|
13
|
+
dragFrom: Position | null;
|
|
14
|
+
previousStates: Position[][];
|
|
15
|
+
constructor(map: Map);
|
|
16
|
+
tearDown(): void;
|
|
17
|
+
finish(): void;
|
|
18
|
+
cancel(): void;
|
|
19
|
+
onMouseMove: (e: MapMouseEvent) => void;
|
|
20
|
+
onClick: (e: MapMouseEvent) => void;
|
|
21
|
+
onDoubleClick: (e: MapMouseEvent) => void;
|
|
22
|
+
onMouseDown: (e: MapMouseEvent) => void;
|
|
23
|
+
onMouseUp: () => void;
|
|
24
|
+
onKeypress: (e: KeyboardEvent) => void;
|
|
25
|
+
onKeyDown: (e: KeyboardEvent) => void;
|
|
26
|
+
addEventListenerSuccess(callback: (f: FeatureWithProps<Polygon>) => void): void;
|
|
27
|
+
addEventListenerUpdated(callback: (f: FeatureWithProps<Polygon>) => void): void;
|
|
28
|
+
addEventListenerFailure(callback: () => void): void;
|
|
29
|
+
clearEventListeners(): void;
|
|
30
|
+
startNew(): void;
|
|
31
|
+
editExisting(feature: Feature<Polygon>): void;
|
|
32
|
+
stop(): void;
|
|
33
|
+
undo(): void;
|
|
34
|
+
private redraw;
|
|
35
|
+
private pointsUpdated;
|
|
36
|
+
private recalculateHovering;
|
|
37
|
+
private polygonFeature;
|
|
38
|
+
private beforeUpdate;
|
|
39
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import nearestPointOnLine from "@turf/nearest-point-on-line";
|
|
2
|
+
import { emptyGeojson, pointFeature, setPrecision, } from "./utils.js";
|
|
3
|
+
import { polygonToolGj, undoLength } from "./stores.js";
|
|
4
|
+
const maxPreviousStates = 100;
|
|
5
|
+
export class PolygonTool {
|
|
6
|
+
map;
|
|
7
|
+
active;
|
|
8
|
+
eventListenersSuccess;
|
|
9
|
+
eventListenersUpdated;
|
|
10
|
+
eventListenersFailure;
|
|
11
|
+
points;
|
|
12
|
+
cursor;
|
|
13
|
+
// The number is an index into points
|
|
14
|
+
hover;
|
|
15
|
+
dragFrom;
|
|
16
|
+
// Storing a full copy of the previous points is sufficient
|
|
17
|
+
previousStates;
|
|
18
|
+
// TODO Inconsistent ordering with point tool
|
|
19
|
+
constructor(map) {
|
|
20
|
+
this.map = map;
|
|
21
|
+
this.active = false;
|
|
22
|
+
this.eventListenersSuccess = [];
|
|
23
|
+
this.eventListenersUpdated = [];
|
|
24
|
+
this.eventListenersFailure = [];
|
|
25
|
+
// This doesn't repeat the first point at the end; it's not closed
|
|
26
|
+
this.points = [];
|
|
27
|
+
this.cursor = null;
|
|
28
|
+
// TODO This is lots of state. Consider
|
|
29
|
+
// https://maplibre.org/maplibre-gl-js-docs/example/drag-a-point/ or port
|
|
30
|
+
// widgetry's World
|
|
31
|
+
this.hover = null;
|
|
32
|
+
this.dragFrom = null;
|
|
33
|
+
this.previousStates = [];
|
|
34
|
+
this.map.on("mousemove", this.onMouseMove);
|
|
35
|
+
this.map.on("click", this.onClick);
|
|
36
|
+
this.map.on("dblclick", this.onDoubleClick);
|
|
37
|
+
this.map.on("mousedown", this.onMouseDown);
|
|
38
|
+
this.map.on("mouseup", this.onMouseUp);
|
|
39
|
+
document.addEventListener("keypress", this.onKeypress);
|
|
40
|
+
document.addEventListener("keydown", this.onKeyDown);
|
|
41
|
+
}
|
|
42
|
+
tearDown() {
|
|
43
|
+
this.map.off("mousemove", this.onMouseMove);
|
|
44
|
+
this.map.off("click", this.onClick);
|
|
45
|
+
this.map.off("dblclick", this.onDoubleClick);
|
|
46
|
+
this.map.off("mousedown", this.onMouseDown);
|
|
47
|
+
this.map.off("mouseup", this.onMouseUp);
|
|
48
|
+
document.removeEventListener("keypress", this.onKeypress);
|
|
49
|
+
document.removeEventListener("keydown", this.onKeyDown);
|
|
50
|
+
}
|
|
51
|
+
// Either a success or failure event will happen, depending on current state
|
|
52
|
+
finish() {
|
|
53
|
+
let polygon = this.polygonFeature();
|
|
54
|
+
if (polygon) {
|
|
55
|
+
// TODO RouteTool passes a copy to each callback for paranoia. Should we
|
|
56
|
+
// do the same everywhere here?
|
|
57
|
+
for (let cb of this.eventListenersSuccess) {
|
|
58
|
+
cb(polygon);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
for (let cb of this.eventListenersFailure) {
|
|
63
|
+
cb();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
this.stop();
|
|
67
|
+
}
|
|
68
|
+
// This stops the tool and fires a failure event
|
|
69
|
+
cancel() {
|
|
70
|
+
for (let cb of this.eventListenersFailure) {
|
|
71
|
+
cb();
|
|
72
|
+
}
|
|
73
|
+
this.stop();
|
|
74
|
+
}
|
|
75
|
+
onMouseMove = (e) => {
|
|
76
|
+
// Don't call beforeUpdate here; just consider drag as one entire action
|
|
77
|
+
if (this.active && !this.dragFrom) {
|
|
78
|
+
this.recalculateHovering(e);
|
|
79
|
+
}
|
|
80
|
+
else if (this.active && this.dragFrom) {
|
|
81
|
+
if (this.hover == "polygon") {
|
|
82
|
+
// Move entire polygon
|
|
83
|
+
let dx = this.dragFrom[0] - e.lngLat.lng;
|
|
84
|
+
let dy = this.dragFrom[1] - e.lngLat.lat;
|
|
85
|
+
for (let pt of this.points) {
|
|
86
|
+
pt[0] -= dx;
|
|
87
|
+
pt[1] -= dy;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
this.points[this.hover] = e.lngLat.toArray();
|
|
92
|
+
}
|
|
93
|
+
this.dragFrom = e.lngLat.toArray();
|
|
94
|
+
this.redraw();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
onClick = (e) => {
|
|
98
|
+
this.beforeUpdate();
|
|
99
|
+
if (this.active && this.cursor) {
|
|
100
|
+
// Insert the new point in the "middle" of the closest line segment
|
|
101
|
+
let candidates = [];
|
|
102
|
+
pointsToLineSegments(this.points).forEach((line, idx) => {
|
|
103
|
+
candidates.push([
|
|
104
|
+
idx + 1,
|
|
105
|
+
nearestPointOnLine(line, this.cursor).properties.dist,
|
|
106
|
+
]);
|
|
107
|
+
});
|
|
108
|
+
candidates.sort((a, b) => a[1] - b[1]);
|
|
109
|
+
if (candidates.length > 0) {
|
|
110
|
+
let idx = candidates[0][0];
|
|
111
|
+
this.points.splice(idx, 0, this.cursor.geometry.coordinates);
|
|
112
|
+
this.hover = idx;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
this.points.push(this.cursor.geometry.coordinates);
|
|
116
|
+
this.hover = this.points.length - 1;
|
|
117
|
+
}
|
|
118
|
+
this.redraw();
|
|
119
|
+
this.pointsUpdated();
|
|
120
|
+
}
|
|
121
|
+
else if (this.active && typeof this.hover === "number") {
|
|
122
|
+
this.points.splice(this.hover, 1);
|
|
123
|
+
this.hover = null;
|
|
124
|
+
this.redraw();
|
|
125
|
+
this.pointsUpdated();
|
|
126
|
+
// TODO Doesn't seem to work; you still have to move the mouse to hover
|
|
127
|
+
// on the polygon
|
|
128
|
+
this.recalculateHovering(e);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
onDoubleClick = (e) => {
|
|
132
|
+
if (!this.active) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
// When we finish, we'll re-enable doubleClickZoom, but we don't want this to zoom in
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
// Double clicks happen as [click, click, dblclick]. The first click adds a
|
|
138
|
+
// point, the second immediately deletes it, and so we simulate a third
|
|
139
|
+
// click to add it again.
|
|
140
|
+
// TODO But since the delete case currently doesn't set cursor during recalculateHovering, do this hack
|
|
141
|
+
this.cursor = pointFeature(e.lngLat.toArray());
|
|
142
|
+
this.onClick(e);
|
|
143
|
+
this.finish();
|
|
144
|
+
};
|
|
145
|
+
onMouseDown = (e) => {
|
|
146
|
+
if (this.active && !this.dragFrom && this.hover != null) {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
this.cursor = null;
|
|
149
|
+
this.dragFrom = e.lngLat.toArray();
|
|
150
|
+
// TODO If no drag actually happens, this'll record a useless edit
|
|
151
|
+
this.beforeUpdate();
|
|
152
|
+
this.redraw();
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
onMouseUp = () => {
|
|
156
|
+
if (this.active && this.dragFrom) {
|
|
157
|
+
this.dragFrom = null;
|
|
158
|
+
this.redraw();
|
|
159
|
+
this.pointsUpdated();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
onKeypress = (e) => {
|
|
163
|
+
if (!this.active) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
let tag = e.target.tagName;
|
|
167
|
+
// Let keys key work if the user is focused on a form
|
|
168
|
+
if (tag == "INPUT" || tag == "TEXTAREA") {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (e.key == "Enter") {
|
|
172
|
+
e.stopPropagation();
|
|
173
|
+
this.finish();
|
|
174
|
+
}
|
|
175
|
+
else if (e.key == "z" && e.ctrlKey) {
|
|
176
|
+
this.undo();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
onKeyDown = (e) => {
|
|
180
|
+
if (!this.active) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
let tag = e.target.tagName;
|
|
184
|
+
// Let keys key work if the user is focused on a form
|
|
185
|
+
if (tag == "INPUT" || tag == "TEXTAREA") {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (e.key == "Escape") {
|
|
189
|
+
e.stopPropagation();
|
|
190
|
+
this.cancel();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
addEventListenerSuccess(callback) {
|
|
194
|
+
this.eventListenersSuccess.push(callback);
|
|
195
|
+
}
|
|
196
|
+
addEventListenerUpdated(callback) {
|
|
197
|
+
this.eventListenersUpdated.push(callback);
|
|
198
|
+
}
|
|
199
|
+
addEventListenerFailure(callback) {
|
|
200
|
+
this.eventListenersFailure.push(callback);
|
|
201
|
+
}
|
|
202
|
+
clearEventListeners() {
|
|
203
|
+
this.eventListenersSuccess = [];
|
|
204
|
+
this.eventListenersUpdated = [];
|
|
205
|
+
this.eventListenersFailure = [];
|
|
206
|
+
}
|
|
207
|
+
startNew() {
|
|
208
|
+
this.active = true;
|
|
209
|
+
// Otherwise, double clicking to finish breaks
|
|
210
|
+
this.map.doubleClickZoom.disable();
|
|
211
|
+
}
|
|
212
|
+
editExisting(feature) {
|
|
213
|
+
this.active = true;
|
|
214
|
+
this.map.doubleClickZoom.disable();
|
|
215
|
+
this.points = JSON.parse(JSON.stringify(feature.geometry.coordinates[0]));
|
|
216
|
+
this.points.pop();
|
|
217
|
+
this.redraw();
|
|
218
|
+
// TODO recalculateHovering, but we need to know where the mouse is
|
|
219
|
+
}
|
|
220
|
+
stop() {
|
|
221
|
+
this.map.doubleClickZoom.enable();
|
|
222
|
+
this.points = [];
|
|
223
|
+
this.cursor = null;
|
|
224
|
+
this.active = false;
|
|
225
|
+
this.hover = null;
|
|
226
|
+
this.dragFrom = null;
|
|
227
|
+
this.previousStates = [];
|
|
228
|
+
this.redraw();
|
|
229
|
+
this.map.getCanvas().style.cursor = "inherit";
|
|
230
|
+
}
|
|
231
|
+
undo() {
|
|
232
|
+
if (this.dragFrom != null || this.previousStates.length == 0) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
this.points = this.previousStates.pop();
|
|
236
|
+
this.hover = null;
|
|
237
|
+
this.redraw();
|
|
238
|
+
}
|
|
239
|
+
redraw() {
|
|
240
|
+
let gj = emptyGeojson();
|
|
241
|
+
this.points.forEach((pt, idx) => {
|
|
242
|
+
let f = pointFeature(pt);
|
|
243
|
+
f.properties.hover = this.hover == idx;
|
|
244
|
+
f.properties.idx = idx;
|
|
245
|
+
gj.features.push(f);
|
|
246
|
+
});
|
|
247
|
+
gj.features = gj.features.concat(pointsToLineSegments(this.points));
|
|
248
|
+
let polygon = this.polygonFeature();
|
|
249
|
+
if (polygon) {
|
|
250
|
+
polygon.properties.hover = this.hover == "polygon";
|
|
251
|
+
gj.features.push(polygon);
|
|
252
|
+
}
|
|
253
|
+
polygonToolGj.set(gj);
|
|
254
|
+
let cursorStyle = "crosshair";
|
|
255
|
+
if (this.hover != null) {
|
|
256
|
+
cursorStyle = this.dragFrom ? "grabbing" : "pointer";
|
|
257
|
+
}
|
|
258
|
+
this.map.getCanvas().style.cursor = cursorStyle;
|
|
259
|
+
undoLength.set(this.previousStates.length);
|
|
260
|
+
}
|
|
261
|
+
// If there's a valid polygon, also passes to eventListenersUpdated
|
|
262
|
+
pointsUpdated() {
|
|
263
|
+
let polygon = this.polygonFeature();
|
|
264
|
+
if (polygon) {
|
|
265
|
+
for (let cb of this.eventListenersUpdated) {
|
|
266
|
+
cb(polygon);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
recalculateHovering(e) {
|
|
271
|
+
this.cursor = null;
|
|
272
|
+
this.hover = null;
|
|
273
|
+
// Order of the layers matters!
|
|
274
|
+
for (let f of this.map.queryRenderedFeatures(e.point, {
|
|
275
|
+
layers: ["edit-polygon-fill", "edit-polygon-vertices"],
|
|
276
|
+
})) {
|
|
277
|
+
if (f.geometry.type == "Polygon") {
|
|
278
|
+
this.hover = "polygon";
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
else if (f.geometry.type == "Point") {
|
|
282
|
+
// Ignore the cursor
|
|
283
|
+
if (Object.hasOwn(f.properties, "idx")) {
|
|
284
|
+
this.hover = f.properties.idx;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (this.hover == null) {
|
|
290
|
+
this.cursor = pointFeature(e.lngLat.toArray());
|
|
291
|
+
}
|
|
292
|
+
this.redraw();
|
|
293
|
+
}
|
|
294
|
+
// TODO Force the proper winding order that geojson requires
|
|
295
|
+
polygonFeature() {
|
|
296
|
+
if (this.points.length < 3) {
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
let trimmed = this.points.map(setPrecision);
|
|
300
|
+
// Deep clone here, or face the wrath of crazy bugs later!
|
|
301
|
+
let coordinates = [JSON.parse(JSON.stringify(trimmed))];
|
|
302
|
+
coordinates[0].push(JSON.parse(JSON.stringify(coordinates[0][0])));
|
|
303
|
+
return {
|
|
304
|
+
type: "Feature",
|
|
305
|
+
geometry: {
|
|
306
|
+
type: "Polygon",
|
|
307
|
+
coordinates,
|
|
308
|
+
},
|
|
309
|
+
properties: {},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
beforeUpdate() {
|
|
313
|
+
this.previousStates.push(JSON.parse(JSON.stringify(this.points)));
|
|
314
|
+
if (this.previousStates.length > maxPreviousStates) {
|
|
315
|
+
this.previousStates.shift();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// Includes the line connecting the last to the first point
|
|
320
|
+
function pointsToLineSegments(points) {
|
|
321
|
+
let lines = [];
|
|
322
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
323
|
+
lines.push({
|
|
324
|
+
type: "Feature",
|
|
325
|
+
geometry: {
|
|
326
|
+
type: "LineString",
|
|
327
|
+
coordinates: [points[i], points[i + 1]],
|
|
328
|
+
},
|
|
329
|
+
properties: {},
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
if (points.length >= 3) {
|
|
333
|
+
lines.push({
|
|
334
|
+
type: "Feature",
|
|
335
|
+
geometry: {
|
|
336
|
+
type: "LineString",
|
|
337
|
+
coordinates: [points[points.length - 1], points[0]],
|
|
338
|
+
},
|
|
339
|
+
properties: {},
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
return lines;
|
|
343
|
+
}
|
package/dist/stores.d.ts
ADDED
package/dist/stores.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { emptyGeojson } from "./utils.js";
|
|
3
|
+
// These are necessary to communicate between components nested under the sidebar and map
|
|
4
|
+
export const polygonToolGj = writable(emptyGeojson());
|
|
5
|
+
export const undoLength = writable(0);
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Feature, FeatureCollection, Point, Geometry, Position } from "geojson";
|
|
2
|
+
import type { ExpressionSpecification } from "maplibre-gl";
|
|
3
|
+
export declare const isPolygon: ExpressionSpecification;
|
|
4
|
+
export declare const isLine: ExpressionSpecification;
|
|
5
|
+
export declare const isPoint: ExpressionSpecification;
|
|
6
|
+
export declare function emptyGeojson(): FeatureCollection;
|
|
7
|
+
export declare function pointFeature(pt: Position): Feature<Point>;
|
|
8
|
+
export declare function setPrecision(pt: Position): Position;
|
|
9
|
+
export type FeatureWithProps<G extends Geometry> = Feature<G> & {
|
|
10
|
+
properties: {
|
|
11
|
+
[name: string]: any;
|
|
12
|
+
};
|
|
13
|
+
};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const isPolygon = [
|
|
2
|
+
"==",
|
|
3
|
+
["geometry-type"],
|
|
4
|
+
"Polygon",
|
|
5
|
+
];
|
|
6
|
+
export const isLine = [
|
|
7
|
+
"==",
|
|
8
|
+
["geometry-type"],
|
|
9
|
+
"LineString",
|
|
10
|
+
];
|
|
11
|
+
export const isPoint = [
|
|
12
|
+
"==",
|
|
13
|
+
["geometry-type"],
|
|
14
|
+
"Point",
|
|
15
|
+
];
|
|
16
|
+
export function emptyGeojson() {
|
|
17
|
+
return {
|
|
18
|
+
type: "FeatureCollection",
|
|
19
|
+
features: [],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function pointFeature(pt) {
|
|
23
|
+
return {
|
|
24
|
+
type: "Feature",
|
|
25
|
+
properties: {},
|
|
26
|
+
geometry: {
|
|
27
|
+
type: "Point",
|
|
28
|
+
coordinates: setPrecision(pt),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// Per https://datatracker.ietf.org/doc/html/rfc7946#section-11.2, 6 decimal
|
|
33
|
+
// places (10cm) is plenty of precision
|
|
34
|
+
export function setPrecision(pt) {
|
|
35
|
+
return [Math.round(pt[0] * 10e6) / 10e6, Math.round(pt[1] * 10e6) / 10e6];
|
|
36
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "maplibre-draw-polygon",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"dev": "vite dev",
|
|
6
|
+
"build": "vite build && npm run package",
|
|
7
|
+
"preview": "vite preview",
|
|
8
|
+
"package": "svelte-kit sync && svelte-package && publint",
|
|
9
|
+
"prepare": "npm run package",
|
|
10
|
+
"prepublishOnly": "npm run package",
|
|
11
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
12
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
13
|
+
"lint": "prettier --check . && eslint .",
|
|
14
|
+
"format": "prettier --write ."
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"svelte": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"!dist/**/*.test.*",
|
|
25
|
+
"!dist/**/*.spec.*"
|
|
26
|
+
],
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"svelte": "^5.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@sveltejs/adapter-auto": "^6.0.1",
|
|
32
|
+
"@sveltejs/kit": "^2.5.27",
|
|
33
|
+
"@sveltejs/package": "^2.3.7",
|
|
34
|
+
"@sveltejs/vite-plugin-svelte": "^6.1.0",
|
|
35
|
+
"@types/eslint": "^9.6.1",
|
|
36
|
+
"@types/geojson": "^7946.0.14",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
38
|
+
"@typescript-eslint/parser": "^8.38.0",
|
|
39
|
+
"eslint": "^9.31.0",
|
|
40
|
+
"eslint-config-prettier": "^10.1.8",
|
|
41
|
+
"eslint-plugin-svelte": "^3.11.0",
|
|
42
|
+
"prettier": "^3.1.1",
|
|
43
|
+
"prettier-plugin-svelte": "^3.2.6",
|
|
44
|
+
"publint": "^0.3.12",
|
|
45
|
+
"svelte": "^5.0.0",
|
|
46
|
+
"svelte-check": "^4.0.0",
|
|
47
|
+
"tslib": "^2.4.1",
|
|
48
|
+
"typescript": "^5.5.0",
|
|
49
|
+
"vite": "^7.0.6"
|
|
50
|
+
},
|
|
51
|
+
"svelte": "./dist/index.js",
|
|
52
|
+
"types": "./dist/index.d.ts",
|
|
53
|
+
"type": "module",
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@turf/nearest-point-on-line": "^7.2.0",
|
|
56
|
+
"svelte-maplibre": "^1.2.0"
|
|
57
|
+
}
|
|
58
|
+
}
|