@nshiab/simple-data-analysis-core 0.0.19 → 0.0.21

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 (63) hide show
  1. package/esm/class/SimpleTable.d.ts +117 -22
  2. package/esm/class/SimpleTable.d.ts.map +1 -1
  3. package/esm/class/SimpleTable.js +172 -104
  4. package/esm/helpers/findGeoColumn.js +1 -1
  5. package/esm/helpers/getProjection.d.ts.map +1 -1
  6. package/esm/helpers/getProjection.js +3 -0
  7. package/esm/helpers/hasGeometryColumn.d.ts +20 -0
  8. package/esm/helpers/hasGeometryColumn.d.ts.map +1 -0
  9. package/esm/helpers/hasGeometryColumn.js +21 -0
  10. package/esm/helpers/writeGeoData.d.ts +9 -0
  11. package/esm/helpers/writeGeoData.d.ts.map +1 -0
  12. package/esm/helpers/writeGeoData.js +110 -0
  13. package/esm/methods/fill.d.ts.map +1 -1
  14. package/esm/methods/fill.js +16 -16
  15. package/esm/methods/fuzzyClean.d.ts +2 -2
  16. package/esm/methods/fuzzyClean.d.ts.map +1 -1
  17. package/esm/methods/fuzzyClean.js +25 -6
  18. package/esm/methods/fuzzyJoin.d.ts +2 -2
  19. package/esm/methods/fuzzyJoin.d.ts.map +1 -1
  20. package/esm/methods/fuzzyJoin.js +3 -3
  21. package/esm/methods/fuzzyJoinQuery.d.ts +1 -1
  22. package/esm/methods/fuzzyJoinQuery.d.ts.map +1 -1
  23. package/esm/methods/fuzzyJoinQuery.js +13 -3
  24. package/esm/methods/loadDataQuery.d.ts +1 -0
  25. package/esm/methods/loadDataQuery.d.ts.map +1 -1
  26. package/esm/methods/loadDataQuery.js +12 -4
  27. package/esm/methods/padQuery.d.ts +5 -0
  28. package/esm/methods/padQuery.d.ts.map +1 -0
  29. package/esm/methods/padQuery.js +14 -0
  30. package/esm/methods/writeGeoDataQuery.d.ts.map +1 -1
  31. package/esm/methods/writeGeoDataQuery.js +4 -1
  32. package/package.json +1 -1
  33. package/script/class/SimpleTable.d.ts +117 -22
  34. package/script/class/SimpleTable.d.ts.map +1 -1
  35. package/script/class/SimpleTable.js +172 -104
  36. package/script/helpers/findGeoColumn.js +1 -1
  37. package/script/helpers/getProjection.d.ts.map +1 -1
  38. package/script/helpers/getProjection.js +3 -0
  39. package/script/helpers/hasGeometryColumn.d.ts +20 -0
  40. package/script/helpers/hasGeometryColumn.d.ts.map +1 -0
  41. package/script/helpers/hasGeometryColumn.js +24 -0
  42. package/script/helpers/writeGeoData.d.ts +9 -0
  43. package/script/helpers/writeGeoData.d.ts.map +1 -0
  44. package/script/helpers/writeGeoData.js +116 -0
  45. package/script/methods/fill.d.ts.map +1 -1
  46. package/script/methods/fill.js +16 -16
  47. package/script/methods/fuzzyClean.d.ts +2 -2
  48. package/script/methods/fuzzyClean.d.ts.map +1 -1
  49. package/script/methods/fuzzyClean.js +25 -6
  50. package/script/methods/fuzzyJoin.d.ts +2 -2
  51. package/script/methods/fuzzyJoin.d.ts.map +1 -1
  52. package/script/methods/fuzzyJoin.js +3 -3
  53. package/script/methods/fuzzyJoinQuery.d.ts +1 -1
  54. package/script/methods/fuzzyJoinQuery.d.ts.map +1 -1
  55. package/script/methods/fuzzyJoinQuery.js +13 -3
  56. package/script/methods/loadDataQuery.d.ts +1 -0
  57. package/script/methods/loadDataQuery.d.ts.map +1 -1
  58. package/script/methods/loadDataQuery.js +12 -4
  59. package/script/methods/padQuery.d.ts +5 -0
  60. package/script/methods/padQuery.d.ts.map +1 -0
  61. package/script/methods/padQuery.js +17 -0
  62. package/script/methods/writeGeoDataQuery.d.ts.map +1 -1
  63. package/script/methods/writeGeoDataQuery.js +4 -1
@@ -47,6 +47,7 @@ import normalizeQuery from "../methods/normalizeQuery.js";
47
47
  import rollingQuery from "../methods/rollingQuery.js";
48
48
  import distanceQuery from "../methods/distanceQuery.js";
49
49
  import getGeoData from "../methods/getGeoData.js";
50
+ import writeGeoData from "../helpers/writeGeoData.js";
50
51
  import splitSpread from "../methods/splitSpread.js";
51
52
  import { readdirSync } from "node:fs";
52
53
  import stringToArray from "../helpers/stringToArray.js";
@@ -54,24 +55,20 @@ import loadDataQuery from "../methods/loadDataQuery.js";
54
55
  import mergeOptions from "../helpers/mergeOptions.js";
55
56
  import queryDB from "../helpers/queryDB.js";
56
57
  import writeDataQuery from "../methods/writeDataQuery.js";
