@loaders.gl/geoarrow 4.4.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/LICENSE +41 -0
  2. package/README.md +7 -0
  3. package/dist/geo-column/geo-column-info.d.ts +20 -0
  4. package/dist/geo-column/geo-column-info.d.ts.map +1 -0
  5. package/dist/geo-column/geo-column-info.js +74 -0
  6. package/dist/geo-column/geo-column.d.ts +61 -0
  7. package/dist/geo-column/geo-column.d.ts.map +1 -0
  8. package/dist/geo-column/geo-column.js +4 -0
  9. package/dist/geoarrow-functions.d.ts +131 -0
  10. package/dist/geoarrow-functions.d.ts.map +1 -0
  11. package/dist/geoarrow-functions.js +218 -0
  12. package/dist/geoarrow-types.d.ts +49 -0
  13. package/dist/geoarrow-types.d.ts.map +1 -0
  14. package/dist/geoarrow-types.js +4 -0
  15. package/dist/get-arrow-bounds.d.ts +11 -0
  16. package/dist/get-arrow-bounds.d.ts.map +1 -0
  17. package/dist/get-arrow-bounds.js +34 -0
  18. package/dist/get-geoarrow-geometry-info.d.ts +30 -0
  19. package/dist/get-geoarrow-geometry-info.d.ts.map +1 -0
  20. package/dist/get-geoarrow-geometry-info.js +124 -0
  21. package/dist/index.cjs +430 -0
  22. package/dist/index.cjs.map +7 -0
  23. package/dist/index.d.ts +11 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +12 -0
  26. package/dist/mesharrow/arrow-fixed-size-list-utils.d.ts +11 -0
  27. package/dist/mesharrow/arrow-fixed-size-list-utils.d.ts.map +1 -0
  28. package/dist/mesharrow/arrow-fixed-size-list-utils.js +39 -0
  29. package/dist/mesharrow/arrow-list-of-fixed-size-list-utils.d.ts +11 -0
  30. package/dist/mesharrow/arrow-list-of-fixed-size-list-utils.d.ts.map +1 -0
  31. package/dist/mesharrow/arrow-list-of-fixed-size-list-utils.js +31 -0
  32. package/dist/mesharrow/get-bounding-box.d.ts +5 -0
  33. package/dist/mesharrow/get-bounding-box.d.ts.map +1 -0
  34. package/dist/mesharrow/get-bounding-box.js +33 -0
  35. package/dist/mesharrow/get-deck-binary-data.d.ts +13 -0
  36. package/dist/mesharrow/get-deck-binary-data.d.ts.map +1 -0
  37. package/dist/mesharrow/get-deck-binary-data.js +24 -0
  38. package/dist/mesharrow/mesh-accessors.d.ts +8 -0
  39. package/dist/mesharrow/mesh-accessors.d.ts.map +1 -0
  40. package/dist/mesharrow/mesh-accessors.js +18 -0
  41. package/dist/metadata/geoarrow-metadata.d.ts +27 -0
  42. package/dist/metadata/geoarrow-metadata.d.ts.map +1 -0
  43. package/dist/metadata/geoarrow-metadata.js +71 -0
  44. package/dist/metadata/geoparquet-metadata-schema.d.ts +79 -0
  45. package/dist/metadata/geoparquet-metadata-schema.d.ts.map +1 -0
  46. package/dist/metadata/geoparquet-metadata-schema.js +69 -0
  47. package/dist/metadata/geoparquet-metadata.d.ts +45 -0
  48. package/dist/metadata/geoparquet-metadata.d.ts.map +1 -0
  49. package/dist/metadata/geoparquet-metadata.js +131 -0
  50. package/dist/metadata/metadata-utils.d.ts +21 -0
  51. package/dist/metadata/metadata-utils.d.ts.map +1 -0
  52. package/dist/metadata/metadata-utils.js +14 -0
  53. package/package.json +63 -0
  54. package/src/geo-column/geo-column-info.ts +114 -0
  55. package/src/geo-column/geo-column.ts +85 -0
  56. package/src/geoarrow-functions.ts +258 -0
  57. package/src/geoarrow-types.ts +72 -0
  58. package/src/get-arrow-bounds.ts +41 -0
  59. package/src/get-geo-column-from-geoarrow.ts.disabled +251 -0
  60. package/src/get-geoarrow-geometry-info.ts +172 -0
  61. package/src/index.ts +57 -0
  62. package/src/mesharrow/arrow-fixed-size-list-utils.ts +62 -0
  63. package/src/mesharrow/arrow-list-of-fixed-size-list-utils.ts +47 -0
  64. package/src/mesharrow/get-bounding-box.ts +39 -0
  65. package/src/mesharrow/get-deck-binary-data.ts +43 -0
  66. package/src/mesharrow/mesh-accessors.ts +27 -0
  67. package/src/metadata/geoarrow-metadata.ts +102 -0
  68. package/src/metadata/geoparquet-metadata-schema.ts +71 -0
  69. package/src/metadata/geoparquet-metadata.ts +195 -0
  70. package/src/metadata/metadata-utils.ts +32 -0
