CREATE TABLE public.country_name (
country_code character varying(2),
name public.hstore,
+ derived_name public.hstore,
country_default_language_code text,
partition integer
);
DECLARE
place addressdata_place;
location RECORD;
+ country RECORD;
current_rank_address INTEGER;
location_isaddress BOOLEAN;
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);
-- 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,
-- ---- 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
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;