@loaders.gl/wkt 4.2.0-alpha.3 → 4.2.0-alpha.5
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/dist/dist.dev.js +133 -220
- package/dist/dist.min.js +10 -0
- package/dist/hex-wkb-loader.d.ts +1 -1
- package/dist/hex-wkb-loader.d.ts.map +1 -1
- package/dist/hex-wkb-loader.js +44 -28
- package/dist/index.cjs +77 -58
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +14 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/lib/encode-twkb.js +158 -143
- package/dist/lib/encode-wkb.js +247 -211
- package/dist/lib/encode-wkt-crs.d.ts +1 -1
- package/dist/lib/encode-wkt-crs.d.ts.map +1 -1
- package/dist/lib/encode-wkt-crs.js +32 -18
- package/dist/lib/encode-wkt.js +35 -27
- package/dist/lib/parse-hex-wkb.js +1 -2
- package/dist/lib/parse-twkb.js +215 -218
- package/dist/lib/parse-wkb-header.js +124 -95
- package/dist/lib/parse-wkb.d.ts +1 -1
- package/dist/lib/parse-wkb.d.ts.map +1 -1
- package/dist/lib/parse-wkb.js +210 -235
- package/dist/lib/parse-wkt-crs.js +110 -86
- package/dist/lib/parse-wkt.js +251 -185
- package/dist/lib/utils/base64-encoder.js +151 -5
- package/dist/lib/utils/binary-reader.js +63 -64
- package/dist/lib/utils/binary-writer.js +115 -109
- package/dist/lib/utils/hex-encoder.js +46 -32
- package/dist/lib/utils/hex-transcoder.js +40 -24
- package/dist/lib/utils/version.js +7 -2
- package/dist/twkb-loader.js +26 -17
- package/dist/twkb-writer.js +18 -13
- package/dist/wkb-loader.js +26 -17
- package/dist/wkb-writer.js +22 -17
- package/dist/wkt-crs-loader.d.ts +1 -1
- package/dist/wkt-crs-loader.d.ts.map +1 -1
- package/dist/wkt-crs-loader.js +22 -15
- package/dist/wkt-crs-writer.d.ts +2 -2
- package/dist/wkt-crs-writer.d.ts.map +1 -1
- package/dist/wkt-crs-writer.js +23 -15
- package/dist/wkt-loader.js +28 -20
- package/dist/wkt-worker.js +1 -1
- package/dist/wkt-writer.js +19 -14
- package/dist/workers/wkb-worker.js +3 -1
- package/dist/workers/wkt-worker.js +3 -1
- package/package.json +10 -6
- package/dist/hex-wkb-loader.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/encode-twkb.js.map +0 -1
- package/dist/lib/encode-wkb.js.map +0 -1
- package/dist/lib/encode-wkt-crs.js.map +0 -1
- package/dist/lib/encode-wkt.js.map +0 -1
- package/dist/lib/parse-hex-wkb.js.map +0 -1
- package/dist/lib/parse-twkb.js.map +0 -1
- package/dist/lib/parse-wkb-header.js.map +0 -1
- package/dist/lib/parse-wkb.js.map +0 -1
- package/dist/lib/parse-wkt-crs.js.map +0 -1
- package/dist/lib/parse-wkt.js.map +0 -1
- package/dist/lib/utils/base64-encoder.js.map +0 -1
- package/dist/lib/utils/binary-reader.js.map +0 -1
- package/dist/lib/utils/binary-writer.js.map +0 -1
- package/dist/lib/utils/hex-encoder.js.map +0 -1
- package/dist/lib/utils/hex-transcoder.js.map +0 -1
- package/dist/lib/utils/version.js.map +0 -1
- package/dist/twkb-loader.js.map +0 -1
- package/dist/twkb-writer.js.map +0 -1
- package/dist/wkb-loader.js.map +0 -1
- package/dist/wkb-writer.js.map +0 -1
- package/dist/wkt-crs-loader.js.map +0 -1
- package/dist/wkt-crs-writer.js.map +0 -1
- package/dist/wkt-loader.js.map +0 -1
- package/dist/wkt-writer.js.map +0 -1
- package/dist/workers/wkb-worker.js.map +0 -1
- package/dist/workers/wkt-worker.js.map +0 -1
package/dist/lib/encode-wkb.js
CHANGED
|
@@ -1,250 +1,286 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Forked from https://github.com/cschwarz/wkx under MIT license, Copyright (c) 2013 Christian Schwarz
|
|
5
|
+
// Reference: https://www.ogc.org/standards/sfa
|
|
1
6
|
import { BinaryWriter } from "./utils/binary-writer.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Integer code for geometry type
|
|
9
|
+
* Reference: https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary
|
|
10
|
+
*/
|
|
11
|
+
export var WKB;
|
|
12
|
+
(function (WKB) {
|
|
13
|
+
WKB[WKB["Point"] = 1] = "Point";
|
|
14
|
+
WKB[WKB["LineString"] = 2] = "LineString";
|
|
15
|
+
WKB[WKB["Polygon"] = 3] = "Polygon";
|
|
16
|
+
WKB[WKB["MultiPoint"] = 4] = "MultiPoint";
|
|
17
|
+
WKB[WKB["MultiLineString"] = 5] = "MultiLineString";
|
|
18
|
+
WKB[WKB["MultiPolygon"] = 6] = "MultiPolygon";
|
|
19
|
+
WKB[WKB["GeometryCollection"] = 7] = "GeometryCollection";
|
|
20
|
+
})(WKB || (WKB = {}));
|
|
21
|
+
/**
|
|
22
|
+
* Encodes a GeoJSON object into WKB
|
|
23
|
+
* @param geojson A GeoJSON Feature or Geometry
|
|
24
|
+
* @returns string
|
|
25
|
+
*/
|
|
26
|
+
export function encodeWKB(geometry, options = {}) {
|
|
27
|
+
if (geometry.type === 'Feature') {
|
|
28
|
+
geometry = geometry.geometry;
|
|
29
|
+
}
|
|
30
|
+
switch (geometry.type) {
|
|
31
|
+
case 'Point':
|
|
32
|
+
return encodePoint(geometry.coordinates, options);
|
|
33
|
+
case 'LineString':
|
|
34
|
+
return encodeLineString(geometry.coordinates, options);
|
|
35
|
+
case 'Polygon':
|
|
36
|
+
return encodePolygon(geometry.coordinates, options);
|
|
37
|
+
case 'MultiPoint':
|
|
38
|
+
return encodeMultiPoint(geometry, options);
|
|
39
|
+
case 'MultiPolygon':
|
|
40
|
+
return encodeMultiPolygon(geometry, options);
|
|
41
|
+
case 'MultiLineString':
|
|
42
|
+
return encodeMultiLineString(geometry, options);
|
|
43
|
+
case 'GeometryCollection':
|
|
44
|
+
return encodeGeometryCollection(geometry, options);
|
|
45
|
+
default:
|
|
46
|
+
const exhaustiveCheck = geometry;
|
|
47
|
+
throw new Error(`Unhandled case: ${exhaustiveCheck}`);
|
|
48
|
+
}
|
|
36
49
|
}
|
|
50
|
+
/** Calculate the binary size (in the WKB encoding) of a specific GeoJSON geometry */
|
|
37
51
|
function getGeometrySize(geometry, options) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
switch (geometry.type) {
|
|
53
|
+
case 'Point':
|
|
54
|
+
return getPointSize(options);
|
|
55
|
+
case 'LineString':
|
|
56
|
+
return getLineStringSize(geometry.coordinates, options);
|
|
57
|
+
case 'Polygon':
|
|
58
|
+
return getPolygonSize(geometry.coordinates, options);
|
|
59
|
+
case 'MultiPoint':
|
|
60
|
+
return getMultiPointSize(geometry, options);
|
|
61
|
+
case 'MultiPolygon':
|
|
62
|
+
return getMultiPolygonSize(geometry, options);
|
|
63
|
+
case 'MultiLineString':
|
|
64
|
+
return getMultiLineStringSize(geometry, options);
|
|
65
|
+
case 'GeometryCollection':
|
|
66
|
+
return getGeometryCollectionSize(geometry, options);
|
|
67
|
+
default:
|
|
68
|
+
const exhaustiveCheck = geometry;
|
|
69
|
+
throw new Error(`Unhandled case: ${exhaustiveCheck}`);
|
|
70
|
+
}
|
|
57
71
|
}
|
|
72
|
+
/** Encode Point geometry as WKB ArrayBuffer */
|
|
58
73
|
function encodePoint(coordinates, options) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
74
|
+
const writer = new BinaryWriter(getPointSize(options));
|
|
75
|
+
writer.writeInt8(1);
|
|
76
|
+
writeWkbType(writer, WKB.Point, options);
|
|
77
|
+
// I believe this special case is to handle writing Point(NaN, NaN) correctly
|
|
78
|
+
if (typeof coordinates[0] === 'undefined' && typeof coordinates[1] === 'undefined') {
|
|
79
|
+
writer.writeDoubleLE(NaN);
|
|
80
|
+
writer.writeDoubleLE(NaN);
|
|
81
|
+
if (options.hasZ) {
|
|
82
|
+
writer.writeDoubleLE(NaN);
|
|
83
|
+
}
|
|
84
|
+
if (options.hasM) {
|
|
85
|
+
writer.writeDoubleLE(NaN);
|
|
86
|
+
}
|
|
67
87
|
}
|
|
68
|
-
|
|
69
|
-
|
|
88
|
+
else {
|
|
89
|
+
writeCoordinate(writer, coordinates, options);
|
|
70
90
|
}
|
|
71
|
-
|
|
72
|
-
writeCoordinate(writer, coordinates, options);
|
|
73
|
-
}
|
|
74
|
-
return writer.arrayBuffer;
|
|
91
|
+
return writer.arrayBuffer;
|
|
75
92
|
}
|
|
93
|
+
/** Write coordinate to buffer */
|
|
76
94
|
function writeCoordinate(writer, coordinate, options) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
95
|
+
writer.writeDoubleLE(coordinate[0]);
|
|
96
|
+
writer.writeDoubleLE(coordinate[1]);
|
|
97
|
+
if (options.hasZ) {
|
|
98
|
+
writer.writeDoubleLE(coordinate[2]);
|
|
99
|
+
}
|
|
100
|
+
if (options.hasM) {
|
|
101
|
+
writer.writeDoubleLE(coordinate[3]);
|
|
102
|
+
}
|
|
85
103
|
}
|
|
104
|
+
/** Get encoded size of Point geometry */
|
|
86
105
|
function getPointSize(options) {
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
const coordinateSize = getCoordinateSize(options);
|
|
107
|
+
return 1 + 4 + coordinateSize;
|
|
89
108
|
}
|
|
109
|
+
/** Encode LineString geometry as WKB ArrayBuffer */
|
|
90
110
|
function encodeLineString(coordinates, options) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
111
|
+
const size = getLineStringSize(coordinates, options);
|
|
112
|
+
const writer = new BinaryWriter(size);
|
|
113
|
+
writer.writeInt8(1);
|
|
114
|
+
writeWkbType(writer, WKB.LineString, options);
|
|
115
|
+
writer.writeUInt32LE(coordinates.length);
|
|
116
|
+
for (const coordinate of coordinates) {
|
|
117
|
+
writeCoordinate(writer, coordinate, options);
|
|
118
|
+
}
|
|
119
|
+
return writer.arrayBuffer;
|
|
100
120
|
}
|
|
121
|
+
/** Get encoded size of LineString geometry */
|
|
101
122
|
function getLineStringSize(coordinates, options) {
|
|
102
|
-
|
|
103
|
-
|
|
123
|
+
const coordinateSize = getCoordinateSize(options);
|
|
124
|
+
return 1 + 4 + 4 + coordinates.length * coordinateSize;
|
|
104
125
|
}
|
|
126
|
+
/** Encode Polygon geometry as WKB ArrayBuffer */
|
|
105
127
|
function encodePolygon(coordinates, options) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
128
|
+
const writer = new BinaryWriter(getPolygonSize(coordinates, options));
|
|
129
|
+
writer.writeInt8(1);
|
|
130
|
+
writeWkbType(writer, WKB.Polygon, options);
|
|
131
|
+
const [exteriorRing, ...interiorRings] = coordinates;
|
|
132
|
+
if (exteriorRing.length > 0) {
|
|
133
|
+
writer.writeUInt32LE(1 + interiorRings.length);
|
|
134
|
+
writer.writeUInt32LE(exteriorRing.length);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
writer.writeUInt32LE(0);
|
|
138
|
+
}
|
|
139
|
+
for (const coordinate of exteriorRing) {
|
|
140
|
+
writeCoordinate(writer, coordinate, options);
|
|
141
|
+
}
|
|
142
|
+
for (const interiorRing of interiorRings) {
|
|
143
|
+
writer.writeUInt32LE(interiorRing.length);
|
|
144
|
+
for (const coordinate of interiorRing) {
|
|
145
|
+
writeCoordinate(writer, coordinate, options);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return writer.arrayBuffer;
|
|
126
149
|
}
|
|
150
|
+
/** Get encoded size of Polygon geometry */
|
|
127
151
|
function getPolygonSize(coordinates, options) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
152
|
+
const coordinateSize = getCoordinateSize(options);
|
|
153
|
+
const [exteriorRing, ...interiorRings] = coordinates;
|
|
154
|
+
let size = 1 + 4 + 4;
|
|
155
|
+
if (exteriorRing.length > 0) {
|
|
156
|
+
size += 4 + exteriorRing.length * coordinateSize;
|
|
157
|
+
}
|
|
158
|
+
for (const interiorRing of interiorRings) {
|
|
159
|
+
size += 4 + interiorRing.length * coordinateSize;
|
|
160
|
+
}
|
|
161
|
+
return size;
|
|
138
162
|
}
|
|
163
|
+
/** Encode MultiPoint geometry as WKB ArrayBufer */
|
|
139
164
|
function encodeMultiPoint(multiPoint, options) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
165
|
+
const writer = new BinaryWriter(getMultiPointSize(multiPoint, options));
|
|
166
|
+
const points = multiPoint.coordinates;
|
|
167
|
+
writer.writeInt8(1);
|
|
168
|
+
writeWkbType(writer, WKB.MultiPoint, options);
|
|
169
|
+
writer.writeUInt32LE(points.length);
|
|
170
|
+
for (const point of points) {
|
|
171
|
+
// TODO: add srid to this options object? {srid: multiPoint.srid}
|
|
172
|
+
const arrayBuffer = encodePoint(point, options);
|
|
173
|
+
writer.writeBuffer(arrayBuffer);
|
|
174
|
+
}
|
|
175
|
+
return writer.arrayBuffer;
|
|
150
176
|
}
|
|
177
|
+
/** Get encoded size of MultiPoint geometry */
|
|
151
178
|
function getMultiPointSize(multiPoint, options) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
179
|
+
let coordinateSize = getCoordinateSize(options);
|
|
180
|
+
const points = multiPoint.coordinates;
|
|
181
|
+
// This is because each point has a 5-byte header?
|
|
182
|
+
coordinateSize += 5;
|
|
183
|
+
return 1 + 4 + 4 + points.length * coordinateSize;
|
|
156
184
|
}
|
|
185
|
+
/** Encode MultiLineString geometry as WKB ArrayBufer */
|
|
157
186
|
function encodeMultiLineString(multiLineString, options) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
187
|
+
const writer = new BinaryWriter(getMultiLineStringSize(multiLineString, options));
|
|
188
|
+
const lineStrings = multiLineString.coordinates;
|
|
189
|
+
writer.writeInt8(1);
|
|
190
|
+
writeWkbType(writer, WKB.MultiLineString, options);
|
|
191
|
+
writer.writeUInt32LE(lineStrings.length);
|
|
192
|
+
for (const lineString of lineStrings) {
|
|
193
|
+
// TODO: Handle srid?
|
|
194
|
+
const encodedLineString = encodeLineString(lineString, options);
|
|
195
|
+
writer.writeBuffer(encodedLineString);
|
|
196
|
+
}
|
|
197
|
+
return writer.arrayBuffer;
|
|
168
198
|
}
|
|
199
|
+
/** Get encoded size of MultiLineString geometry */
|
|
169
200
|
function getMultiLineStringSize(multiLineString, options) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
201
|
+
let size = 1 + 4 + 4;
|
|
202
|
+
const lineStrings = multiLineString.coordinates;
|
|
203
|
+
for (const lineString of lineStrings) {
|
|
204
|
+
size += getLineStringSize(lineString, options);
|
|
205
|
+
}
|
|
206
|
+
return size;
|
|
176
207
|
}
|
|
177
208
|
function encodeMultiPolygon(multiPolygon, options) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
209
|
+
const writer = new BinaryWriter(getMultiPolygonSize(multiPolygon, options));
|
|
210
|
+
const polygons = multiPolygon.coordinates;
|
|
211
|
+
writer.writeInt8(1);
|
|
212
|
+
writeWkbType(writer, WKB.MultiPolygon, options);
|
|
213
|
+
writer.writeUInt32LE(polygons.length);
|
|
214
|
+
for (const polygon of polygons) {
|
|
215
|
+
const encodedPolygon = encodePolygon(polygon, options);
|
|
216
|
+
writer.writeBuffer(encodedPolygon);
|
|
217
|
+
}
|
|
218
|
+
return writer.arrayBuffer;
|
|
188
219
|
}
|
|
189
220
|
function getMultiPolygonSize(multiPolygon, options) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
221
|
+
let size = 1 + 4 + 4;
|
|
222
|
+
const polygons = multiPolygon.coordinates;
|
|
223
|
+
for (const polygon of polygons) {
|
|
224
|
+
size += getPolygonSize(polygon, options);
|
|
225
|
+
}
|
|
226
|
+
return size;
|
|
196
227
|
}
|
|
197
228
|
function encodeGeometryCollection(collection, options) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
229
|
+
const writer = new BinaryWriter(getGeometryCollectionSize(collection, options));
|
|
230
|
+
writer.writeInt8(1);
|
|
231
|
+
writeWkbType(writer, WKB.GeometryCollection, options);
|
|
232
|
+
writer.writeUInt32LE(collection.geometries.length);
|
|
233
|
+
for (const geometry of collection.geometries) {
|
|
234
|
+
// TODO: handle srid? {srid: collection.srid}
|
|
235
|
+
const arrayBuffer = encodeWKB(geometry, options);
|
|
236
|
+
writer.writeBuffer(arrayBuffer);
|
|
237
|
+
}
|
|
238
|
+
return writer.arrayBuffer;
|
|
207
239
|
}
|
|
208
240
|
function getGeometryCollectionSize(collection, options) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
241
|
+
let size = 1 + 4 + 4;
|
|
242
|
+
for (const geometry of collection.geometries) {
|
|
243
|
+
size += getGeometrySize(geometry, options);
|
|
244
|
+
}
|
|
245
|
+
return size;
|
|
214
246
|
}
|
|
247
|
+
// HELPERS
|
|
248
|
+
/**
|
|
249
|
+
* Construct and write WKB integer code
|
|
250
|
+
* Reference: https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary
|
|
251
|
+
*/
|
|
215
252
|
function writeWkbType(writer, geometryType, options) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
writer.writeUInt32LE(dimensionType + geometryType >>> 0);
|
|
253
|
+
const { hasZ, hasM, srid } = options;
|
|
254
|
+
let dimensionType = 0;
|
|
255
|
+
if (!srid) {
|
|
256
|
+
if (hasZ && hasM) {
|
|
257
|
+
dimensionType += 3000;
|
|
258
|
+
}
|
|
259
|
+
else if (hasZ) {
|
|
260
|
+
dimensionType += 1000;
|
|
261
|
+
}
|
|
262
|
+
else if (hasM) {
|
|
263
|
+
dimensionType += 2000;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
if (hasZ) {
|
|
268
|
+
dimensionType |= 0x80000000;
|
|
269
|
+
}
|
|
270
|
+
if (hasM) {
|
|
271
|
+
dimensionType |= 0x40000000;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
writer.writeUInt32LE((dimensionType + geometryType) >>> 0);
|
|
239
275
|
}
|
|
276
|
+
/** Get coordinate size given Z/M dimensions */
|
|
240
277
|
function getCoordinateSize(options) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}
|
|
250
|
-
//# sourceMappingURL=encode-wkb.js.map
|
|
278
|
+
let coordinateSize = 16;
|
|
279
|
+
if (options.hasZ) {
|
|
280
|
+
coordinateSize += 8;
|
|
281
|
+
}
|
|
282
|
+
if (options.hasM) {
|
|
283
|
+
coordinateSize += 8;
|
|
284
|
+
}
|
|
285
|
+
return coordinateSize;
|
|
286
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode-wkt-crs.d.ts","sourceRoot":"","sources":["../../src/lib/encode-wkt-crs.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,MAAM,EAAC,
|
|
1
|
+
{"version":3,"file":"encode-wkt-crs.d.ts","sourceRoot":"","sources":["../../src/lib/encode-wkt-crs.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,MAAM,EAAC,2BAAwB;AAE5C,MAAM,MAAM,mBAAmB,GAAG;IAChC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF;;;GAGG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,MAAM,CAyB/E"}
|
|
@@ -1,21 +1,35 @@
|
|
|
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
|
+
* convert JSON representation of Well-Known Text
|
|
7
|
+
* back to standard Well-Known Text
|
|
8
|
+
*/
|
|
1
9
|
export function encodeWKTCRS(wkt, options) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
const [kw, ...attrs] = wkt;
|
|
6
|
-
const str = `${kw}[${attrs.map(attr => {
|
|
7
|
-
if (Array.isArray(attr)) {
|
|
8
|
-
return encodeWKTCRS(attr, options);
|
|
9
|
-
} else if (typeof attr === 'number') {
|
|
10
|
-
return attr.toString();
|
|
11
|
-
} else if (typeof attr === 'string') {
|
|
12
|
-
if (attr.startsWith('raw:')) {
|
|
13
|
-
return attr.replace('raw:', '');
|
|
14
|
-
}
|
|
15
|
-
return `"${attr}"`;
|
|
10
|
+
if (Array.isArray(wkt) && wkt.length === 1 && Array.isArray(wkt[0])) {
|
|
11
|
+
wkt = wkt[0]; // ignore first extra wrapper array
|
|
16
12
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
const [kw, ...attrs] = wkt;
|
|
14
|
+
const str = `${kw}[${attrs
|
|
15
|
+
.map((attr) => {
|
|
16
|
+
if (Array.isArray(attr)) {
|
|
17
|
+
return encodeWKTCRS(attr, options);
|
|
18
|
+
}
|
|
19
|
+
else if (typeof attr === 'number') {
|
|
20
|
+
return attr.toString();
|
|
21
|
+
}
|
|
22
|
+
else if (typeof attr === 'string') {
|
|
23
|
+
// can't automatically convert all caps to varibale
|
|
24
|
+
// because EPSG is string in AUTHORITY["EPSG", ...]
|
|
25
|
+
if (attr.startsWith('raw:')) {
|
|
26
|
+
// convert "raw:NORTH" to NORTH
|
|
27
|
+
return attr.replace('raw:', '');
|
|
28
|
+
}
|
|
29
|
+
return `"${attr}"`;
|
|
30
|
+
}
|
|
31
|
+
throw new Error(`[wktcrs] unexpected attribute "${attr}"`);
|
|
32
|
+
})
|
|
33
|
+
.join(',')}]`;
|
|
34
|
+
return str;
|
|
20
35
|
}
|
|
21
|
-
//# sourceMappingURL=encode-wkt-crs.js.map
|
package/dist/lib/encode-wkt.js
CHANGED
|
@@ -1,39 +1,47 @@
|
|
|
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
|
+
/**
|
|
6
|
+
* Stringifies a GeoJSON object into WKT
|
|
7
|
+
* @param geojson
|
|
8
|
+
* @returns string
|
|
9
|
+
*/
|
|
1
10
|
export function encodeWKT(geometry) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
if (geometry.type === 'Feature') {
|
|
12
|
+
geometry = geometry.geometry;
|
|
13
|
+
}
|
|
14
|
+
switch (geometry.type) {
|
|
15
|
+
case 'Point':
|
|
16
|
+
return `POINT ${wrapParens(pairWKT(geometry.coordinates))}`;
|
|
17
|
+
case 'LineString':
|
|
18
|
+
return `LINESTRING ${wrapParens(ringWKT(geometry.coordinates))}`;
|
|
19
|
+
case 'Polygon':
|
|
20
|
+
return `POLYGON ${wrapParens(ringsWKT(geometry.coordinates))}`;
|
|
21
|
+
case 'MultiPoint':
|
|
22
|
+
return `MULTIPOINT ${wrapParens(ringWKT(geometry.coordinates))}`;
|
|
23
|
+
case 'MultiPolygon':
|
|
24
|
+
return `MULTIPOLYGON ${wrapParens(multiRingsWKT(geometry.coordinates))}`;
|
|
25
|
+
case 'MultiLineString':
|
|
26
|
+
return `MULTILINESTRING ${wrapParens(ringsWKT(geometry.coordinates))}`;
|
|
27
|
+
case 'GeometryCollection':
|
|
28
|
+
return `GEOMETRYCOLLECTION ${wrapParens(geometry.geometries.map(encodeWKT).join(', '))}`;
|
|
29
|
+
default:
|
|
30
|
+
throw new Error('stringify requires a valid GeoJSON Feature or geometry object as input');
|
|
31
|
+
}
|
|
23
32
|
}
|
|
24
33
|
function pairWKT(c) {
|
|
25
|
-
|
|
34
|
+
return c.join(' ');
|
|
26
35
|
}
|
|
27
36
|
function ringWKT(r) {
|
|
28
|
-
|
|
37
|
+
return r.map(pairWKT).join(', ');
|
|
29
38
|
}
|
|
30
39
|
function ringsWKT(r) {
|
|
31
|
-
|
|
40
|
+
return r.map(ringWKT).map(wrapParens).join(', ');
|
|
32
41
|
}
|
|
33
42
|
function multiRingsWKT(r) {
|
|
34
|
-
|
|
43
|
+
return r.map(ringsWKT).map(wrapParens).join(', ');
|
|
35
44
|
}
|
|
36
45
|
function wrapParens(s) {
|
|
37
|
-
|
|
46
|
+
return `(${s})`;
|
|
38
47
|
}
|
|
39
|
-
//# sourceMappingURL=encode-wkt.js.map
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=parse-hex-wkb.js.map
|
|
1
|
+
"use strict";
|