solid-map-gl 1.11.2 → 1.12.0

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.
Files changed (31) hide show
  1. package/dist/esm/index.js +1 -1
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/source/components/Draw/drawingStyles.js +225 -0
  4. package/dist/source/components/Draw/index.jsx +35 -8
  5. package/dist/source/components/Draw/modes/index.js +9 -0
  6. package/dist/source/components/Draw/modes/line_string.js +43 -0
  7. package/dist/source/components/Draw/modes/measurements.js +76 -0
  8. package/dist/source/components/Draw/modes/multi_point.js +37 -0
  9. package/dist/source/components/Draw/modes/point.js +25 -0
  10. package/dist/source/components/Draw/modes/polygon.js +25 -0
  11. package/dist/source/components/Draw/modes/radius.js +138 -0
  12. package/dist/source/components/Draw/modes/rectangle.js +118 -0
  13. package/dist/source/components/Draw/modes/rectangle_assisted.js +233 -0
  14. package/dist/source/components/Layer/index.jsx +17 -17
  15. package/dist/source/components/Layer3D/index.jsx +256 -21
  16. package/dist/source/components/Popup/index.jsx +10 -10
  17. package/dist/source/index.jsx +14 -14
  18. package/dist/types/components/Draw/drawingStyles.d.ts +20 -0
  19. package/dist/types/components/Draw/index.d.ts +5 -3
  20. package/dist/types/components/Draw/modes/index.d.ts +6 -0
  21. package/dist/types/components/Draw/modes/line_string.d.ts +2 -0
  22. package/dist/types/components/Draw/modes/multi_point.d.ts +2 -0
  23. package/dist/types/components/Draw/modes/point.d.ts +2 -0
  24. package/dist/types/components/Draw/modes/polygon.d.ts +2 -0
  25. package/dist/types/components/Draw/modes/radius.d.ts +2 -0
  26. package/dist/types/components/Draw/modes/rectangle.d.ts +2 -0
  27. package/dist/types/components/Draw/modes/rectangle_assisted.d.ts +18 -0
  28. package/dist/types/components/Layer3D/index.d.ts +7 -2
  29. package/dist/types/components/Popup/index.d.ts +2 -2
  30. package/dist/types/index.d.ts +16 -16
  31. package/package.json +20 -13