@@ -0,0 +1,195 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /* eslint-disable camelcase */
6
+
7
+ import {Metadata, getMetadataValue, setMetadataValue} from './metadata-utils';
8
+
9
+ /**
10
+ * A geoarrow / geoparquet geo metadata object
11
+ * (stored in stringified form in the top level metadata 'geo' key)
12
+ * @see https://github.com/opengeospatial/geoparquet/blob/main/format-specs/geoparquet.md
13
+ * @see https://github.com/geoarrow/geoarrow
14
+ * */
15
+ export type GeoMetadata = {
16
+ version?: string;
17
+ primary_column?: string;
18
+ columns: Record<string, GeoColumnMetadata>;
19
+ [key: string]: unknown;
20
+ };
21
+
22
+ /** A geoarrow / geoparquet geo metadata for one geometry column */
23
+ export type GeoColumnMetadata = {
24
+ encoding: 'wkb' | 'wkt';
25
+ geometry_types: GeoParquetGeometryType[];
26
+ crs?: object | null;
27
+ orientation?: 'counterclockwise';
28
+ bbox?: [number, number, number, number] | [number, number, number, number, number, number];
29
+ edges?: 'planar' | 'spherical';
30
+ epoch?: number;
31
+ [key: string]: unknown;
32
+ };
33
+
34
+ /** A GeoParquet metadata geometry type */
35
+ export type GeoParquetGeometryType =
36
+ | 'Point'
37
+ | 'LineString'
38
+ | 'Polygon'
39
+ | 'MultiPoint'
40
+ | 'MultiLineString'
41
+ | 'MultiPolygon'
42
+ | 'GeometryCollection'
43
+ | 'Point Z'
44
+ | 'LineString Z'
45
+ | 'Polygon Z'
46
+ | 'MultiPoint Z'
47
+ | 'MultiLineString Z'
48
+ | 'MultiPolygon Z'
49
+ | 'GeometryCollection Z';
50
+
51
+ // GEO METADATA
52
+
53
+ /**
54
+ * Reads the GeoMetadata object from the metadata
55
+ * @note geoarrow / parquet schema is stringified into a single key-value pair in the parquet metadata
56
+ */
57
+ export function getGeoMetadata(metadata: Metadata): GeoMetadata | null {
58
+ const stringifiedGeoMetadata = getMetadataValue(metadata, 'geo');
59
+ const geoMetadata = stringifiedGeoMetadata && parseJSONStringMetadata(stringifiedGeoMetadata);
60
+ if (!geoMetadata) {
61
+ return null;
62
+ }
63
+ for (const column of Object.values(geoMetadata.columns || {})) {
64
+ if (column.encoding) {
65
+ column.encoding = column.encoding.toLowerCase();
66
+ }
67
+ }
68
+ return geoMetadata as GeoMetadata;
69
+ }
70
+
71
+ /**
72
+ * Stores a geoarrow / geoparquet geo metadata object in the schema
73
+ * @note geoarrow / geoparquet geo metadata is a single stringified JSON field
74
+ */
75
+ export function setGeoMetadata(metadata: Metadata, geoMetadata: GeoMetadata): void {
76
+ const stringifiedGeoMetadata = JSON.stringify(geoMetadata);
77
+ setMetadataValue(metadata, 'geo', stringifiedGeoMetadata);
78
+ }
79
+
80
+ /**
81
+ * Unpacks geo metadata into separate metadata fields (parses the long JSON string)
82
+ * @note geoarrow / parquet schema is stringified into a single key-value pair in the parquet metadata
83
+ */
84
+ export function unpackGeoMetadata(metadata: Metadata): void {
85
+ const geoMetadata = getGeoMetadata(metadata);
86
+ if (!geoMetadata) {
87
+ return;
88
+ }
89
+
90
+ // Store Parquet Schema Level Metadata
91
+
92
+ const {version, primary_column, columns} = geoMetadata;
93
+ if (version) {
94
+ setMetadataValue(metadata, 'geo.version', version);
95
+ }
96
+
97
+ if (primary_column) {
98
+ setMetadataValue(metadata, 'geo.primary_column', primary_column);
99
+ }
100
+
101
+ // store column names as comma separated list
102
+ setMetadataValue(metadata, 'geo.columns', Object.keys(columns || {}).join(''));
103
+
104
+ // for (const [columnName, columnMetadata] of Object.entries(columns || {})) {
105
+ // const field = schema.fields.find((field) => field.name === columnName);
106
+ // if (field) {
107
+ // if (field.name === primary_column) {
108
+ // setFieldMetadata(field, 'geo.primary_field', 'true');
109
+ // }
110
+ // unpackGeoFieldMetadata(field, columnMetadata);
111
+ // }
112
+ // }
113
+ }
114
+
115
+ export function unpackJSONStringMetadata(metadata: Metadata, metadataKey: string): void {
116
+ const stringifiedGeoMetadata = getMetadataValue(metadata, 'geo');
117
+ const json = stringifiedGeoMetadata && parseJSONStringMetadata(stringifiedGeoMetadata);
118
+ for (const [key, value] of Object.entries(json || {})) {
119
+ setMetadataValue(
120
+ metadata,
121
+ `${metadataKey}.${key}`,
122
+ typeof value === 'string' ? value : JSON.stringify(value)
123
+ );
124
+ }
125
+ }
126
+
127
+ // eslint-disable-next-line complexity
128
+ // function unpackGeoFieldMetadata(fieldMetadata: Metadata, columnMetadata): void {
129
+ // for (const [key, value] of Object.entries(columnMetadata || {})) {
130
+ // switch (key) {
131
+ // case 'geometry_types':
132
+ // setFieldMetadata(field, `geo.${key}`, (value as string[]).join(','));
133
+ // break;
134
+ // case 'bbox':
135
+ // setFieldMetadata(field, `geo.crs.${key}`, JSON.stringify(value));
136
+ // break;
137
+ // case 'crs':
138
+ // // @ts-ignore
139
+ // for (const [crsKey, crsValue] of Object.entries(value || {})) {
140
+ // switch (crsKey) {
141
+ // case 'id':
142
+ // // prettier-ignore
143
+ // const crsId =
144
+ // typeof crsValue === 'object'
145
+ // ? // @ts-ignore
146
+ // `${crsValue?.authority}:${crsValue?.code}`
147
+ // : JSON.stringify(crsValue);
148
+ // setFieldMetadata(field, `geo.crs.${crsKey}`, crsId);
149
+ // break;
150
+ // default:
151
+ // setFieldMetadata(
152
+ // field,
153
+ // `geo.crs.${crsKey}`,
154
+ // typeof crsValue === 'string' ? crsValue : JSON.stringify(crsValue)
155
+ // );
156
+ // break;
157
+ // }
158
+ // }
159
+ // break;
160
+ // case 'edges':
161
+ // default:
162
+ // setFieldMetadata(
163
+ // field,
164
+ // `geo.${key}`,
165
+ // typeof value === 'string' ? value : JSON.stringify(value)
166
+ // );
167
+ // }
168
+ // }
169
+ // }
170
+
171
+ // function setFieldMetadata(field: Field, key: string, value: string): void {
172
+ // field.metadata = field.metadata || {};
173
+ // field.metadata[key] = value;
174
+ // }
175
+
176
+ // HELPERS
177
+
178
+ /** Parse a key with stringified arrow metadata */
179
+ export function parseJSONStringMetadata(
180
+ stringifiedMetadata: string
181
+ ): Record<string, unknown> | null {
182
+ if (!stringifiedMetadata) {
183
+ return null;
184
+ }
185
+
186
+ try {
187
+ const metadata = JSON.parse(stringifiedMetadata);
188
+ if (!metadata || typeof metadata !== 'object') {
189
+ return null;
190
+ }
191
+ return metadata;
192
+ } catch {
193
+ return null;
194
+ }
195
+ }
@@ -0,0 +1,32 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /**
6
+ * A type that matches a string-to-string metadata map
7
+ * in the form of a map or an object.
8
+ */
9
+ export type Metadata = Map<string, string> | Record<string, string>;
10
+
11
+ /**
12
+ * A type that matches a variety of Apache arrow like schemas.
13
+ *
14
+ */
15
+ export type SchemaWithMetadata = {
16
+ /** Schema level metadata */
17
+ metadata?: Metadata;
18
+ /** Field top-level metadata */
19
+ fields?: {name: string; metadata?: Metadata}[];
20
+ };
21
+
22
+ export function getMetadataValue(metadata: Metadata, key: string): string | null {
23
+ return metadata instanceof Map ? metadata.get(key) || null : metadata[key] || null;
24
+ }
25
+
26
+ export function setMetadataValue(metadata: Metadata, key: string, value: string): void {
27
+ if (metadata instanceof Map) {
28
+ metadata.set('key', key);
29
+ } else {
30
+ metadata.key = key;
31
+ }
32
+ }