import {z} from 'zod' export type GetApiTilesCogStatisticsArgs = {data_path?: (string | null) | undefined, carbon_pool?: (string | null) | undefined, product_type?: (string | null) | undefined, product_variant?: (string | null) | undefined, resolution?: (number | null) | undefined, year?: (number | null) | undefined, analysis_completed_at?: (string | null) | undefined, url?: (string | null) | undefined, bidx?: (Array | null) | undefined, expression?: (string | null) | undefined, nodata?: (number | number | string | null) | undefined, unscale?: (boolean | null) | undefined, resampling?: ('nearest' | 'bilinear' | 'cubic' | 'cubic_spline' | 'lanczos' | 'average' | 'mode' | 'gauss' | 'rms' | null) | undefined, reproject?: ('nearest' | 'bilinear' | 'cubic' | 'cubic_spline' | 'lanczos' | 'average' | 'mode' | 'sum' | 'rms' | null) | undefined, max_size?: number | undefined, height?: (number | null) | undefined, width?: (number | null) | undefined, algorithm?: ('hillshade' | 'contours' | 'normalizedIndex' | 'terrarium' | 'terrainrgb') | undefined, algorithm_params?: (string | null) | undefined, categorical?: (boolean | null) | undefined, c?: (Array | null) | undefined, p?: (Array | null) | undefined, histogram_bins?: (string | null) | undefined, histogram_range?: (string | null) | undefined}; export type GetApiTilesCogStatisticsOptions = {baseUrl: string, APIKeyQuery?: string | undefined, fetch?: typeof fetch | undefined}; export const bandStatistics = z.object({min: z.number(), max: z.number(), mean: z.number(), count: z.number(), sum: z.number(), std: z.number(), median: z.number(), majority: z.number(), minority: z.number(), unique: z.number(), histogram: z.array(z.array(z.union([z.number(), z.number().int()]))), valid_percent: z.number(), masked_pixels: z.number(), valid_pixels: z.number()}).and(z.record(z.string(), z.unknown())); export const getApiTilesCogStatisticsResponse = z.record(z.string(), bandStatistics); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Statistics * * GET /tiles/cog/statistics */ export const getApiTilesCogStatistics = async (args: GetApiTilesCogStatisticsArgs, options: GetApiTilesCogStatisticsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyQuery} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.data_path != null) query.append("data_path", String(args.data_path)) if (args.carbon_pool != null) query.append("carbon_pool", String(args.carbon_pool)) if (args.product_type != null) query.append("product_type", String(args.product_type)) if (args.product_variant != null) query.append("product_variant", String(args.product_variant)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (args.year != null) query.append("year", String(args.year)) if (args.analysis_completed_at != null) query.append("analysis_completed_at", String(args.analysis_completed_at)) if (args.url != null) query.append("url", String(args.url)) for (const item of args.bidx ?? []) query.append("bidx", String(item)) if (args.expression != null) query.append("expression", String(args.expression)) if (args.nodata != null) query.append("nodata", String(args.nodata)) if (args.unscale != null) query.append("unscale", String(args.unscale)) if (args.resampling != null) query.append("resampling", String(args.resampling)) if (args.reproject != null) query.append("reproject", String(args.reproject)) if (args.max_size != null) query.append("max_size", String(args.max_size)) if (args.height != null) query.append("height", String(args.height)) if (args.width != null) query.append("width", String(args.width)) if (args.algorithm != null) query.append("algorithm", String(args.algorithm)) if (args.algorithm_params != null) query.append("algorithm_params", String(args.algorithm_params)) if (args.categorical != null) query.append("categorical", String(args.categorical)) for (const item of args.c ?? []) query.append("c", String(item)) for (const item of args.p ?? []) query.append("p", String(item)) if (args.histogram_bins != null) query.append("histogram_bins", String(args.histogram_bins)) if (args.histogram_range != null) query.append("histogram_range", String(args.histogram_range)) if (APIKeyQuery !== undefined) query.set("access_token", APIKeyQuery) const url = `${baseUrl.replace(/\/+$/, '')}/tiles/cog/statistics${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiTilesCogStatisticsResponse, await res.json()) };