Skip to content

Commit

Permalink
Improve Tilejson typings to include properties used in Carto platform (
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigg authored Dec 10, 2024
1 parent 4d5b9c7 commit 5463752
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

export {SOURCE_DEFAULTS} from './base-source';
export type {
VectorLayer,
RasterMetadataBandStats,
RasterBandColorinterp,
RasterMetadataBand,
RasterMetadata,
TilejsonResult,
GeojsonResult,
JsonResult,
Expand Down
149 changes: 144 additions & 5 deletions src/sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,51 @@ export interface Tilejson {
attribution: string;
scheme: string;
tiles: string[];
properties_tiles: string[];
minresolution: number;
maxresolution: number;
minzoom: number;
maxzoom: number;
bounds: [number, number, number, number];
center: [number, number, number];
bounds: [left: number, bottom: number, right: number, top: number];
center: [longitute: number, latitude: number, zoom: number];
vector_layers: VectorLayer[];

//
// Carto additions over standard Tilejson properties
//

minresolution: number;
maxresolution: number;
properties_tiles: string[];
tilestats: Tilestats;
tileResolution?: TileResolution;

/**
* Resolution of data in spatial-index dataset (e.g. H3, Quadbin).
*
* @internal
*/
dataresolution?: number;

/**
* Array of ratios of dropped features per zoom level.
*
* Example: `[0,0,0.5]` - means that 50% of features are dropped at zoom 2 and bigger.
*
* @internal
*/
fraction_dropped_per_zoom?: number[];

/**
* Names of bands - rasters only.
*
* @internal
*/
raster_bands?: string[];

/**
* Raster metadata - rasters only.
*
* @internal
*/
raster_metadata?: RasterMetadata;
}

export interface Tilestats {
Expand All @@ -221,12 +256,116 @@ export interface Attribute {
}

export interface VectorLayer {
// tilejson standard
id: string;
minzoom: number;
maxzoom: number;
fields: Record<string, string>;

// Carto additions over standard Tilejson properties
geometry_type?: string;
}

export type RasterMetadataBandStats = {
approximated_stats?: boolean;
min: number;
max: number;
mean: number;
stddev: number;
sum: number;
sum_squares: number;
count: number;

/**
* Quantiles by number of buckets.
*
* Example:
* ```ts
* {
* // for 3 buckets, first 1/3 of items lies in range [min, 20], second 1/3 is in [20, 40], and last 1/3 is in [40, max]
* 3: [20, 40],
* 4: [20, 30, 50], for 4 buckets ...
* }
* ```
*/
quantiles?: Record<number, number[]>;

/**
* Top values by number of values.
*
* Key of dictionary is value, value is count.
* Key order is random.
*
* Example:
* ```
* {
* 3: 5, // means there are 5 pixels with value 3
* 11: 222,
* 12: 333, // means that 12 is most common value with count 333
* ... // (assuming 333 was largest value in dict)
* }
* ```
*/
top_values?: Record<number, number>;

/**
* Raster loader version.
*/
version?: string;
};

export enum RasterBandColorinterp {
Gray = 'gray',
Red = 'red',
Green = 'green',
Blue = 'blue',
Alpha = 'alpha',
Palette = 'palette',
}

export type RasterMetadataBand = {
type: string;
name: string;
stats: RasterMetadataBandStats;
/**
* Known values:
* * `palette`: use unique value and `colortable` ad default mapping
* * `red`, `green`, `blue`: use the band as color channel
* * `gray`: use the band as grayscale
*/
colorinterp?: string | RasterBandColorinterp; // use RasterBandColorinterp, but it's external value, so let's use string

/**
* Default color mapping for unique values (or if coloprinterp is `palette`)
*/
colortable?: Record<string, number[]>;

/**
* No value representation.
* Observed values:
* * `'nan'` for `NaN`
* * `number`: both as string as number, so parsing is needed
*/
nodata: string | number; // 255, '0', 'nan'
};

export type RasterMetadata = {
block_resolution: number;
minresolution: number;
maxresolution: number;
nodata: number | string;
bands: RasterMetadataBand[];
bounds: [left: number, bottom: number, right: number, top: number];
center: [longitute: number, latitude: number, zoom: number];
width: number;
height: number;
block_width: number;
block_height: number;
num_blocks: number;
num_pixels: number;
pixel_resolution: number;
};

export type TilejsonResult = Tilejson & {accessToken: string};
export type GeojsonResult = {type: 'FeatureCollection'; features: Feature[]};
export type JsonResult = any[];
Expand Down

0 comments on commit 5463752

Please sign in to comment.