@@ -3,6 +3,9 @@ import { geoLevels } from "$lib/config/geo-levels.js";
33import getChildAreas from "$lib/api/geo/getChildAreas.js" ;
44import hasObservation from "./hasObservation.js" ;
55import { isValidMonth , isValidYear } from "$lib/api/utils.js" ;
6+ import readData from "$lib/data" ;
7+
8+ const areasClusters = await readData ( "areas-clusters" ) ;
69
710export function ascending ( a , b ) {
811 return a == null || b == null ? NaN : a < b ? - 1 : a > b ? 1 : a >= b ? 0 : NaN ;
@@ -13,12 +16,12 @@ export function makeFilter(param) {
1316 return d => set . has ( d [ 0 ] ) ;
1417}
1518
16- export function makeGeoFilter ( geo , geoExtent ) {
19+ export function makeGeoFilter ( geo , geoExtent , geoCluster ) {
1720 const codes = new Set ( ) ;
1821 const types = new Set ( ) ;
1922 for ( const g of [ geo ] . flat ( ) ) {
2023 // if (g.match(/^[EKNSW]\d{2}$/)) types.add(g);
21- if ( geoLevels [ g ] ) {
24+ if ( geoLevels [ g ] && geoCluster === "all" ) {
2225 if ( geoExtent . match ( / ^ [ E K N S W ] \d { 8 } $ / ) ) {
2326 const children = getChildAreas ( { code : geoExtent , geoLevel : g , includeNames : false } ) ;
2427 for ( const child of children ) codes . add ( child ) ;
@@ -28,6 +31,13 @@ export function makeGeoFilter(geo, geoExtent) {
2831 }
2932 else if ( g . match ( / ^ [ E K N S W ] \d { 8 } $ / ) && ! types . has ( g . slice ( 0 , 3 ) ) ) codes . add ( g ) ;
3033 }
34+ if ( geoCluster ) {
35+ const [ grouping , cluster ] = geoCluster . split ( "_" ) ;
36+ const cds = areasClusters . clusters ?. [ grouping ] ?. [ cluster ] ;
37+ if ( Array . isArray ( cds ) ) {
38+ for ( const cd of cds ) codes . add ( cd ) ;
39+ }
40+ }
3141 return codes . size > 0 && types . size > 0 ? d => codes . has ( d [ 0 ] ) || types . has ( d [ 0 ] . slice ( 0 , 3 ) ) :
3242 types . size > 0 ? d => types . has ( d [ 0 ] . slice ( 0 , 3 ) ) :
3343 codes . size > 0 ? d => codes . has ( d [ 0 ] ) :
@@ -56,11 +66,13 @@ export function getTime(values, params = {}) {
5666
5767 const periods = values . map ( v => ( { value : v , period : periodToDateRange ( v [ 0 ] ) } ) ) ;
5868 const nearest = params . nearest || "none" ;
59- const isRange = periods [ 0 ] . period . length > 1 ;
60- const date = isRange || params . time . length === 10 ? toPlainDate ( params . time , true ) : [ toPlainDate ( params . time , false ) , toPlainDate ( params . time , true ) ] ;
69+ const periodIsRange = periods [ 0 ] . period . length > 1 ; // Time periods have a duration component
70+ const dateIsExact = params . time . length === 10 ; // Requested date is to nearest day
71+ const date = periodIsRange || dateIsExact ? toPlainDate ( params . time , true ) : [ toPlainDate ( params . time , false ) , toPlainDate ( params . time , true ) ] ;
6172
6273 let match ;
63- if ( isRange ) match = periods . findLast ( p => Temporal . PlainDate . compare ( date , p . period [ 0 ] ) !== - 1 && Temporal . PlainDate . compare ( date , p . period [ 1 ] ) !== 1 ) ;
74+ if ( periodIsRange ) match = periods . findLast ( p => Temporal . PlainDate . compare ( date , p . period [ 0 ] ) !== - 1 && Temporal . PlainDate . compare ( date , p . period [ 1 ] ) !== 1 ) ;
75+ else if ( dateIsExact ) match = periods . findLast ( p => Temporal . PlainDate . compare ( date , p . period [ 0 ] ) === 0 ) ;
6476 else match = periods . findLast ( p => Temporal . PlainDate . compare ( p . period [ 0 ] , date [ 0 ] ) !== - 1 && Temporal . PlainDate . compare ( p . period [ 0 ] , date [ 1 ] ) !== 1 ) ;
6577 if ( match ) return [ match . value ] ;
6678
0 commit comments