57
- import writeGeoDataQuery from "../methods/writeGeoDataQuery.js";
58
58
  import runQuery from "../helpers/runQuery.js";
59
59
  import aggregateGeoQuery from "../methods/aggregateGeoQuery.js";
60
60
  import summarize from "../methods/summarize.js";
61
61
  import correlations from "../methods/correlations.js";
62
62
  import linearRegressions from "../methods/linearRegressions.js";
63
63
  import joinGeo from "../methods/joinGeo.js";
64
- import shouldFlipBeforeExport from "../helpers/shouldFlipBeforeExport.js";
65
64
  import getProjection from "../helpers/getProjection.js";
66
65
  import cache from "../methods/cache.js";
67
66
  import camelCase from "../helpers/camelCase.js";
68
67
  import formatNumber from "../helpers/formatNumber.js";
69
68
  import createDirectory from "../helpers/createDirectory.js";
70
- import rewind from "../helpers/rewind.js";
71
69
  import writeDataAsArrays from "../helpers/writeDataAsArrays.js";
72
70
  import logData from "../helpers/logData.js";
73
71
  import fill from "../methods/fill.js";
74
- import { readFileSync, writeFileSync } from "node:fs";
75
72
  import loadArray from "../methods/loadArray.js";
76
73
  import cleanPath from "../helpers/cleanPath.js";
77
74
  import Simple from "./Simple.js";
@@ -86,11 +83,11 @@ import getExtension from "../helpers/getExtension.js";
86
83
  import getIdenticalColumns from "../helpers/getIdenticalColumns.js";
87
84
  import capitalizeQuery from "../methods/capitalizeQuery.js";
88
85
  import truncateQuery from "../methods/truncateQuery.js";
86
+ import padQuery from "../methods/padQuery.js";
89
87
  import getProjectionParquet from "../helpers/getProjectionParquet.js";
88
+ import hasGeometryColumn from "../helpers/hasGeometryColumn.js";
90
89
  import unifyColumns from "../helpers/unifyColumns.js";
91
90
  import accumulateQuery from "../helpers/accumulateQuery.js";
92
- import stringifyDates from "../helpers/stringifyDates.js";
93
- import stringifyDatesInvert from "../helpers/stringifyDatesInvert.js";
94
91
  import unnestQuery from "../helpers/unnestQuery.js";
95
92
  import nestQuery from "../helpers/nestQuery.js";
96
93
  import concatenateRowQuery from "../helpers/concatenateRowQuery.js";
@@ -286,6 +283,7 @@ export default class SimpleTable extends Simple {
286
283
  * @param options.fileName - A boolean indicating whether to include the file name as a new column in the loaded data. Defaults to `false`.
287
284
  * @param options.unifyColumns - A boolean indicating whether to unify columns across multiple files when their structures differ. Missing columns will be filled with `NULL` values. Defaults to `false`.
288
285
  * @param options.columnTypes - An object mapping column names to their expected data types. By default, types are inferred.
286
+ * @param options.columns - An array of column names to load. When provided, only the specified columns are loaded, reducing memory usage and improving load times. Not supported for Excel files — combining `columns` with Excel files throws an error. If an invalid column name is provided, DuckDB will throw its native error. An empty array behaves the same as omitting the option (loads all columns). Defaults to loading all columns.
289
287
  * @param options.header - A boolean indicating whether the file has a header row. Applicable to CSV files. Defaults to `true`.
290
288
  * @param options.allText - A boolean indicating whether all columns should be treated as text. Applicable to CSV files. Defaults to `false`.
291
289
  * @param options.delim - The delimiter used in the file. Applicable to CSV and DSV files. By default, the delimiter is inferred.
@@ -332,6 +330,12 @@ export default class SimpleTable extends Simple {
332
330
  * "https://some-website.com/some-data3.parquet"
333
331
  * ], { unifyColumns: true });
334
332
  * ```
333
+ *
334
+ * @example
335
+ * ```ts
336
+ * // Load only specific columns from a CSV file
337
+ * await table.loadData("./employees.csv", { columns: ["name", "salary"] });
338
+ * ```
335
339
  */
336
340
  async loadData(files, options = {}) {
337
341
  await queryDB(this, loadDataQuery(this.name, stringToArray(files), options), mergeOptions(this, {
@@ -352,6 +356,7 @@ export default class SimpleTable extends Simple {
352
356
  * @param options.fileName - A boolean indicating whether to include the file name as a new column in the loaded data. Defaults to `false`.
353
357
  * @param options.unifyColumns - A boolean indicating whether to unify columns across multiple files when their structures differ. Missing columns will be filled with `NULL` values. Defaults to `false`.
354
358
  * @param options.columnTypes - An object mapping column names to their expected data types. By default, types are inferred.
359
+ * @param options.columns - An array of column names to load. When provided, only the specified columns are loaded, reducing memory usage and improving load times. Not supported for Excel files — combining `columns` with Excel files throws an error. If an invalid column name is provided, DuckDB will throw its native error. An empty array behaves the same as omitting the option (loads all columns). Defaults to loading all columns.
355
360
  * @param options.header - A boolean indicating whether the file has a header row. Applicable to CSV files. Defaults to `true`.
356
361
  * @param options.allText - A boolean indicating whether all columns should be treated as text. Applicable to CSV files. Defaults to `false`.
357
362
  * @param options.delim - The delimiter used in the file. Applicable to CSV and DSV files. By default, the delimiter is inferred.
@@ -372,6 +377,12 @@ export default class SimpleTable extends Simple {
372
377
  * // Load all supported data files from the "./data/" directory
373
378
  * await table.loadDataFromDirectory("./data/");
374
379
  * ```
