country_name: use separate columns for names from OSM

This allows us to distinguish between base names and imported ones
and consiquently removing imported ones if necessary.
This commit is contained in:
Sarah Hoffmann 2022-02-22 23:35:48 +01:00
parent a3e4e8e5cd
commit a9e3329c39
4 changed files with 27 additions and 18 deletions

View File

@ -5,6 +5,7 @@
CREATE TABLE public.country_name (
country_code character varying(2),
name public.hstore,
derived_name public.hstore,
country_default_language_code text,
partition integer
);

View File

@ -108,6 +108,7 @@ CREATE OR REPLACE FUNCTION get_addressdata(in_place_id BIGINT, in_housenumber IN
DECLARE
place addressdata_place;
location RECORD;
country RECORD;
current_rank_address INTEGER;
location_isaddress BOOLEAN;
BEGIN
@ -198,6 +199,16 @@ BEGIN
WHERE place_id = place.place_id
LOOP
--RAISE WARNING '%',location;
-- mix in default names for countries
IF location.rank_address = 4 and place.country_code is not NULL THEN
FOR country IN
SELECT coalesce(name, ''::hstore) as name FROM country_name
WHERE country_code = place.country_code LIMIT 1
LOOP
place.name := country.name || place.name;
END LOOP;
END IF;
IF location.rank_address < 4 THEN
-- no country locations for ranks higher than country
place.country_code := NULL::varchar(2);
@ -272,7 +283,8 @@ BEGIN
-- If no country was included yet, add the name information from country_name.
IF current_rank_address > 4 THEN
FOR location IN
SELECT name FROM country_name WHERE country_code = place.country_code LIMIT 1
SELECT name || coalesce(derived_name, ''::hstore) as name FROM country_name
WHERE country_code = place.country_code LIMIT 1
LOOP
--RAISE WARNING '% % %',current_rank_address,searchcountrycode,countryname;
RETURN NEXT ROW(null, null, null, location.name, 'place', 'country', NULL,

View File

@ -117,16 +117,6 @@ BEGIN
-- ---- All other place types.
-- Patch in additional country names
IF NEW.admin_level = 2 and NEW.type = 'administrative' and NEW.address ? 'country'
THEN
FOR country IN
SELECT name FROM country_name WHERE country_code = lower(NEW.address->'country')
LOOP
NEW.name = country.name || NEW.name;
END LOOP;
END IF;
-- When an area is changed from large to small: log and discard change
IF existing.geometry is not null AND ST_IsValid(existing.geometry)
AND ST_Area(existing.geometry) > 0.02

View File

@ -1044,16 +1044,22 @@ BEGIN
AND NEW.class = 'boundary' AND NEW.type = 'administrative'
AND NEW.country_code IS NOT NULL AND NEW.osm_type = 'R'
THEN
-- Update the list of country names. Adding an additional sanity
-- check here: make sure the country does overlap with the area where
-- we expect it to be as per static country grid.
-- Update the list of country names.
-- Only take the name from the largest area for the given country code
-- in the hope that this is the authoritive one.
-- Also replace any old names so that all mapping mistakes can
-- be fixed through regular OSM updates.
FOR location IN
SELECT country_code FROM country_osm_grid
WHERE ST_Covers(geometry, NEW.centroid) and country_code = NEW.country_code
SELECT osm_id FROM placex
WHERE rank_search = 4 and osm_type = 'R'
and country_code = NEW.country_code
ORDER BY ST_Area(geometry) desc
LIMIT 1
LOOP
{% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
UPDATE country_name SET name = name || NEW.name WHERE country_code = NEW.country_code;
IF location.osm_id = NEW.osm_id THEN
{% if debug %}RAISE WARNING 'Updating names for country '%' with: %', NEW.country_code, NEW.name;{% endif %}
UPDATE country_name SET derived_name = NEW.name WHERE country_code = NEW.country_code;
END IF;
END LOOP;
END IF;