11import { countFeatures , venueTypes } from './stores' ;
22import type { Dataset , Venue } from '$lib/types' ;
3- import { type Option } from 'svelte-multiselect' ;
3+ import type { ObjectOption } from 'svelte-multiselect' ;
44
55import { base } from '$app/paths' ;
66
7- export const loadData = async ( ) => {
8- const res = await fetch ( base + 'datasets/index.json' ) ;
7+ type Fetch = typeof fetch ;
8+
9+ export const loadData = async ( fetch : Fetch ) => {
10+ const res = await fetch ( base + '/datasets/index.jsonld' ) ;
911 const index = await res . json ( ) ;
1012
1113 const venuesData : Venue [ ] = [ ] ;
1214
13- index . dataset . forEach ( async ( dataset : Dataset ) => {
14- console . log ( dataset [ '@id' ] ) ;
15- const data = await fetch ( dataset [ '@id' ] ) . then ( ( res ) => res . json ( ) ) ;
16- venuesData . push ( ...data ) ;
17- } ) ;
15+ if ( index . dataset && Array . isArray ( index . dataset ) ) {
16+ await Promise . all (
17+ index . dataset . map ( async ( dataset : Dataset ) => {
18+ const contentUrl = dataset . distribution . find (
19+ ( distribution ) => distribution . encodingFormat === 'application/ld+json'
20+ ) ?. contentUrl ;
21+
22+ const res = await fetch ( 'datasets/' + contentUrl ) ;
23+ const data = await res . json ( ) ;
24+ venuesData . push ( ...data . hasPart ) ;
25+ } )
26+ ) ;
27+ } else {
28+ console . error ( 'No dataset array found in the index:' , index ) ;
29+ }
1830
1931 const geoJsonFeatures = convertToGeoJson ( venuesData ) ;
2032 countFeatures . set ( geoJsonFeatures . features . length ) ;
@@ -26,48 +38,63 @@ export const loadData = async () => {
2638} ;
2739
2840const convertToGeoJson = ( data : Venue [ ] ) => {
41+ const features = data . reduce ( ( features : GeoJSON . Feature [ ] , venue , index ) => {
42+ if ( ! venue . location ?. location ?. geo ?. longitude || ! venue . location ?. location ?. geo ?. latitude ) {
43+ return features ;
44+ }
45+
46+ const type = Array . isArray ( venue . additionalType )
47+ ? venue . additionalType . map ( ( type ) => type [ '@id' ] )
48+ : [ ] ;
49+
50+ const startYear = venue . location ?. startDate
51+ ? new Date ( venue . location . startDate ) . getFullYear ( )
52+ : null ;
53+ const endYear = venue . location ?. endDate
54+ ? new Date ( venue . location . endDate ) . getFullYear ( )
55+ : new Date ( ) . getFullYear ( ) ;
56+
57+ const feature : GeoJSON . Feature = {
58+ id : index ,
59+ type : 'Feature' ,
60+ geometry : {
61+ type : 'Point' ,
62+ coordinates : [ venue . location . location . geo . longitude , venue . location . location . geo . latitude ]
63+ } ,
64+ properties : {
65+ name : venue . name ,
66+ nameLower : venue . name . toLowerCase ( ) ,
67+ type : type ,
68+ startYear : startYear ,
69+ endYear : endYear ,
70+ venue : venue // Full venue data
71+ }
72+ } ;
73+
74+ features . push ( feature ) ;
75+ return features ;
76+ } , [ ] ) ;
77+
2978 const geojson = {
3079 type : 'FeatureCollection' ,
31- features : data . map ( ( venue ) => {
32- const i = data . indexOf ( venue ) ;
33- const type = venue . additionalType . map ( ( type ) => type [ '@id' ] ) ;
34- const startYear = venue . location . startDate
35- ? new Date ( venue . location . startDate ) . getFullYear ( )
36- : null ;
37- const endYear = venue . location . endDate
38- ? new Date ( venue . location . endDate ) . getFullYear ( )
39- : new Date ( ) . getFullYear ( ) ;
40- return {
41- id : i ,
42- type : 'Feature' ,
43- geometry : {
44- type : 'Point' ,
45- coordinates : [ venue . location . location . geo . longitude , venue . location . location . geo . latitude ]
46- } ,
47- properties : {
48- name : venue . name ,
49- type : type ,
50- startYear : startYear ,
51- endYear : endYear ,
52- venue : venue // Full venue data (as string)
53- }
54- } ;
55- } )
80+ features : features
5681 } ;
5782
5883 return geojson ;
5984} ;
6085
6186const getVenueTypes = ( data : Venue [ ] ) => {
62- const venueTypes = data . map ( ( venue ) => venue . additionalType ) ;
63- const uniqueVenueTypes = [ ...new Set ( venueTypes . flat ( ) ) ] ;
87+ const allVenueTypes = data . flatMap ( ( venue ) => venue . additionalType ) ;
6488
65- const options : Option [ ] = uniqueVenueTypes . map ( ( venueType ) => {
66- return {
67- label : venueType . name ,
68- value : venueType [ '@id' ]
69- } ;
89+ const uniqueTypesMap = new Map ( ) ;
90+ allVenueTypes . forEach ( ( venueType ) => {
91+ uniqueTypesMap . set ( venueType [ '@id' ] , venueType ) ;
7092 } ) ;
7193
72- return options . sort ( ) ;
94+ const options : ObjectOption [ ] = Array . from ( uniqueTypesMap . values ( ) ) . map ( ( venueType ) => ( {
95+ label : venueType . name ,
96+ value : venueType [ '@id' ]
97+ } ) ) ;
98+
99+ return options . sort ( ( a , b ) => String ( a . label ) . localeCompare ( String ( b . label ) ) ) ;
73100} ;
0 commit comments