terra-draw 0.0.1-alpha.4 → 0.0.1-alpha.41

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 (101) hide show
  1. package/CHANGELOG.md +432 -0
  2. package/README.md +25 -4
  3. package/dist/adapters/common/adapter-listener.d.ts +22 -0
  4. package/dist/adapters/common/base.adapter.d.ts +47 -0
  5. package/dist/adapters/google-maps.adapter.d.ts +87 -38
  6. package/dist/adapters/leaflet.adapter.d.ts +98 -35
  7. package/dist/adapters/mapbox-gl.adapter.d.ts +89 -36
  8. package/dist/adapters/maplibre-gl.adapter.d.ts +72 -0
  9. package/dist/adapters/openlayers.adapter.d.ts +117 -0
  10. package/dist/common.d.ts +93 -99
  11. package/dist/geometry/boolean/is-valid-coordinate.d.ts +4 -0
  12. package/dist/geometry/boolean/is-valid-linestring-feature.d.ts +2 -0
  13. package/dist/geometry/boolean/is-valid-point.d.ts +2 -0
  14. package/dist/geometry/boolean/is-valid-polygon-feature.d.ts +3 -0
  15. package/dist/geometry/boolean/point-in-polygon.d.ts +2 -2
  16. package/dist/geometry/boolean/self-intersects.d.ts +2 -2
  17. package/dist/geometry/centroid.d.ts +2 -2
  18. package/dist/geometry/coordinates-identical.d.ts +2 -2
  19. package/dist/geometry/get-coordinates-as-points.d.ts +6 -6
  20. package/dist/geometry/get-midpoints.d.ts +8 -7
  21. package/dist/geometry/helpers.d.ts +4 -4
  22. package/dist/geometry/limit-decimal-precision.d.ts +1 -1
  23. package/dist/geometry/measure/haversine-distance.d.ts +2 -2
  24. package/dist/geometry/measure/pixel-distance-to-line.d.ts +10 -10
  25. package/dist/geometry/measure/pixel-distance.d.ts +7 -7
  26. package/dist/geometry/measure/rhumb-bearing.d.ts +2 -2
  27. package/dist/geometry/measure/rhumb-destination.d.ts +2 -2
  28. package/dist/geometry/measure/rhumb-distance.d.ts +2 -2
  29. package/dist/geometry/midpoint-coordinate.d.ts +3 -2
  30. package/dist/geometry/shape/create-circle.d.ts +7 -7
  31. package/dist/geometry/shape/great-circle-line.d.ts +12 -0
  32. package/dist/geometry/transform/rotate.d.ts +2 -2
  33. package/dist/geometry/transform/scale.d.ts +2 -2
  34. package/dist/modes/base.behavior.d.ts +19 -19
  35. package/dist/modes/base.mode.d.ts +55 -30
  36. package/dist/modes/circle/circle.mode.d.ts +49 -27
  37. package/dist/modes/click-bounding-box.behavior.d.ts +7 -7
  38. package/dist/modes/freehand/freehand.mode.d.ts +55 -29
  39. package/dist/modes/great-circle-snapping.behavior.d.ts +13 -0
  40. package/dist/modes/greatcircle/great-circle.mode.d.ts +58 -0
  41. package/dist/modes/linestring/linestring.mode.d.ts +61 -34
  42. package/dist/modes/pixel-distance.behavior.d.ts +7 -7
  43. package/dist/modes/point/point.mode.d.ts +39 -18
  44. package/dist/modes/polygon/behaviors/closing-points.behavior.d.ts +19 -0
  45. package/dist/modes/polygon/polygon.mode.d.ts +64 -37
  46. package/dist/modes/rectangle/rectangle.mode.d.ts +50 -0
  47. package/dist/modes/render/render.mode.d.ts +39 -0
  48. package/dist/modes/select/behaviors/drag-coordinate.behavior.d.ts +19 -13
  49. package/dist/modes/select/behaviors/drag-feature.behavior.d.ts +19 -17
  50. package/dist/modes/select/behaviors/features-at-mouse-event.behavior.d.ts +15 -15
  51. package/dist/modes/select/behaviors/midpoint.behavior.d.ts +18 -18
  52. package/dist/modes/select/behaviors/rotate-feature.behavior.d.ts +13 -13
  53. package/dist/modes/select/behaviors/scale-feature.behavior.d.ts +13 -13
  54. package/dist/modes/select/behaviors/selection-point.behavior.d.ts +18 -18
  55. package/dist/modes/select/select.mode.d.ts +100 -62
  56. package/dist/modes/snapping.behavior.d.ts +13 -13
  57. package/dist/modes/static/static.mode.d.ts +32 -13
  58. package/dist/store/spatial-index/quickselect.d.ts +2 -2
  59. package/dist/store/spatial-index/rbush.d.ts +35 -35
  60. package/dist/store/spatial-index/spatial-index.d.ts +18 -18
  61. package/dist/store/store-feature-validation.d.ts +15 -0
  62. package/dist/store/store.d.ts +48 -48
  63. package/dist/terra-draw.cjs +1 -1
  64. package/dist/terra-draw.cjs.map +1 -1
  65. package/dist/terra-draw.d.ts +159 -47
  66. package/dist/terra-draw.modern.js +1 -1
  67. package/dist/terra-draw.modern.js.map +1 -1
  68. package/dist/terra-draw.module.js +1 -1
  69. package/dist/terra-draw.module.js.map +1 -1
  70. package/dist/terra-draw.umd.js +1 -1
  71. package/dist/terra-draw.umd.js.map +1 -1
  72. package/dist/util/geoms.d.ts +3 -3
  73. package/dist/util/id.d.ts +1 -1
  74. package/dist/util/styling.d.ts +2 -2
  75. package/{CONTRIBUTING.md → guides/CONTRIBUTING.md} +7 -7
  76. package/guides/DEVELOPMENT.md +156 -0
  77. package/guides/GETTING_STARTED.md +292 -0
  78. package/jest.config.ts +22 -22
  79. package/logo.png +0 -0
  80. package/package.json +155 -87
  81. package/readme.gif +0 -0
  82. package/scripts/next-alpha-version.ts +21 -0
  83. package/tsconfig.json +20 -12
  84. package/DEVELOPMENT.md +0 -77
  85. package/dist/bundle.js +0 -6
  86. package/dist/bundle.js.LICENSE.txt +0 -4
  87. package/dist/geometry/create-circle.d.ts +0 -6
  88. package/dist/geometry/get-pixel-distance-to-line.d.ts +0 -10
  89. package/dist/geometry/get-pixel-distance.d.ts +0 -7
  90. package/dist/geometry/haversine-distance.d.ts +0 -1
  91. package/dist/geometry/point-in-polygon.d.ts +0 -1
  92. package/dist/geometry/self-intersects.d.ts +0 -2
  93. package/dist/modes/circle.mode.d.ts +0 -18
  94. package/dist/modes/freehand.mode.d.ts +0 -20
  95. package/dist/modes/line-string.mode.d.ts +0 -21
  96. package/dist/modes/point.mode.d.ts +0 -14
  97. package/dist/modes/polygon/behaviors/start-end-point.behavior.d.ts +0 -11
  98. package/dist/modes/polygon.mode.d.ts +0 -21
  99. package/dist/modes/select.mode.d.ts +0 -21
  100. package/dist/modes/static.mode.d.ts +0 -10
  101. /package/{CODE_OF_CONDUCT.md → guides/CODE_OF_CONDUCT.md} +0 -0
