@carto/api-client 0.4.6-0 → 0.4.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 (81) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/api-client.cjs +208 -2167
  3. package/build/api-client.cjs.map +1 -1
  4. package/build/api-client.modern.js +208 -2040
  5. package/build/api-client.modern.js.map +1 -1
  6. package/build/constants.d.ts +0 -22
  7. package/build/index.d.ts +1 -4
  8. package/build/models/index.d.ts +1 -1
  9. package/build/sources/h3-tileset-source.d.ts +1 -2
  10. package/build/sources/index.d.ts +1 -1
  11. package/build/sources/quadbin-tileset-source.d.ts +1 -2
  12. package/build/sources/vector-tileset-source.d.ts +1 -2
  13. package/build/spatial-index.d.ts +7 -1
  14. package/build/types-internal.d.ts +0 -4
  15. package/build/types.d.ts +1 -61
  16. package/build/widget-sources/index.d.ts +1 -3
  17. package/build/widget-sources/types.d.ts +22 -31
  18. package/build/widget-sources/{widget-source.d.ts → widget-base-source.d.ts} +38 -13
  19. package/build/widget-sources/widget-query-source.d.ts +2 -2
  20. package/build/widget-sources/widget-table-source.d.ts +2 -2
  21. package/package.json +11 -14
  22. package/src/constants.ts +0 -25
  23. package/src/index.ts +1 -5
  24. package/src/models/index.ts +1 -1
  25. package/src/sources/h3-tileset-source.ts +6 -18
  26. package/src/sources/index.ts +1 -1
  27. package/src/sources/quadbin-tileset-source.ts +6 -18
  28. package/src/sources/raster-source.ts +0 -1
  29. package/src/sources/vector-query-source.ts +2 -5
  30. package/src/sources/vector-table-source.ts +2 -5
  31. package/src/sources/vector-tileset-source.ts +6 -19
  32. package/src/spatial-index.ts +8 -5
  33. package/src/types-internal.ts +0 -6
  34. package/src/types.ts +2 -60
  35. package/src/widget-sources/index.ts +1 -3
  36. package/src/widget-sources/types.ts +22 -32
  37. package/src/widget-sources/{widget-remote-source.ts → widget-base-source.ts} +147 -12
  38. package/src/widget-sources/widget-query-source.ts +3 -6
  39. package/src/widget-sources/widget-table-source.ts +3 -6
  40. package/build/filters/Filter.d.ts +0 -13
  41. package/build/filters/FilterTypes.d.ts +0 -3
  42. package/build/filters/geosjonFeatures.d.ts +0 -8
  43. package/build/filters/index.d.ts +0 -6
  44. package/build/filters/tileFeatures.d.ts +0 -20
  45. package/build/filters/tileFeaturesGeometries.d.ts +0 -13
  46. package/build/filters/tileFeaturesSpatialIndex.d.ts +0 -10
  47. package/build/operations/aggregation.d.ts +0 -8
  48. package/build/operations/applySorting.d.ts +0 -20
  49. package/build/operations/groupBy.d.ts +0 -15
  50. package/build/operations/groupByDate.d.ts +0 -11
  51. package/build/operations/histogram.d.ts +0 -13
  52. package/build/operations/index.d.ts +0 -6
  53. package/build/operations/scatterPlot.d.ts +0 -14
  54. package/build/utils/dateUtils.d.ts +0 -10
  55. package/build/utils/getTileFormat.d.ts +0 -3
  56. package/build/utils/makeIntervalComplete.d.ts +0 -2
  57. package/build/utils/transformTileCoordsToWGS84.d.ts +0 -8
  58. package/build/utils/transformToTileCoords.d.ts +0 -9
  59. package/build/widget-sources/widget-remote-source.d.ts +0 -18
  60. package/build/widget-sources/widget-tileset-source.d.ts +0 -76
  61. package/src/filters/Filter.ts +0 -169
  62. package/src/filters/FilterTypes.ts +0 -109
  63. package/src/filters/geosjonFeatures.ts +0 -32
  64. package/src/filters/index.ts +0 -6
  65. package/src/filters/tileFeatures.ts +0 -56
  66. package/src/filters/tileFeaturesGeometries.ts +0 -444
  67. package/src/filters/tileFeaturesSpatialIndex.ts +0 -119
  68. package/src/operations/aggregation.ts +0 -154
  69. package/src/operations/applySorting.ts +0 -109
  70. package/src/operations/groupBy.ts +0 -59
  71. package/src/operations/groupByDate.ts +0 -98
  72. package/src/operations/histogram.ts +0 -66
  73. package/src/operations/index.ts +0 -6
  74. package/src/operations/scatterPlot.ts +0 -50
  75. package/src/utils/dateUtils.ts +0 -28
  76. package/src/utils/getTileFormat.ts +0 -9
  77. package/src/utils/makeIntervalComplete.ts +0 -17
  78. package/src/utils/transformTileCoordsToWGS84.ts +0 -77
  79. package/src/utils/transformToTileCoords.ts +0 -85
  80. package/src/widget-sources/widget-source.ts +0 -160
  81. package/src/widget-sources/widget-tileset-source.ts +0 -407
