Skip to content

Commit

Permalink
prettier format
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara committed Dec 2, 2024
1 parent 7c74ae2 commit 25ae33d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from './filters.js';
export * from './geo.js';
export * from './widget-sources/index.js';
export * from './types.js';
export * from './spatial-index.js'
export * from './spatial-index.js';

export {
APIErrorContext,
Expand Down
2 changes: 1 addition & 1 deletion src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {$TODO} from '../types-internal.js';
import {assert} from '../utils.js';
import {ModelRequestOptions, makeCall} from './common.js';
import {ApiVersion} from '../constants.js';
import { SpatialDataType, SpatialFilterPolyfillMode } from '../sources/types.js';
import {SpatialDataType, SpatialFilterPolyfillMode} from '../sources/types.js';

/** @internalRemarks Source: @carto/react-api */
const AVAILABLE_MODELS = [
Expand Down
2 changes: 1 addition & 1 deletion src/sources/quadbin-table-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const quadbinTableSource = async function (
...options,
// NOTE: passing redundant spatialDataColumn here to apply the default value 'quadbin'
spatialDataColumn,
spatialDataType: 'quadbin'
spatialDataType: 'quadbin',
}),
})
);
Expand Down
2 changes: 1 addition & 1 deletion src/sources/vector-query-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type VectorQuerySourceOptions = SourceOptions &
QuerySourceOptions &
FilterOptions &
ColumnsOption & {
spatialDataType: 'geo'
spatialDataType: 'geo';
};

