@loaders.gl/wkt 4.2.0-alpha.4 → 4.2.0-alpha.6

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 (75) hide show
  1. package/dist/dist.dev.js +137 -218
  2. package/dist/dist.min.js +10 -0
  3. package/dist/hex-wkb-loader.d.ts +1 -1
  4. package/dist/hex-wkb-loader.d.ts.map +1 -1
  5. package/dist/hex-wkb-loader.js +44 -28
  6. package/dist/index.cjs +87 -62
  7. package/dist/index.cjs.map +7 -0
  8. package/dist/index.d.ts +14 -14
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +4 -1
  11. package/dist/lib/encode-twkb.js +158 -143
  12. package/dist/lib/encode-wkb.js +247 -211
  13. package/dist/lib/encode-wkt-crs.d.ts +1 -1
  14. package/dist/lib/encode-wkt-crs.d.ts.map +1 -1
  15. package/dist/lib/encode-wkt-crs.js +32 -18
  16. package/dist/lib/encode-wkt.js +35 -27
  17. package/dist/lib/parse-hex-wkb.js +1 -2
  18. package/dist/lib/parse-twkb.js +215 -218
  19. package/dist/lib/parse-wkb-header.js +124 -95
  20. package/dist/lib/parse-wkb.d.ts +1 -1
  21. package/dist/lib/parse-wkb.d.ts.map +1 -1
  22. package/dist/lib/parse-wkb.js +210 -235
  23. package/dist/lib/parse-wkt-crs.js +110 -86
  24. package/dist/lib/parse-wkt.js +251 -185
  25. package/dist/lib/utils/base64-encoder.js +151 -5
  26. package/dist/lib/utils/binary-reader.js +67 -64
  27. package/dist/lib/utils/binary-writer.js +117 -109
  28. package/dist/lib/utils/hex-encoder.js +46 -32
  29. package/dist/lib/utils/hex-transcoder.js +40 -24
  30. package/dist/lib/utils/version.js +7 -2
  31. package/dist/twkb-loader.js +26 -17
  32. package/dist/twkb-writer.js +18 -13
  33. package/dist/wkb-loader.js +26 -17
  34. package/dist/wkb-writer.js +22 -17
  35. package/dist/wkt-crs-loader.d.ts +1 -1
  36. package/dist/wkt-crs-loader.d.ts.map +1 -1
  37. package/dist/wkt-crs-loader.js +22 -15
  38. package/dist/wkt-crs-writer.d.ts +2 -2
  39. package/dist/wkt-crs-writer.d.ts.map +1 -1
  40. package/dist/wkt-crs-writer.js +23 -15
  41. package/dist/wkt-loader.js +28 -20
  42. package/dist/wkt-worker.js +4 -2
  43. package/dist/wkt-writer.js +19 -14
  44. package/dist/workers/wkb-worker.js +3 -1
  45. package/dist/workers/wkt-worker.js +3 -1
  46. package/package.json +11 -6
  47. package/src/lib/parse-wkb.ts +2 -2
  48. package/dist/hex-wkb-loader.js.map +0 -1
  49. package/dist/index.js.map +0 -1
  50. package/dist/lib/encode-twkb.js.map +0 -1
  51. package/dist/lib/encode-wkb.js.map +0 -1
  52. package/dist/lib/encode-wkt-crs.js.map +0 -1
  53. package/dist/lib/encode-wkt.js.map +0 -1
  54. package/dist/lib/parse-hex-wkb.js.map +0 -1
  55. package/dist/lib/parse-twkb.js.map +0 -1
  56. package/dist/lib/parse-wkb-header.js.map +0 -1
  57. package/dist/lib/parse-wkb.js.map +0 -1
  58. package/dist/lib/parse-wkt-crs.js.map +0 -1
  59. package/dist/lib/parse-wkt.js.map +0 -1
  60. package/dist/lib/utils/base64-encoder.js.map +0 -1
  61. package/dist/lib/utils/binary-reader.js.map +0 -1
  62. package/dist/lib/utils/binary-writer.js.map +0 -1
  63. package/dist/lib/utils/hex-encoder.js.map +0 -1
  64. package/dist/lib/utils/hex-transcoder.js.map +0 -1
  65. package/dist/lib/utils/version.js.map +0 -1
  66. package/dist/twkb-loader.js.map +0 -1
  67. package/dist/twkb-writer.js.map +0 -1
  68. package/dist/wkb-loader.js.map +0 -1
  69. package/dist/wkb-writer.js.map +0 -1
  70. package/dist/wkt-crs-loader.js.map +0 -1
  71. package/dist/wkt-crs-writer.js.map +0 -1
  72. package/dist/wkt-loader.js.map +0 -1
  73. package/dist/wkt-writer.js.map +0 -1
  74. package/dist/workers/wkb-worker.js.map +0 -1
  75. package/dist/workers/wkt-worker.js.map +0 -1