@@ -0,0 +1,118 @@
1
+ import MapboxDraw from "@mapbox/mapbox-gl-draw";
2
+ import { getArea, getLength } from "./measurements";
3
+ const RectangleMode = {
4
+ ...MapboxDraw.modes.draw_line_string,
5
+ // When the mode starts this function will be called.
6
+ onSetup: function (opts) {
7
+ const props = MapboxDraw.modes.draw_line_string.onSetup.call(this, opts);
8
+ const rectangle = this.newFeature({
9
+ type: "Feature",
10
+ properties: {},
11
+ geometry: {
12
+ type: "Polygon",
13
+ coordinates: [[]],
14
+ },
15
+ });
16
+ this.addFeature(rectangle);
17
+ return { ...props, ...opts, rectangle };
18
+ },
19
+ // support mobile taps
20
+ onTap: function (state, e) {
21
+ // emulate 'move mouse' to update feature coords
22
+ if (state.startPoint)
23
+ this.onMouseMove(state, e);
24
+ // emulate onClick
25
+ this.onClick(state, e);
26
+ },
27
+ // Whenever a user clicks on the map, Draw will call `onClick`
28
+ onClick: function (state, e) {
29
+ if (state.startPoint &&
30
+ state.startPoint[0] !== e.lngLat.lng &&
31
+ state.startPoint[1] !== e.lngLat.lat) {
32
+ // if state.startPoint exist, means its second click
33
+ // change to simple_select mode
34
+ this.updateUIClasses({ mouse: "pointer" });
35
+ state.endPoint = [e.lngLat.lng, e.lngLat.lat];
36
+ this.changeMode("simple_select", { featuresId: state.rectangle.id });
37
+ }
38
+ else {
39
+ // if its first click, save clicked point coords as starting for rectangle
40
+ state.startPoint = [e.lngLat.lng, e.lngLat.lat];
41
+ }
42
+ },
43
+ onMouseMove: function (state, e) {
44
+ // if startPoint, update the feature coordinates, using the bounding box concept
45
+ // we are simply using the startingPoint coordinates and the current Mouse Position
46
+ // coordinates to calculate the bounding box on the fly, which will be our rectangle
47
+ if (state.startPoint) {
48
+ state.rectangle.updateCoordinate("0.0", state.startPoint[0], state.startPoint[1]); //minX, minY - the starting point
49
+ state.rectangle.updateCoordinate("0.1", e.lngLat.lng, state.startPoint[1]); // maxX, minY
50
+ state.rectangle.updateCoordinate("0.2", e.lngLat.lng, e.lngLat.lat); // maxX, maxY
51
+ state.rectangle.updateCoordinate("0.3", state.startPoint[0], e.lngLat.lat); // minX,maxY
52
+ state.rectangle.updateCoordinate("0.4", state.startPoint[0], state.startPoint[1]); //minX,minY - ending point (equals to starting point)
53
+ }
54
+ },
55
+ // Whenever a user clicks on a key while focused on the map, it will be sent here
56
+ onKeyUp: function (state, e) {
57
+ if (e.keyCode === 27)
58
+ return this.changeMode("simple_select");
59
+ },
60
+ onStop: function (state) {
61
+ MapboxDraw.modes.draw_line_string.onStop.call(this, state);
62
+ // check to see if we've deleted this feature
63
+ if (this.getFeature(state.rectangle.id) === undefined)
64
+ return;
65
+ //remove last added coordinate
66
+ state.rectangle.removeCoordinate("0.4");
67
+ if (state.rectangle.isValid()) {
68
+ this.map.fire("draw.create", {
69
+ features: [state.rectangle.toGeoJSON()],
70
+ });
71
+ }
72
+ else {
73
+ this.deleteFeature([state.rectangle.id], { silent: true });
74
+ this.changeMode("simple_select", {}, { silent: true });
75
+ }
76
+ },
77
+ toDisplayFeatures: function (state, geojson, display) {
78
+ console.log(state);
79
+ const isActivePolygon = geojson.properties.id === state.rectangle.id;
80
+ geojson.properties.active = isActivePolygon ? "true" : "false";
81
+ if (!isActivePolygon)
82
+ return display(geojson);
83
+ // Only render the rectangular polygon if it has the starting point
84
+ if (!state.startPoint)
85
+ return;
86
+ display(geojson);
87
+ // create custom feature for the current pointer position
88
+ if (state.showLength) {
89
+ display(getLength({
90
+ type: "Feature",
91
+ geometry: {
92
+ type: "LineString",
93
+ coordinates: [
94
+ geojson.geometry.coordinates[0][0],
95
+ geojson.geometry.coordinates[0][1],
96
+ ],
97
+ },
98
+ }, { parent: state.rectangle.id, anchor: "bottom" }));
99
+ display(getLength({
100
+ type: "Feature",
101
+ geometry: {
102
+ type: "LineString",
103
+ coordinates: [
104
+ geojson.geometry.coordinates[0][1],
105
+ geojson.geometry.coordinates[0][2],
106
+ ],
107
+ },
108
+ }, { parent: state.rectangle.id, anchor: "left" }));
109
+ }
110
+ state.showArea && display(getArea(geojson, { parent: state.rectangle.id }));
111
+ return null;
112
+ },
113
+ onTrash: function (state) {
114
+ this.deleteFeature([state.rectangle.id], { silent: true });
115
+ this.changeMode("simple_select");
116
+ },
117
+ };
118
+ export default RectangleMode;
@@ -0,0 +1,233 @@
1
+ const doubleClickZoom = {
2
+ enable: (ctx) => {
3
+ setTimeout(() => {
4
+ // First check we've got a map and some context.
5
+ if (!ctx.map ||
6
+ !ctx.map.doubleClickZoom ||
7
+ !ctx._ctx ||
8
+ !ctx._ctx.store ||
9
+ !ctx._ctx.store.getInitialConfigValue)
10
+ return;
11
+ if (!ctx._ctx.store.getInitialConfigValue("doubleClickZoom"))
12
+ return;
13
+ ctx.map.doubleClickZoom.enable();
14
+ }, 0);
15
+ },
16
+ disable(ctx) {
17
+ setTimeout(() => {
18
+ if (!ctx.map || !ctx.map.doubleClickZoom)
19
+ return;
20
+ ctx.map.doubleClickZoom.disable();
21
+ }, 0);
22
+ },
23
+ };
24
+ const RectangleAssistedMode = {
25
+ onSetup: function (opts) {
26
+ const rectangle = this.newFeature({
27
+ type: "Feature",
28
+ properties: {},
29
+ geometry: {
30
+ type: "Polygon",
31
+ coordinates: [[]],
32
+ },
33
+ });
34
+ this.addFeature(rectangle);
35
+ this.clearSelectedFeatures();
36
+ doubleClickZoom.disable(this);
37
+ this.updateUIClasses({
38
+ mouse: "add",
39
+ });
40
+ this.setActionableState({
41
+ trash: true,
42
+ });
43
+ return {
44
+ rectangle,
45
+ currentVertexPosition: 0,
46
+ };
47
+ },
48
+ onTap: function (state, e) {
49
+ this.onClick(state, e);
50
+ },
51
+ onClick: function (state, e) {
52
+ if (state.currentVertexPosition === 2) {
53
+ const getpXY3 = this.calculatepXY3(state, e, false);
54
+ if (getpXY3) {
55
+ state.rectangle.updateCoordinate(`0.${state.currentVertexPosition + 1}`, getpXY3[0], getpXY3[1]);
56
+ }
57
+ this.updateUIClasses({
58
+ mouse: "pointer",
59
+ });
60
+ return this.changeMode("simple_select", {
61
+ featuresId: state.rectangle.id,
62
+ });
63
+ }
64
+ else {
65
+ state.rectangle.updateCoordinate(`0.${state.currentVertexPosition}`, e.lngLat.lng, e.lngLat.lat);
66
+ state.currentVertexPosition++;
67
+ state.rectangle.updateCoordinate(`0.${state.currentVertexPosition}`, e.lngLat.lng, e.lngLat.lat);
68
+ }
69
+ },
70
+ onMouseMove: function (state, e) {
71
+ state.rectangle.updateCoordinate("0." + state.currentVertexPosition, e.lngLat.lng, e.lngLat.lat);
72
+ if (state.currentVertexPosition && state.currentVertexPosition > 0) {
73
+ this.calculateOrientedAnglePolygon(state);
74
+ }
75
+ if (state.currentVertexPosition === 2) {
76
+ const getpXY3 = this.calculatepXY3(state, e, true);
77
+ if (getpXY3) {
78
+ state.rectangle.updateCoordinate("0." + (state.currentVertexPosition + 1), getpXY3[0], getpXY3[1]);
79
+ }
80
+ }
81
+ },
82
+ deegrees2meters(px) {
83
+ //gist from https://gist.github.com/springmeyer/871897
84
+ const x = (px[0] * 20037508.34) / 180;
85
+ let y = Math.log(Math.tan(((90 + px[1]) * Math.PI) / 360)) / (Math.PI / 180);
86
+ y = (y * 20037508.34) / 180;
87
+ return [x, y];
88
+ },
89
+ meters2degress(px) {
90
+ //gist from https://gist.github.com/springmeyer/871897
91
+ const lon = (px[0] * 180) / 20037508.34;
92
+ const lat = (Math.atan(Math.exp((px[1] * Math.PI) / 20037508.34)) * 360) / Math.PI -
93
+ 90;
94
+ return [lon, lat];
95
+ },
96
+ calculateOrientedAnglePolygon: function (state) {
97
+ const pXY0 = state.rectangle.getCoordinate("0.0");
98
+ const pXY0_3857 = this.deegrees2meters(pXY0);
99
+ const pXY1 = state.rectangle.getCoordinate("0.1");
100
+ const pXY1_3857 = this.deegrees2meters(pXY1);
101
+ const angleStdGraus = (Math.atan2(pXY1_3857[1] - pXY0_3857[1], pXY1_3857[0] - pXY0_3857[0]) *
102
+ 180) /
103
+ Math.PI;
104
+ let angleSudGraus = -1.0 * (angleStdGraus + 90);
105
+ const angle = angleSudGraus < 0 ? angleSudGraus + 360 : angleSudGraus;
106
+ state.angle = parseFloat(angle.toFixed(2));
107
+ },
108
+ calculatepXY3: function (state, e, tmp) {
109
+ const pXY0 = state.rectangle.getCoordinate("0.0");
110
+ const pXY0_3857 = this.deegrees2meters(pXY0);
111
+ const pXY1 = state.rectangle.getCoordinate("0.1");
112
+ const pXY1_3857 = this.deegrees2meters(pXY1);
113
+ let pXY2_3857 = this.deegrees2meters([e.lngLat.lng, e.lngLat.lat]);
114
+ const mouse_3857 = this.deegrees2meters([e.lngLat.lng, e.lngLat.lat]);
115
+ if (pXY0_3857[0] === pXY1_3857[0]) {
116
+ pXY2_3857 = [mouse_3857[0], pXY1_3857[1]];
117
+ }
118
+ else if (pXY0_3857[1] === pXY1_3857[1]) {
119
+ pXY2_3857 = [pXY1_3857[0], mouse_3857[1]];
120
+ }
121
+ else {
122
+ const vector1_3857 = (pXY1_3857[1] - pXY0_3857[1]) / (pXY1_3857[0] - pXY0_3857[0]);
123
+ const vector2_3857 = -1.0 / vector1_3857;
124
+ if (Math.abs(vector2_3857) < 1) {
125
+ pXY2_3857[1] =
126
+ vector2_3857 * (mouse_3857[0] - pXY1_3857[0]) + pXY1_3857[1];
127
+ }
128
+ else {
129
+ pXY2_3857[0] =
130
+ pXY1_3857[0] + (pXY2_3857[1] - pXY1_3857[1]) / vector2_3857;
131
+ }
132
+ }
133
+ const vector_3857 = [
134
+ pXY1_3857[0] - pXY0_3857[0],
135
+ pXY1_3857[1] - pXY0_3857[1],
136
+ ];
137
+ const pXY3_3857 = [
138
+ pXY2_3857[0] - vector_3857[0],
139
+ pXY2_3857[1] - vector_3857[1],
140
+ ];
141
+ const pXY2G = this.meters2degress(pXY2_3857);
142
+ const pXY3G = this.meters2degress(pXY3_3857);
143
+ state.rectangle.updateCoordinate("0.2", pXY2G[0], pXY2G[1]);
144
+ state.rectangle.updateCoordinate("0.3", pXY3G[0], pXY3G[1]);
145
+ return pXY3G;
146
+ },
147
+ onKeyUp: function (state, e) {
148
+ if (e.keyCode === 27)
149
+ return this.changeMode("simple_select");
150
+ },
151
+ onStop: function (state) {
152
+ doubleClickZoom.enable(this);
153
+ this.updateUIClasses({
154
+ mouse: "none",
155
+ });
156
+ this.activateUIButton();
157
+ // check to see if we've deleted this feature
158
+ if (this.getFeature(state.rectangle.id) === undefined)
159
+ return;
160
+ //remove last added coordinate
161
+ state.rectangle.removeCoordinate("0.4");
162
+ if (state.rectangle.isValid()) {
163
+ this.map.fire("draw.create", {
164
+ features: [state.rectangle.toGeoJSON()],
165
+ });
166
+ }
167
+ else {
168
+ this.deleteFeature([state.rectangle.id], {
169
+ silent: true,
170
+ });
171
+ this.changeMode("simple_select", {}, {
172
+ silent: true,
173
+ });
174
+ }
175
+ },
176
+ toDisplayFeatures: function (state, geojson, display) {
177
+ const isActivePolygon = geojson.properties.id === state.rectangle.id;
178
+ geojson.properties.active = isActivePolygon ? "true" : "false";
179
+ geojson.properties.angle = state.angle;
180
+ geojson.angle = state.angle;
181
+ if (!isActivePolygon)
182
+ return display(geojson);
183
+ const coordinateCount = geojson.geometry.coordinates[0].length;
184
+ if (coordinateCount < 3) {
185
+ const coordinates = geojson.geometry.coordinates[0][0];
186
+ const vertexPoint = {
187
+ type: "Feature",
188
+ properties: geojson.properties,
189
+ angle: state.angle,
190
+ geometry: {
191
+ coordinates: geojson.geometry.coordinates[0][0],
192
+ type: "Point",
193
+ },
194
+ };
195
+ if (coordinates) {
196
+ display(vertexPoint);
197
+ }
198
+ return;
199
+ }
200
+ if (coordinateCount >= 3 && coordinateCount <= 4) {
201
+ const lineCoordinates = [
202
+ [
203
+ geojson.geometry.coordinates[0][0][0],
204
+ geojson.geometry.coordinates[0][0][1],
205
+ ],
206
+ [
207
+ geojson.geometry.coordinates[0][1][0],
208
+ geojson.geometry.coordinates[0][1][1],
209
+ ],
210
+ ];
211
+ display({
212
+ type: "Feature",
213
+ properties: geojson.properties,
214
+ angle: state.angle,
215
+ geometry: {
216
+ coordinates: lineCoordinates,
217
+ type: "LineString",
218
+ },
219
+ });
220
+ if (coordinateCount === 3) {
221
+ return;
222
+ }
223
+ }
224
+ return display(geojson);
225
+ },
226
+ onTrash: function (state) {
227
+ this.deleteFeature([state.rectangle.id], {
228
+ silent: true,
229
+ });
230
+ this.changeMode("simple_select");
231
+ },
232
+ };
233
+ export default RectangleAssistedMode;
@@ -38,8 +38,8 @@ const updateStyle = (oldStyle) => {
38
38
  };