@@ -1,3 +1,3 @@
1
- import { Feature, LineString, Polygon, Position } from "geojson";
2
- export declare function createPolygon(coordinates?: Position[][]): Feature<Polygon>;
3
- export declare function createLineString(coordinates: Position[]): Feature<LineString>;
1
+ import { Feature, LineString, Polygon, Position } from "geojson";
2
+ export declare function createPolygon(coordinates?: Position[][]): Feature<Polygon>;
3
+ export declare function createLineString(coordinates: Position[]): Feature<LineString>;
package/dist/util/id.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const uuid4: () => string;
1
+ export declare const uuid4: () => string;
@@ -1,2 +1,2 @@
1
- import { TerraDrawAdapterStyling } from "../common";
2
- export declare const getDefaultStyling: () => TerraDrawAdapterStyling;
1
+ import { TerraDrawAdapterStyling } from "../common";
2
+ export declare const getDefaultStyling: () => TerraDrawAdapterStyling;
@@ -4,14 +4,14 @@
4
4
 
5
5
  This document will receive a more thorough update in the future. For the time being, if you wish to contribute to Terra Draw, we request that you:
6
6
 
7
- 1) Make an issue on GitHub describing the bug you intend to fix or the feature you intend to add.
8
- 2) Create a fork of the project, and work on a branch that represents the issue
9
- 3) Ensure that the work you have done is unit tested, aiming for 80% code coverage
10
- 4) Create a PR that represents this work. Please refer to the issue from the PR
11
- 5) We will address the PR and give you feedback
7
+ 1. Make an issue on GitHub describing the bug you intend to fix or the feature you intend to add.
8
+ 2. Create a fork of the project, and work on a branch that represents the issue
9
+ 3. Ensure that the work you have done is unit tested, aiming for 80% code coverage
10
+ 4. Create a PR that represents this work. Please refer to the issue from the PR
11
+ 5. We will address the PR and give you feedback
12
12
 
