@@ -109,23 +109,59 @@ def generate_csw_metadata(base_url: str = DMS_DATASETS_BASE, lang="en"):
109109 ).aggregate ("id, lang, string_agg(description, '\n ') as description" )
110110 log .debug (abstracts )
111111
112+ licenses = (
113+ resources .set_alias ("r" )
114+ .join (datasets .set_alias ("d" ), condition = "r.dataset_id = d.id" )
115+ .select (
116+ """r.id, unnest(json_transform(d.metadata->'$.rightsList[*]', '[{"lang": "VARCHAR", "rights": "VARCHAR", "rightsURI": "VARCHAR", "rightsIdentifier": "VARCHAR", "schemeUri": "VARCHAR"}]'), recursive := true)""" # noqa: E501
117+ )
118+ .filter (duckdb .ColumnExpression ("lang" ) == duckdb .ConstantExpression (lang ))
119+ .aggregate ("id, lang, first(rights) as rights, first(rightsURI) as url" )
120+ )
121+ log .debug (licenses )
122+
123+ subjects = (
124+ resources .set_alias ("r" )
125+ .join (datasets .set_alias ("d" ), condition = "r.dataset_id = d.id" )
126+ .select (
127+ """r.id, unnest(json_transform(d.metadata->'$.subjects[*]', '[{"subject": "VARCHAR", "subjectScheme": "VARCHAR", "schemeURI": "VARCHAR", "valueURI": "VARCHAR", "classificationCode": "VARCHAR"}]'), recursive := true)""" # noqa: E501
128+ )
129+ .aggregate (
130+ "id, array_agg(subject) as keywords, subjectScheme as scheme_name, schemeURI as url" # noqa: E501
131+ )
132+ .aggregate (
133+ """id,
134+ json_group_object(
135+ coalesce(scheme_name, 'default'),
136+ case when url is not null then
137+ {"keywords_type": 'theme', "keywords": keywords, "vocabulary": {
138+ "name": scheme_name, "url": url
139+ },}
140+ else
141+ {"keywords_type": 'theme', "keywords": keywords }
142+ end
143+ ) as keywords
144+ """
145+ )
146+ )
147+ log .debug (subjects )
148+
112149 identification = conn .sql ("""
113150 from resources as r
114151 join datasets as d on r.dataset_id = d.id
115152 left join abstracts as a on a.id = r.id
153+ left join licenses as l on l.id = r.id
154+ left join subjects as s on s.id = r.id
116155 select
117156 r.id,
118157 {
119158 identifier: r.id,
120- -- TODO: these needs to be read from the dataset
121159 language: d.metadata->'$.language',
122- -- TODO: these is probably a combination
123- title: r.title,
160+ title: d.title || ' - ' || r.title,
124161 fee: 'None',
125162 status: 'completed',
126- -- TODO: these needs to be read from the dataset
127- rights: '',
128- abstract: a.description,
163+ rights: l.rights,
164+ abstract: a.description || '\n ' || r.description,
129165 url: r.uri,
130166 dates: {
131167 creation: r.created_at,
@@ -150,17 +186,16 @@ def generate_csw_metadata(base_url: str = DMS_DATASETS_BASE, lang="en"):
150186 end
151187 },
152188 license: {
153- -- need to join with dataset to read those!
154- "name": coalesce('All rights reserved'),
155- "uri": coalesce(''),
189+ "name": coalesce(l.rights, 'All rights reserved'),
190+ "uri": coalesce(l.url, ''),
156191 },
157- keywords: {
192+ keywords: coalesce(s.keywords, {
158193 "default": {
159194 -- need to join with dataset to read those!
160195 keywords_type: 'theme',
161196 keywords: [],
162197 }
163- },
198+ }) ,
164199 }::json as identification
165200 """ ) # noqa: E501
166201
@@ -221,7 +256,7 @@ def generate_csw_metadata(base_url: str = DMS_DATASETS_BASE, lang="en"):
221256 {
222257 file: {
223258 url: uri,
224- type: case when type = 'raster' then 'FILE:RASTER '
259+ type: case when type = 'raster' then 'FILE:GEO '
225260 when type = 'vector' then 'FILE:GEO'
226261 else 'download'
227262 end,
@@ -271,17 +306,17 @@ def generate_csw_metadata(base_url: str = DMS_DATASETS_BASE, lang="en"):
271306 strftime(r.created_at, '%Y-%m-%d'),
272307 ''
273308 ) as insert_date,
274- r.title,
309+ r.metadata->>'$.identification.title' as title,
275310 coalesce(
276311 strftime(r.last_modified_at, '%Y-%m-%d'),
277312 ''
278313 ) as date_modified,
279314 'dataset' as type,
280315 null::varchar as format,
281- case
282- when r.extent is not null then ST_AsText( r.extent)
316+ ST_AsText( case
317+ when r.extent is not null then r.extent
283318 else st_makeEnvelope(4.99207807783, 58.0788841824, 31.29341841, 80.6571442736)
284- end as wkt_geometry,
319+ end) as wkt_geometry,
285320 r.xml as metadata,
286321 r.xml,
287322 coalesce(
@@ -315,6 +350,8 @@ def generate_csw_metadata(base_url: str = DMS_DATASETS_BASE, lang="en"):
315350
316351 log .debug (csw )
317352
353+ csw .write_parquet ("dms-metadata.parquet" )
354+
318355
319356if __name__ == "__main__" :
320357 app ()
0 commit comments