|
| 1 | +-- --------------------------------------------------------------------------- |
| 2 | +-- |
| 3 | +-- Theme: explore |
| 4 | +-- Topic: protected_areas |
| 5 | +-- |
| 6 | +-- --------------------------------------------------------------------------- |
| 7 | + |
| 8 | +local themepark, theme, cfg = ... |
| 9 | + |
| 10 | +themepark:add_table({ |
| 11 | + name = 'protected_areas_boundaries', |
| 12 | + ids_type = 'area', |
| 13 | + geom = 'multipolygon', |
| 14 | + columns = themepark:columns('core/name', { |
| 15 | + { column = 'boundary', type = 'text' }, |
| 16 | + { column = 'protect_class', type = 'text' }, |
| 17 | + { column = 'protection_title', type = 'text' }, |
| 18 | + { column = 'short_protection_title', type = 'text' }, |
| 19 | + { column = 'nature_reserve', type = 'bool', not_null = true }, -- has tag leisure=nature_reserve |
| 20 | + { column = 'ref', type = 'text' }, |
| 21 | + { column = 'access', type = 'text' }, |
| 22 | + { column = 'wikidata', type = 'text' }, |
| 23 | + { column = 'website', type = 'text' }, |
| 24 | + { column = 'operator', type = 'text' }, |
| 25 | + { column = 'operator_wikidata', type = 'text' }, |
| 26 | + { column = 'related_law', type = 'text' }, |
| 27 | + { column = 'related_law_url', type = 'text' }, |
| 28 | + { column = 'iucn', type = 'text' }, -- tag "ref:IUCN" |
| 29 | + { column = 'iucn_level', type = 'text' }, -- tag "iucn_level" |
| 30 | + { column = 'wdpa', type = 'int' }, -- WDPA |
| 31 | + { column = 'capad_pa_id', type = 'text' }, -- Collaborative Australian Protected Areas Database (CAPAD) |
| 32 | + { column = 'dtp_id', type = 'text' }, -- DIGITIZE THE PLANET |
| 33 | + }), |
| 34 | +}) |
| 35 | + |
| 36 | +themepark:add_table({ |
| 37 | + name = 'protected_areas_errors', |
| 38 | + ids_type = 'area', |
| 39 | + geom = 'multipolygon', |
| 40 | + columns = { |
| 41 | + { column = 'errormsg', type = 'text', not_null = true }, |
| 42 | + { column = 'value', type = 'text' }, |
| 43 | + } |
| 44 | +}) |
| 45 | + |
| 46 | +local get_qcode = function(wd) |
| 47 | + if wd and wd:match('^Q%d+$') then |
| 48 | + return wd |
| 49 | + end |
| 50 | + return nil |
| 51 | +end |
| 52 | + |
| 53 | +themepark:add_proc('area', function(object, data) |
| 54 | + local t = object.tags |
| 55 | + if t.boundary == 'protected_area' or t.boundary == 'national_park' or t.boundary == 'water_protection_area' or t.leisure == 'nature_reserve' or t.protected_area then |
| 56 | + |
| 57 | + -- Check that wikidata tags contain syntactically valid Q codes |
| 58 | + -- https://www.wikidata.org/wiki/{wikidata_code} |
| 59 | + local wikidata_code = get_qcode(t.wikidata) |
| 60 | + local operator_wikidata_code = get_qcode(t.operator_wikidata) |
| 61 | + |
| 62 | + if t.wikidata ~= wikidata_code then |
| 63 | + themepark:insert('protected_areas_errors', { errormsg = 'invalid Q-item code for wikidata tag', value = t.wikidata }, t) |
| 64 | + end |
| 65 | + if t.operator_wikidata ~= operator_wikidata_code then |
| 66 | + themepark:insert('protected_areas_errors', { errormsg = 'invalid Q-item code for operator_wikidata tag', value = t.operator_wikidata }, t) |
| 67 | + end |
| 68 | + |
| 69 | + -- WDPA is always an integer |
| 70 | + -- https://www.protectedplanet.net/{wdpa} |
| 71 | + local wdpa = t['ref:WDPA'] |
| 72 | + if wdpa and wdpa:match('^%d+$') then |
| 73 | + wdpa = tonumber(wdpa) |
| 74 | + else |
| 75 | + themepark:insert('protected_areas_errors', { errormsg = 'not a number in ref:WDPA tag', value = wdpa }, t) |
| 76 | + wdpa = nil |
| 77 | + end |
| 78 | + |
| 79 | + -- Digitize The Planet uses UUIDs |
| 80 | + -- https://content.digitizetheplanet.org/de/rules/show_protectedarea/{dtp_id} |
| 81 | + local dtp_id = t.dtp_id |
| 82 | + if dtp_id and not dtp_id:match('^[0-9a-f-]+$') then |
| 83 | + themepark:insert('protected_areas_errors', { errormsg = 'not a UUID in dtp_id tag', value = dtp_id }, t) |
| 84 | + end |
| 85 | + |
| 86 | + local a = { |
| 87 | + boundary = t.boundary, |
| 88 | + protect_class = t.protect_class, |
| 89 | + protection_title = t.protection_title, |
| 90 | + short_protection_title = t.short_protection_title, |
| 91 | + nature_reserve = (t.leisure == 'nature_reserve'), |
| 92 | + protected_area = t.protected_area, |
| 93 | + ref = t.ref, |
| 94 | + access = t.access, |
| 95 | + wikidata = wikidata_code, |
| 96 | + website = t.website, |
| 97 | + operator = t.operator, |
| 98 | + operator_wikidata = operator_wikidata_code, |
| 99 | + related_law = t.related_law, |
| 100 | + related_law_url = t['related_law:url'], |
| 101 | + iucn = t['ref:IUCN'], |
| 102 | + iucn_level = t.iucn_level, |
| 103 | + wdpa = wdpa, |
| 104 | + capad_pa_id = t['ref:capad:pa_id'], |
| 105 | + dtp_id = dtp_id, |
| 106 | + geom = object:as_area(), |
| 107 | + } |
| 108 | + |
| 109 | + themepark.themes.core.add_name(a, object) |
| 110 | + |
| 111 | + themepark:insert('protected_areas_boundaries', a, t) |
| 112 | + end |
| 113 | +end) |
| 114 | + |
| 115 | +-- --------------------------------------------------------------------------- |
0 commit comments