Repository navigation
Expand file tree
/
Copy pathupdate_poi.sql
More file actions
405 lines (362 loc) · 18.8 KB
/
Copy pathupdate_poi.sql
File metadata and controls
405 lines (362 loc) · 18.8 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
--
-- Post data update request : update POI table
--
-- Read opening state according to tags
CREATE OR REPLACE FUNCTION opening_state(tags HSTORE) RETURNS VARCHAR AS $$
DECLARE
status VARCHAR := 'unknown';
oh_c19 VARCHAR;
oh VARCHAR;
BEGIN
oh_c19 := tags->'opening_hours:covid19';
oh := tags->'opening_hours';
-- opening_hours:covid19 defined
IF oh_c19 != '' THEN
-- opening_hours:covid19 = open
IF oh_c19 IN ('open', 'same', 'yes') THEN
-- opening_hours closed + opening_hours:covid19 same
IF oh ILIKE 'off%' AND oh_c19 = 'same' THEN
status := 'closed';
-- opening_hours open
ELSE
status := 'open';
END IF;
-- opening_hours:covid19 = closed
ELSIF oh_c19 ILIKE 'off%' OR oh_c19 = 'closed' THEN
status := 'closed';
-- opening_hours:covid19 = opening_hours
ELSIF oh_c19 = oh THEN
status := 'open';
-- opening_hours:covid19 = opening_hours syntax
ELSE
status := 'open_adapted';
END IF;
-- Has any covid19 related service
ELSIF tags->'delivery:covid19' IN ('yes', 'only') OR tags->'drive_through:covid19' IN ('yes', 'only') OR tags->'takeaway:covid19' IN ('yes', 'only') OR tags->'pickup:covid19' IN ('yes', 'only') THEN
status := 'open_adapted';
-- Has access:covid19 explicitly set to yes
ELSIF tags->'access:covid19' = 'yes' THEN
status := 'open';
-- Self-service / vending machines
ELSIF (tags->'amenity' = 'fuel' AND tags->'self_service' = 'yes') OR tags->'amenity' = 'vending_machine' THEN
status := 'open';
END IF;
RETURN status;
END;
$$ LANGUAGE plpgsql;
-- Is a POI having any contact information
CREATE OR REPLACE FUNCTION has_contact_tag(tags HSTORE) RETURNS BOOLEAN AS $$
BEGIN
RETURN tags ?| ARRAY[
'phone', 'contact:phone', 'email', 'contact:email',
'facebook', 'contact:facebook', 'website', 'contact:website', 'brand:website',
'mobile', 'contact:mobile',
'fax', 'contact:fax'
];
END;
$$ LANGUAGE plpgsql;
-- Clean-up sub_country values
CREATE OR REPLACE FUNCTION clean_sub_country(val VARCHAR) RETURNS VARCHAR AS $$
BEGIN
RETURN NULLIF(val, '<nil>');
END;
$$ LANGUAGE plpgsql;
-- Deprecated function to get normalized category label
DROP INDEX IF EXISTS idx_imposm_osm_point_search;
DROP INDEX IF EXISTS idx_imposm_osm_polygon_search;
DROP FUNCTION IF EXISTS normcat(amenity VARCHAR, shop VARCHAR, craft VARCHAR, office VARCHAR, tobacco VARCHAR);
DROP INDEX IF EXISTS idx_imposm_osm_point_search_v2;
DROP INDEX IF EXISTS idx_imposm_osm_polygon_search_v2;
-- Next poi_osm table
CREATE TABLE IF NOT EXISTS poi_osm_next(
fid VARCHAR PRIMARY KEY,
geom GEOMETRY(Point, 3857),
name VARCHAR,
cat VARCHAR,
normalized_cat VARCHAR DEFAULT 'other',
brand VARCHAR,
brand_wikidata VARCHAR,
brand_hours VARCHAR,
brand_infos VARCHAR,
status VARCHAR DEFAULT 'unknown',
status_order INTEGER,
opening_hours VARCHAR,
delivery VARCHAR DEFAULT 'unknown',
takeaway VARCHAR DEFAULT 'unknown',
hydroalcoholic_gel VARCHAR DEFAULT NULL,
mask VARCHAR DEFAULT NULL,
lumignon VARCHAR DEFAULT NULL,
has_contact BOOLEAN DEFAULT NULL,
country VARCHAR,
sub_country VARCHAR,
source_status VARCHAR DEFAULT 'osm',
tags JSONB
);
TRUNCATE TABLE poi_osm_next;
-- Only add POI with appropriate tagging
WITH
selection
AS
(SELECT
concat('n', osm_id),
way,
name,
get_subcategory(tags, COALESCE(clean_sub_country(sub_country), country_iso2)),
get_category(tags, COALESCE(clean_sub_country(sub_country), country_iso2)) AS normalized_cat,
COALESCE(tags->'brand', tags->'operator'),
COALESCE(tags->'brand:wikidata', tags->'operator:wikidata', tags->'wikidata'),
COALESCE(tags->'description:covid19', tags->'note:covid19'),
opening_state(tags),
status_order_value(opening_state(tags)),
CASE
WHEN "opening_hours:covid19" NOT IN ('off', 'same', '') AND NOT "opening_hours:covid19" ILIKE 'off%' THEN "opening_hours:covid19"
WHEN amenity = 'vending_machine' AND tags->'opening_hours' IN ('', '24/7') THEN '24/7'
ELSE NULL
END,
CASE
WHEN tags->'delivery:covid19' IN ('yes', 'no', 'only') THEN tags->'delivery:covid19'
WHEN tags->'delivery' IN ('yes', 'no', 'only') AND opening_state(tags) = 'ouvert' THEN tags->'delivery'
ELSE 'unknown'
END,
CASE
WHEN tags->'takeaway:covid19' IN ('yes', 'no', 'only') THEN tags->'takeaway:covid19'
WHEN tags->'pickup:covid19' IN ('yes', 'no', 'only') THEN tags->'pickup:covid19'
WHEN tags->'takeaway' IN ('yes', 'no', 'only') AND opening_state(tags) = 'ouvert' THEN tags->'takeaway'
WHEN tags->'pickup' IN ('yes', 'no', 'only') AND opening_state(tags) = 'ouvert' THEN tags->'pickup'
ELSE 'unknown'
END,
has_contact_tag(tags),
country_iso2,
clean_sub_country(sub_country),
hstore_to_jsonb(tags) AS tags
FROM imposm_osm_point
WHERE
-- The line below is automatically edited using categories.json
-- Do not edit directly, run "yarn run categories" instead
country_iso2 IN ('DE', 'FR', 'ES', 'AD', 'CH', 'AT', 'PH', 'FI', 'MC', 'CD', 'IT', 'GB', 'IE') AND ("opening_hours:covid19" != '' OR "amenity" IN ('atm', 'bank', 'bar', 'cafe', 'car_rental', 'car_wash', 'childcare', 'clinic', 'doctors', 'fast_food', 'fuel', 'hospital', 'ice_cream', 'kindergarten', 'library', 'marketplace', 'mobile_money_agent', 'money_transfer', 'pharmacy', 'place_of_worship', 'police', 'post_office', 'pub', 'recycling', 'restaurant', 'townhall', 'vending_machine') OR "office" IN ('employment_agency', 'insurance') OR "shop" IN ('agrarian', 'alcohol', 'appliance', 'art', 'bag', 'bakery', 'bathroom_furnishing', 'beauty', 'bed', 'beverages', 'bicycle', 'books', 'boutique', 'butcher', 'cannery', 'car', 'car_parts', 'car_repair', 'carpet', 'charity', 'cheese', 'chemist', 'chocolate', 'clothes', 'coffee', 'computer', 'confectionery', 'convenience', 'copyshop', 'cosmetics', 'curtain', 'dairy', 'deli', 'department_store', 'doityourself', 'dry_cleaning', 'e-cigarette', 'electrical', 'electronics', 'fabric', 'farm', 'fashion', 'fireplace', 'fishing', 'florist', 'frozen_food', 'funeral_directors', 'furniture', 'garden_centre', 'gas', 'general', 'gift', 'glaziery', 'greengrocer', 'haberdashery', 'hairdresser', 'hardware', 'health_food', 'hearing_aids', 'herbalist', 'hifi', 'honey', 'household_linen', 'houseware', 'hunting', 'ice_cream', 'insurance', 'interior_decoration', 'jewelry', 'kiosk', 'kitchen', 'laundry', 'lighting', 'massage', 'medical_supply', 'mobile_phone', 'money_lender', 'motorcycle', 'music', 'musical_instrument', 'newsagent', 'newsagent;tobacco', 'optician', 'outdoor', 'paint', 'pasta', 'pastry', 'perfumery', 'pet', 'photo', 'pottery', 'printer_ink', 'seafood', 'second_hand', 'sewing', 'shoes', 'spices', 'sports', 'stationery', 'supermarket', 'tailor', 'tea', 'ticket', 'tiles', 'tobacco', 'tobacco;newsagent', 'toys', 'trade', 'travel_agency', 'tyres', 'variety_store', 'video', 'video_games', 'watches', 'weapons', 'wholesale', 'wine', 'winery', 'wool') OR "tourism" IN ('hostel', 'hotel', 'information', 'motel', 'museum') OR "craft" IN ('distillery', 'electronics_repair', 'key_cutter', 'optician', 'sewing', 'shoemaker') OR "historic" IN ('archaeological_site') OR "leisure" IN ('beach_resort', 'sports_centre') OR "healthcare" IN ('centre', 'clinic', 'doctor', 'hospital', 'laboratory', 'rehabilitation') OR "tobacco" IN ('only', 'yes')) --CATEGORIES
UNION ALL
SELECT
CASE WHEN osm_id < 0 THEN concat('r', -osm_id) ELSE concat('w', osm_id) END,
ST_Centroid(way),
name,
get_subcategory(tags, COALESCE(clean_sub_country(sub_country), country_iso2)),
get_category(tags, COALESCE(clean_sub_country(sub_country), country_iso2)),
COALESCE(tags->'brand', tags->'operator'),
COALESCE(tags->'brand:wikidata', tags->'operator:wikidata', tags->'wikidata'),
COALESCE(tags->'description:covid19', tags->'note:covid19'),
opening_state(tags),
status_order_value(opening_state(tags)),
CASE
WHEN "opening_hours:covid19" NOT IN ('off', 'same', '') AND NOT "opening_hours:covid19" ILIKE 'off%' THEN "opening_hours:covid19"
WHEN amenity = 'vending_machine' AND tags->'opening_hours' IN ('', '24/7') THEN '24/7'
ELSE NULL
END,
CASE
WHEN tags->'delivery:covid19' IN ('yes', 'no', 'only') THEN tags->'delivery:covid19'
WHEN tags->'delivery' IN ('yes', 'no', 'only') AND opening_state(tags) = 'ouvert' THEN tags->'delivery'
ELSE 'unknown'
END,
CASE
WHEN tags->'takeaway:covid19' IN ('yes', 'no', 'only') THEN tags->'takeaway:covid19'
WHEN tags->'pickup:covid19' IN ('yes', 'no', 'only') THEN tags->'pickup:covid19'
WHEN tags->'takeaway' IN ('yes', 'no', 'only') AND opening_state(tags) = 'ouvert' THEN tags->'takeaway'
WHEN tags->'pickup' IN ('yes', 'no', 'only') AND opening_state(tags) = 'ouvert' THEN tags->'pickup'
ELSE 'unknown'
END,
has_contact_tag(tags),
country_iso2,
clean_sub_country(sub_country),
hstore_to_jsonb(tags)
FROM imposm_osm_polygon
WHERE
-- The line below is automatically edited using categories.json
-- Do not edit directly, run "yarn run categories" instead
country_iso2 IN ('DE', 'FR', 'ES', 'AD', 'CH', 'AT', 'PH', 'FI', 'MC', 'CD', 'IT', 'GB', 'IE') AND ("opening_hours:covid19" != '' OR "amenity" IN ('atm', 'bank', 'bar', 'cafe', 'car_rental', 'car_wash', 'childcare', 'clinic', 'doctors', 'fast_food', 'fuel', 'hospital', 'ice_cream', 'kindergarten', 'library', 'marketplace', 'mobile_money_agent', 'money_transfer', 'pharmacy', 'place_of_worship', 'police', 'post_office', 'pub', 'recycling', 'restaurant', 'townhall', 'vending_machine') OR "office" IN ('employment_agency', 'insurance') OR "shop" IN ('agrarian', 'alcohol', 'appliance', 'art', 'bag', 'bakery', 'bathroom_furnishing', 'beauty', 'bed', 'beverages', 'bicycle', 'books', 'boutique', 'butcher', 'cannery', 'car', 'car_parts', 'car_repair', 'carpet', 'charity', 'cheese', 'chemist', 'chocolate', 'clothes', 'coffee', 'computer', 'confectionery', 'convenience', 'copyshop', 'cosmetics', 'curtain', 'dairy', 'deli', 'department_store', 'doityourself', 'dry_cleaning', 'e-cigarette', 'electrical', 'electronics', 'fabric', 'farm', 'fashion', 'fireplace', 'fishing', 'florist', 'frozen_food', 'funeral_directors', 'furniture', 'garden_centre', 'gas', 'general', 'gift', 'glaziery', 'greengrocer', 'haberdashery', 'hairdresser', 'hardware', 'health_food', 'hearing_aids', 'herbalist', 'hifi', 'honey', 'household_linen', 'houseware', 'hunting', 'ice_cream', 'insurance', 'interior_decoration', 'jewelry', 'kiosk', 'kitchen', 'laundry', 'lighting', 'massage', 'medical_supply', 'mobile_phone', 'money_lender', 'motorcycle', 'music', 'musical_instrument', 'newsagent', 'newsagent;tobacco', 'optician', 'outdoor', 'paint', 'pasta', 'pastry', 'perfumery', 'pet', 'photo', 'pottery', 'printer_ink', 'seafood', 'second_hand', 'sewing', 'shoes', 'spices', 'sports', 'stationery', 'supermarket', 'tailor', 'tea', 'ticket', 'tiles', 'tobacco', 'tobacco;newsagent', 'toys', 'trade', 'travel_agency', 'tyres', 'variety_store', 'video', 'video_games', 'watches', 'weapons', 'wholesale', 'wine', 'winery', 'wool') OR "tourism" IN ('hostel', 'hotel', 'information', 'motel', 'museum') OR "craft" IN ('distillery', 'electronics_repair', 'key_cutter', 'optician', 'sewing', 'shoemaker') OR "historic" IN ('archaeological_site') OR "leisure" IN ('beach_resort', 'sports_centre') OR "healthcare" IN ('centre', 'clinic', 'doctor', 'hospital', 'laboratory', 'rehabilitation') OR "tobacco" IN ('only', 'yes')) --CATEGORIES
)
INSERT INTO poi_osm_next(fid, geom, name, cat, normalized_cat, brand, brand_wikidata, brand_infos, status, status_order, opening_hours, delivery, takeaway, has_contact, country, sub_country, tags)
SELECT *
FROM selection
-- Remove edge cases needing advanced filtering like vending machines
WHERE normalized_cat IS NOT NULL AND NOT (tags ? 'access' AND tags->>'access' NOT IN ('yes', 'public', 'permissive'));
-- Join legal status
UPDATE poi_osm_next
SET
status = 'closed',
status_order = status_order_value('closed'),
source_status = 'legal'
FROM legal_rules r
WHERE
r.legal_state = 'closed'
AND poi_osm_next.status = 'unknown'
AND poi_osm_next.country = r.country
AND (r.country_subarea IS NULL OR poi_osm_next.sub_country = r.country_subarea)
AND (r.category = poi_osm_next.cat OR r.category = poi_osm_next.normalized_cat);
-- Join brand informations
UPDATE poi_osm_next
SET
status = b.opening_rule,
status_order = status_order_value(b.opening_rule),
opening_hours = COALESCE(poi_osm_next.opening_hours, b.opening_hours),
brand = b.brand_name,
brand_hours = COALESCE(poi_osm_next.brand_hours, b.opening_hours_url),
brand_infos = COALESCE(poi_osm_next.brand_infos, b.description),
tags = CASE WHEN b.osm_additional_tags IS NOT NULL THEN b.osm_additional_tags::jsonb || poi_osm_next.tags ELSE poi_osm_next.tags END,
source_status = 'brand'
FROM brand_rules b
WHERE
poi_osm_next.status = 'unknown'
AND poi_osm_next.country = b.country
AND b.opening_rule IS NOT NULL
AND poi_osm_next.brand_wikidata = b.wikidata_id;
UPDATE poi_osm_next
SET
status = b.opening_rule,
status_order = status_order_value(b.opening_rule),
opening_hours = COALESCE(poi_osm_next.opening_hours, b.opening_hours),
brand = b.brand_name,
brand_hours = COALESCE(poi_osm_next.brand_hours, b.opening_hours_url),
brand_infos = COALESCE(poi_osm_next.brand_infos, b.description),
tags = CASE WHEN b.osm_additional_tags IS NOT NULL THEN b.osm_additional_tags::jsonb || poi_osm_next.tags ELSE poi_osm_next.tags END,
source_status = 'brand'
FROM brand_rules b
WHERE
poi_osm_next.status = 'unknown'
AND poi_osm_next.country = b.country
AND b.opening_rule IS NOT NULL
AND lower(trim(unaccent(poi_osm_next.brand))) = lower(trim(unaccent(b.brand_name)));
UPDATE poi_osm_next
SET
status = b.opening_rule,
status_order = status_order_value(b.opening_rule),
opening_hours = COALESCE(poi_osm_next.opening_hours, b.opening_hours),
brand = b.brand_name,
brand_hours = COALESCE(poi_osm_next.brand_hours, b.opening_hours_url),
brand_infos = COALESCE(poi_osm_next.brand_infos, b.description),
tags = CASE WHEN b.osm_additional_tags IS NOT NULL THEN b.osm_additional_tags::jsonb || poi_osm_next.tags ELSE poi_osm_next.tags END,
source_status = 'brand'
FROM brand_rules b
WHERE
poi_osm_next.status = 'unknown'
AND poi_osm_next.country = b.country
AND b.opening_rule IS NOT NULL
AND lower(trim(unaccent(name))) = lower(trim(unaccent(b.brand_name)));
UPDATE poi_osm_next
SET
status = 'open',
status_order = status_order_value('open'),
source_status = 'legal'
FROM legal_rules r
WHERE
r.legal_state = 'open'
AND poi_osm_next.status = 'unknown'
AND poi_osm_next.country = r.country
AND (r.country_subarea IS NULL OR poi_osm_next.sub_country = r.country_subarea)
AND (r.category = poi_osm_next.cat OR r.category = poi_osm_next.normalized_cat);
-- Set default
UPDATE poi_osm_next
SET
status = 'open',
status_order = status_order_value(r.legal_state),
source_status = 'legal'
FROM legal_rules r
WHERE
poi_osm_next.status = 'unknown'
AND poi_osm_next.country = r.country
AND (r.country_subarea IS NULL OR poi_osm_next.sub_country = r.country_subarea)
AND r.category = 'default'
AND NOT EXISTS (
-- Exclude states not applyed
SELECT
FROM legal_rules AS rr
WHERE
rr.legal_state NOT IN ('open', 'close')
AND rr.country = r.country
AND rr.country_subarea IS NOT DISTINCT FROM r.country_subarea
AND rr.category != 'default'
AND (rr.category = poi_osm_next.cat OR rr.category = poi_osm_next.normalized_cat)
);
UPDATE poi_osm_next
SET status = 'open', status_order = status_order_value('open'), opening_hours = '24/7'
WHERE status IN ('unknown', 'open_adapted') AND cat = 'fuel' AND tags->>'opening_hours' = '24/7';
UPDATE poi_osm_next
SET opening_hours = tags->>'opening_hours'
WHERE status = 'open' AND opening_hours IS NULL;
-- Join custom tags from poi_cro
UPDATE poi_osm_next
SET
tags = poi_osm_next.tags || c.tags || CONCAT('{ "cro:date": "',EXTRACT(EPOCH FROM c.lastupdate)::int,'" }')::jsonb,
hydroalcoholic_gel = c.tags->>'vending:hydroalcoholic_gel',
mask = c.tags->>'vending:mask',
lumignon = c.tags->>'vending:lumignon'
FROM poi_cro c
WHERE poi_osm_next.fid = c.osmid;
-- Join custom POIs from poi_custom
UPDATE poi_osm_next
SET
name = COALESCE(pc.name, poi_osm_next.name),
normalized_cat = pc.category,
cat = pc.subcategory,
tags = CASE WHEN pc.tags IS NOT NULL THEN poi_osm_next.tags || pc.tags ELSE poi_osm_next.tags END
FROM poi_custom pc
WHERE
pc.osm_id IS NOT NULL
AND pc.osm_id = poi_osm_next.fid;
INSERT INTO poi_osm_next(fid, geom, name, cat, normalized_cat, status, status_order, opening_hours, delivery, takeaway, country, sub_country, source_status, tags)
WITH pc AS (
SELECT *, ST_Transform(ST_SetSRID(ST_Point(lng, lat), 4326), 3857) AS geom
FROM poi_custom
WHERE osm_id IS NULL
)
SELECT
pc.cro_id,
pc.geom,
pc.name,
pc.category,
pc.subcategory,
'open',
status_order_value('open'),
pc.tags->>'opening_hours',
pc.tags->>'delivery',
pc.tags->>'takeaway',
c.country_iso2,
c.sub_country,
'cro',
tags
FROM pc
LEFT JOIN countries_subcountries c ON c.wkb_geometry && pc.geom AND ST_Intersects(c.wkb_geometry, pc.geom)
WHERE pc.osm_id IS NULL;
-- Index creation and table switch
REINDEX TABLE poi_osm_next;
CREATE INDEX IF NOT EXISTS poi_osm_next_geom_idx ON poi_osm_next USING GIST(geom);
CREATE INDEX IF NOT EXISTS poi_osm_next_status_idx ON poi_osm_next(status);
CREATE INDEX IF NOT EXISTS poi_osm_next_opening_hours_idx ON poi_osm_next(opening_hours);
CREATE INDEX IF NOT EXISTS poi_osm_next_has_contact_idx ON poi_osm_next(has_contact);
CREATE SCHEMA IF NOT EXISTS previous;
DROP TABLE IF EXISTS previous.poi_osm CASCADE;
ALTER TABLE IF EXISTS poi_osm SET SCHEMA previous;
--DROP TABLE IF EXISTS poi_osm CASCADE;
ALTER TABLE poi_osm_next RENAME TO poi_osm;
ALTER INDEX poi_osm_next_pkey RENAME TO poi_osm_pkey;
ALTER INDEX poi_osm_next_geom_idx RENAME TO poi_osm_geom_idx;
ALTER INDEX poi_osm_next_status_idx RENAME TO poi_osm_status_idx;
ALTER INDEX poi_osm_next_opening_hours_idx RENAME TO poi_osm_opening_hours_idx;
ALTER INDEX poi_osm_next_has_contact_idx RENAME TO poi_osm_has_contact_idx;
CREATE OR REPLACE VIEW poi_osm_light AS
SELECT fid, fid AS id, geom, name, cat, normalized_cat, status, delivery, takeaway, hydroalcoholic_gel, mask, lumignon
FROM poi_osm;
-- Quality insurance views
CREATE OR REPLACE VIEW poi_osm_qa AS
SELECT fid, geom, name, normalized_cat, cat, opening_hours, has_contact
FROM poi_osm;
-- POI list by city view
CREATE OR REPLACE VIEW poi_osm_bycity AS
SELECT p.fid, p.geom, p.name, p.normalized_cat, p.cat, p.brand, p.tags, c.insee AS city_code, c.nom AS city_name
FROM poi_osm p
JOIN communes c ON ST_Intersects(p.geom, c.wkb_geometry);
-- Analysis requests
-- SELECT country, SUM((status != 'unknown')::int)::float / COUNT(*) * 100 AS pct_info_connue FROM poi_osm GROUP BY country;
-- SELECT country, SUM((status NOT IN ('unknown', 'partial'))::int)::float / COUNT(*) * 100 AS pct_info_connue FROM poi_osm GROUP BY country;
-- SELECT status, COUNT(*) FROM poi_osm GROUP BY status ORDER BY COUNT(*) DESC;
-- SELECT normalized_cat, COUNT(*) FROM poi_osm GROUP BY normalized_cat ORDER BY COUNT(*) DESC;
-- SELECT country, brand, COUNT(*) FROM poi_osm WHERE status = 'unknown' GROUP BY country, brand HAVING COUNT(*) > 20 ORDER BY COUNT(*) DESC;
-- SELECT country, cat, COUNT(*) FROM poi_osm WHERE status = 'unknown' GROUP BY country, cat HAVING COUNT(*) > 20 ORDER BY COUNT(*) DESC;