13
- Please note that we can not garauntee that all PRs will be merged. There are cases where a PR may deviate from the long-term vision for Terra Draw and hence we may have to decline it. Creating the issue in advance helps us discuss any potential issues upfront before the PR is made.
13
+ Please note that we can not guarantee that all PRs will be merged. There are cases where a PR may deviate from the long-term vision for Terra Draw and hence we may have to decline it. Creating the issue in advance helps us discuss any potential issues upfront before the PR is made.
14
14
 
15
15
  ## Code of Conduct
16
16
 
17
- We have a code of conduct which we expect individuals interacting with the project to respect. Please see the [full Code of Conduct in the repository](./CODE_OF_CONDUCT.md).
17
+ We have a code of conduct which we expect individuals interacting with the project to respect. Please see the [full Code of Conduct in the repository](./CODE_OF_CONDUCT.md).
@@ -0,0 +1,156 @@
1
+ # Development
2
+
3
+ This file acts as a way to document how to develop the Terra Draw project locally.
4
+
5
+ ### Prerequisites
6
+
7
+ A few things you will need to have installed in order to develop on this project:
8
+
9
+ - git
10
+ - Node LTS - currently v16
11
+ - npm 8
12
+
13
+ ### Folder Structure
14
+
15
+ - `.github` - used for all GitHub related configuration such as the GitHub Actions work flows
16
+ - `.husky` - used to storing the precommit hooks that are used on the project
17
+ - `src` - source files for the project
18
+ - `dist` - the bundled distributed files of the project
19
+ - `docs` - the demo app that is published to GitHub pages
20
+ - `development` - the local development app that is used for developing locally (see below)
21
+ - `common` - code that is used across `development` and `docs` folder`
22
+
23
+ ### Technologies Used
24
+
25
+ Terra Draw
26
+
27
+ - [TypeScript](https://www.typescriptlang.org/) - provides strong compile time typing for JavaScript
28
+ - Jest - used for testing (see more information below)
29
+ - microbundle - used for the production bundle
30
+ - Webpack - used for bundling locally in development (`development` and `docs` folders)
31
+ - esno - for running tests quickly without type checking
32
+
33
+ ### Precommit Hooks
34
+
35
+ It is probably useful to be aware of the precommit hooks you will face when trying to run a git commit on the project. There are two currently in use, namely:
36
+
37
+ - Uses pre-commit hook to run lint rules (eslint/prettier) on code before commit
38
+ - Uses pre-commit hook to ensure [conventional commit messages](https://www.conventionalcommits.org/en/v1.0.0/) are used
39
+
40
+ ### Testing
41
+
42
+ Terra Draw uses [jest](https://jestjs.io/) as it's testing framework. You can distinguish a test by it's `.spec.ts` prefix on the file name.
43
+
44
+ To run the tests as they would run in CI:
45
+
46
+ ```
47
+ npm run test
48
+ ```
49
+
50
+ You can also check the coverage by running:
51
+
52
+ ```
53
+ npm run test:coverage
54
+ ```
55
+
56
+ For local development you may benefit from the `nocheck` option which allows you to avoid running TypeScript type checking when running the tests. This option also only checks files which are explicitly tested (i.e. have a spec file.)
57
+
58
+ ```
59
+ npm run test:nocheck
60
+ npm run test:nocheck:coverage
61
+ ```
62
+
63
+ ### Developing Locally
64
+
65
+ A folder called `development` has been set up locally to allow you to test developing Terra Draw locally more easily. It allows you to run the different adapters and test different map providers in parallel, ensuring. You will need to update the .env file in the `development` folder in order to use the related adapters working. An example `.env` file in the `development` folder:
66
+
67
+ ```
68
+ GOOGLE_API_KEY=YOUR_KEY_HERE
69
+ MAPBOX_ACCESS_TOKEN=YOUR_KEY_HERE
70
+ ```
71
+
72
+ ## Terra Draw Concepts
73
+
74
+ Terra Draw has a few elementary concepts that allow it to stay strongly decoupled and relatively map library agnostic. Lets walk through them:
75
+
76
+ - **Store**: - at the heart of the library, where geometry data is stored, created, updated and deleted. Data is stored as standard GeoJSON.
77
+ - **Adapters**: thin wrappers that contain map library specific logic, for creating and updating layers that can rendered by the mapping library
78
+ - **Modes**: - modes represent the logic for a specific drawing tool, for example the point, line and polygon modes
79
+
80
+ ### Adapters Explained
81
+
82
+ Adapters are a core aspect of Terra Draw - they are the layer between the core of the library and the external mapping libraries (Leaflet, Google Maps etc). In simple terms an adapter takes input events, passes them through to create geometries in the store and then passes them back to the adapter to be rendered specifically for the mapping library. For example, in the `LeafletAdapter` we create and update a GeoJSON layer that Leaflet knows how to render to the screen. In theory an adapter could be created for any mapping library that can fill out the Adapter abstract class (TerraDrawBaseAdapter). Namely these are methods that would need to be completed:
83
+
84
+ ```typescript
85
+ public project(...args: Parameters<Project>): ReturnType<Project>;
86
+
87
+ public unproject(
88
+ ...args: Parameters<Unproject>
89
+ ): ReturnType<Unproject>;
90
+
91
+ public setCursor(
92
+ ...args: Parameters<SetCursor>
93
+ ): ReturnType<SetCursor>;
94
+
95
+ public getLngLatFromEvent(event: PointerEvent | MouseEvent): {
96
+ lng: number;
97
+ lat: number;
98
+ } | null;
99
+
100
+ public setDraggability(enabled: boolean): void;
101
+
102
+ public setDoubleClickToZoom(enabled: boolean): void;
103
+
104
+ public getMapContainer(): HTMLElement;
105
+
106
+ public render(
107
+ changes: TerraDrawChanges,
108
+ styling: TerraDrawStylingFunction
109
+ ): void;
110
+ ```
111
+
112
+ ### Modes Explained
113
+
114
+ Modes are another important concept in Terra Draw. Modes are a way to encapsulate specific logic for drawing a certain entity. For example, there are built in modes for drawing points, lines, polygons, circles and rectangles.
115
+
116
+ Modes can go beyond just drawing however, for example the built in `TerraDrawSelectMode` allows for selection and editing of geometries that have previously been drawn. Another example of a mode that is not explicitly just editing is `TerraDrawRenderMode` which can be considered to be a 'view only mode' and used if you wanted to show some contextual data in your application alongside data that you want to edit.
117
+
118
+ Assuming that a mode extends from `TerraDrawBaseMode`
119
+
120
+ ```typescript
121
+ /** @internal */
122
+ start() {
123
+ this.setStarted();
124
+
125
+ }
126
+
127
+ /** @internal */
128
+ stop() {
129
+ this.setStopped();
130
+ }
131
+
132
+ /** @internal */
133
+ onClick(event: TerraDrawMouseEvent) {}
134
+
135
+ /** @internal */
136
+ onMouseMove(event: TerraDrawMouseEvent) {}
137
+
138
+ /** @internal */
139
+ onKeyDown(event: TerraDrawKeyboardEvent) {}
140
+
141
+ /** @internal */
142
+ onKeyUp(event: TerraDrawKeyboardEvent) {}
143
+
144
+
145
+ /** @internal */
146
+ onDragStart(event: TerraDrawMouseEvent) {}
147
+
148
+ /** @internal */
149
+ onDrag(event: TerraDrawMouseEvent) {}
150
+
151
+ /** @internal */
152
+ onDragEnd(event: TerraDrawMouseEvent) {}
153
+
154
+ /** @internal */
155
+ styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling {}
156
+ ```
@@ -0,0 +1,292 @@
1
+ # Getting Started
2
+
3
+ ## Install
4
+
5
+ We start with the assumption that you have [Node.js](https://nodejs.org/en) installed and [npm](https://www.npmjs.com/) (node package manager).
6
+
7
+ You can install the Terra Draw into your project like so:
8
+
9
+ ```shell
10
+ npm install terra-draw
11
+ ```
12
+
13
+ Be aware Terra Draw is currently in alpha, the initial API is still being finalised. It is strongly advised to pin your installation to a specific version i.e. not using carat, asterix or tilde for versions but giving a version explicitly in your `package.json`
14
+
15
+ ```
16
+ "terra-draw": "0.0.1-alpha.22"
17
+ ```
18
+
19
+ Once terra-draw is out of alpha this suggestion will be removed as we will aim to move to semantic versioning.
20
+
21
+ ## API Docs
22
+
23
+ You can find the full autogenerated [API docs on the terra draw website](https://terradraw.io/#/api)
24
+
25
+ ## Using Terra Draw
26
+
27
+ Terra Draw can be used with a set of different adapters to allow you to use it with your mapping library of choice. Each adapter has slightly different configuration but the API is the same for them all. For a deeper explanation about what adapters are and how to write you own see the [development guide](./DEVELOPMENT.md).
28
+
29
+ ### Leaflet
30
+
31
+ Here is some simple starter code for using Terra Draw with the popular open source mapping library Leaflet. It assumes you are going instantiate your map in a div with and id of `map`, which is assumed to exist within your HTML.
32
+
33
+ ```typescript
34
+ // Import the leaflet mapping library
35
+ import * as L from "leaflet";
36
+
37
+ // Import Terra Draw, we will import the leaflet adapter, select mode and polygon mode
38
+ import {
39
+ TerraDraw,
40
+ TerraDrawLeafletAdapter,
41
+ TerraDrawSelectMode,
42
+ TerraDrawPolygonMode,
43
+ } from "terra-draw";
44
+
45
+ // Initialize a new Leaflet map, providing the id of the div to display the map
46
+ // Set the initial center (latitude, longitude) and the zoom level
47
+ const leafletMap = L.map("map", {
48
+ center: [lat, lng],
49
+ zoom: 4, // starting zoom,
50
+ minZoom: 3,
51
+ maxZoom: 20,
52
+ tapTolerance: 10,
53
+ });
54
+
55
+ // Add a basemap from OpenStreetMap to the Leaflet map
56
+ L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
57
+ maxZoom: 19,
58
+ attribution:
59
+ '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
60
+ }).addTo(leafletMap);
61
+
62
+ // Instantiate the TerraDraw API with a Leaflet adapter and custom modes
63
+ const draw = new TerraDraw({
64
+ adapter: new TerraDrawLeafletAdapter({
65
+ // The leaflet library object
66
+ lib: leaflet,
67
+
68
+ // The leaflet map object we created
69
+ map: leafletMap,
70
+
71
+ // The decimal precision of the coordinates created
72
+ coordinatePrecision: 9,
73
+ }),
74
+
75
+ // Modes is an object containing all the modes we wish to
76
+ // instantiate Terra Draw with
77
+ modes: {
78
+ select: new TerraDrawSelectMode({
79
+ flags: {
80
+ // Following flags determine what you can do in
81
+ // select mode for features of a given mode - in this case polygon
82
+ polygon: {
83
+ feature: {
84
+ scaleable: true,
85
+ rotateable: true,
86
+ draggable: true,
87
+ coordinates: {
88
+ midpoints: true,
89
+ draggable: true,
90
+ deletable: true,
91
+ },
92
+ },
93
+ },
94
+ },
95
+ }),
96
+ polygon: new TerraDrawPolygonMode({
97
+ allowSelfIntersections: false,
98
+ pointerDistance: 30,
99
+ }),
100
+ },
101
+ });
102
+
103
+ // Start drawing
104
+ draw.start();
105
+
106
+ // Set the mode to polygon
107
+ draw.setMode("polygon");
108
+ ```
109
+
110
+ ### Google Maps API
111
+
112
+ Here is some starter code for using Terra Draw with the Google Maps API. It assumes you are going instantiate your map in a div with and id of `map`, which is assumed to exist within your HTML.
113
+
114
+ ```typescript
115
+ // Import the google maps api loader
116
+ import { Loader } from "@googlemaps/js-api-loader";
117
+
118
+ // Import Terra Draw, we will import the leaflet adapter, select mode and polygon mode
119
+ import {
120
+ TerraDraw,
121
+ TerraDrawLeafletAdapter,
122
+ TerraDrawSelectMode,
123
+ TerraDrawPolygonMode,
124
+ } from "terra-draw";
125
+
126
+ const loader = new Loader({
127
+ apiKey: "INSERT_YOUR_API_KEY_HERE", // You need to provide your own API key here
128
+ version: "weekly",
129
+ });
130
+
131
+ // We need to await the google maps api
132
+ loader.load().then((google) => {
133
+ // Create the map
134
+ const map = new google.maps.Map(document.getElementById("map"), {
135
+ disableDefaultUI: true,
136
+ center: { lat: this.lat, lng: this.lng },
137
+ zoom: this.zoom,
138
+
139
+ //Setting clickableIcons to false is used to disable the default behavior of the
140
+ // Google Maps API, which is to allow users to interact with clickable icons on the
141
+ // map, such as points of interest (POIs) or other interactive map elements.
142
+ clickableIcons: false,
143
+ });
144
+
145
+ // When using Google Maps, it's important to wrap the creation of custom overlays or other // map-related initializations inside the 'projection_changed' event listener to ensure
146
+ // that the map's projection is fully loaded before your code runs.
147
+ map.addListener("projection_changed", () => {
148
+ const draw = new TerraDraw({
149
+ adapter: new TerraDrawGoogleMapsAdapter({
150
+ lib: google.maps,
151
+ map,
152
+ coordinatePrecision: 9,
153
+ }),
154
+ modes: {
155
+ select: new TerraDrawSelectMode({
156
+ flags: {
157
+ // Following flags determine what you can do in
158
+ // select mode for features of a given mode - in this case polygon
159
+ polygon: {
160
+ feature: {
161
+ scaleable: true,
162
+ rotateable: true,
163
+ draggable: true,
164
+ coordinates: {
165
+ midpoints: true,
166
+ draggable: true,
167
+ deletable: true,
168
+ },
169
+ },
170
+ },
171
+ },
172
+ }),
173
+ polygon: new TerraDrawPolygonMode({
174
+ allowSelfIntersections: false,
175
+ pointerDistance: 30,
176
+ }),
177
+ },
178
+ });
179
+
180
+ // Start drawing
181
+ draw.start();
182
+
183
+ // Set the mode to polygon
184
+ draw.setMode("polygon");
185
+ });
186
+ });
187
+ ```
188
+
189
+ ### OpenLayers
190
+
191
+ Coming soon - please see development folder for example for now.
192
+
193
+ ### MapboxGL JS
194
+
195
+ Coming soon - please see development folder for example for now.
196
+
197
+ ### MapLibreGL JS
198
+
199
+ Coming soon - please see development folder for example for now.
200
+
201
+ ## Other Examples
202
+
203
+ There are a few other working examples that you can use as points of reference for creating a new app using Terra Draw.
204
+
205
+ ### Development Example
206
+
207
+ The [development folder features an example](https://github.com/JamesLMilner/terra-draw/tree/main/development) of using Terra Draw with all the adapters. It is meant for local development but can also be used as a guide of how to use Terra Draw with each adapter without any frame work.
208
+
209
+ ### Full Example
210
+
211
+ The [Terra Draw official website](https://github.com/JamesLMilner/terra-draw-website) is open source acts as a full working implementation of how to use Terra Draw. In this case it is used with popular framework Preact (has very similar API to React).
212
+
213
+ ## Common Patterns
214
+
215
+ ### Loading in External Data
216
+
217
+ It is common pattern to want to load in data from an external source (GeoJSON file, API call, etc). This can be achieved with the `addFeatures` method on the Terra Draw instance. The method call works out which mode to add the feature based on looking at its `mode` property in the Features `properties` property. All modes have a method called `validateFeature` that ensures that a given feature is valid for the mode. For example if you wanted to add a series of points to the TerraDrawPointMode you could do this by ensuring that the points you feed in have the `mode` property set to `point`.
218
+
219
+ ```javascript
220
+ points.forEach((point) => {
221
+ point.properties.mode = "point";
222
+ });
223
+
224
+ draw.addFeatures(points);
225
+ ```
226
+
227
+ ### Render Only Modes with TerraDrawRenderMode
228
+
229
+ If you just want to render some data onto the map without it being editable, you can use `TerraDrawRenderMode` in combination with `addFeatures` like so:
230
+
231
+ ```javascript
232
+ const draw = new TerraDraw({
233
+ adapter: new TerraDrawLeafletAdapter({
234
+ lib: L,
235
+ map,
236
+ coordinatePrecision: 9,
237
+ }),
238
+ modes: {
239
+ arbitary: new TerraDrawRenderMode(),
240
+ },
241
+ });
242
+
243
+ draw.start();
244
+
245
+ points.forEach((point) => {
246
+ point.properties.mode = "arbitary";
247
+ });
248
+
249
+ draw.addFeatures(points);
250
+
251
+ // This will add the points to hte TerraDrawRenderMode 'arbitary' rendering them to the screen
252
+ ```
253
+
254
+ ### Styling Selected Data
255
+
256
+ To style selected data you will want to past styles to the select mode you are using (probably TerraDrawSelectMode).
257
+
258
+ ```typescript
259
+ const draw = new TerraDraw({
260
+ adapter: new TerraDrawMapboxGLAdapter({
261
+ map,
262
+ coordinatePrecision: 9,
263
+ }),
264
+ modes: {
265
+ polygon: new TerraDrawPolygonMode(),
266
+ select: new TerraDrawSelectMode({
267
+ flags: {
268
+ polygon: {
269
+ feature: {
270
+ draggable: true,
271
+ coordinates: {
272
+ midpoints: true,
273
+ draggable: true,
274
+ deletable: true,
275
+ },
276
+ },
277
+ },
278
+ },
279
+ styles: {
280
+ selectedPolygonColor: "#222222", // Any hex color you like
281
+ selectedPolygonFillOpacity: 0.7, // 0 - 1
282
+ selectedPolygonOutlineColor: "#333333", // Any hex color you like
283
+ selectedPolygonOutlineWidth: 2, // Integer
284
+ },
285
+ }),
286
+ },
287
+ });
288
+
289
+ draw.start();
290
+ ```
291
+
292
+ Please note at the moment it is not possible to style against specific modes but only universally against geometry type (Point, LineString, Polygon)
package/jest.config.ts CHANGED
@@ -1,27 +1,27 @@
1
-
2
- let options = {}
1
+ let options = {};
3
2
 
4
3
  if (process.env.NO_CHECK) {
5
- options = {
6
- "transform": {
7
- "^.+\\.(t|j)sx?$": "@swc/jest"
8
- },
9
- "setupFilesAfterEnv": ["<rootDir>/src/test/jest.matchers.ts"],
10
- }
4
+ options = {
5
+ transform: {
6
+ "^.+\\.(t|j)sx?$": "@swc/jest",
7
+ },
8
+ coveragePathIgnorePatterns: ["<rootDir>/src/test/"],
9
+ setupFilesAfterEnv: ["<rootDir>/src/test/jest.matchers.ts"],
10
+ };
11
11
  } else {
12
- options = {
13
- "preset": "ts-jest",
14
- "testEnvironment": "node",
15
- "coveragePathIgnorePatterns": ["<rootDir>/src/test/"],
16
- "setupFilesAfterEnv": ["<rootDir>/src/test/jest.matchers.ts"],
17
- "collectCoverage": true,
18
- "collectCoverageFrom": ["./src/**"],
19
- "coverageThreshold": {
20
- "global": {
21
- "lines": 65
22
- }
23
- }
24
- }
12
+ options = {
13
+ preset: "ts-jest",
14
+ testEnvironment: "node",
15
+ coveragePathIgnorePatterns: ["<rootDir>/src/test/"],
16
+ setupFilesAfterEnv: ["<rootDir>/src/test/jest.matchers.ts"],
17
+ collectCoverage: true,
18
+ collectCoverageFrom: ["./src/**"],
19
+ coverageThreshold: {
20
+ global: {
21
+ lines: 65,
22
+ },
23
+ },
24
+ };
25
25
  }
26
26
 
27
- module.exports = options
27
+ module.exports = options;
package/logo.png ADDED
Binary file