@@ -1,96 +1,120 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // parse-wkt-crs was forked from https://github.com/DanielJDufour/wkt-crs under Creative Commons CC0 1.0 license.
5
+ /**
6
+ *
7
+ * @param wkt
8
+ * @param options
9
+ * @returns
10
+ */
1
11
  export function parseWKTCRS(wkt, options) {
2
- if (options !== null && options !== void 0 && options.debug) {
3
- console.log('[wktcrs] parse starting with\n', wkt);
4
- }
5
- wkt = wkt.replace(/[A-Z][A-Z\d_]+\[/gi, match => `["${match.substr(0, match.length - 1)}",`);
6
- wkt = wkt.replace(/, ?([A-Z][A-Z\d_]+[,\]])/gi, (match, p1) => {
7
- const varname = p1.substr(0, p1.length - 1);
8
- return ',' + `"${options !== null && options !== void 0 && options.raw ? 'raw:' : ''}${varname}"${p1[p1.length - 1]}`;
9
- });
10
- if (options !== null && options !== void 0 && options.raw) {
11
- wkt = wkt.replace(/, {0,2}(-?[\.\d]+)(?=,|\])/g, function (match, p1) {
12
- return ',' + `"${options !== null && options !== void 0 && options.raw ? 'raw:' : ''}${p1}"`;
13
- });
14
- }
15
- if (options !== null && options !== void 0 && options.debug) {
16
- console.log(`[wktcrs] json'd wkt: '${wkt}'`);
17
- }
18
- let data;
19
- try {
20
- data = JSON.parse(wkt);
21
- } catch (error) {
22
- console.error(`[wktcrs] failed to parse '${wkt}'`);
23
- throw error;
24
- }
25
- if (options !== null && options !== void 0 && options.debug) {
26
- console.log(`[wktcrs] json parsed: '${wkt}'`);
27
- }
28
- function process(data, parent) {
29
- const kw = data[0];
30
- data.forEach(function (it) {
31
- if (Array.isArray(it)) {
32
- process(it, data);
33
- }
12
+ if (options?.debug) {
13
+ console.log('[wktcrs] parse starting with\n', wkt);
14
+ }
15
+ // move all keywords into first array item slot
16
+ // from PARAM[12345, 67890] to ["PARAM", 12345, 67890]
17
+ wkt = wkt.replace(/[A-Z][A-Z\d_]+\[/gi, (match) => `["${match.substr(0, match.length - 1)}",`);
18
+ // wrap variables in strings
19
+ // from [...,NORTH] to [...,"NORTH"]
20
+ wkt = wkt.replace(/, ?([A-Z][A-Z\d_]+[,\]])/gi, (match, p1) => {
21
+ const varname = p1.substr(0, p1.length - 1);
22
+ return ',' + `"${options?.raw ? 'raw:' : ''}${varname}"${p1[p1.length - 1]}`;
34
23
  });
35
- const kwarr = `MULTIPLE_${kw}`;
36
- if (kwarr in parent) {
37
- parent[kwarr].push(data);
38
- } else if (kw in parent) {
39
- parent[kwarr] = [parent[kw], data];
40
- delete parent[kw];
41
- } else {
42
- parent[kw] = data;
24
+ if (options?.raw) {
25
+ // replace all numbers with strings
26
+ wkt = wkt.replace(/, {0,2}(-?[\.\d]+)(?=,|\])/g, function (match, p1) {
27
+ return ',' + `"${options?.raw ? 'raw:' : ''}${p1}"`;
28
+ });
29
+ }
30
+ // str should now be valid JSON
31
+ if (options?.debug) {
32
+ console.log(`[wktcrs] json'd wkt: '${wkt}'`);
33
+ }
34
+ let data;
35
+ try {
36
+ data = JSON.parse(wkt);
43
37
  }
44
- return parent;
45
- }
46
- const result = process(data, [data]);
47
- if (options !== null && options !== void 0 && options.debug) {
48
- console.log('[wktcrs] parse returning', result);
49
- }
50
- if (options !== null && options !== void 0 && options.sort) {
51
- sort(result, options);
52
- }
53
- return result;
38
+ catch (error) {
39
+ console.error(`[wktcrs] failed to parse '${wkt}'`);
40
+ throw error;
41
+ }
42
+ if (options?.debug) {
43
+ console.log(`[wktcrs] json parsed: '${wkt}'`);
44
+ }
45
+ function process(data, parent) {
46
+ const kw = data[0];
47
+ // after removing the first element with .shift()
48
+ // data is now just an array of attributes
49
+ data.forEach(function (it) {
50
+ if (Array.isArray(it)) {
51
+ process(it, data);
52
+ }
53
+ });
54
+ const kwarr = `MULTIPLE_${kw}`;
55
+ if (kwarr in parent) {
56
+ parent[kwarr].push(data);
57
+ }
58
+ else if (kw in parent) {
59
+ parent[kwarr] = [parent[kw], data];
60
+ delete parent[kw];
61
+ }
62
+ else {
63
+ parent[kw] = data;
64
+ }
65
+ return parent;
66
+ }
67
+ const result = process(data, [data]);
68
+ if (options?.debug) {
69
+ console.log('[wktcrs] parse returning', result);
70
+ }
71
+ if (options?.sort) {
72
+ sort(result, options);
73
+ }
74
+ return result;
54
75
  }
55
76
  function sort(data, options) {
56
- const keys = Object.keys(data).filter(k => !/\d+/.test(k));
57
- const keywords = (options === null || options === void 0 ? void 0 : options.keywords) || [];
58
- if (!(options !== null && options !== void 0 && options.keywords)) {
59
- const counts = {};
60
- if (Array.isArray(data)) {
61
- data.forEach(it => {
62
- if (Array.isArray(it) && it.length >= 2 && typeof it[1] === 'string') {
63
- const k = it[0];
64
- if (!counts[k]) counts[k] = 0;
65
- counts[k]++;
77
+ const keys = Object.keys(data).filter((k) => !/\d+/.test(k));
78
+ const keywords = options?.keywords || [];
79
+ if (!options?.keywords) {
80
+ // try to find multiples
81
+ const counts = {};
82
+ if (Array.isArray(data)) {
83
+ data.forEach((it) => {
84
+ if (Array.isArray(it) && it.length >= 2 && typeof it[1] === 'string') {
85
+ const k = it[0];
86
+ if (!counts[k])
87
+ counts[k] = 0;
88
+ counts[k]++;
89
+ }
90
+ });
91
+ for (const k in counts) {
92
+ if (counts[k] > 0)
93
+ keywords.push(k);
94
+ }
66
95
  }
67
- });
68
- for (const k in counts) {
69
- if (counts[k] > 0) keywords.push(k);
70
- }
71
96
  }
72
- }
73
- keys.forEach(key => {
74
- data[key] = sort(data[key]);
75
- });
76
- keywords.forEach(key => {
77
- const indices = [];
78
- const params = [];
79
- data.forEach((item, i) => {
80
- if (Array.isArray(item) && item[0] === key) {
81
- indices.push(i);
82
- params.push(item);
83
- }
84
- });
85
- params.sort((a, b) => {
86
- a = a[1].toString();
87
- b = b[1].toString();
88
- return a < b ? -1 : a > b ? 1 : 0;
97
+ keys.forEach((key) => {
98
+ data[key] = sort(data[key]);
89
99
  });
90
- params.forEach((param, i) => {
91
- data[indices[i]] = param;
100
+ keywords.forEach((key) => {
101
+ const indices = [];
102
+ const params = [];
103
+ data.forEach((item, i) => {
104
+ if (Array.isArray(item) && item[0] === key) {
105
+ indices.push(i);
106
+ params.push(item);
107
+ }
108
+ });
109
+ params.sort((a, b) => {
110
+ a = a[1].toString();
111
+ b = b[1].toString();
112
+ return a < b ? -1 : a > b ? 1 : 0;
113
+ });
114
+ // replace in order
115
+ params.forEach((param, i) => {
116
+ data[indices[i]] = param;
117
+ });
92
118
  });
93
- });
94
- return data;
119
+ return data;
95
120
  }
96
- //# sourceMappingURL=parse-wkt-crs.js.map
@@ -1,222 +1,288 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Fork of https://github.com/mapbox/wellknown under ISC license (MIT/BSD-2-clause equivalent)
5
+ /* eslint-disable */
6
+ // @ts-nocheck
1
7
  const numberRegexp = /[-+]?([0-9]*\.[0-9]+|[0-9]+)([eE][-+]?[0-9]+)?/;
8
+ // Matches sequences like '100 100' or '100 100 100'.
2
9
  const tuples = new RegExp('^' + numberRegexp.source + '(\\s' + numberRegexp.source + '){1,}');
3
- export const WKT_MAGIC_STRINGS = ['POINT(', 'LINESTRING(', 'POLYGON(', 'MULTIPOINT(', 'MULTILINESTRING(', 'MULTIPOLYGON(', 'GEOMETRYCOLLECTION('];
10
+ export const WKT_MAGIC_STRINGS = [
11
+ 'POINT(',
12
+ 'LINESTRING(',
13
+ 'POLYGON(',
14
+ 'MULTIPOINT(',
15
+ 'MULTILINESTRING(',
16
+ 'MULTIPOLYGON(',
17
+ 'GEOMETRYCOLLECTION('
18
+ // We only support this "geojson" subset of the OGC simple features standard
19
+ ];
20
+ /**
21
+ * Check if a string is WKT.
22
+ * @param input A potential WKT geometry string
23
+ * @return `true` if input appears to be a WKT geometry string, `false` otherwise
24
+
25
+ * @note We only support the "geojson" subset of the OGC simple features standard
26
+ * @todo Does not handle leading spaces which appear to be permitted per the spec:
27
+ * "A WKT string contains no white space outside of double quotes.
28
+ * However padding with white space to improve human readability is permitted;
29
+ * the examples of WKT that are included in this document have
30
+ * spaces and line feeds inserted to improve clarity. Any padding is stripped out or ignored by parsers."
31
+ */
4
32
  export function isWKT(input) {
5
- return WKT_MAGIC_STRINGS.some(magicString => input.startsWith(magicString));
33
+ return WKT_MAGIC_STRINGS.some((magicString) => input.startsWith(magicString));
6
34
  }
35
+ /**
36
+ * Parse WKT and return GeoJSON.
37
+ * @param input A WKT geometry string
38
+ * @return A GeoJSON geometry object
39
+ *
40
+ * @note We only support the "geojson" subset of the OGC simple features standard
41
+ **/
7
42
  export function parseWKT(input, options) {
8
- return parseWKTToGeometry(input, options);
43
+ // TODO handle options.wkt.shape
44
+ return parseWKTToGeometry(input, options);
9
45
  }
46
+ /** Parse into GeoJSON geometry */
10
47
  function parseWKTToGeometry(input, options) {
11
- var _options$wkt;
12
- const parts = input.split(';');
13
- let _ = parts.pop();
14
- const srid = (parts.shift() || '').split('=').pop();
15
- const state = {
16
- parts,
17
- _,
18
- i: 0
19
- };
20
- const geometry = parseGeometry(state);
21
- return options !== null && options !== void 0 && (_options$wkt = options.wkt) !== null && _options$wkt !== void 0 && _options$wkt.crs ? addCRS(geometry, srid) : geometry;
48
+ const parts = input.split(';');
49
+ let _ = parts.pop();
50
+ const srid = (parts.shift() || '').split('=').pop();
51
+ const state = { parts, _, i: 0 };
52
+ const geometry = parseGeometry(state);
53
+ return options?.wkt?.crs ? addCRS(geometry, srid) : geometry;
22
54
  }
23
55
  function parseGeometry(state) {
24
- return parsePoint(state) || parseLineString(state) || parsePolygon(state) || parseMultiPoint(state) || parseMultiLineString(state) || parseMultiPolygon(state) || parseGeometryCollection(state);
56
+ return (parsePoint(state) ||
57
+ parseLineString(state) ||
58
+ parsePolygon(state) ||
59
+ parseMultiPoint(state) ||
60
+ parseMultiLineString(state) ||
61
+ parseMultiPolygon(state) ||
62
+ parseGeometryCollection(state));
25
63
  }
64
+ /** Adds a coordinate reference system as an undocumented */
26
65
  function addCRS(obj, srid) {
27
- if (obj && srid !== null && srid !== void 0 && srid.match(/\d+/)) {
28
- const crs = {
29
- type: 'name',
30
- properties: {
31
- name: 'urn:ogc:def:crs:EPSG::' + srid
32
- }
33
- };
34
- obj.crs = crs;
35
- }
36
- return obj;
66
+ if (obj && srid?.match(/\d+/)) {
67
+ const crs = {
68
+ type: 'name',
69
+ properties: {
70
+ name: 'urn:ogc:def:crs:EPSG::' + srid
71
+ }
72
+ };
73
+ // @ts-expect-error we assign an undocumented property on the geometry
74
+ obj.crs = crs;
75
+ }
76
+ return obj;
37
77
  }
78
+ // GEOMETRIES
38
79
  function parsePoint(state) {
39
- if (!$(/^(POINT(\sz)?)/i, state)) {
40
- return null;
41
- }
42
- white(state);
43
- if (!$(/^(\()/, state)) {
44
- return null;
45
- }
46
- const c = coords(state);
47
- if (!c) {
48
- return null;
49
- }
50
- white(state);
51
- if (!$(/^(\))/, state)) {
52
- return null;
53
- }
54
- return {
55
- type: 'Point',
56
- coordinates: c[0]
57
- };
80
+ if (!$(/^(POINT(\sz)?)/i, state)) {
81
+ return null;
82
+ }
83
+ white(state);
84
+ if (!$(/^(\()/, state)) {
85
+ return null;
86
+ }
87
+ const c = coords(state);
88
+ if (!c) {
89
+ return null;
90
+ }
91
+ white(state);
92
+ if (!$(/^(\))/, state)) {
93
+ return null;
94
+ }
95
+ return {
96
+ type: 'Point',
97
+ coordinates: c[0]
98
+ };
58
99
  }
59
100
  function parseMultiPoint(state) {
60
- var _state$_, _state$_2;
61
- if (!$(/^(MULTIPOINT)/i, state)) {
62
- return null;
63
- }
64
- white(state);
65
- const newCoordsFormat = (_state$_ = state._) === null || _state$_ === void 0 ? void 0 : _state$_.substring(((_state$_2 = state._) === null || _state$_2 === void 0 ? void 0 : _state$_2.indexOf('(')) + 1, state._.length - 1).replace(/\(/g, '').replace(/\)/g, '');
66
- state._ = 'MULTIPOINT (' + newCoordsFormat + ')';
67
- const c = multicoords(state);
68
- if (!c) {
69
- return null;
70
- }
71
- white(state);
72
- return {
73
- type: 'MultiPoint',
74
- coordinates: c
75
- };
101
+ if (!$(/^(MULTIPOINT)/i, state)) {
102
+ return null;
103
+ }
104
+ white(state);
105
+ const newCoordsFormat = state._?.substring(state._?.indexOf('(') + 1, state._.length - 1)
106
+ .replace(/\(/g, '')
107
+ .replace(/\)/g, '');
108
+ state._ = 'MULTIPOINT (' + newCoordsFormat + ')';
109
+ const c = multicoords(state);
110
+ if (!c) {
111
+ return null;
112
+ }
113
+ white(state);
114
+ return {
115
+ type: 'MultiPoint',
116
+ coordinates: c
117
+ };
76
118
  }
77
119
  function parseLineString(state) {
78
- if (!$(/^(LINESTRING(\sz)?)/i, state)) {
79
- return null;
80
- }
81
- white(state);
82
- if (!$(/^(\()/, state)) {
83
- return null;
84
- }
85
- const c = coords(state);
86
- if (!c) {
87
- return null;
88
- }
89
- if (!$(/^(\))/, state)) {
90
- return null;
91
- }
92
- return {
93
- type: 'LineString',
94
- coordinates: c
95
- };
120
+ if (!$(/^(LINESTRING(\sz)?)/i, state)) {
121
+ return null;
122
+ }
123
+ white(state);
124
+ if (!$(/^(\()/, state)) {
125
+ return null;
126
+ }
127
+ const c = coords(state);
128
+ if (!c) {
129
+ return null;
130
+ }
131
+ if (!$(/^(\))/, state)) {
132
+ return null;
133
+ }
134
+ return {
135
+ type: 'LineString',
136
+ coordinates: c
137
+ };
96
138
  }
97
139
  function parseMultiLineString(state) {
98
- if (!$(/^(MULTILINESTRING)/i, state)) return null;
99
- white(state);
100
- const c = multicoords(state);
101
- if (!c) {
102
- return null;
103
- }
104
- white(state);
105
- return {
106
- type: 'MultiLineString',
107
- coordinates: c
108
- };
140
+ if (!$(/^(MULTILINESTRING)/i, state))
141
+ return null;
142
+ white(state);
143
+ const c = multicoords(state);
144
+ if (!c) {
145
+ return null;
146
+ }
147
+ white(state);
148
+ return {
149
+ // @ts-ignore
150
+ type: 'MultiLineString',
151
+ // @ts-expect-error
152
+ coordinates: c
153
+ };
109
154
  }
110
155
  function parsePolygon(state) {
111
- if (!$(/^(POLYGON(\sz)?)/i, state)) {
112
- return null;
113
- }
114
- white(state);
115
- const c = multicoords(state);
116
- if (!c) {
117
- return null;
118
- }
119
- return {
120
- type: 'Polygon',
121
- coordinates: c
122
- };
156
+ if (!$(/^(POLYGON(\sz)?)/i, state)) {
157
+ return null;
158
+ }
159
+ white(state);
160
+ const c = multicoords(state);
161
+ if (!c) {
162
+ return null;
163
+ }
164
+ return {
165
+ // @ts-ignore
166
+ type: 'Polygon',
167
+ // @ts-expect-error
168
+ coordinates: c
169
+ };
123
170
  }
124
171
  function parseMultiPolygon(state) {
125
- if (!$(/^(MULTIPOLYGON)/i, state)) {
126
- return null;
127
- }
128
- white(state);
129
- const c = multicoords(state);
130
- if (!c) {
131
- return null;
132
- }
133
- return {
134
- type: 'MultiPolygon',
135
- coordinates: c
136
- };
172
+ if (!$(/^(MULTIPOLYGON)/i, state)) {
173
+ return null;
174
+ }
175
+ white(state);
176
+ const c = multicoords(state);
177
+ if (!c) {
178
+ return null;
179
+ }
180
+ return {
181
+ type: 'MultiPolygon',
182
+ // @ts-expect-error
183
+ coordinates: c
184
+ };
137
185
  }
138
186
  function parseGeometryCollection(state) {
139
- const geometries = [];
140
- let geometry;
141
- if (!$(/^(GEOMETRYCOLLECTION)/i, state)) {
142
- return null;
143
- }
144
- white(state);
145
- if (!$(/^(\()/, state)) {
146
- return null;
147
- }
148
- while (geometry = parseGeometry(state)) {
149
- geometries.push(geometry);
150
- white(state);
151
- $(/^(,)/, state);
187
+ const geometries = [];
188
+ let geometry;
189
+ if (!$(/^(GEOMETRYCOLLECTION)/i, state)) {
190
+ return null;
191
+ }
152
192
  white(state);
153
- }
154
- if (!$(/^(\))/, state)) {
155
- return null;
156
- }
157
- return {
158
- type: 'GeometryCollection',
159
- geometries: geometries
160
- };
193
+ if (!$(/^(\()/, state)) {
194
+ return null;
195
+ }
196
+ while ((geometry = parseGeometry(state))) {
197
+ geometries.push(geometry);
198
+ white(state);
199
+ $(/^(,)/, state);
200
+ white(state);
201
+ }
202
+ if (!$(/^(\))/, state)) {
203
+ return null;
204
+ }
205
+ return {
206
+ type: 'GeometryCollection',
207
+ geometries: geometries
208
+ };
161
209
  }
210
+ // COORDINATES
162
211
  function multicoords(state) {
163
- white(state);
164
- let depth = 0;
165
- const rings = [];
166
- const stack = [rings];
167
- let pointer = rings;
168
- let elem;
169
- while (elem = $(/^(\()/, state) || $(/^(\))/, state) || $(/^(,)/, state) || $(tuples, state)) {
170
- if (elem === '(') {
171
- stack.push(pointer);
172
- pointer = [];
173
- stack[stack.length - 1].push(pointer);
174
- depth++;
175
- } else if (elem === ')') {
176
- if (pointer.length === 0) return null;
177
- pointer = stack.pop();
178
- if (!pointer) return null;
179
- depth--;
180
- if (depth === 0) break;
181
- } else if (elem === ',') {
182
- pointer = [];
183
- stack[stack.length - 1].push(pointer);
184
- } else if (!elem.split(/\s/g).some(isNaN)) {
185
- Array.prototype.push.apply(pointer, elem.split(/\s/g).map(parseFloat));
186
- } else {
187
- return null;
188
- }
189
212
  white(state);
190
- }
191
- if (depth !== 0) return null;
192
- return rings;
213
+ let depth = 0;
214
+ const rings = [];
215
+ const stack = [rings];
216
+ let pointer = rings;
217
+ let elem;
218
+ while ((elem = $(/^(\()/, state) || $(/^(\))/, state) || $(/^(,)/, state) || $(tuples, state))) {
219
+ if (elem === '(') {
220
+ stack.push(pointer);
221
+ pointer = [];
222
+ stack[stack.length - 1].push(pointer);
223
+ depth++;
224
+ }
225
+ else if (elem === ')') {
226
+ // For the case: Polygon(), ...
227
+ if (pointer.length === 0)
228
+ return null;
229
+ // @ts-ignore
230
+ pointer = stack.pop();
231
+ // the stack was empty, input was malformed
232
+ if (!pointer)
233
+ return null;
234
+ depth--;
235
+ if (depth === 0)
236
+ break;
237
+ }
238
+ else if (elem === ',') {
239
+ pointer = [];
240
+ stack[stack.length - 1].push(pointer);
241
+ }
242
+ else if (!elem.split(/\s/g).some(isNaN)) {
243
+ Array.prototype.push.apply(pointer, elem.split(/\s/g).map(parseFloat));
244
+ }
245
+ else {
246
+ return null;
247
+ }
248
+ white(state);
249
+ }
250
+ if (depth !== 0)
251
+ return null;
252
+ return rings;
193
253
  }
194
254
  function coords(state) {
195
- const list = [];
196
- let item;
197
- let pt;
198
- while (pt = $(tuples, state) || $(/^(,)/, state)) {
199
- if (pt === ',') {
200
- list.push(item);
201
- item = [];
202
- } else if (!pt.split(/\s/g).some(isNaN)) {
203
- if (!item) item = [];
204
- Array.prototype.push.apply(item, pt.split(/\s/g).map(parseFloat));
255
+ const list = [];
256
+ let item;
257
+ let pt;
258
+ while ((pt = $(tuples, state) || $(/^(,)/, state))) {
259
+ if (pt === ',') {
260
+ list.push(item);
261
+ item = [];
262
+ }
263
+ else if (!pt.split(/\s/g).some(isNaN)) {
264
+ if (!item)
265
+ item = [];
266
+ Array.prototype.push.apply(item, pt.split(/\s/g).map(parseFloat));
267
+ }
268
+ white(state);
205
269
  }
206
- white(state);
207
- }
208
- if (item) list.push(item);else return null;
209
- return list.length ? list : null;
270
+ if (item)
271
+ list.push(item);
272
+ else
273
+ return null;
274
+ return list.length ? list : null;
210
275
  }
276
+ // HELPERS
211
277
  function $(regexp, state) {
212
- var _state$_3;
213
- const match = (_state$_3 = state._) === null || _state$_3 === void 0 ? void 0 : _state$_3.substring(state.i).match(regexp);
214
- if (!match) return null;else {
215
- state.i += match[0].length;
216
- return match[0];
217
- }
278
+ const match = state._?.substring(state.i).match(regexp);
279
+ if (!match)
280
+ return null;
281
+ else {
282
+ state.i += match[0].length;
283
+ return match[0];
284
+ }
218
285
  }
219
286
  function white(state) {
220
- $(/^\s*/, state);
287
+ $(/^\s*/, state);
221
288
  }
222
- //# sourceMappingURL=parse-wkt.js.map