380
+ *
381
+ * @example
382
+ * ```ts
383
+ * // Load only specific columns from all CSV files in a directory
384
+ * await table.loadDataFromDirectory("./data/", { columns: ["name", "salary"] });
385
+ * ```
375
386
  */
376
387
  async loadDataFromDirectory(directory, options = {}) {
377
388
  const files = readdirSync(directory).map((file) => `${directory.slice(-1) === "/" ? directory : directory + "/"}${file}`);
@@ -407,7 +418,13 @@ export default class SimpleTable extends Simple {
407
418
  *
408
419
  * @example
409
420
  * ```ts
410
- * // Load geospatial data from a shapefile and reproject to WGS84
421
+ * // Load geospatial data from a shapefile (with relevant files in the same folder) and reproject to WGS84
422
+ * await table.loadGeoData("./some-data/some-data.shp", { toWGS84: true });
423
+ * ```
424
+ *
425
+ * @example
426
+ * ```ts
427
+ * // Load geospatial data from a zipped shapefile and reproject to WGS84
411
428
  * await table.loadGeoData("./some-data.shp.zip", { toWGS84: true });
412
429
  * ```
413
430
  */
@@ -1888,48 +1905,57 @@ export default class SimpleTable extends Simple {
1888
1905
  * @param rightTable - The SimpleTable instance to be joined with this table.
1889
1906
  * @param leftColumn - The name of the column in this (left) table containing the text to compare.
1890
1907
  * @param rightColumn - The name of the column in the right table containing the text to compare.
1908
+ * @param threshold - The minimum similarity score (0–100) required for two rows to be joined. For `method: "ratio"`, a length-based pre-filter is automatically applied based on the threshold to improve performance without losing accuracy.
1891
1909
  * @param options - An optional object with configuration options:
1892
1910
  * @param options.method - The rapidfuzz similarity algorithm to use. Defaults to `"ratio"`.
1893
1911
  * - `"ratio"`: Overall similarity (Levenshtein-based).
1894
1912
  * - `"partial_ratio"`: Best partial/substring similarity.
1895
1913
  * - `"token_sort_ratio"`: Similarity after sorting tokens (words), useful for reordered words.
1896
1914
  * - `"token_set_ratio"`: Similarity based on sets of tokens, ignoring duplicates and word order.
1897
- * @param options.threshold - The minimum similarity score (0–100) required for two rows to be joined. Defaults to `80`.
1898
1915
  * @param options.similarityColumn - If provided, a column with this name is added to the result containing the similarity score (0–100). If omitted, the score is not included in the output.
1899
1916
  * @param options.outputTable - If `true`, the results will be stored in a new table with a generated name. If a string, it will be used as the name for the new table. If `false` or omitted, the current table will be overwritten. Defaults to `false`.
1917
+ * @param options.preFilterPrefixLen - An optional prefix length. Only strings sharing the same first N characters are compared. Note that prefix filtering is lossy (e.g. "John" vs. "Phon" will not match despite high similarity).
1900
1918
  * @returns A promise that resolves to a table instance containing the fuzzy-joined data (either the modified current table or a new table).
1901
1919
  * @category Table Operations
1902
1920
  *
1903
1921
  * @example
1904
1922
  * ```ts
1905
- * // Fuzzy left join tableA with tableB on 'name' (left) and 'standardName' (right) (ratio >= 80)
1906
- * await tableA.fuzzyJoin(tableB, "name", "standardName");
1923
+ * // Fuzzy left join tableA with tableB on 'name' (left) and 'standardName' (right) with a threshold of 80
1924
+ * // A length-based pre-filter is automatically applied.
1925
+ * await tableA.fuzzyJoin(tableB, "name", "standardName", 80);
1926
+ * ```
1927
+ *
1928
+ * @example
1929
+ * ```ts
1930
+ * // Fuzzy join with a prefix-based pre-filter and a threshold of 80
1931
+ * await tableA.fuzzyJoin(tableB, "name", "standardName", 80, {
1932
+ * preFilterPrefixLen: 3, // Must share the same first 3 characters
1933
+ * });
1907
1934
  * ```
1908
1935
  *
1909
1936
  * @example
1910
1937
  * ```ts
1911
1938
  * // Fuzzy join with a custom threshold and method, storing results in a new table
1912
- * const tableC = await tableA.fuzzyJoin(tableB, "name", "standardName", {
1939
+ * const tableC = await tableA.fuzzyJoin(tableB, "name", "standardName", 90, {
1913
1940
  * method: "token_sort_ratio",
1914
- * threshold: 90,
1915
1941
  * outputTable: "tableC",
1916
1942
  * });
1917
1943
  * ```
1918
1944
  *
1919
1945
  * @example
1920
1946
  * ```ts
1921
- * // Fuzzy join with a custom similarity column name
1922
- * await tableA.fuzzyJoin(tableB, "name", "standardName", {
1947
+ * // Fuzzy join with a custom similarity column name and a threshold of 80
1948
+ * await tableA.fuzzyJoin(tableB, "name", "standardName", 80, {
1923
1949
  * similarityColumn: "matchScore",
1924
1950
  * });
1925
1951
  * ```
1926
1952
  */
1927
- async fuzzyJoin(rightTable, leftColumn, rightColumn, options = {}) {
1953
+ async fuzzyJoin(rightTable, leftColumn, rightColumn, threshold, options = {}) {
1928
1954
  if (options.outputTable === true) {
1929
1955
  options.outputTable = `table${this.sdb.tableIncrement}`;
1930
1956
  this.sdb.tableIncrement += 1;
1931
1957
  }
1932
- return await fuzzyJoin(this, rightTable, leftColumn, rightColumn, options);
1958
+ return await fuzzyJoin(this, rightTable, leftColumn, rightColumn, threshold, options);
1933
1959
  }
1934
1960
  /**
1935
1961
  * Normalizes string values in a column by detecting fuzzy duplicates and replacing them with a single canonical value.
@@ -1943,42 +1969,52 @@ export default class SimpleTable extends Simple {
1943
1969
  *
1944
1970
  * @param column - The name of the column containing the strings to normalize.
1945
1971
  * @param newColumn - The name of the column to write the normalized values to. Use the same name as `column` to normalize in-place.
1972
+ * @param threshold - The minimum similarity score (0–100) for two strings to be considered duplicates. For `method: "ratio"`, a length-based pre-filter is automatically applied based on the threshold to improve performance without losing accuracy.
1946
1973
  * @param options - An optional object with configuration options:
1947
1974
  * @param options.method - The rapidfuzz similarity algorithm to use. Defaults to `"ratio"`.
1948
1975
  * - `"ratio"`: Overall similarity.
1949
1976
  * - `"partial_ratio"`: Best partial/substring similarity.
1950
1977
  * - `"token_sort_ratio"`: Similarity after sorting tokens (words), useful for reordered words.
1951
1978
  * - `"token_set_ratio"`: Similarity based on sets of tokens, ignoring duplicates and word order.
1952
- * @param options.threshold - The minimum similarity score (0–100) for two strings to be considered duplicates. Defaults to `80`.
1953
1979
  * @param options.keep - The strategy for choosing the canonical value within each cluster of similar strings. Defaults to `"mostCommon"`.
1954
1980
  * - `"mostCommon"`: Keep the value that appears most frequently in the original column.
1955
1981
  * - `"longestString"`: Keep the longest string in the cluster.
1956
1982
  * - `"shortestString"`: Keep the shortest string in the cluster.
1957
1983
  * - `"mostCentral"`: Keep the string with the highest total similarity score to all other cluster members (the most "central" string).
1958
1984
  * - `"maxScore"`: Keep the string that participates in the single highest-scoring pairwise match within the cluster.
1985
+ * @param options.preFilterPrefixLen - An optional prefix length. Only strings sharing the same first N characters are compared. Note that prefix filtering is lossy (e.g. "John" vs. "Phon" will not match despite high similarity).
1959
1986
  * @returns A promise that resolves when the column has been normalized.
1960
1987
  * @category Updating Data
1961
1988
  *
1962
1989
  * @example
1963
1990
  * ```ts
1964
- * // Normalize 'city' into a new 'cityClean' column, keeping the most common string per cluster
1965
- * await table.fuzzyClean("city", "cityClean");
1991
+ * // Normalize 'city' into a new 'cityClean' column, keeping the most common string per cluster with a threshold of 80
1992
+ * // A length-based pre-filter is automatically applied.
1993
+ * await table.fuzzyClean("city", "cityClean", 80);
1994
+ * ```
1995
+ *
1996
+ * @example
1997
+ * ```ts
1998
+ * // Normalize with a prefix-based pre-filter and a threshold of 80
1999
+ * await table.fuzzyClean("city", "cityClean", 80, {
2000
+ * preFilterPrefixLen: 5, // Must share the same first 5 characters
2001
+ * });
1966
2002
  * ```
1967
2003
  *
1968
2004
  * @example
1969
2005
  * ```ts
1970
- * // Normalize 'companyName' into a new column using token_sort_ratio and a stricter threshold
1971
- * await table.fuzzyClean("companyName", "companyNameClean", { method: "token_sort_ratio", threshold: 90 });
2006
+ * // Normalize 'companyName' into a new column using token_sort_ratio and a threshold of 90
2007
+ * await table.fuzzyClean("companyName", "companyNameClean", 90, { method: "token_sort_ratio" });
1972
2008
  * ```
1973
2009
  *
1974
2010
  * @example
1975
2011
  * ```ts
1976
- * // Normalize 'category' in-place, keeping the longest string in each cluster
1977
- * await table.fuzzyClean("category", "category", { keep: "longestString" });
2012
+ * // Normalize 'category' in-place, keeping the longest string in each cluster and a threshold of 80
2013
+ * await table.fuzzyClean("category", "category", 80, { keep: "longestString" });
1978
2014
  * ```
1979
2015
  */
1980
- async fuzzyClean(column, newColumn, options = {}) {
1981
- await fuzzyClean(this, column, newColumn, options);
2016
+ async fuzzyClean(column, newColumn, threshold, options = {}) {
2017
+ await fuzzyClean(this, column, newColumn, threshold, options);
1982
2018
  }
1983
2019
  /**
1984
2020
  * Replaces specified strings in the selected columns.
@@ -2132,6 +2168,72 @@ export default class SimpleTable extends Simple {
2132
2168
  parameters: { column, length },
2133
2169
  }));
2134
2170
  }
2171
+ /**
2172
+ * Pads the strings in the specified columns to a target length.
2173
+ *
2174
+ * The columns must contain string (VARCHAR) values. An error is thrown if any
2175
+ * column is of a different type. `null` values remain `null`. If any string
2176
+ * already exceeds the target length, an error is thrown (no silent truncation).
2177
+ *
2178
+ * @param columns - The column name(s) containing strings to be padded.
2179
+ * @param length - The target length of the padded strings.
2180
+ * @param options - An optional object with configuration options:
2181
+ * @param options.method - Which side to pad. `'left'` (default) or `'right'`.
2182
+ * @param options.char - The character to use for padding. Defaults to `'0'`.
2183
+ * @returns A promise that resolves when the padding operation is complete.
2184
+ * @throws {Error} If any column is not of string (VARCHAR) type.
2185
+ * @throws {Error} If any string value exceeds the target length.
2186
+ * @category Updating Data
2187
+ *
2188
+ * @example
2189
+ * ```ts
2190
+ * // Left-pad 'id' column to 3 characters with zeros (default)
2191
+ * await table.pad("id", 3);
2192
+ * // Result: '1' -> '001', '23' -> '023', null -> null
2193
+ * ```
2194
+ *
2195
+ * @example
2196
+ * ```ts
2197
+ * // Right-pad 'code' column to 5 characters with spaces
2198
+ * await table.pad("code", 5, { method: "right", char: " " });
2199
+ * // Result: '123' -> '123 ', '45' -> '45 ', null -> null
2200
+ * ```
2201
+ *
2202
+ * @example
2203
+ * ```ts
2204
+ * // Left-pad multiple columns to 5 characters with dashes
2205
+ * await table.pad(["id", "code"], 5, { method: "left", char: "-" });
2206
+ * // Result: '1' -> '----1', '23' -> '---23'
2207
+ * ```
2208
+ */
2209
+ async pad(columns, length, options = {}) {
2210
+ const columnList = stringToArray(columns);
2211
+ // Validate all columns are string type
2212
+ const allTypes = await this.getTypes();
2213
+ for (const column of columnList) {
2214
+ if (allTypes[column] !== "VARCHAR") {
2215
+ throw new Error(`The column "${column}" is of type ${allTypes[column]}. The pad() method only works with string (VARCHAR) columns. Please convert the column to string first with the .convert() method.`);
2216
+ }
2217
+ }
2218
+ // Pre-validation: check for strings exceeding target length
2219
+ for (const column of columnList) {
2220
+ const overflowResult = await queryDB(this, `SELECT COUNT(*) AS cnt FROM "${this.name}" WHERE LENGTH("${column}") > ${length};`, mergeOptions(this, {
2221
+ table: this.name,
2222
+ method: "pad()",
2223
+ parameters: { columns, length, options },
2224
+ returnDataFrom: "query",
2225
+ }));
2226
+ const overflowCount = Number(overflowResult[0].cnt);
2227
+ if (overflowCount > 0) {
2228
+ throw new Error(`The column "${column}" has ${overflowCount} string(s) exceeding the target length of ${length}. The pad() method does not truncate. Shorten the strings first or use a larger target length.`);
2229
+ }
2230
+ }
2231
+ await queryDB(this, padQuery(this.name, columnList, length, options), mergeOptions(this, {
2232
+ table: this.name,
2233
+ method: "pad()",
2234
+ parameters: { columns, length, options },
2235
+ }));
2236
+ }
2135
2237
  /**
2136
2238
  * Splits strings in a specified column by a separator and extracts a substring at a given index, storing the result in a new or existing column.
2137
2239
  * If the index is out of bounds, an empty string will be returned for that row.
@@ -2477,9 +2579,18 @@ export default class SimpleTable extends Simple {
2477
2579
  * // Round 'columnA' and 'columnB' values to 1 decimal place using ceiling method
2478
2580
  * await table.round(["columnA", "columnB"], { decimals: 1, method: "ceiling" });
2479
2581
  * ```
2582
+ *
2583
+ * @example
2584
+ * ```ts
2585
+ * // Round 'column1' values to 2 decimal places using the shorthand
2586
+ * await table.round("column1", 2);
2587
+ * ```
2480
2588
  */
2481
2589
  async round(columns, options = {}) {
2482
- await queryDB(this, roundQuery(this.name, stringToArray(columns), options), mergeOptions(this, {
2590
+ const optionsNormalized = typeof options === "number"
2591
+ ? { decimals: options }
2592
+ : options;
2593
+ await queryDB(this, roundQuery(this.name, stringToArray(columns), optionsNormalized), mergeOptions(this, {
2483
2594
  table: this.name,
2484
2595
  method: "round()",
2485
2596
  parameters: { columns, options },
@@ -3835,6 +3946,9 @@ export default class SimpleTable extends Simple {
3835
3946
  * ```
3836
3947
  */
3837
3948
  async getData(options = {}) {
3949
+ if (await hasGeometryColumn(this)) {
3950
+ throw new Error("Table contains geometry columns. Use getGeoData() instead.");
3951
+ }
3838
3952
  const columns = options.columns
3839
3953
  ? (typeof options.columns === "string"
3840
3954
  ? [options.columns]
@@ -3899,8 +4013,9 @@ export default class SimpleTable extends Simple {
3899
4013
  * ```
3900
4014
  */
3901
4015
  async points(columnLat, columnLon, newColumn) {
3902
- await queryDB(this, `INSTALL spatial; LOAD spatial;
3903
- ALTER TABLE "${this.name}" ADD COLUMN "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Point2D("${columnLat}", "${columnLon}")`, mergeOptions(this, {
4016
+ await queryDB(this, (await this.getColumns()).includes(newColumn)
4017
+ ? `INSTALL spatial; LOAD spatial; UPDATE "${this.name}" SET "${newColumn}" = ST_Point2D("${columnLat}", "${columnLon}")`
4018
+ : `INSTALL spatial; LOAD spatial; ALTER TABLE "${this.name}" ADD COLUMN "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Point2D("${columnLat}", "${columnLon}")`, mergeOptions(this, {
3904
4019
  table: this.name,
3905
4020
  method: "points()",
3906
4021
  parameters: { columnLat, columnLon, newColumn },
@@ -4316,7 +4431,9 @@ export default class SimpleTable extends Simple {
4316
4431
  const column = typeof options.column === "string"
4317
4432
  ? options.column
4318
4433
  : await findGeoColumn(this);
4319
- await queryDB(this, `ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Buffer("${column}", ${distance});`, mergeOptions(this, {
4434
+ await queryDB(this, (await this.getColumns()).includes(newColumn)
4435
+ ? `INSTALL spatial; LOAD spatial; UPDATE "${this.name}" SET "${newColumn}" = ST_Buffer("${column}", ${distance})`
4436
+ : `INSTALL spatial; LOAD spatial; ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Buffer("${column}", ${distance})`, mergeOptions(this, {
4320
4437
  table: this.name,
4321
4438
  method: "buffer()",
4322
4439
  parameters: { column, newColumn, distance },
@@ -4402,7 +4519,9 @@ export default class SimpleTable extends Simple {
4402
4519
  if (this.projections[column1] !== this.projections[column2]) {
4403
4520
  throw new Error(`${column1} and ${column2} don't have the same projection.\n${column1}: ${this.projections[column1]}\n${column2}: ${this.projections[column2]}`);
4404
4521
  }
4405
- await queryDB(this, `ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Intersection("${column1}", "${column2}")`, mergeOptions(this, {
4522
+ await queryDB(this, (await this.getColumns()).includes(newColumn)
4523
+ ? `INSTALL spatial; LOAD spatial; UPDATE "${this.name}" SET "${newColumn}" = ST_Intersection("${column1}", "${column2}")`
4524
+ : `INSTALL spatial; LOAD spatial; ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Intersection("${column1}", "${column2}")`, mergeOptions(this, {
4406
4525
  table: this.name,
4407
4526
  method: "intersection()",
4408
4527
  parameters: { column1, column2, newColumn },
@@ -4527,7 +4646,9 @@ export default class SimpleTable extends Simple {
4527
4646
  if (this.projections[column1] !== this.projections[column2]) {
4528
4647
  throw new Error(`${column1} and ${column2} don't have the same projection.\n${column1}: ${this.projections[column1]}\n${column2}: ${this.projections[column2]}`);
4529
4648
  }
4530
- await queryDB(this, `ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Union("${column1}", "${column2}")`, mergeOptions(this, {
4649
+ await queryDB(this, (await this.getColumns()).includes(newColumn)
4650
+ ? `INSTALL spatial; LOAD spatial; UPDATE "${this.name}" SET "${newColumn}" = ST_Union("${column1}", "${column2}")`
4651
+ : `INSTALL spatial; LOAD spatial; ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Union("${column1}", "${column2}")`, mergeOptions(this, {
4531
4652
  table: this.name,
4532
4653
  method: "union()",
4533
4654
  parameters: { column1, column2, newColumn },
@@ -4618,7 +4739,9 @@ export default class SimpleTable extends Simple {
4618
4739
  const column = typeof options.column === "string"
4619
4740
  ? options.column
4620
4741
  : await findGeoColumn(this);
4621
- await queryDB(this, `ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Centroid("${column}");`, mergeOptions(this, {
4742
+ await queryDB(this, (await this.getColumns()).includes(newColumn)
4743
+ ? `INSTALL spatial; LOAD spatial; UPDATE "${this.name}" SET "${newColumn}" = ST_Centroid("${column}")`
4744
+ : `INSTALL spatial; LOAD spatial; ALTER TABLE "${this.name}" ADD "${newColumn}" GEOMETRY; UPDATE "${this.name}" SET "${newColumn}" = ST_Centroid("${column}")`, mergeOptions(this, {
4622
4745
  table: this.name,
4623
4746
  method: "centroid()",
4624
4747
  parameters: { column, newColumn },
@@ -4903,6 +5026,9 @@ export default class SimpleTable extends Simple {
4903
5026
  * ```
4904
5027
  */
4905
5028
  async writeData(file, options = {}) {
5029
+ if (await hasGeometryColumn(this)) {
5030
+ throw new Error("Table contains geometry columns. Use writeGeoData() instead.");
5031
+ }
4906
5032
  createDirectory(file);
4907
5033
  const extension = getExtension(file);
4908
5034
  if (options.dataAsArrays) {
@@ -4917,12 +5043,12 @@ export default class SimpleTable extends Simple {
4917
5043
  }
4918
5044
  }
4919
5045
  /**
4920
- * Writes the table's geospatial data to a file in GeoJSON or GeoParquet format.
5046
+ * Writes the table's geospatial data to a file in GeoJSON, GeoParquet, or Shapefile format.
4921
5047
  * If the specified path does not exist, it will be created.
4922
5048
  *
4923
5049
  * For GeoJSON files (`.geojson` or `.json`), if the projection is WGS84 or EPSG:4326 (`[latitude, longitude]` axis order), the coordinates will be flipped to follow the RFC7946 standard (`[longitude, latitude]` axis order) in the output.
4924
5050
  *
4925
- * @param file - The absolute path to the output file (e.g., `"./output.geojson"`, `"./output.geoparquet"`).
5051
+ * @param file - The absolute path to the output file (e.g., `"./output.geojson"`, `"./output.geoparquet"`, `"./shapefile-folder/output.shp"`).
4926
5052
  * @param options - An optional object with configuration options:
4927
5053
  * @param options.precision - For GeoJSON, the maximum number of figures after the decimal separator to write in coordinates. Defaults to `undefined` (full precision).
4928
5054
  * @param options.compression - For GeoParquet, if `true`, the output will be ZSTD compressed. Defaults to `false`.
@@ -4946,6 +5072,12 @@ export default class SimpleTable extends Simple {
4946
5072
  *
4947
5073
  * @example
4948
5074
  * ```ts
5075
+ * // Write geospatial data to a Shapefile with all relevant files in the same folder
5076
+ * await table.writeGeoData("./shapefile-folder/output.shp");
5077
+ * ```
5078
+ *
5079
+ * @example
5080
+ * ```ts
4949
5081
  * // Write GeoJSON with specific precision and metadata
4950
5082
  * await table.writeGeoData("./output_high_precision.geojson", {
4951
5083
  * precision: 6,
@@ -4954,71 +5086,7 @@ export default class SimpleTable extends Simple {
4954
5086
  * ```
4955
5087
  */
4956
5088
  async writeGeoData(file, options = {}) {
4957
- createDirectory(file);
4958
- const fileExtension = getExtension(file);
4959
- if (fileExtension === "geojson" || fileExtension === "json") {
4960
- let types;
4961
- if (options.formatDates === true) {
4962
- types = await this.getTypes();
4963
- if (Object.values(types).includes("DATE") ||
4964
- Object.values(types).includes("TIMESTAMP")) {
4965
- await stringifyDates(this, types);
4966
- }
4967
- }
4968
- if (typeof options.compression === "boolean") {
4969
- throw new Error("The compression option is not supported for writing GeoJSON files.");
4970
- }
4971
- const geoColumn = await findGeoColumn(this);
4972
- const flip = shouldFlipBeforeExport(this.projections[geoColumn]);
4973
- if (flip) {
4974
- await this.flipCoordinates(geoColumn);
4975
- await queryDB(this, writeGeoDataQuery(this.name, file, fileExtension, options), mergeOptions(this, {
4976
- table: this.name,
4977
- method: "writeGeoData()",
4978
- parameters: { file, options },
4979
- }));
4980
- await this.flipCoordinates(geoColumn);
4981
- }
4982
- else {
4983
- await queryDB(this, writeGeoDataQuery(this.name, file, fileExtension, options), mergeOptions(this, {
4984
- table: this.name,
4985
- method: "writeGeoData()",
4986
- parameters: { file, options },
4987
- }));
4988
- }
4989
- if (options.metadata) {
4990
- const fileData = JSON.parse(readFileSync(file, "utf-8"));
4991
- fileData.metadata = options.metadata;
4992
- writeFileSync(file, JSON.stringify(fileData));
4993
- }
4994
- if (options.rewind) {
4995
- const fileData = JSON.parse(readFileSync(file, "utf-8"));
4996
- const fileRewinded = rewind(fileData);
4997
- writeFileSync(file, JSON.stringify(fileRewinded));
4998
- }
4999
- if (types && (Object.values(types).includes("DATE") ||
5000
- Object.values(types).includes("TIMESTAMP"))) {
5001
- await stringifyDatesInvert(this, types);
5002
- }
5003
- }
5004
- else if (fileExtension === "geoparquet") {
5005
- if (typeof options.precision === "number") {
5006
- throw new Error("The precision option is not supported for writing PARQUET files. Use the .reducePrecision() method.");
5007
- }
5008
- if (typeof options.rewind === "boolean") {
5009
- throw new Error("The rewind option is not supported for writing PARQUET files.");
5010
- }
5011
- await queryDB(this, `COPY "${this.name}" TO '${cleanPath(file)}' WITH (FORMAT PARQUET${options.compression === true ? ", COMPRESSION 'zstd'" : ""}, KV_METADATA {
5012
- projections: '${JSON.stringify(this.projections)}'
5013
- });`, mergeOptions(this, {
5014
- table: this.name,
5015
- method: "writeGeoData()",
5016
- parameters: { file, options },
5017
- }));
5018
- }
5019
- else {
5020
- throw new Error(`Unknown extension ${fileExtension}`);
5021
- }
5089
+ await writeGeoData(this, file, options);
5022
5090
  }
5023
5091
  /**
5024
5092
  * Caches the results of computations in `./.sda-cache`.
@@ -5226,10 +5294,10 @@ export default class SimpleTable extends Simple {
5226
5294
  async logDescription() {
5227
5295
  if (this.connection === undefined ||
5228
5296
  !(await this.sdb.hasTable(this.name))) {
5229
- console.log(`\ntable ${this.name}: no data`);
5297
+ console.log(`\nTable ${this.name}: no data`);
5230
5298
  }
5231
5299
  else {
5232
- console.log(`\ntable ${this.name}:`);
5300
+ console.log(`\nTable ${this.name}:`);
5233
5301
  console.table(await getDescription(this));
5234
5302
  }
5235
5303
  }
@@ -5246,7 +5314,7 @@ export default class SimpleTable extends Simple {
5246
5314
  * ```
5247
5315
  */
5248
5316
  async logProjections() {
5249
- console.log(`\ntable ${this.name} projections:`);
5317
+ console.log(`\nTable ${this.name} projections:`);
5250
5318
  console.log(this.projections);
5251
5319
  return await this;
5252
5320
  }
@@ -5263,7 +5331,7 @@ export default class SimpleTable extends Simple {
5263
5331
  * ```
5264
5332
  */
5265
5333
  async logTypes() {
5266
- console.log(`\ntable ${this.name} types:`);
5334
+ console.log(`\nTable ${this.name} types:`);
5267
5335
  console.log(await this.getTypes());
5268
5336
  return await this;
5269
5337
  }
@@ -5291,7 +5359,7 @@ export default class SimpleTable extends Simple {
5291
5359
  */
5292
5360
  async logUniques(column, options = {}) {
5293
5361
  const values = await this.getUniques(column);
5294
- console.log(`\nUnique values in ${column}:`);
5362
+ console.log(`\nTable ${this.name} — unique values in ${column}:`);
5295
5363
  if (options.stringify) {
5296
5364
  console.log(JSON.stringify(values, null, 2));
5297
5365
  }
@@ -3,7 +3,7 @@ export default async function findGeoColumn(SimpleTable) {
3
3
  const types = await SimpleTable.getTypes();
4
4
  const geometries = Object.values(types).filter((d) => d.toLowerCase() === "geometry");
5
5
  if (geometries.length === 0) {
6
- throw new Error("No column storing geometries");
6
+ throw new Error("Table contains no geometry columns.");
7
7
  }
8
8
  else if (geometries.length > 1) {
9
9
  throw new Error("More than one column storing geometries. If the method allows to specify one, do it. Otherwise, use the selectColumns methods beforehand.");
@@ -1 +1 @@
1
- {"version":3,"file":"getProjection.d.ts","sourceRoot":"","sources":["../../src/helpers/getProjection.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAC;AAGjD,wBAA8B,aAAa,CACzC,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,mBA8Bb"}
1
+ {"version":3,"file":"getProjection.d.ts","sourceRoot":"","sources":["../../src/helpers/getProjection.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAC;AAGjD,wBAA8B,aAAa,CACzC,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,mBAiCb"}
@@ -15,6 +15,9 @@ export default async function getProjection(simpleDB, file) {
15
15
  throw new Error("No queryResults");
16
16
  }
17
17
  const proj4 = queryResult[0].proj4;
18
+ if (proj4 === null) {
19
+ return "UNKNOWN";
20
+ }
18
21
  if (typeof proj4 !== "string") {
19
22
  throw new Error(`Expected proj4 to be a string, got ${typeof proj4}`);
20
23
  }
@@ -0,0 +1,20 @@
1
+ import type SimpleTable from "../class/SimpleTable.js";
2
+ /**
3
+ * Returns `true` if the table has one or more columns of type geometry.
4
+ *
5
+ * Uses `getTypes()` to inspect column types and checks for any column
6
+ * whose type normalizes to `"geometry"`.
7
+ *
8
+ * @param table - The SimpleTable instance to inspect.
9
+ * @returns `true` if at least one geometry column exists, `false` otherwise.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const hasGeo = await hasGeometryColumn(table);
14
+ * if (hasGeo) {
15
+ * console.log("This table contains geometry columns");
16
+ * }
17
+ * ```
18
+ */
19
+ export default function hasGeometryColumn(table: SimpleTable): Promise<boolean>;
20
+ //# sourceMappingURL=hasGeometryColumn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hasGeometryColumn.d.ts","sourceRoot":"","sources":["../../src/helpers/hasGeometryColumn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAEvD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAA8B,iBAAiB,CAC7C,KAAK,EAAE,WAAW,GACjB,OAAO,CAAC,OAAO,CAAC,CAGlB"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Returns `true` if the table has one or more columns of type geometry.
3
+ *
4
+ * Uses `getTypes()` to inspect column types and checks for any column
5
+ * whose type normalizes to `"geometry"`.
6
+ *
7
+ * @param table - The SimpleTable instance to inspect.
8
+ * @returns `true` if at least one geometry column exists, `false` otherwise.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const hasGeo = await hasGeometryColumn(table);
13
+ * if (hasGeo) {
14
+ * console.log("This table contains geometry columns");
15
+ * }
16
+ * ```
17
+ */
18
+ export default async function hasGeometryColumn(table) {
19
+ const types = await table.getTypes();
20
+ return Object.values(types).some((t) => t.toLowerCase() === "geometry");
21
+ }
@@ -0,0 +1,9 @@
1
+ import type SimpleTable from "../class/SimpleTable.js";
2
+ export default function writeGeoData(table: SimpleTable, file: string, options?: {
3
+ precision?: number;
4
+ compression?: boolean;
5
+ rewind?: boolean;
6
+ metadata?: unknown;
7
+ formatDates?: boolean;
8
+ }): Promise<void>;
9
+ //# sourceMappingURL=writeGeoData.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writeGeoData.d.ts","sourceRoot":"","sources":["../../src/helpers/writeGeoData.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAEvD,wBAA8B,YAAY,CACxC,KAAK,EAAE,WAAW,EAClB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE;IACP,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,OAAO,CAAC;CAClB,GACL,OAAO,CAAC,IAAI,CAAC,CAqIf"}