39
39
  export const Layer = (props) => {
40
40
  const [ctx] = useMapContext();
41
- const sourceId = props.style?.source || useSourceId();
42
- props.id = props.id || props.customLayer?.id || createUniqueId();
41
+ const sourceId = props.sourceId || props.style?.source || useSourceId();
42
+ const layerId = props.id || props.customLayer?.id || createUniqueId();
43
43
  const debug = (text, value) => {
44
44
  (ctx.map.debug || ctx.map.debugEvents) &&
45
45
  console.debug("%c[MapGL]", "color: #10b981", text, value || "");
@@ -47,7 +47,7 @@ export const Layer = (props) => {
47
47
  // Add Layer
48
48
  ctx.map.addLayer(props.customLayer || {
49
49
  ...updateStyle(props.style),
50
- id: props.id,
50
+ id: layerId,
51
51
  source: sourceId,
52
52
  slot: props.slot || "",
53
53
  metadata: {
@@ -56,19 +56,19 @@ export const Layer = (props) => {
56
56
  }, props.beforeType
57
57
  ? ctx.map.getStyle().layers.find((l) => l.type === props.beforeType)?.id
58
58
  : props.beforeId);
59
- ctx.map.layerIdList.push(props.id);
59
+ ctx.map.layerIdList.push(layerId);
60
60
  if (props.customLayer)
61
61
  ctx.map.fire("load");
62
- debug("Add Layer:", props.id);
62
+ debug("Add Layer:", layerId);
63
63
  // Hook up events
64
64
  layerEvents.forEach((item) => {
65
65
  if (props[item]) {
66
66
  const event = item.slice(2).toLowerCase();
67
- ctx.map.on(event, props.id, (evt) => {
67
+ ctx.map.on(event, layerId, (evt) => {
68
68
  evt.clickOnLayer = true;
69
69
  props[item](evt);
70
70
  ctx.map.debugEvent &&
71
- debug(`Layer '${event}' event on '${props.id}':`, evt);
71
+ debug(`Layer '${event}' event on '${layerId}':`, evt);
72
72
  });
73
73
  }
74
74
  });
@@ -78,22 +78,22 @@ export const Layer = (props) => {
78
78
  if (style === prev)
79
79
  return;
80
80
  if (style.layout !== prev?.layout)
81
- diff(style.layout, prev?.layout).forEach(([key, value]) => ctx.map.setLayoutProperty(props.id, key, value, { validate: false }));
81
+ diff(style.layout, prev?.layout).forEach(([key, value]) => ctx.map.setLayoutProperty(layerId, key, value, { validate: false }));
82
82
  if (style.paint !== prev?.paint)
83
- diff(style.paint, prev?.paint).forEach(([key, value]) => ctx.map.setPaintProperty(props.id, key, value, { validate: false }));
83
+ diff(style.paint, prev?.paint).forEach(([key, value]) => ctx.map.setPaintProperty(layerId, key, value, { validate: false }));
84
84
  if (style.minzoom !== prev?.minzoom || style.maxzoom !== prev?.maxzoom)
85
- ctx.map.setLayerZoomRange(props.id, style.minzoom, style.maxzoom);
85
+ ctx.map.setLayerZoomRange(layerId, style.minzoom, style.maxzoom);
86
86
  if (style.filter !== prev?.filter)
87
- ctx.map.setFilter(props.id, style.filter, { validate: false });
88
- debug("Update Layer Style:", props.id);
87
+ ctx.map.setFilter(layerId, style.filter, { validate: false });
88
+ debug("Update Layer Style:", layerId);
89
89
  return style;
90
90
  }, updateStyle(props.style));
91
91
  // Update Visibility
92
92
  createEffect((prev) => {
93
93
  if (props.visible === prev)
94
94
  return;
95
- ctx.map.setLayoutProperty(props.id, "visibility", props.visible ? "visible" : "none", { validate: false });
96
- debug(`Update Visibility (${props.id}):`, props.visible.toString());
95
+ ctx.map.setLayoutProperty(layerId, "visibility", props.visible ? "visible" : "none", { validate: false });
96
+ debug(`Update Visibility (${layerId}):`, props.visible.toString());
97
97
  return props.visible;
98
98
  }, props.visible);
99
99
  // Update Filter
@@ -101,8 +101,8 @@ export const Layer = (props) => {
101
101
  if (!props.filter)
102
102
  return;
103
103
  !ctx.map.isStyleLoaded() && (await ctx.map.once("styledata"));
104
- ctx.map.setFilter(props.id, props.filter);
105
- debug(`Update Filter (${props.id}):`, props.filter);
104
+ ctx.map.setFilter(layerId, props.filter);
105
+ debug(`Update Filter (${layerId}):`, props.filter);
106
106
  });
107
107
  // Update Feature State
108
108
  createEffect(async () => {
@@ -120,6 +120,6 @@ export const Layer = (props) => {
120
120
  }, props.featureState.state);
121
121
  });
122
122
  //Remove Layer
123
- onCleanup(() => ctx.map?.getLayer(props.id) && ctx.map?.removeLayer(props.id));
123
+ onCleanup(() => ctx.map?.getLayer(layerId) && ctx.map?.removeLayer(layerId));
124
124
  return props.children;
125
125
  };