type UrlParameters = {
Expand Down
47 changes: 33 additions & 14 deletions src/spatial-index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { DEFAULT_AGGREGATION_RES_LEVEL_H3, DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN } from "./constants-internal";
import { ModelSource } from "./models/model";
import { AggregationOptions } from "./sources/types";
import {
DEFAULT_AGGREGATION_RES_LEVEL_H3,
DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN,
} from './constants-internal';
import {ModelSource} from './models/model';
import {AggregationOptions} from './sources/types';

const DEFAULT_TILE_SIZE = 512;
const QUADBIN_ZOOM_MAX_OFFSET = 4;

export function getSpatialFiltersResolution({ source, viewState }: {
export function getSpatialFiltersResolution({
source,
viewState,
}: {
source: ModelSource & AggregationOptions;
viewState: {
zoom: number;
latitude: number;
longitude: number;
}
};
}) {
if (source.spatialDataType === 'geo') {
return undefined;
Expand All @@ -27,22 +33,26 @@ export function getSpatialFiltersResolution({ source, viewState }: {
? DEFAULT_AGGREGATION_RES_LEVEL_H3
: DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN);

const aggregationResLevelOffset = Math.max(0, Math.floor(aggregationResLevel));
const aggregationResLevelOffset = Math.max(
0,
Math.floor(aggregationResLevel)
);

const currentZoomInt = Math.ceil(currentZoom);
if (source.spatialDataType === 'h3') {
const tileSize = DEFAULT_TILE_SIZE;
const maxResolutionForZoom =
maxH3SpatialFiltersResolutions.find(([zoom]) => zoom === currentZoomInt)?.[1] ??
Math.max(0, currentZoomInt - 3);
maxH3SpatialFiltersResolutions.find(
([zoom]) => zoom === currentZoomInt
)?.[1] ?? Math.max(0, currentZoomInt - 3);

const maxSpatialFiltersResolution = maxResolutionForZoom
? Math.min(dataResolution, maxResolutionForZoom)
: dataResolution;

const hexagonResolution =
getHexagonResolution(
{ zoom: currentZoom, latitude: viewState.latitude },
{zoom: currentZoom, latitude: viewState.latitude},
tileSize
) + aggregationResLevelOffset;

Expand All @@ -51,9 +61,13 @@ export function getSpatialFiltersResolution({ source, viewState }: {

if (source.spatialDataType === 'quadbin') {
const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
const maxSpatialFiltersResolution = Math.min(dataResolution, maxResolutionForZoom);
const maxSpatialFiltersResolution = Math.min(
dataResolution,
maxResolutionForZoom
);

const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
const quadsResolution =
Math.floor(viewState.zoom) + aggregationResLevelOffset;
return Math.min(quadsResolution, maxSpatialFiltersResolution);
}

Expand All @@ -80,7 +94,7 @@ const maxH3SpatialFiltersResolutions = [
[4, 2],
[3, 1],
[2, 1],
[1, 0]
[1, 0],
];

// stolen from https://github.com/visgl/deck.gl/blob/master/modules/carto/src/layers/h3-tileset-2d.ts
Expand All @@ -99,8 +113,13 @@ export function getHexagonResolution(
// expressed as an offset to the viewport zoom.
const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
const hexagonScaleFactor = (2 / 3) * (viewport.zoom - zoomOffset);
const latitudeScaleFactor = Math.log(1 / Math.cos((Math.PI * viewport.latitude) / 180));
const latitudeScaleFactor = Math.log(
1 / Math.cos((Math.PI * viewport.latitude) / 180)
);

// Clip and bias
return Math.max(0, Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS));
return Math.max(
0,
Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS)
);
}
77 changes: 63 additions & 14 deletions src/widget-sources/widget-base-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
async getCategories(
options: CategoryRequestOptions
): Promise<CategoryResponse> {
const {filterOwner, spatialFilter, spatialFiltersMode, spatialFiltersResolution, abortController, ...params} = options;
const {
filterOwner,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
abortController,
...params
} = options;
const {column, operation, operationColumn} = params;

type CategoriesModelResponse = {rows: {name: string; value: number}[]};
Expand Down Expand Up @@ -130,7 +137,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
async getFeatures(
options: FeaturesRequestOptions
): Promise<FeaturesResponse> {
const {filterOwner, spatialFilter, spatialFiltersMode, spatialFiltersResolution, abortController, ...params} = options;
const {
filterOwner,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
abortController,
...params
} = options;
const {columns, dataType, featureIds, z, limit, tileResolution} = params;

type FeaturesModelResponse = {rows: Record<string, unknown>[]};
Expand All @@ -141,7 +155,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {
columns,
Expand Down Expand Up @@ -185,7 +199,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {column: column ?? '*', operation, operationExp},
opts: {abortController},
Expand All @@ -203,7 +217,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
async getHistogram(
options: HistogramRequestOptions
): Promise<HistogramResponse> {
const {filterOwner, spatialFilter, spatialFiltersMode, spatialFiltersResolution, abortController, ...params} = options;
const {
filterOwner,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
abortController,
...params
} = options;
const {column, operation, ticks} = params;

type HistogramModelResponse = {rows: {tick: number; value: number}[]};
Expand All @@ -214,7 +235,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {column, operation, ticks},
opts: {abortController},
Expand Down Expand Up @@ -243,7 +264,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
* or rendering a range slider UI for filtering.
*/
async getRange(options: RangeRequestOptions): Promise<RangeResponse> {
const {filterOwner, spatialFilter, spatialFiltersMode, spatialFiltersResolution, abortController, ...params} = options;
const {
filterOwner,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
abortController,
...params
} = options;
const {column} = params;

type RangeModelResponse = {rows: {min: number; max: number}[]};
Expand All @@ -254,7 +282,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {column},
opts: {abortController},
Expand All @@ -270,7 +298,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
* values. Suitable for rendering scatter plots.
*/
async getScatter(options: ScatterRequestOptions): Promise<ScatterResponse> {
const {filterOwner, spatialFilter, spatialFiltersMode, spatialFiltersResolution, abortController, ...params} = options;
const {
filterOwner,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
abortController,
...params
} = options;
const {xAxisColumn, xAxisJoinOperation, yAxisColumn, yAxisJoinOperation} =
params;

Expand All @@ -285,7 +320,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {
xAxisColumn,
Expand All @@ -309,7 +344,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
* sorting. Suitable for displaying tables and lists.
*/
async getTable(options: TableRequestOptions): Promise<TableResponse> {
const {filterOwner, spatialFilter, spatialFiltersMode, spatialFiltersResolution, abortController, ...params} = options;
const {
filterOwner,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
abortController,
...params
} = options;
const {columns, sortBy, sortDirection, offset = 0, limit = 10} = params;

type TableModelResponse = {
Expand All @@ -323,7 +365,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {
column: columns,
Expand Down Expand Up @@ -351,7 +393,14 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
async getTimeSeries(
options: TimeSeriesRequestOptions
): Promise<TimeSeriesResponse> {
const {filterOwner, abortController, spatialFilter, spatialFiltersMode, spatialFiltersResolution, ...params} = options;
const {
filterOwner,
abortController,
spatialFilter,
spatialFiltersMode,
spatialFiltersResolution,
...params
} = options;
const {
column,
operationColumn,
Expand All @@ -375,7 +424,7 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
...this.getModelSource(filterOwner),
spatialFiltersResolution,
spatialFiltersMode,
spatialFilter
spatialFilter,
},
params: {
column,
Expand Down

0 comments on commit 25ae33d

Please sign in to comment.