@@ -3,10 +3,7 @@ import {
3
3
  QuadbinTableSourceOptions,
4
4
  VectorTableSourceOptions,
5
5
  } from '../sources/index.js';
6
- import {
7
- WidgetRemoteSource,
8
- WidgetRemoteSourceProps,
9
- } from './widget-remote-source.js';
6
+ import {WidgetBaseSource, WidgetBaseSourceProps} from './widget-base-source.js';
10
7
  import {ModelSource} from '../models/model.js';
11
8
 
12
9
  type LayerTableSourceOptions =
@@ -38,8 +35,8 @@ export type WidgetTableSourceResult = {widgetSource: WidgetTableSource};
38
35
  * const { widgetSource } = await data;
39
36
  * ```
40
37
  */
41
- export class WidgetTableSource extends WidgetRemoteSource<
42
- LayerTableSourceOptions & WidgetRemoteSourceProps
38
+ export class WidgetTableSource extends WidgetBaseSource<
39
+ LayerTableSourceOptions & WidgetBaseSourceProps
43
40
  > {
44
41
  protected override getModelSource(owner: string): ModelSource {
45
42
  return {
@@ -1,13 +0,0 @@
1
- import { FilterLogicalOperator, Filters } from '../types';
2
- import { Feature } from 'geojson';
3
- import { FeatureData } from '../types-internal';
4
- import { BinaryFeature } from '@loaders.gl/schema';
5
- export declare function buildFeatureFilter({ filters, type, filtersLogicalOperator, }: {
6
- filters?: Filters;
7
- type?: 'number' | 'boolean';
8
- filtersLogicalOperator?: FilterLogicalOperator;
9
- }): (feature: Feature | FeatureData) => number | boolean;
10
- export declare function applyFilters(features: FeatureData[], filters: Filters, filtersLogicalOperator: FilterLogicalOperator): FeatureData[];
11
- export declare function buildBinaryFeatureFilter({ filters }: {
12
- filters: Filters;
13
- }): (() => number) | ((featureIdIdx: number, binaryData: BinaryFeature) => boolean);
@@ -1,3 +0,0 @@
1
- import { FilterType } from '../constants';
2
- export type FilterFunction = (filterValues: unknown[], featureValue: unknown, params?: Record<string, unknown>) => boolean;
3
- export declare const filterFunctions: Record<FilterType, FilterFunction>;
@@ -1,8 +0,0 @@
1
- import { FeatureCollection } from 'geojson';
2
- import { SpatialFilter } from '..';
3
- import { FeatureData } from '../types-internal';
4
- export declare function geojsonFeatures({ geojson, spatialFilter, uniqueIdProperty, }: {
5
- geojson: FeatureCollection;
6
- spatialFilter: SpatialFilter;
7
- uniqueIdProperty?: string;
8
- }): FeatureData[];
@@ -1,6 +0,0 @@
1
- export * from './Filter.js';
2
- export * from './FilterTypes.js';
3
- export * from './geosjonFeatures.js';
4
- export * from './tileFeatures.js';
5
- export * from './tileFeaturesGeometries.js';
6
- export * from './tileFeaturesSpatialIndex.js';
@@ -1,20 +0,0 @@
1
- import { SpatialFilter, Tile } from '../types';
2
- import { TileFormat } from '../constants';
3
- import { FeatureData } from '../types-internal';
4
- import { SpatialDataType } from '../sources/types';
5
- /** @internalRemarks Source: @carto/react-core */
6
- export type TileFeatures = {
7
- tiles: Tile[];
8
- tileFormat: TileFormat;
9
- spatialDataType: SpatialDataType;
10
- spatialDataColumn?: string;
11
- spatialFilter?: SpatialFilter;
12
- uniqueIdProperty?: string;
13
- options?: TileFeatureExtractOptions;
14
- };
15
- /** @internalRemarks Source: @carto/react-core */
16
- export type TileFeatureExtractOptions = {
17
- storeGeometry?: boolean;
18
- };
19
- /** @internalRemarks Source: @carto/react-core */
20
- export declare function tileFeatures({ tiles, spatialFilter, uniqueIdProperty, tileFormat, spatialDataColumn, spatialDataType, options, }: TileFeatures): FeatureData[];
@@ -1,13 +0,0 @@
1
- import { TileFormat } from '../constants.js';
2
- import { SpatialFilter, Tile } from '../types.js';
3
- import { FeatureData } from '../types-internal.js';
4
- export declare const FEATURE_GEOM_PROPERTY = "__geomValue";
5
- export declare function tileFeaturesGeometries({ tiles, tileFormat, spatialFilter, uniqueIdProperty, options, }: {
6
- tiles: Tile[];
7
- tileFormat?: TileFormat;
8
- spatialFilter: SpatialFilter;
9
- uniqueIdProperty?: string;
10
- options?: {
11
- storeGeometry?: boolean;
12
- };
13
- }): FeatureData[];
@@ -1,10 +0,0 @@
1
- import { SpatialFilter, SpatialIndexTile } from '../types.js';
2
- import { FeatureData } from '../types-internal.js';
3
- import { SpatialDataType } from '../sources/types.js';
4
- export type TileFeaturesSpatialIndexOptions = {
5
- tiles: SpatialIndexTile[];
6
- spatialFilter: SpatialFilter;
7
- spatialDataColumn: string;
8
- spatialDataType: SpatialDataType;
9
- };
10
- export declare function tileFeaturesSpatialIndex({ tiles, spatialFilter, spatialDataColumn, spatialDataType, }: TileFeaturesSpatialIndexOptions): FeatureData[];
@@ -1,8 +0,0 @@
1
- import { AggregationType } from '../types';
2
- import { FeatureData } from '../types-internal';
3
- /** @internalRemarks Source: @carto/react-core */
4
- export type AggregationFunction = (values: unknown[] | FeatureData[], keys?: string[] | string, joinOperation?: AggregationType) => number;
5
- /** @internalRemarks Source: @carto/react-core */
6
- export declare const aggregationFunctions: Record<Exclude<AggregationType, 'custom'>, AggregationFunction>;
7
- /** @internalRemarks Source: @carto/react-core */
8
- export declare function aggregate(feature: FeatureData, keys?: string[], operation?: AggregationType): unknown;
@@ -1,20 +0,0 @@
1
- import { SortDirection } from '../types';
2
- import { FeatureData } from '../types-internal';
3
- type SortColumns = string | string[] | object[];
4
- interface SortOptions {
5
- sortBy?: SortColumns;
6
- sortByDirection?: SortDirection;
7
- sortByColumnType?: 'number' | 'string' | 'date';
8
- }
9
- /**
10
- * Apply sort structure to a collection of features
11
- * @param features
12
- * @param [sortOptions]
13
- * @param [sortOptions.sortBy] - One or more columns to sort by
14
- * @param [sortOptions.sortByDirection] - Direction by the columns will be sorted
15
- * @param [sortOptions.sortByColumnType] - Column type
16
- * @internal
17
- * @internalRemarks Source: @carto/react-core
18
- */
19
- export declare function applySorting(features: FeatureData[], { sortBy, sortByDirection, sortByColumnType, }?: SortOptions): FeatureData[];
20
- export {};
@@ -1,15 +0,0 @@
1
- import { AggregationType } from '../types';
2
- import { FeatureData } from '../types-internal';
3
- /** @internalRemarks Source: @carto/react-core */
4
- export type GroupByFeature = {
5
- name: string;
6
- value: number;
7
- }[];
8
- /** @internalRemarks Source: @carto/react-core */
9
- export declare function groupValuesByColumn({ data, valuesColumns, joinOperation, keysColumn, operation, }: {
10
- data: FeatureData[];
11
- valuesColumns?: string[];
12
- joinOperation?: AggregationType;
13
- keysColumn: string;
14
- operation: AggregationType;
15
- }): GroupByFeature | null;
@@ -1,11 +0,0 @@
1
- import { AggregationType, GroupDateType } from '../types.js';
2
- import { GroupByFeature } from './groupBy.js';
3
- /** @internalRemarks Source: @carto/react-core */
4
- export declare function groupValuesByDateColumn({ data, valuesColumns, joinOperation, keysColumn, groupType, operation, }: {
5
- data: Record<string, unknown>[];
6
- valuesColumns?: string[];
7
- joinOperation?: Exclude<AggregationType, 'custom'>;
8
- keysColumn: string;
9
- groupType: GroupDateType;
10
- operation?: Exclude<AggregationType, 'custom'>;
11
- }): GroupByFeature | null;
@@ -1,13 +0,0 @@
1
- import { AggregationType } from '../types';
2
- import { FeatureData } from '../types-internal';
3
- /**
4
- * Histogram computation.
5
- * @internalRemarks Source: @carto/react-core
6
- */
7
- export declare function histogram({ data, valuesColumns, joinOperation, ticks, operation, }: {
8
- data: FeatureData[];
9
- valuesColumns?: string[];
10
- joinOperation?: Exclude<AggregationType, 'custom'>;
11
- ticks: number[];
12
- operation: Exclude<AggregationType, 'custom'>;
13
- }): number[];
@@ -1,6 +0,0 @@
1
- export * from './aggregation.js';
2
- export * from './applySorting.js';
3
- export * from './groupBy.js';
4
- export * from './groupByDate.js';
5
- export * from './histogram.js';
6
- export * from './scatterPlot.js';
@@ -1,14 +0,0 @@
1
- import { FeatureData } from '../types-internal';
2
- import { AggregationType } from '../types';
3
- export type ScatterPlotFeature = [number, number][];
4
- /**
5
- * Filters invalid features and formats data.
6
- * @internalRemarks Source: @carto/react-core
7
- */
8
- export declare function scatterPlot({ data, xAxisColumns, xAxisJoinOperation, yAxisColumns, yAxisJoinOperation, }: {
9
- data: FeatureData[];
10
- xAxisColumns: string[];
11
- xAxisJoinOperation?: AggregationType;
12
- yAxisColumns: string[];
13
- yAxisJoinOperation?: AggregationType;
14
- }): ScatterPlotFeature;
@@ -1,10 +0,0 @@
1
- /**
2
- * Returns midnight (local time) on the Monday preceeding a given date, in
3
- * milliseconds since the UNIX epoch.
4
- */
5
- export declare function getMonday(date: number | Date): number;
6
- /**
7
- * Returns midnight (UTC) on the Monday preceeding a given date, in
8
- * milliseconds since the UNIX epoch.
9
- */
10
- export declare function getUTCMonday(date: number | Date): number;
@@ -1,3 +0,0 @@
1
- import { TileFormat } from '../constants.js';
2
- import { Tilejson } from '../sources/types.js';
3
- export declare function getTileFormat(tilejson: Tilejson): TileFormat;
@@ -1,2 +0,0 @@
1
- import { FilterInterval, FilterIntervalComplete } from '../types';
2
- export declare function makeIntervalComplete(intervals: FilterInterval[]): FilterIntervalComplete[];
@@ -1,8 +0,0 @@
1
- import { BBox, Geometry } from 'geojson';
2
- /**
3
- * Transform tile coords to WGS84 coordinates.
4
- *
5
- * @param geometry - any valid geojson geometry
6
- * @param bbox - geojson bbox
7
- */
8
- export declare function transformTileCoordsToWGS84<T extends Geometry>(geometry: T, bbox: BBox): T;
@@ -1,9 +0,0 @@
1
- import { BBox, Geometry } from 'geojson';
2
- /**
3
- * Transform WGS84 coordinates to tile coords.
4
- * It's the inverse of deck.gl coordinate-transform (https://github.com/visgl/deck.gl/blob/master/modules/geo-layers/src/mvt-layer/coordinate-transform.js)
5
- *
6
- * @param geometry - any valid geojson geometry
7
- * @param bbox - geojson bbox
8
- */
9
- export declare function transformToTileCoords<T extends Geometry>(geometry: T, bbox: BBox): T;
@@ -1,18 +0,0 @@
1
- import { CategoryRequestOptions, CategoryResponse, FeaturesRequestOptions, FeaturesResponse, FormulaRequestOptions, FormulaResponse, HistogramRequestOptions, HistogramResponse, RangeRequestOptions, RangeResponse, ScatterRequestOptions, ScatterResponse, TableRequestOptions, TableResponse, TimeSeriesRequestOptions, TimeSeriesResponse } from './types.js';
2
- import { WidgetSource, WidgetSourceProps } from './widget-source.js';
3
- export type WidgetRemoteSourceProps = WidgetSourceProps;
4
- /**
5
- * Source for Widget API requests.
6
- *
7
- * Abstract class. Use {@link WidgetQuerySource} or {@link WidgetTableSource}.
8
- */
9
- export declare abstract class WidgetRemoteSource<Props extends WidgetRemoteSourceProps> extends WidgetSource<Props> {
10
- getCategories(options: CategoryRequestOptions): Promise<CategoryResponse>;
11
- getFeatures(options: FeaturesRequestOptions): Promise<FeaturesResponse>;
12
- getFormula(options: FormulaRequestOptions): Promise<FormulaResponse>;
13
- getHistogram(options: HistogramRequestOptions): Promise<HistogramResponse>;
14
- getRange(options: RangeRequestOptions): Promise<RangeResponse>;
15
- getScatter(options: ScatterRequestOptions): Promise<ScatterResponse>;
16
- getTable(options: TableRequestOptions): Promise<TableResponse>;
17
- getTimeSeries(options: TimeSeriesRequestOptions): Promise<TimeSeriesResponse>;
18
- }
@@ -1,76 +0,0 @@
1
- import { TilesetSourceOptions } from '../sources/index.js';
2
- import type { ModelSource } from '../models/index.js';
3
- import { CategoryRequestOptions, CategoryResponse, FeaturesRequestOptions, FeaturesResponse, FormulaRequestOptions, FormulaResponse, HistogramRequestOptions, HistogramResponse, RangeRequestOptions, RangeResponse, ScatterRequestOptions, ScatterResponse, TableRequestOptions, TableResponse, TimeSeriesRequestOptions, TimeSeriesResponse } from './types.js';
4
- import { TileFormat } from '../constants.js';
5
- import { SpatialFilter } from '../types.js';
6
- import { TileFeatureExtractOptions } from '../filters/index.js';
7
- import { FeatureCollection } from 'geojson';
8
- import { SpatialDataType } from '../sources/types.js';
9
- import { WidgetSource, WidgetSourceProps } from './widget-source.js';
10
- export type WidgetTilesetSourceProps = WidgetSourceProps & Omit<TilesetSourceOptions, 'filters'> & {
11
- tileFormat: TileFormat;
12
- spatialDataType: SpatialDataType;
13
- };
14
- export type WidgetTilesetSourceResult = {
15
- widgetSource: WidgetTilesetSource;
16
- };
17
- /**
18
- * Source for Widget API requests on a data source defined by a tileset.
19
- *
20
- * Generally not intended to be constructed directly. Instead, call
21
- * {@link vectorTilesetSource}, {@link h3TilesetSource}, or {@link quadbinTilesetSource},
22
- * which can be shared with map layers. Sources contain a `widgetSource`
23
- * property, for use by widget implementations.
24
- *
25
- * Example:
26
- *
27
- * ```javascript
28
- * import { vectorTilesetSource } from '@carto/api-client';
29
- *
30
- * const data = vectorTilesetSource({
31
- * accessToken: '••••',
32
- * connectionName: 'carto_dw',
33
- * tableName: 'carto-demo-data.demo_rasters.my_tileset_source'
34
- * });
35
- *
36
- * const { widgetSource } = await data;
37
- * ```
38
- */
39
- export declare class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps> {
40
- private _tiles;
41
- private _features;
42
- protected getModelSource(owner: string): ModelSource;
43
- /**
44
- * Loads features as a list of tiles (typically provided by deck.gl).
45
- * After tiles are loaded, {@link extractTileFeatures} must be called
46
- * before computing statistics on the tiles.
47
- */
48
- loadTiles(tiles: unknown[]): void;
49
- /**
50
- * Extracts feature data from tiles previously loaded with {@link loadTiles}.
51
- * Must be called before computing statistics on tiles.
52
- */
53
- extractTileFeatures({ spatialFilter, uniqueIdProperty, options, }: {
54
- spatialFilter: SpatialFilter;
55
- uniqueIdProperty?: string;
56
- options?: TileFeatureExtractOptions;
57
- }): void;
58
- /** Loads features as GeoJSON (used for testing). */
59
- loadGeoJSON({ geojson, spatialFilter, uniqueIdProperty, }: {
60
- geojson: FeatureCollection;
61
- spatialFilter: SpatialFilter;
62
- uniqueIdProperty?: string;
63
- }): void;
64
- getFeatures(options: FeaturesRequestOptions): Promise<FeaturesResponse>;
65
- getFormula({ column, operation, joinOperation, filterOwner, }: FormulaRequestOptions): Promise<FormulaResponse>;
66
- getHistogram({ operation, ticks, column, joinOperation, filterOwner, }: HistogramRequestOptions): Promise<HistogramResponse>;
67
- getCategories({ column, operation, operationColumn, joinOperation, filterOwner, }: CategoryRequestOptions): Promise<CategoryResponse>;
68
- getScatter({ xAxisColumn, yAxisColumn, xAxisJoinOperation, yAxisJoinOperation, filterOwner, }: ScatterRequestOptions): Promise<ScatterResponse>;
69
- getTable(options: TableRequestOptions): Promise<TableResponse>;
70
- getTimeSeries({ column, stepSize, operation, operationColumn, joinOperation, filterOwner, }: TimeSeriesRequestOptions): Promise<TimeSeriesResponse>;
71
- getRange({ column, filterOwner, }: RangeRequestOptions): Promise<RangeResponse>;
72
- /****************************************************************************
73
- * INTERNAL
74
- */
75
- private _getFilteredFeatures;
76
- }
@@ -1,169 +0,0 @@
1
- import {filterFunctions} from './FilterTypes';
2
- import {Filter, FilterLogicalOperator, Filters} from '../types';
3
- import {Feature} from 'geojson';
4
- import {FilterType} from '../constants';
5
- import {FeatureData} from '../types-internal';
6
- import {BinaryFeature} from '@loaders.gl/schema';
7
-
8
- const LOGICAL_OPERATOR_METHODS: Record<
9
- FilterLogicalOperator,
10
- 'every' | 'some'
11
- > = {
12
- and: 'every',
13
- or: 'some',
14
- };
15
-
16
- function passesFilter(
17
- columns: string[],
18
- filters: Filters,
19
- feature: FeatureData,
20
- filtersLogicalOperator: FilterLogicalOperator
21
- ): boolean {
22
- const method = LOGICAL_OPERATOR_METHODS[filtersLogicalOperator];
23
- return columns[method]((column) => {
24
- const columnFilters = filters[column];
25
- const columnFilterTypes = Object.keys(columnFilters) as FilterType[];
26
-
27
- if (!feature || feature[column] === null || feature[column] === undefined) {
28
- return false;
29
- }
30
-
31
- return columnFilterTypes.every((filter) => {
32
- const filterFunction = filterFunctions[filter];
33
-
34
- if (!filterFunction) {
35
- throw new Error(`"${filter}" filter is not implemented.`);
36
- }
37
-
38
- return filterFunction(
39
- columnFilters[filter]!.values,
40
- feature[column],
41
- (columnFilters[filter] as Filter[FilterType.STRING_SEARCH])!.params
42
- );
43
- });
44
- });
45
- }
46
-
47
- export function buildFeatureFilter({
48
- filters = {},
49
- type = 'boolean',
50
- filtersLogicalOperator = 'and',
51
- }: {
52
- filters?: Filters;
53
- type?: 'number' | 'boolean';
54
- filtersLogicalOperator?: FilterLogicalOperator;
55
- }) {
56
- const columns = Object.keys(filters);
57
-
58
- if (!columns.length) {
59
- return () => (type === 'number' ? 1 : true);
60
- }
61
-
62
- return (feature: Feature | FeatureData) => {
63
- const f = feature.properties || feature;
64
- const featurePassesFilter = passesFilter(
65
- columns,
66
- filters,
67
- f as FeatureData,
68
- filtersLogicalOperator
69
- );
70
-
71
- return type === 'number'
72
- ? Number(featurePassesFilter)
73
- : featurePassesFilter;
74
- };
75
- }
76
-
77
- // Apply certain filters to a collection of features
78
- export function applyFilters(
79
- features: FeatureData[],
80
- filters: Filters,
81
- filtersLogicalOperator: FilterLogicalOperator
82
- ) {
83
- return Object.keys(filters).length
84
- ? features.filter(buildFeatureFilter({filters, filtersLogicalOperator}))
85
- : features;
86
- }
87
-
88
- // Binary
89
- export function buildBinaryFeatureFilter({filters = {}}: {filters: Filters}) {
90
- const columns = Object.keys(filters);
91
-
92
- if (!columns.length) {
93
- return () => 1;
94
- }
95
-
96
- return (featureIdIdx: number, binaryData: BinaryFeature) =>
97
- passesFilterUsingBinary(columns, filters, featureIdIdx, binaryData);
98
- }
99
-
100
- function getValueFromNumericProps(
101
- featureIdIdx: number,
102
- binaryData: BinaryFeature,
103
- {column}: {column: string}
104
- ) {
105
- return binaryData.numericProps?.[column]?.value[featureIdIdx];
106
- }
107
-
108
- function getValueFromProperties(
109
- featureIdIdx: number,
110
- binaryData: BinaryFeature,
111
- {column}: {column: string}
112
- ) {
113
- const propertyIdx = binaryData.featureIds.value[featureIdIdx];
114
- return (binaryData.properties[propertyIdx] as Record<string, unknown>)?.[
115
- column
116
- ];
117
- }
118
-
119
- const GET_VALUE_BY_BINARY_PROP = {
120
- properties: getValueFromProperties,
121
- numericProps: getValueFromNumericProps,
122
- };
123
-
124
- function getBinaryPropertyByFilterValues(filterValues: unknown[]) {
125
- return typeof filterValues.flat()[0] === 'string'
126
- ? 'properties'
127
- : 'numericProps';
128
- }
129
-
130
- function getFeatureValue(
131
- featureIdIdx: number,
132
- binaryData: any,
133
- filter: {type: FilterType; column: string; values: unknown[]}
134
- ) {
135
- const {column, values} = filter;
136
- const binaryProp = getBinaryPropertyByFilterValues(values);
137
- const getFeatureValueFn = GET_VALUE_BY_BINARY_PROP[binaryProp];
138
- return getFeatureValueFn(featureIdIdx, binaryData, {column});
139
- }
140
-
141
- function passesFilterUsingBinary(
142
- columns: string[],
143
- filters: Filters,
144
- featureIdIdx: number,
145
- binaryData: BinaryFeature
146
- ) {
147
- return columns.every((column) => {
148
- const columnFilters = filters[column];
149
-
150
- return Object.entries(columnFilters).every(([type, {values}]) => {
151
- const filterFn = filterFunctions[type as FilterType];
152
- if (!filterFn) {
153
- throw new Error(`"${type}" filter is not implemented.`);
154
- }
155
-
156
- if (!values) return 0;
157
-
158
- const featureValue = getFeatureValue(featureIdIdx, binaryData, {
159
- type: type as FilterType,
160
- column,
161
- values,
162
- });
163
-
164
- if (featureValue === undefined || featureValue === null) return 0;
165
-
166
- return filterFn(values, featureValue);
167
- });
168
- });
169
- }
@@ -1,109 +0,0 @@
1
- import {FilterType} from '../constants';
2
- import {FilterInterval, StringSearchOptions} from '../types';
3
- import {makeIntervalComplete} from '../utils/makeIntervalComplete.js';
4
-
5
- export type FilterFunction = (
6
- filterValues: unknown[],
7
- featureValue: unknown,
8
- params?: Record<string, unknown>
9
- ) => boolean;
10
-
11
- export const filterFunctions: Record<FilterType, FilterFunction> = {
12
- [FilterType.IN]: filterIn,
13
- [FilterType.BETWEEN]: filterBetween,
14
- [FilterType.TIME]: filterTime,
15
- [FilterType.CLOSED_OPEN]: filterClosedOpen,
16
- [FilterType.STRING_SEARCH]: filterStringSearch,
17
- };
18
-
19
- function filterIn(filterValues: unknown[], featureValue: unknown): boolean {
20
- return filterValues.includes(featureValue);
21
- }
22
-
23
- // FilterTypes.BETWEEN
24
- function filterBetween(
25
- filterValues: unknown[],
26
- featureValue: unknown
27
- ): boolean {
28
- const checkRange = (range: [number, number]) => {
29
- const [lowerBound, upperBound] = range;
30
- return (
31
- (featureValue as number) >= lowerBound &&
32
- (featureValue as number) <= upperBound
33
- );
34
- };
35
-
36
- return makeIntervalComplete(filterValues as FilterInterval[]).some(
37
- checkRange
38
- );
39
- }
40
-
41
- function filterTime(filterValues: unknown[], featureValue: unknown) {
42
- const featureValueAsTimestamp = new Date(featureValue as number).getTime();
43
- if (isFinite(featureValueAsTimestamp)) {
44
- return filterBetween(filterValues, featureValueAsTimestamp);
45
- } else {
46
- throw new Error(`Column used to filter by time isn't well formatted.`);
47
- }
48
- }
49
-
50
- // FilterTypes.CLOSED_OPEN
51
- function filterClosedOpen(
52
- filterValues: unknown[],
53
- featureValue: unknown
54
- ): boolean {
55
- const checkRange = (range: [number, number]) => {
56
- const [lowerBound, upperBound] = range;
57
- return (
58
- (featureValue as number) >= lowerBound &&
59
- (featureValue as number) < upperBound
60
- );
61
- };
62
-
63
- return makeIntervalComplete(filterValues as [number, number][]).some(
64
- checkRange
65
- );
66
- }
67
-
68
- // FilterTypes.STRING_SEARCH
69
- function filterStringSearch(
70
- filterValues: unknown[],
71
- featureValue: unknown,
72
- params: StringSearchOptions = {}
73
- ): boolean {
74
- const normalizedFeatureValue = normalize(featureValue, params);
75
- const stringRegExp = params.useRegExp
76
- ? filterValues
77
- : filterValues.map((filterValue) => {
78
- let stringRegExp = escapeRegExp(normalize(filterValue, params));
79
-
80
- if (params.mustStart) stringRegExp = `^${stringRegExp}`;
81
- if (params.mustEnd) stringRegExp = `${stringRegExp}$`;
82
-
83
- return stringRegExp;
84
- });
85
-
86
- const regex = new RegExp(
87
- stringRegExp.join('|'),
88
- params.caseSensitive ? 'g' : 'gi'
89
- );
90
- return !!normalizedFeatureValue.match(regex);
91
- }
92
-
93
- // Aux
94
- const specialCharRegExp = /[.*+?^${}()|[\]\\]/g;
95
- const normalizeRegExp = /\p{Diacritic}/gu;
96
-
97
- function escapeRegExp(value: string) {
98
- return value.replace(specialCharRegExp, '\\$&');
99
- }
100
-
101
- function normalize(data: unknown, params: StringSearchOptions) {
102
- let normalizedData = String(data);
103
- if (!params.keepSpecialCharacters)
104
- normalizedData = normalizedData
105
- .normalize('NFD')
106
- .replace(normalizeRegExp, '');
107
-
108
- return normalizedData;
109
- }
@@ -1,32 +0,0 @@
1
- import intersects from '@turf/boolean-intersects';
2
- import {FeatureCollection} from 'geojson';
3
- import {SpatialFilter} from '..';
4
- import {FeatureData} from '../types-internal';
5
-
6
- export function geojsonFeatures({
7
- geojson,
8
- spatialFilter,
9
- uniqueIdProperty,
10
- }: {
11
- geojson: FeatureCollection;
12
- spatialFilter: SpatialFilter;
13
- uniqueIdProperty?: string;
14
- }): FeatureData[] {
15
- let uniqueIdx = 0;
16
- const map = new Map();
17
-
18
- if (!spatialFilter) {
19
- return [];
20
- }
21
-
22
- for (const feature of geojson.features) {
23
- const uniqueId = uniqueIdProperty
24
- ? feature.properties![uniqueIdProperty]
25
- : ++uniqueIdx;
26
- if (!map.has(uniqueId) && intersects(spatialFilter, feature)) {
27
- map.set(uniqueId, feature.properties);
28
- }
29
- }
30
-
31
- return Array.from(map.values());
32
- }
@@ -1,6 +0,0 @@
1
- export * from './Filter.js';
2
- export * from './FilterTypes.js';
3
- export * from './geosjonFeatures.js';
4
- export * from './tileFeatures.js';
5
- export * from './tileFeaturesGeometries.js';
6
- export * from './tileFeaturesSpatialIndex.js';