generated from trias-project/checklist-recipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwc_event.sql
124 lines (112 loc) · 3.84 KB
/
dwc_event.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Schema: https://rs.gbif.org/core/dwc_event_2022-02-02.xml
*/
/* HELPER TABLES */
WITH
-- DATA RIGHTS HOLDERS
-- Concatenate up to 3 data rights holders with " | " into a single value
-- E.g. Federal Agency for Nature Conservation (BfN) | Research and Technology Centre (Buesum) (FTZ)
datarightsholders AS (
SELECT
DataRightsHolder AS Key,
COALESCE(
edmo_1.Description || ' | ' || edmo_2.Description || ' | ' || edmo_3.Description,
edmo_1.Description || ' | ' || edmo_2.Description,
edmo_1.Description
) AS Description
FROM
(
SELECT DISTINCT
DataRightsHolder,
DataRightsHolder_1,
DataRightsHolder_2,
DataRightsHolder_3
FROM
campaigns
) AS c
LEFT JOIN edmo AS edmo_1
ON c.DataRightsHolder_1 = edmo_1.Key
LEFT JOIN edmo AS edmo_2
ON c.DataRightsHolder_2 = edmo_2.Key
LEFT JOIN edmo AS edmo_3
ON c.DataRightsHolder_3 = edmo_3.Key
)
/* RECORD-LEVEL */
SELECT
'ICES' AS institutionCode,
'ESAS' AS collectionCode,
'https://esas.ices.dk' AS datasetID,
'European Seabirds At Sea (ESAS)' AS datasetName,
'https://creativecommons.org/licenses/by/4.0/' AS license,
-- Type is set to specific values, rather than 'Event' for all
-- See https://github.com/iobis/env-data/issues/4#issuecomment-331807994
*
FROM (
/* CAMPAIGNS */
SELECT
-- RECORD-LEVEL
datarightsholders.Description AS rightsHolder,
'cruise' AS type,
-- EVENT
c.CampaignID AS eventID,
NULL AS parentEventID,
date(c.StartDate) || '/' || date(c.EndDate) AS eventDate,
c.Notes AS eventRemarks,
-- LOCATION
NULL AS decimalLatitude,
NULL AS decimalLongitude,
NULL AS geodeticDatum,
NULL AS georeferenceRemarks
FROM
campaigns AS c
LEFT JOIN datarightsholders
ON c.DataRightsHolder = datarightsholders.Key
UNION
/* SAMPLES */
SELECT
-- RECORD-LEVEL
datarightsholders.Description AS rightsHolder,
'sample' AS type,
-- EVENT
c.CampaignID || '_' || s.SampleID AS eventID,
c.CampaignID AS parentEventID,
date(s.Date) AS eventDate,
s.Notes AS eventRemarks,
-- LOCATION
NULL AS decimalLatitude,
NULL AS decimalLongitude,
NULL AS geodeticDatum,
NULL AS georeferenceRemarks
FROM
samples AS s
LEFT JOIN campaigns AS c
ON s.CampaignID = c.campaignID
LEFT JOIN datarightsholders
ON c.DataRightsHolder = datarightsholders.Key
UNION
/* POSITIONS */
SELECT
-- RECORD-LEVEL
datarightsholders.Description AS rightsHolder,
'subSample' AS type,
-- EVENT
c.CampaignID || '_' || s.SampleID || '_' || p.PositionID AS eventID,
c.CampaignID || '_' || s.SampleID AS parentEventID,
date(s.Date) || 'T' || time(p.Time) || 'Z' AS eventDate, -- p.Time is in UTC
NULL AS eventRemarks,
-- LOCATION
p.Latitude AS decimalLatitude,
p.Longitude AS decimalLongitude,
'EPSG:4326' AS geodeticDatum,
'coordinate uncertainty unknown, see https://github.com/inbo/esas2obis/issues/5' AS georeferenceRemarks
FROM
positions AS p
LEFT JOIN samples AS s
ON p.SampleID = s.sampleID
LEFT JOIN campaigns AS c
ON s.CampaignID = c.campaignID
LEFT JOIN datarightsholders
ON c.DataRightsHolder = datarightsholders.Key
)
ORDER BY
eventID