1 --DROP TRIGGER IF EXISTS place_before_insert on placex;
2 --DROP TRIGGER IF EXISTS place_before_update on placex;
3 --CREATE TYPE addresscalculationtype AS (
9 CREATE OR REPLACE FUNCTION getclasstypekey(c text, t text) RETURNS TEXT
16 LANGUAGE plpgsql IMMUTABLE;
18 CREATE OR REPLACE FUNCTION isbrokengeometry(place geometry) RETURNS BOOLEAN
24 IF ST_IsEmpty(NEWgeometry) OR NOT ST_IsValid(NEWgeometry) OR ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
30 LANGUAGE plpgsql IMMUTABLE;
32 CREATE OR REPLACE FUNCTION clean_geometry(place geometry) RETURNS geometry
38 IF ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
39 NEWgeometry := ST_buffer(NEWgeometry,0);
40 IF ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
41 RETURN ST_SetSRID(ST_Point(0,0),4326);
47 LANGUAGE plpgsql IMMUTABLE;
49 CREATE OR REPLACE FUNCTION geometry_sector(partition INTEGER, place geometry) RETURNS INTEGER
54 -- RAISE WARNING '%',place;
56 IF ST_IsEmpty(NEWgeometry) OR NOT ST_IsValid(NEWgeometry) OR ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
57 NEWgeometry := ST_buffer(NEWgeometry,0);
58 IF ST_IsEmpty(NEWgeometry) OR NOT ST_IsValid(NEWgeometry) OR ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
62 RETURN (partition*1000000) + (500-ST_X(ST_Centroid(NEWgeometry))::integer)*1000 + (500-ST_Y(ST_Centroid(NEWgeometry))::integer);
65 LANGUAGE plpgsql IMMUTABLE;
67 CREATE OR REPLACE FUNCTION debug_geometry_sector(osmid integer, place geometry) RETURNS INTEGER
72 -- RAISE WARNING '%',osmid;
77 IF ST_IsEmpty(NEWgeometry) OR NOT ST_IsValid(NEWgeometry) OR ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
78 NEWgeometry := ST_buffer(NEWgeometry,0);
79 IF ST_IsEmpty(NEWgeometry) OR NOT ST_IsValid(NEWgeometry) OR ST_X(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEWgeometry))::text in ('NaN','Infinity','-Infinity') THEN
83 RETURN (500-ST_X(ST_Centroid(NEWgeometry))::integer)*1000 + (500-ST_Y(ST_Centroid(NEWgeometry))::integer);
86 LANGUAGE plpgsql IMMUTABLE;
88 CREATE OR REPLACE FUNCTION transliteration(text) RETURNS text
89 AS '{modulepath}/nominatim.so', 'transliteration'
90 LANGUAGE c IMMUTABLE STRICT;
92 CREATE OR REPLACE FUNCTION gettokenstring(text) RETURNS text
93 AS '{modulepath}/nominatim.so', 'gettokenstring'
94 LANGUAGE c IMMUTABLE STRICT;
96 CREATE OR REPLACE FUNCTION make_standard_name(name TEXT) RETURNS TEXT
101 o := gettokenstring(transliteration(name));
102 RETURN trim(substr(o,1,length(o)));
105 LANGUAGE 'plpgsql' IMMUTABLE;
107 CREATE OR REPLACE FUNCTION getorcreate_word_id(lookup_word TEXT)
112 return_word_id INTEGER;
114 lookup_token := trim(lookup_word);
115 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class is null and type is null into return_word_id;
116 IF return_word_id IS NULL THEN
117 return_word_id := nextval('seq_word');
118 INSERT INTO word VALUES (return_word_id, lookup_token, regexp_replace(lookup_token,E'([^0-9])\\1+',E'\\1','g'), null, null, null, null, 0, null);
120 RETURN return_word_id;
125 CREATE OR REPLACE FUNCTION getorcreate_housenumber_id(lookup_word TEXT)
130 return_word_id INTEGER;
132 lookup_token := ' '||trim(lookup_word);
133 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class='place' and type='house' into return_word_id;
134 IF return_word_id IS NULL THEN
135 return_word_id := nextval('seq_word');
136 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, 'place', 'house', null, 0, null);
138 RETURN return_word_id;
143 CREATE OR REPLACE FUNCTION getorcreate_country(lookup_word TEXT, lookup_country_code varchar(2))
148 return_word_id INTEGER;
150 lookup_token := ' '||trim(lookup_word);
151 SELECT min(word_id) FROM word WHERE word_token = lookup_token and country_code=lookup_country_code into return_word_id;
152 IF return_word_id IS NULL THEN
153 return_word_id := nextval('seq_word');
154 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, null, null, lookup_country_code, 0, null);
156 RETURN return_word_id;
161 CREATE OR REPLACE FUNCTION getorcreate_amenity(lookup_word TEXT, lookup_class text, lookup_type text)
166 return_word_id INTEGER;
168 lookup_token := ' '||trim(lookup_word);
169 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class=lookup_class and type = lookup_type into return_word_id;
170 IF return_word_id IS NULL THEN
171 return_word_id := nextval('seq_word');
172 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, lookup_class, lookup_type, null, 0, null);
174 RETURN return_word_id;
179 CREATE OR REPLACE FUNCTION getorcreate_tagpair(lookup_class text, lookup_type text)
184 return_word_id INTEGER;
186 lookup_token := lookup_class||'='||lookup_type;
187 SELECT min(word_id) FROM word WHERE word_token = lookup_token into return_word_id;
188 IF return_word_id IS NULL THEN
189 return_word_id := nextval('seq_word');
190 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, null, null, null, 0, null);
192 RETURN return_word_id;
197 CREATE OR REPLACE FUNCTION get_tagpair(lookup_class text, lookup_type text)
202 return_word_id INTEGER;
204 lookup_token := lookup_class||'='||lookup_type;
205 SELECT min(word_id) FROM word WHERE word_token = lookup_token into return_word_id;
206 RETURN return_word_id;
211 CREATE OR REPLACE FUNCTION getorcreate_amenityoperator(lookup_word TEXT, lookup_class text, lookup_type text, op text)
216 return_word_id INTEGER;
218 lookup_token := ' '||trim(lookup_word);
219 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class=lookup_class and type = lookup_type and operator = op into return_word_id;
220 IF return_word_id IS NULL THEN
221 return_word_id := nextval('seq_word');
222 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, lookup_class, lookup_type, null, 0, op, null);
224 RETURN return_word_id;
229 CREATE OR REPLACE FUNCTION getorcreate_name_id(lookup_word TEXT, src_word TEXT)
234 nospace_lookup_token TEXT;
235 return_word_id INTEGER;
237 lookup_token := ' '||trim(lookup_word);
238 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class is null and type is null into return_word_id;
239 IF return_word_id IS NULL THEN
240 return_word_id := nextval('seq_word');
241 INSERT INTO word VALUES (return_word_id, lookup_token, regexp_replace(lookup_token,E'([^0-9])\\1+',E'\\1','g'), src_word, null, null, null, 0, null);
242 -- nospace_lookup_token := replace(replace(lookup_token, '-',''), ' ','');
243 -- IF ' '||nospace_lookup_token != lookup_token THEN
244 -- INSERT INTO word VALUES (return_word_id, '-'||nospace_lookup_token, null, src_word, null, null, null, 0, null);
247 RETURN return_word_id;
252 CREATE OR REPLACE FUNCTION getorcreate_name_id(lookup_word TEXT)
257 RETURN getorcreate_name_id(lookup_word, '');
262 CREATE OR REPLACE FUNCTION get_word_id(lookup_word TEXT)
267 return_word_id INTEGER;
269 lookup_token := trim(lookup_word);
270 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class is null and type is null into return_word_id;
271 RETURN return_word_id;
274 LANGUAGE plpgsql IMMUTABLE;
276 CREATE OR REPLACE FUNCTION get_name_id(lookup_word TEXT)
281 return_word_id INTEGER;
283 lookup_token := ' '||trim(lookup_word);
284 SELECT min(word_id) FROM word WHERE word_token = lookup_token and class is null and type is null into return_word_id;
285 RETURN return_word_id;
288 LANGUAGE plpgsql IMMUTABLE;
290 CREATE OR REPLACE FUNCTION array_merge(a INTEGER[], b INTEGER[])
297 IF array_upper(a, 1) IS NULL THEN
300 IF array_upper(b, 1) IS NULL THEN
304 FOR i IN 1..array_upper(b, 1) LOOP
305 IF NOT (ARRAY[b[i]] <@ r) THEN
312 LANGUAGE plpgsql IMMUTABLE;
314 CREATE OR REPLACE FUNCTION make_keywords(src HSTORE) RETURNS INTEGER[]
324 result := '{}'::INTEGER[];
326 FOR item IN SELECT (each(src)).* LOOP
328 s := make_standard_name(item.value);
330 w := getorcreate_name_id(s, item.value);
332 IF not(ARRAY[w] <@ result) THEN
333 result := result || w;
336 words := string_to_array(s, ' ');
337 IF array_upper(words, 1) IS NOT NULL THEN
338 FOR j IN 1..array_upper(words, 1) LOOP
339 IF (words[j] != '') THEN
340 w = getorcreate_word_id(words[j]);
341 IF NOT (ARRAY[w] <@ result) THEN
342 result := result || w;
348 words := regexp_split_to_array(item.value, E'[,;()]');
349 IF array_upper(words, 1) != 1 THEN
350 FOR j IN 1..array_upper(words, 1) LOOP
351 s := make_standard_name(words[j]);
353 w := getorcreate_word_id(s);
354 IF NOT (ARRAY[w] <@ result) THEN
355 result := result || w;
361 s := regexp_replace(item.value, '市$', '');
362 IF s != item.value THEN
363 s := make_standard_name(s);
365 w := getorcreate_name_id(s, item.value);
366 IF NOT (ARRAY[w] <@ result) THEN
367 result := result || w;
377 LANGUAGE plpgsql IMMUTABLE;
379 CREATE OR REPLACE FUNCTION make_keywords(src TEXT) RETURNS INTEGER[]
389 result := '{}'::INTEGER[];
391 s := make_standard_name(src);
392 w := getorcreate_name_id(s);
394 IF NOT (ARRAY[w] <@ result) THEN
395 result := result || w;
398 words := string_to_array(s, ' ');
399 IF array_upper(words, 1) IS NOT NULL THEN
400 FOR j IN 1..array_upper(words, 1) LOOP
401 IF (words[j] != '') THEN
402 w = getorcreate_word_id(words[j]);
403 IF NOT (ARRAY[w] <@ result) THEN
404 result := result || w;
413 LANGUAGE plpgsql IMMUTABLE;
415 CREATE OR REPLACE FUNCTION get_word_score(wordscores wordscore[], words text[]) RETURNS integer
422 IF (wordscores is null OR words is null) THEN
427 FOR idxword in 1 .. array_upper(words, 1) LOOP
428 FOR idxscores in 1 .. array_upper(wordscores, 1) LOOP
429 IF wordscores[idxscores].word = words[idxword] THEN
430 result := result + wordscores[idxscores].score;
438 LANGUAGE plpgsql IMMUTABLE;
440 CREATE OR REPLACE FUNCTION get_country_code(place geometry) RETURNS TEXT
443 place_centre GEOMETRY;
446 place_centre := ST_Centroid(place);
448 --RAISE WARNING 'start: %', ST_AsText(place_centre);
450 -- Try for a OSM polygon first
451 FOR nearcountry IN select country_code from location_area_country where country_code is not null and not isguess and st_contains(geometry, place_centre) limit 1
453 RETURN nearcountry.country_code;
456 --RAISE WARNING 'osm fallback: %', ST_AsText(place_centre);
458 -- Try for OSM fallback data
459 FOR nearcountry IN select country_code from country_osm_grid where st_contains(geometry, place_centre) limit 1
461 RETURN nearcountry.country_code;
464 --RAISE WARNING 'natural earth: %', ST_AsText(place_centre);
466 -- Natural earth data (first fallback)
467 FOR nearcountry IN select country_code from country_naturalearthdata where st_contains(geometry, place_centre) limit 1
469 RETURN nearcountry.country_code;
472 -- Natural earth data (first fallback)
473 FOR nearcountry IN select country_code from country_naturalearthdata where st_distance(geometry, place_centre) < 0.5 limit 1
475 RETURN nearcountry.country_code;
478 --RAISE WARNING 'in country: %', ST_AsText(place_centre);
480 -- WorldBoundaries data (second fallback - think there might be something broken in this data)
481 FOR nearcountry IN select country_code from country where st_contains(geometry, place_centre) limit 1
483 RETURN nearcountry.country_code;
486 --RAISE WARNING 'near country: %', ST_AsText(place_centre);
488 -- Still not in a country - try nearest within ~12 miles of a country
489 FOR nearcountry IN select country_code from country where st_distance(geometry, place_centre) < 0.5
490 order by st_distance(geometry, place) limit 1
492 RETURN nearcountry.country_code;
498 LANGUAGE plpgsql IMMUTABLE;
500 CREATE OR REPLACE FUNCTION get_country_code(place geometry, in_country_code VARCHAR(2)) RETURNS TEXT
505 FOR nearcountry IN select country_code from country_name where country_code = lower(in_country_code)
507 RETURN nearcountry.country_code;
509 RETURN get_country_code(place);
512 LANGUAGE plpgsql IMMUTABLE;
514 CREATE OR REPLACE FUNCTION get_country_language_code(search_country_code VARCHAR(2)) RETURNS TEXT
519 FOR nearcountry IN select distinct country_default_language_code from country_name where country_code = search_country_code limit 1
521 RETURN lower(nearcountry.country_default_language_code);
526 LANGUAGE plpgsql IMMUTABLE;
528 CREATE OR REPLACE FUNCTION get_partition(place geometry, in_country_code VARCHAR(10)) RETURNS INTEGER
531 place_centre GEOMETRY;
534 FOR nearcountry IN select partition from country_name where country_code = in_country_code
536 RETURN nearcountry.partition;
541 LANGUAGE plpgsql IMMUTABLE;
543 CREATE OR REPLACE FUNCTION delete_location(OLD_place_id BIGINT) RETURNS BOOLEAN
547 DELETE FROM location_area where place_id = OLD_place_id;
548 -- TODO:location_area
554 CREATE OR REPLACE FUNCTION add_location(
556 country_code varchar(2),
560 rank_address INTEGER,
581 IF rank_search > 25 THEN
582 RAISE EXCEPTION 'Adding location with rank > 25 (% rank %)', place_id, rank_search;
585 -- RAISE WARNING 'Adding location with rank > 25 (% rank %)', place_id, rank_search;
587 x := deleteLocationArea(partition, place_id);
590 IF (ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(geometry)) THEN
593 centroid := ST_Centroid(geometry);
595 xmin := floor(st_xmin(geometry));
596 xmax := ceil(st_xmax(geometry));
597 ymin := floor(st_ymin(geometry));
598 ymax := ceil(st_ymax(geometry));
600 IF xmin = xmax OR ymin = ymax OR (xmax-xmin < 2 AND ymax-ymin < 2) THEN
601 x := insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, false, centroid, geometry);
603 -- RAISE WARNING 'Spliting geometry: % to %, % to %', xmin, xmax, ymin, ymax;
604 FOR lon IN xmin..(xmax-1) LOOP
605 FOR lat IN ymin..(ymax-1) LOOP
606 secbox := ST_SetSRID(ST_MakeBox2D(ST_Point(lon,lat),ST_Point(lon+1,lat+1)),4326);
607 IF st_intersects(geometry, secbox) THEN
608 secgeo := st_intersection(geometry, secbox);
609 IF NOT ST_IsEmpty(secgeo) AND ST_GeometryType(secgeo) in ('ST_Polygon','ST_MultiPolygon') THEN
610 x := insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, false, centroid, secgeo);
617 ELSEIF rank_search < 26 THEN
620 IF rank_address = 0 THEN
622 ELSEIF rank_search <= 14 THEN
624 ELSEIF rank_search <= 15 THEN
626 ELSEIF rank_search <= 16 THEN
628 ELSEIF rank_search <= 17 THEN
630 ELSEIF rank_search <= 21 THEN
632 ELSEIF rank_search = 25 THEN
636 -- RAISE WARNING 'adding % diameter %', place_id, diameter;
638 secgeo := ST_Buffer(geometry, diameter);
639 x := insertLocationAreaLarge(partition, place_id, country_code, keywords, rank_search, rank_address, true, ST_Centroid(geometry), secgeo);
644 secgeo := ST_Buffer(geometry, 0.0002);
645 x := insertLocationAreaRoadNear(partition, place_id, country_code, keywords, rank_search, rank_address, true, ST_Centroid(geometry), secgeo);
648 secgeo := ST_Buffer(geometry, 0.001);
649 x := insertLocationAreaRoadFar(partition, place_id, country_code, keywords, rank_search, rank_address, true, ST_Centroid(geometry), secgeo);
658 CREATE OR REPLACE FUNCTION update_location(
661 place_country_code varchar(2),
664 rank_address INTEGER,
672 b := deleteLocationArea(partition, place_id);
673 -- result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
674 RETURN add_location(place_id, place_country_code, name, rank_search, rank_address, geometry);
679 CREATE OR REPLACE FUNCTION search_name_add_words(parent_place_id BIGINT, to_add INTEGER[])
690 -- this should just be an update, but it seems to do insane things to the index size (delete and insert doesn't)
691 FOR childplace IN select * from search_name,place_addressline
692 where address_place_id = parent_place_id
693 and search_name.place_id = place_addressline.place_id
695 delete from search_name where place_id = childplace.place_id;
696 IF not (ARRAY[to_add] <@ childplace.nameaddress_vector) THEN
697 childplace.nameaddress_vector := childplace.nameaddress_vector || to_add;
699 IF childplace.place_id = parent_place_id and not (ARRAY[to_add] <@ childplace.name_vector) THEN
700 childplace.name_vector := childplace.name_vector || to_add;
702 insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid)
703 values (childplace.place_id, childplace.search_rank, childplace.address_rank, childplace.country_code,
704 childplace.name_vector, childplace.nameaddress_vector, childplace.centroid);
712 CREATE OR REPLACE FUNCTION update_location_nameonly(partition INTEGER, OLD_place_id BIGINT, name hstore) RETURNS BOOLEAN
715 newkeywords INTEGER[];
716 addedkeywords INTEGER[];
717 removedkeywords INTEGER[];
721 newkeywords := make_keywords(name);
722 select coalesce(newkeywords,'{}'::INTEGER[]) - coalesce(location_point.keywords,'{}'::INTEGER[]),
723 coalesce(location_point.keywords,'{}'::INTEGER[]) - coalesce(newkeywords,'{}'::INTEGER[]) from location_point
724 where place_id = OLD_place_id into addedkeywords, removedkeywords;
726 -- RAISE WARNING 'update_location_nameonly for %: new:% added:% removed:%', OLD_place_id, newkeywords, addedkeywords, removedkeywords;
728 IF #removedkeywords > 0 THEN
729 -- abort due to tokens removed
733 IF #addedkeywords > 0 THEN
734 -- short circuit - no changes
738 UPDATE location_area set keywords = newkeywords where place_id = OLD_place_id;
739 RETURN search_name_add_words(OLD_place_id, addedkeywords);
745 CREATE OR REPLACE FUNCTION create_interpolation(wayid INTEGER, interpolationtype TEXT) RETURNS INTEGER
757 orginalstartnumber INTEGER;
758 originalnumberrange INTEGER;
761 search_place_id BIGINT;
764 havefirstpoint BOOLEAN;
768 IF interpolationtype = 'odd' OR interpolationtype = 'even' OR interpolationtype = 'all' THEN
770 select postcode from placex where osm_type = 'W' and osm_id = wayid INTO defpostalcode;
771 select nodes from planet_osm_ways where id = wayid INTO waynodes;
772 --RAISE WARNING 'interpolation % % %',wayid,interpolationtype,waynodes;
773 IF array_upper(waynodes, 1) IS NOT NULL THEN
775 havefirstpoint := false;
777 FOR nodeidpos in 1..array_upper(waynodes, 1) LOOP
779 select min(place_id) from placex where osm_type = 'N' and osm_id = waynodes[nodeidpos]::INTEGER and type = 'house' INTO search_place_id;
780 IF search_place_id IS NULL THEN
781 -- null record of right type
782 select * from placex where osm_type = 'N' and osm_id = waynodes[nodeidpos]::INTEGER and type = 'house' limit 1 INTO nextnode;
783 select ST_SetSRID(ST_Point(lon::float/10000000,lat::float/10000000),4326) from planet_osm_nodes where id = waynodes[nodeidpos] INTO nextnode.geometry;
785 select * from placex where place_id = search_place_id INTO nextnode;
788 --RAISE WARNING 'interpolation node % % % ',nextnode.housenumber,ST_X(nextnode.geometry),ST_Y(nextnode.geometry);
790 IF havefirstpoint THEN
792 -- add point to the line string
793 linestr := linestr||','||ST_X(nextnode.geometry)||' '||ST_Y(nextnode.geometry);
794 endnumber := ('0'||substring(nextnode.housenumber,'[0-9]+'))::integer;
796 IF startnumber IS NOT NULL and startnumber > 0 AND endnumber IS NOT NULL and endnumber > 0 AND @(startnumber - endnumber) < 1000 THEN
798 --RAISE WARNING 'interpolation end % % ',nextnode.place_id,endnumber;
800 IF startnumber != endnumber THEN
802 linestr := linestr || ')';
803 --RAISE WARNING 'linestr %',linestr;
804 linegeo := ST_GeomFromText(linestr,4326);
805 linestr := 'LINESTRING('||ST_X(nextnode.geometry)||' '||ST_Y(nextnode.geometry);
806 IF (startnumber > endnumber) THEN
807 housenum := endnumber;
808 endnumber := startnumber;
809 startnumber := housenum;
810 linegeo := ST_Reverse(linegeo);
812 orginalstartnumber := startnumber;
813 originalnumberrange := endnumber - startnumber;
815 -- Too much broken data worldwide for this test to be worth using
816 -- IF originalnumberrange > 500 THEN
817 -- RAISE WARNING 'Number block of % while processing % %', originalnumberrange, prevnode, nextnode;
820 IF (interpolationtype = 'odd' AND startnumber%2 = 0) OR (interpolationtype = 'even' AND startnumber%2 = 1) THEN
821 startnumber := startnumber + 1;
824 IF (interpolationtype = 'odd' OR interpolationtype = 'even') THEN
825 startnumber := startnumber + 2;
827 ELSE -- everything else assumed to be 'all'
828 startnumber := startnumber + 1;
832 endnumber := endnumber - 1;
833 delete from placex where osm_type = 'N' and osm_id = prevnode.osm_id and type = 'house' and place_id != prevnode.place_id;
834 FOR housenum IN startnumber..endnumber BY stepsize LOOP
835 -- this should really copy postcodes but it puts a huge burdon on the system for no big benefit
836 -- ideally postcodes should move up to the way
837 insert into placex (osm_type, osm_id, class, type, admin_level, housenumber, street, isin, postcode,
838 country_code, parent_place_id, rank_address, rank_search, indexed_status, geometry)
839 values ('N',prevnode.osm_id, prevnode.class, prevnode.type, prevnode.admin_level, housenum, prevnode.street, prevnode.isin, coalesce(prevnode.postcode, defpostalcode),
840 prevnode.country_code, prevnode.parent_place_id, prevnode.rank_address, prevnode.rank_search, 1, ST_Line_Interpolate_Point(linegeo, (housenum::float-orginalstartnumber::float)/originalnumberrange::float));
841 newpoints := newpoints + 1;
842 --RAISE WARNING 'interpolation number % % ',prevnode.place_id,housenum;
845 havefirstpoint := false;
849 IF NOT havefirstpoint THEN
850 startnumber := ('0'||substring(nextnode.housenumber,'[0-9]+'))::integer;
851 IF startnumber IS NOT NULL AND startnumber > 0 THEN
852 havefirstpoint := true;
853 linestr := 'LINESTRING('||ST_X(nextnode.geometry)||' '||ST_Y(nextnode.geometry);
854 prevnode := nextnode;
856 --RAISE WARNING 'interpolation start % % ',nextnode.place_id,startnumber;
862 --RAISE WARNING 'interpolation points % ',newpoints;
869 CREATE OR REPLACE FUNCTION placex_insert() RETURNS TRIGGER
875 country_code VARCHAR(2);
876 default_language VARCHAR(10);
880 -- RAISE WARNING '%',NEW.osm_id;
883 IF NEW.class = 'highway' and NEW.type in ('turning_circle','traffic_signals','mini_roundabout','noexit','crossing') THEN
884 -- RAISE WARNING 'bad highway %',NEW.osm_id;
887 IF NEW.class in ('landuse','natural') and NEW.name is null THEN
888 -- RAISE WARNING 'empty landuse %',NEW.osm_id;
892 IF ST_IsEmpty(NEW.geometry) OR NOT ST_IsValid(NEW.geometry) OR ST_X(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') THEN
893 -- block all invalid geometary - just not worth the risk. seg faults are causing serious problems.
894 -- RAISE WARNING 'invalid geometry %',NEW.osm_id;
898 IF NEW.osm_type = 'R' THEN
899 -- invalid multipolygons can crash postgis, don't even bother to try!
902 NEW.geometry := ST_buffer(NEW.geometry,0);
903 IF ST_IsEmpty(NEW.geometry) OR NOT ST_IsValid(NEW.geometry) OR ST_X(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') THEN
904 -- RAISE WARNING 'Invalid geometary, rejecting: % %', NEW.osm_type, NEW.osm_id;
909 NEW.place_id := nextval('seq_place');
910 NEW.indexed_status := 1; --STATUS_NEW
912 IF NEW.rank_search >= 4 THEN
913 NEW.country_code := lower(get_country_code(NEW.geometry, NEW.country_code));
915 NEW.country_code := NULL;
918 NEW.partition := get_partition(NEW.geometry, NEW.country_code);
919 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
921 -- copy 'name' to or from the default language (if there is a default language)
922 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
923 default_language := get_country_language_code(NEW.country_code);
924 IF default_language IS NOT NULL THEN
925 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
926 NEW.name := NEW.name || (('name:'||default_language) => (NEW.name -> 'name'));
927 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
928 NEW.name := NEW.name || ('name' => (NEW.name -> ('name:'||default_language)));
933 IF NEW.admin_level > 15 THEN
934 NEW.admin_level := 15;
937 IF NEW.housenumber IS NOT NULL THEN
938 i := getorcreate_housenumber_id(make_standard_name(NEW.housenumber));
941 IF NEW.osm_type = 'X' THEN
942 -- E'X'ternal records should already be in the right format so do nothing
944 NEW.rank_search := 30;
945 NEW.rank_address := NEW.rank_search;
947 -- By doing in postgres we have the country available to us - currently only used for postcode
948 IF NEW.class in ('place','boundary') AND NEW.type in ('postcode','postal_code') THEN
950 IF NEW.postcode IS NULL THEN
951 -- most likely just a part of a multipolygon postcode boundary, throw it away
955 NEW.name := 'ref'=>NEW.postcode;
957 IF NEW.country_code = 'gb' THEN
959 IF NEW.postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9][A-Z][A-Z])$' THEN
960 NEW.rank_search := 25;
961 NEW.rank_address := 5;
962 ELSEIF NEW.postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])$' THEN
963 NEW.rank_search := 23;
964 NEW.rank_address := 5;
965 ELSEIF NEW.postcode ~ '^([A-Z][A-Z]?[0-9][0-9A-Z])$' THEN
966 NEW.rank_search := 21;
967 NEW.rank_address := 5;
970 ELSEIF NEW.country_code = 'de' THEN
972 IF NEW.postcode ~ '^([0-9]{5})$' THEN
973 NEW.rank_search := 21;
974 NEW.rank_address := 11;
978 -- Guess at the postcode format and coverage (!)
979 IF upper(NEW.postcode) ~ '^[A-Z0-9]{1,5}$' THEN -- Probably too short to be very local
980 NEW.rank_search := 21;
981 NEW.rank_address := 11;
983 -- Does it look splitable into and area and local code?
984 postcode := substring(upper(NEW.postcode) from '^([- :A-Z0-9]+)([- :][A-Z0-9]+)$');
986 IF postcode IS NOT NULL THEN
987 NEW.rank_search := 25;
988 NEW.rank_address := 11;
989 ELSEIF NEW.postcode ~ '^[- :A-Z0-9]{6,}$' THEN
990 NEW.rank_search := 21;
991 NEW.rank_address := 11;
996 ELSEIF NEW.class = 'place' THEN
997 IF NEW.type in ('continent') THEN
998 NEW.rank_search := 2;
999 NEW.rank_address := NEW.rank_search;
1000 ELSEIF NEW.type in ('sea') THEN
1001 NEW.rank_search := 2;
1002 NEW.rank_address := 0;
1003 ELSEIF NEW.type in ('country') THEN
1004 NEW.rank_search := 4;
1005 NEW.rank_address := NEW.rank_search;
1006 ELSEIF NEW.type in ('state') THEN
1007 NEW.rank_search := 8;
1008 NEW.rank_address := NEW.rank_search;
1009 ELSEIF NEW.type in ('region') THEN
1010 NEW.rank_search := 10;
1011 NEW.rank_address := NEW.rank_search;
1012 ELSEIF NEW.type in ('county') THEN
1013 NEW.rank_search := 12;
1014 NEW.rank_address := NEW.rank_search;
1015 ELSEIF NEW.type in ('city') THEN
1016 NEW.rank_search := 16;
1017 NEW.rank_address := NEW.rank_search;
1018 ELSEIF NEW.type in ('island') THEN
1019 NEW.rank_search := 17;
1020 NEW.rank_address := 0;
1021 ELSEIF NEW.type in ('town') THEN
1022 NEW.rank_search := 18;
1023 NEW.rank_address := 16;
1024 ELSEIF NEW.type in ('village','hamlet','municipality','district','unincorporated_area','borough') THEN
1025 NEW.rank_search := 19;
1026 NEW.rank_address := 16;
1027 ELSEIF NEW.type in ('airport') AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
1028 NEW.rank_search := 18;
1029 NEW.rank_address := 17;
1030 ELSEIF NEW.type in ('moor') AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
1031 NEW.rank_search := 17;
1032 NEW.rank_address := 18;
1033 ELSEIF NEW.type in ('moor') THEN
1034 NEW.rank_search := 17;
1035 NEW.rank_address := 0;
1036 ELSEIF NEW.type in ('national_park') THEN
1037 NEW.rank_search := 18;
1038 NEW.rank_address := 18;
1039 ELSEIF NEW.type in ('suburb','croft','subdivision') THEN
1040 NEW.rank_search := 20;
1041 NEW.rank_address := NEW.rank_search;
1042 ELSEIF NEW.type in ('farm','locality','islet','isolated_dwelling','mountain_pass') THEN
1043 NEW.rank_search := 20;
1044 NEW.rank_address := 0;
1045 ELSEIF NEW.type in ('hall_of_residence','neighbourhood','housing_estate','nature_reserve') THEN
1046 NEW.rank_search := 22;
1047 NEW.rank_address := 22;
1048 ELSEIF NEW.type in ('airport','street') THEN
1049 NEW.rank_search := 26;
1050 NEW.rank_address := NEW.rank_search;
1051 ELSEIF NEW.type in ('house','building') THEN
1052 NEW.rank_search := 30;
1053 NEW.rank_address := NEW.rank_search;
1054 ELSEIF NEW.type in ('houses') THEN
1055 -- can't guarantee all required nodes loaded yet due to caching in osm2pgsql
1056 -- insert new point into place for each derived building
1057 --i := create_interpolation(NEW.osm_id, NEW.housenumber);
1058 NEW.rank_search := 28;
1059 NEW.rank_address := 0;
1062 ELSEIF NEW.class = 'boundary' THEN
1063 IF ST_GeometryType(NEW.geometry) NOT IN ('ST_Polygon','ST_MultiPolygon') THEN
1064 -- RAISE WARNING 'invalid boundary %',NEW.osm_id;
1067 NEW.rank_search := NEW.admin_level * 2;
1068 NEW.rank_address := NEW.rank_search;
1069 ELSEIF NEW.class = 'landuse' AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
1070 NEW.rank_search := 22;
1071 NEW.rank_address := NEW.rank_search;
1072 -- any feature more than 5 square miles is probably worth indexing
1073 ELSEIF ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_Area(NEW.geometry) > 0.1 THEN
1074 NEW.rank_search := 22;
1075 NEW.rank_address := NEW.rank_search;
1076 ELSEIF NEW.class = 'highway' AND NEW.name is NULL AND
1077 NEW.type in ('service','cycleway','path','footway','steps','bridleway','track','byway','motorway_link','primary_link','trunk_link','secondary_link','tertiary_link') THEN
1078 -- RAISE WARNING 'unnamed minor feature %',NEW.osm_id;
1080 ELSEIF NEW.class = 'railway' AND NEW.type in ('rail') THEN
1082 ELSEIF NEW.class = 'waterway' AND NEW.name is NULL THEN
1084 ELSEIF NEW.class = 'waterway' THEN
1085 NEW.rank_address := 17;
1086 ELSEIF NEW.class = 'highway' AND NEW.osm_type != 'N' AND NEW.type in ('service','cycleway','path','footway','steps','bridleway','motorway_link','primary_link','trunk_link','secondary_link','tertiary_link') THEN
1087 NEW.rank_search := 27;
1088 NEW.rank_address := NEW.rank_search;
1089 ELSEIF NEW.class = 'highway' AND NEW.osm_type != 'N' THEN
1090 NEW.rank_search := 26;
1091 NEW.rank_address := NEW.rank_search;
1092 ELSEIF NEW.class = 'natural' and NEW.type = 'sea' THEN
1093 NEW.rank_search := 4;
1094 NEW.rank_address := NEW.rank_search;
1095 ELSEIF NEW.class = 'natural' and NEW.type in ('coastline') THEN
1097 ELSEIF NEW.class = 'natural' and NEW.type in ('peak','volcano') THEN
1098 NEW.rank_search := 18;
1099 NEW.rank_address := 0;
1104 IF NEW.rank_search > 30 THEN
1105 NEW.rank_search := 30;
1108 IF NEW.rank_address > 30 THEN
1109 NEW.rank_address := 30;
1112 IF (NEW.extratags -> 'capital') = 'yes' THEN
1113 NEW.rank_search := NEW.rank_search -1;
1116 -- Block import below rank 22
1117 -- IF NEW.rank_search > 22 THEN
1121 RETURN NEW; -- The following is not needed until doing diff updates, and slows the main index process down
1123 IF (ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_IsValid(NEW.geometry)) THEN
1124 -- Performance: We just can't handle re-indexing for country level changes
1125 IF st_area(NEW.geometry) < 1 THEN
1126 -- mark items within the geometry for re-indexing
1127 -- RAISE WARNING 'placex poly insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
1129 -- work around bug in postgis, this may have been fixed in 2.0.0 (see http://trac.osgeo.org/postgis/ticket/547)
1130 update placex set indexed_status = 2 where (ST_Contains(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
1131 AND rank_search > NEW.rank_search and indexed_status = 0 and ST_geometrytype(placex.geometry) = 'ST_Point' and (rank_search < 28 or name is not null);
1132 update placex set indexed_status = 2 where (ST_Contains(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
1133 AND rank_search > NEW.rank_search and indexed_status = 0 and ST_geometrytype(placex.geometry) != 'ST_Point' and (rank_search < 28 or name is not null);
1136 -- mark nearby items for re-indexing, where 'nearby' depends on the features rank_search and is a complete guess :(
1138 -- 16 = city, anything higher than city is effectively ignored (polygon required!)
1139 IF NEW.type='postcode' THEN
1141 ELSEIF NEW.rank_search < 16 THEN
1143 ELSEIF NEW.rank_search < 18 THEN
1145 ELSEIF NEW.rank_search < 20 THEN
1147 ELSEIF NEW.rank_search = 21 THEN
1149 ELSEIF NEW.rank_search < 24 THEN
1151 ELSEIF NEW.rank_search < 26 THEN
1152 diameter := 0.002; -- 100 to 200 meters
1153 ELSEIF NEW.rank_search < 28 THEN
1154 diameter := 0.001; -- 50 to 100 meters
1156 IF diameter > 0 THEN
1157 -- RAISE WARNING 'placex point insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,diameter;
1158 update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter) and (rank_search < 28 or name is not null);
1163 -- add to tables for special search
1164 -- Note: won't work on initial import because the classtype tables
1165 -- do not yet exist. It won't hurt either.
1166 classtable := 'place_classtype_' || NEW.class || '_' || NEW.type;
1167 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable INTO result;
1169 EXECUTE 'INSERT INTO ' || classtable::regclass || ' (place_id, centroid) VALUES ($1,$2)'
1170 USING NEW.place_id, ST_Centroid(NEW.geometry);
1174 -- IF NEW.rank_search < 26 THEN
1175 -- RAISE WARNING 'placex insert: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
1184 CREATE OR REPLACE FUNCTION placex_update() RETURNS
1189 place_centroid GEOMETRY;
1191 search_maxdistance FLOAT[];
1192 search_mindistance FLOAT[];
1193 address_havelevel BOOLEAN[];
1194 -- search_scores wordscore[];
1195 -- search_scores_pos INTEGER;
1202 relation_members TEXT[];
1204 linkedplacex RECORD;
1205 search_diameter FLOAT;
1206 search_prevdiameter FLOAT;
1207 search_maxrank INTEGER;
1208 address_maxrank INTEGER;
1209 address_street_word_id INTEGER;
1210 parent_place_id_rank BIGINT;
1215 location_rank_search INTEGER;
1216 location_distance FLOAT;
1220 default_language TEXT;
1221 name_vector INTEGER[];
1222 nameaddress_vector INTEGER[];
1227 --RAISE WARNING '%',NEW.place_id;
1228 --RAISE WARNING '%', NEW;
1230 IF NEW.class = 'place' AND NEW.type = 'postcodearea' THEN
1231 -- Silently do nothing
1236 IF OLD.indexed_status = 100 THEN
1237 delete from placex where place_id = OLD.place_id;
1241 IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
1243 NEW.indexed_date = now();
1245 IF NEW.class = 'place' AND NEW.type = 'houses' THEN
1246 i := create_interpolation(NEW.osm_id, NEW.housenumber);
1250 IF OLD.indexed_status > 1 THEN
1251 result := deleteSearchName(NEW.partition, NEW.place_id);
1252 DELETE FROM place_addressline WHERE place_id = NEW.place_id;
1253 DELETE FROM place_boundingbox where place_id = NEW.place_id;
1254 result := deleteRoad(NEW.partition, NEW.place_id);
1255 result := deleteLocationArea(NEW.partition, NEW.place_id);
1256 UPDATE placex set linked_place_id = null where linked_place_id = NEW.place_id;
1259 -- reclaculate country and partition (should probably have a country_code and calculated_country_code as seperate fields)
1260 IF NEW.rank_search >= 4 THEN
1261 SELECT country_code from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO NEW.country_code;
1262 NEW.country_code := lower(get_country_code(NEW.geometry, NEW.country_code));
1264 NEW.country_code := NULL;
1266 NEW.partition := get_partition(NEW.geometry, NEW.country_code);
1267 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.geometry);
1269 -- Adding ourselves to the list simplifies address calculations later
1270 INSERT INTO place_addressline VALUES (NEW.place_id, NEW.place_id, true, true, 0, NEW.rank_address);
1272 -- What level are we searching from
1273 search_maxrank := NEW.rank_search;
1275 -- Speed up searches - just use the centroid of the feature
1276 -- cheaper but less acurate
1277 place_centroid := ST_Centroid(NEW.geometry);
1278 NEW.centroid := null;
1280 -- Thought this wasn't needed but when we add new languages to the country_name table
1281 -- we need to update the existing names
1282 IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
1283 default_language := get_country_language_code(NEW.country_code);
1284 IF default_language IS NOT NULL THEN
1285 IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
1286 NEW.name := NEW.name || (('name:'||default_language) => (NEW.name -> 'name'));
1287 ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
1288 NEW.name := NEW.name || ('name' => (NEW.name -> ('name:'||default_language)));
1293 -- Initialise the name vector using our name
1294 name_vector := make_keywords(NEW.name);
1295 nameaddress_vector := '{}'::int[];
1297 -- some tag combinations add a special id for search
1298 tagpairid := get_tagpair(NEW.class,NEW.type);
1299 IF tagpairid IS NOT NULL THEN
1300 name_vector := name_vector + tagpairid;
1304 address_havelevel[i] := false;
1307 --RAISE WARNING '% %', NEW.place_id, NEW.rank_search;
1309 -- For low level elements we inherit from our parent road
1310 IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
1312 --RAISE WARNING 'finding street for %', NEW;
1314 NEW.parent_place_id := null;
1316 -- to do that we have to find our parent road
1317 -- Copy data from linked items (points on ways, addr:street links, relations)
1318 -- Note that addr:street links can only be indexed once the street itself is indexed
1319 IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
1321 -- Is this node part of a relation?
1322 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id::integer] and members @> ARRAY['n'||NEW.osm_id]
1324 -- At the moment we only process one type of relation - associatedStreet
1325 IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
1326 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
1327 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
1328 --RAISE WARNING 'node in relation %',relation;
1329 SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::integer
1330 and rank_search = 26 INTO NEW.parent_place_id;
1336 --RAISE WARNING 'x1';
1337 -- Is this node part of a way?
1338 FOR way IN select id from planet_osm_ways where nodes @> ARRAY[NEW.osm_id::integer] LOOP
1339 --RAISE WARNING '%', way;
1340 FOR location IN select * from placex where osm_type = 'W' and osm_id = way.id
1342 --RAISE WARNING '%', location;
1343 -- Way IS a road then we are on it - that must be our road
1344 IF location.rank_search = 26 AND NEW.parent_place_id IS NULL THEN
1345 --RAISE WARNING 'node in way that is a street %',location;
1346 NEW.parent_place_id := location.place_id;
1349 -- Is the WAY part of a relation
1350 IF NEW.parent_place_id IS NULL THEN
1351 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[location.osm_id::integer] and members @> ARRAY['w'||location.osm_id]
1353 -- At the moment we only process one type of relation - associatedStreet
1354 IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
1355 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
1356 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
1357 --RAISE WARNING 'node in way that is in a relation %',relation;
1358 SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::integer
1359 and rank_search = 26 INTO NEW.parent_place_id;
1366 -- If the way contains an explicit name of a street copy it
1367 IF NEW.street IS NULL AND location.street IS NOT NULL THEN
1368 --RAISE WARNING 'node in way that has a streetname %',location;
1369 NEW.street := location.street;
1372 -- If this way is a street interpolation line then it is probably as good as we are going to get
1373 IF NEW.parent_place_id IS NULL AND NEW.street IS NULL AND location.class = 'place' and location.type='houses' THEN
1374 -- Try and find a way that is close roughly parellel to this line
1375 FOR relation IN SELECT place_id FROM placex
1376 WHERE ST_DWithin(location.geometry, placex.geometry, 0.001) and placex.rank_search = 26
1377 and st_geometrytype(location.geometry) in ('ST_LineString')
1378 ORDER BY (ST_distance(placex.geometry, ST_Line_Interpolate_Point(location.geometry,0))+
1379 ST_distance(placex.geometry, ST_Line_Interpolate_Point(location.geometry,0.5))+
1380 ST_distance(placex.geometry, ST_Line_Interpolate_Point(location.geometry,1))) ASC limit 1
1382 --RAISE WARNING 'using nearest street to address interpolation line,0.001 %',relation;
1383 NEW.parent_place_id := relation.place_id;
1392 --RAISE WARNING 'x2';
1394 IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'W' THEN
1395 -- Is this way part of a relation?
1396 FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id::integer] and members @> ARRAY['w'||NEW.osm_id]
1398 -- At the moment we only process one type of relation - associatedStreet
1399 IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
1400 FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
1401 IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
1402 --RAISE WARNING 'way that is in a relation %',relation;
1403 SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::integer
1404 and rank_search = 26 INTO NEW.parent_place_id;
1411 --RAISE WARNING 'x3 %',NEW.parent_place_id;
1413 IF NEW.parent_place_id IS NULL AND NEW.street IS NOT NULL THEN
1414 address_street_word_id := get_name_id(make_standard_name(NEW.street));
1415 IF address_street_word_id IS NOT NULL THEN
1416 FOR location IN SELECT * from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_id) LOOP
1417 NEW.parent_place_id := location.place_id;
1422 --RAISE WARNING 'x4 %',NEW.parent_place_id;
1423 -- Still nothing, just use the nearest road
1424 IF NEW.parent_place_id IS NULL THEN
1425 FOR location IN SELECT place_id FROM getNearestRoadFeature(NEW.partition, place_centroid) LOOP
1426 NEW.parent_place_id := location.place_id;
1431 --RAISE WARNING 'x6 %',NEW.parent_place_id;
1433 -- If we didn't find any road fallback to standard method
1434 IF NEW.parent_place_id IS NOT NULL THEN
1436 -- Add the street to the address as zero distance to force to front of list
1437 -- INSERT INTO place_addressline VALUES (NEW.place_id, NEW.parent_place_id, true, true, 0, 26);
1438 address_havelevel[26] := true;
1440 -- Import address details from parent, reclculating distance in process
1441 -- INSERT INTO place_addressline select NEW.place_id, x.address_place_id, x.fromarea, x.isaddress, ST_distance(NEW.geometry, placex.geometry), placex.rank_address
1442 -- from place_addressline as x join placex on (address_place_id = placex.place_id)
1443 -- where x.place_id = NEW.parent_place_id and x.address_place_id != NEW.parent_place_id;
1445 -- Get the details of the parent road
1446 select * from search_name where place_id = NEW.parent_place_id INTO location;
1447 NEW.country_code := location.country_code;
1449 --RAISE WARNING '%', NEW.name;
1450 -- If there is no name it isn't searchable, don't bother to create a search record
1451 IF NEW.name is NULL THEN
1455 -- Merge address from parent
1456 nameaddress_vector := array_merge(nameaddress_vector, location.nameaddress_vector);
1458 -- Performance, it would be more acurate to do all the rest of the import process but it takes too long
1459 -- Just be happy with inheriting from parent road only
1461 IF NEW.rank_search <= 25 THEN
1462 result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
1465 result := insertSearchName(NEW.partition, NEW.place_id, NEW.country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid);
1472 -- RAISE WARNING ' INDEXING: %',NEW;
1474 IF NEW.osm_type = 'R' AND NEW.rank_search < 26 THEN
1476 -- see if we have any special relation members
1477 select members from planet_osm_rels where id = NEW.osm_id INTO relation_members;
1479 FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['label']) as member LOOP
1481 select * from placex where osm_type = upper(substring(relMember.member,1,1))
1482 and osm_id = substring(relMember.member,2,10000)::integer order by rank_search desc limit 1 into linkedPlacex;
1484 -- If we don't already have one use this as the centre point of the geometry
1485 IF NEW.centroid IS NULL THEN
1486 NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
1489 -- merge in the label name, re-init word vector
1490 NEW.name := linkedPlacex.name || NEW.name;
1491 name_vector := make_keywords(NEW.name);
1493 -- merge in extra tags
1494 NEW.extratags := linkedPlacex.extratags || NEW.extratags;
1496 -- mark the linked place (excludes from search results)
1497 UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
1501 IF NEW.centroid IS NULL THEN
1503 FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['admin_center','admin_centre']) as member LOOP
1505 select * from placex where osm_type = upper(substring(relMember.member,1,1))
1506 and osm_id = substring(relMember.member,2,10000)::integer order by rank_search desc limit 1 into linkedPlacex;
1508 IF NEW.name->'name' = linkedPlacex.name->'name' AND NEW.rank_search = linkedPlacex.rank_search THEN
1509 -- If we don't already have one use this as the centre point of the geometry
1510 IF NEW.centroid IS NULL THEN
1511 NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
1514 -- merge in the name, re-init word vector
1515 NEW.name := linkedPlacex.name || NEW.name;
1516 name_vector := make_keywords(NEW.name);
1518 -- merge in extra tags
1519 NEW.extratags := linkedPlacex.extratags || NEW.extratags;
1521 -- mark the linked place (excludes from search results)
1522 UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
1529 -- not found one yet? how about doing a name search
1530 IF NEW.centroid IS NULL THEN
1531 FOR linkedPlacex IN select placex.* from search_name join placex using (place_id) WHERE
1532 search_name.name_vector @> ARRAY[getorcreate_name_id(make_standard_name(NEW.name->'name'))]
1533 AND search_name.search_rank = NEW.rank_search
1534 AND search_name.place_id != NEW.place_id
1536 AND NEW.name->'name' = placex.name->'name'
1537 AND st_contains(NEW.geometry, placex.geometry)
1539 -- If we don't already have one use this as the centre point of the geometry
1540 IF NEW.centroid IS NULL THEN
1541 NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
1544 -- merge in the name, re-init word vector
1545 NEW.name := linkedPlacex.name || NEW.name;
1546 name_vector := make_keywords(NEW.name);
1548 -- merge in extra tags
1549 NEW.extratags := linkedPlacex.extratags || NEW.extratags;
1551 -- mark the linked place (excludes from search results)
1552 UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
1557 IF NEW.centroid IS NOT NULL THEN
1558 place_centroid := NEW.centroid;
1563 NEW.parent_place_id = 0;
1564 parent_place_id_rank = 0;
1566 -- convert isin to array of tokenids
1567 isin_tokens := '{}'::int[];
1568 IF NEW.isin IS NOT NULL THEN
1569 isin := regexp_split_to_array(NEW.isin, E'[;,]');
1570 IF array_upper(isin, 1) IS NOT NULL THEN
1571 FOR i IN 1..array_upper(isin, 1) LOOP
1572 address_street_word_id := get_name_id(make_standard_name(isin[i]));
1573 IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
1574 isin_tokens := isin_tokens || address_street_word_id;
1579 IF NEW.postcode IS NOT NULL THEN
1580 isin := regexp_split_to_array(NEW.postcode, E'[;,]');
1581 IF array_upper(isin, 1) IS NOT NULL THEN
1582 FOR i IN 1..array_upper(isin, 1) LOOP
1583 address_street_word_id := get_name_id(make_standard_name(isin[i]));
1584 IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
1585 isin_tokens := isin_tokens || address_street_word_id;
1590 --RAISE WARNING 'ISIN: %', isin_tokens;
1592 -- Process area matches
1593 location_rank_search := 100;
1594 location_distance := 0;
1595 --RAISE WARNING ' getNearFeatures(%,''%'',%,''%'')',NEW.partition, place_centroid, search_maxrank, isin_tokens;
1596 FOR location IN SELECT * from getNearFeatures(NEW.partition, place_centroid, search_maxrank, isin_tokens) LOOP
1598 --RAISE WARNING ' AREA: %',location;
1600 IF location.rank_search < location_rank_search THEN
1601 location_rank_search := location.rank_search;
1602 location_distance := location.distance * 1.5;
1605 IF location.distance < location_distance OR NOT location.isguess THEN
1607 -- Add it to the list of search terms
1608 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
1609 INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, true, NOT address_havelevel[location.rank_address], location.distance, location.rank_address);
1610 address_havelevel[location.rank_address] := true;
1612 --RAISE WARNING ' Terms: (%) %',location, nameaddress_vector;
1614 IF location.rank_address > parent_place_id_rank THEN
1615 NEW.parent_place_id = location.place_id;
1616 parent_place_id_rank = location.rank_address;
1623 -- try using the isin value to find parent places
1624 IF array_upper(isin_tokens, 1) IS NOT NULL THEN
1625 FOR i IN 1..array_upper(isin_tokens, 1) LOOP
1626 --RAISE WARNING ' getNearestNamedFeature: % % % %',NEW.partition, place_centroid, search_maxrank, isin_tokens[i];
1628 FOR location IN SELECT * from getNearestNamedFeature(NEW.partition, place_centroid, search_maxrank, isin_tokens[i]) LOOP
1630 --RAISE WARNING ' ISIN: %',location;
1632 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
1633 INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, false, NOT address_havelevel[location.rank_address], location.distance, location.rank_address);
1634 address_havelevel[location.rank_address] := true;
1636 IF location.rank_address > parent_place_id_rank THEN
1637 NEW.parent_place_id = location.place_id;
1638 parent_place_id_rank = location.rank_address;
1646 -- for long ways we should add search terms for the entire length
1647 IF st_length(NEW.geometry) > 0.05 THEN
1649 location_rank_search := 100;
1650 location_distance := 0;
1652 FOR location IN SELECT * from getNearFeatures(NEW.partition, NEW.geometry, search_maxrank, isin_tokens) LOOP
1654 IF location.rank_search < location_rank_search THEN
1655 location_rank_search := location.rank_search;
1656 location_distance := location.distance * 1.5;
1659 IF location.distance < location_distance THEN
1661 -- Add it to the list of search terms
1662 nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
1663 INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, true, false, location.distance, location.rank_address);
1671 -- if we have a name add this to the name search table
1672 IF NEW.name IS NOT NULL THEN
1674 IF NEW.rank_search <= 25 THEN
1675 result := add_location(NEW.place_id, NEW.country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
1678 IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
1679 result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.country_code, NEW.geometry);
1682 result := insertSearchName(NEW.partition, NEW.place_id, NEW.country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid);
1684 -- INSERT INTO search_name values (NEW.place_id, NEW.rank_search, NEW.rank_search, 0, NEW.country_code, name_vector, nameaddress_vector, place_centroid);
1687 -- If we've not managed to pick up a better one - default centroid
1688 IF NEW.centroid IS NULL THEN
1689 NEW.centroid := place_centroid;
1699 CREATE OR REPLACE FUNCTION placex_delete() RETURNS TRIGGER
1706 update placex set linked_place_id = null where linked_place_id = OLD.place_id;
1707 update placex set indexed_status = 2 where linked_place_id = OLD.place_id and indexed_status = 0;
1709 IF OLD.rank_address < 30 THEN
1711 -- mark everything linked to this place for re-indexing
1712 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = OLD.place_id
1713 and placex.place_id = place_addressline.place_id and indexed_status = 0;
1715 DELETE FROM place_addressline where address_place_id = OLD.place_id;
1717 b := deleteRoad(OLD.partition, OLD.place_id);
1719 update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
1723 IF OLD.rank_address < 26 THEN
1724 b := deleteLocationArea(OLD.partition, OLD.place_id);
1727 IF OLD.name is not null THEN
1728 b := deleteSearchName(OLD.partition, OLD.place_id);
1731 DELETE FROM place_addressline where place_id = OLD.place_id;
1733 -- remove from tables for special search
1734 classtable := 'place_classtype_' || OLD.class || '_' || OLD.type;
1735 SELECT count(*)>0 FROM pg_tables WHERE tablename = classtable INTO b;
1737 EXECUTE 'DELETE FROM ' || classtable::regclass || ' WHERE place_id = $1' USING OLD.place_id;
1746 CREATE OR REPLACE FUNCTION place_delete() RETURNS TRIGGER
1752 -- RAISE WARNING 'delete: % % % %',OLD.osm_type,OLD.osm_id,OLD.class,OLD.type;
1754 -- deleting large polygons can have a massive effect ont he system - require manual intervention to let them through
1755 IF st_area(OLD.geometry) > 2 THEN
1756 insert into import_polygon_delete values (OLD.osm_type,OLD.osm_id,OLD.class,OLD.type);
1761 UPDATE placex set indexed_status = 100 where osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type;
1763 -- delete from placex where osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type;
1770 CREATE OR REPLACE FUNCTION place_insert() RETURNS TRIGGER
1775 existingplacex RECORD;
1776 existinggeometry GEOMETRY;
1777 existingplace_id BIGINT;
1782 IF FALSE and NEW.osm_type = 'R' THEN
1783 RAISE WARNING '-----------------------------------------------------------------------------------';
1784 RAISE WARNING 'place_insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,st_area(NEW.geometry);
1785 select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
1786 RAISE WARNING '%', existingplacex;
1789 -- Just block these - lots and pointless
1790 IF NEW.class = 'highway' and NEW.type in ('turning_circle','traffic_signals','mini_roundabout','noexit','crossing') THEN
1793 IF NEW.class in ('landuse','natural') and NEW.name is null THEN
1797 IF ST_IsEmpty(NEW.geometry) OR NOT ST_IsValid(NEW.geometry) OR ST_X(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') THEN
1798 INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code,
1799 now(), ST_IsValidReason(NEW.geometry), null, NEW.geometry);
1800 -- RAISE WARNING 'Invalid Geometry: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
1804 -- Patch in additional country names
1805 IF NEW.admin_level = 2 AND NEW.type = 'administrative' AND NEW.country_code is not null THEN
1806 select country_name.name || NEW.name from country_name where country_name.country_code = lower(NEW.country_code) INTO NEW.name;
1809 -- Have we already done this place?
1810 select * from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existing;
1812 -- Get the existing place_id
1813 select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
1815 -- Handle a place changing type by removing the old data
1816 -- My generated 'place' types are causing havok because they overlap with real keys
1817 -- TODO: move them to their own special purpose key/class to avoid collisions
1818 IF existing.osm_type IS NULL AND (NEW.type not in ('postcode','house','houses')) THEN
1819 DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type not in ('postcode','house','houses');
1822 -- RAISE WARNING 'Existing: %',existing.place_id;
1825 IF existing.geometry is not null AND st_isvalid(existing.geometry)
1826 AND st_area(existing.geometry) > 0.02
1827 AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
1828 AND st_area(NEW.geometry) < st_area(existing.geometry)*0.5
1830 INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code, now(),
1831 'Area reduced from '||st_area(existing.geometry)||' to '||st_area(NEW.geometry), existing.geometry, NEW.geometry);
1835 DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
1836 DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
1838 -- To paraphrase, if there isn't an existing item, OR if the admin level has changed, OR if it is a major change in geometry
1839 IF existing.osm_type IS NULL
1840 OR existingplacex.osm_type IS NULL
1841 OR coalesce(existing.admin_level, 100) != coalesce(NEW.admin_level, 100)
1842 OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
1843 OR (existing.geometry::text != NEW.geometry::text AND ST_Distance(ST_Centroid(existing.geometry),ST_Centroid(NEW.geometry)) > 0.01 AND NOT
1844 (ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon') AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')))
1847 -- IF existing.osm_type IS NULL THEN
1848 -- RAISE WARNING 'no existing place';
1850 -- IF existingplacex.osm_type IS NULL THEN
1851 -- RAISE WARNING 'no existing placex %', existingplacex;
1854 -- RAISE WARNING 'delete and replace';
1856 IF existing.osm_type IS NOT NULL THEN
1857 -- RAISE WARNING 'insert delete % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,ST_Distance(ST_Centroid(existing.geometry),ST_Centroid(NEW.geometry)),existing;
1858 DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
1861 -- RAISE WARNING 'delete and replace2';
1863 -- No - process it as a new insertion (hopefully of low rank or it will be slow)
1864 insert into placex (osm_type, osm_id, class, type, name, admin_level, housenumber,
1865 street, isin, postcode, country_code, extratags, geometry)
1866 values (NEW.osm_type
1881 -- RAISE WARNING 'insert done % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
1886 -- Various ways to do the update
1888 -- Debug, what's changed?
1890 IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '') THEN
1891 RAISE WARNING 'update details, name: % % % %',NEW.osm_type,NEW.osm_id,existing.name::text,NEW.name::text;
1893 IF coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '') THEN
1894 RAISE WARNING 'update details, housenumber: % % % %',NEW.osm_type,NEW.osm_id,existing.housenumber,NEW.housenumber;
1896 IF coalesce(existing.street, '') != coalesce(NEW.street, '') THEN
1897 RAISE WARNING 'update details, street: % % % %',NEW.osm_type,NEW.osm_id,existing.street,NEW.street;
1899 IF coalesce(existing.isin, '') != coalesce(NEW.isin, '') THEN
1900 RAISE WARNING 'update details, isin: % % % %',NEW.osm_type,NEW.osm_id,existing.isin,NEW.isin;
1902 IF coalesce(existing.postcode, '') != coalesce(NEW.postcode, '') THEN
1903 RAISE WARNING 'update details, postcode: % % % %',NEW.osm_type,NEW.osm_id,existing.postcode,NEW.postcode;
1905 IF coalesce(existing.country_code, '') != coalesce(NEW.country_code, '') THEN
1906 RAISE WARNING 'update details, country_code: % % % %',NEW.osm_type,NEW.osm_id,existing.country_code,NEW.country_code;
1910 -- Special case for polygon shape changes because they tend to be large and we can be a bit clever about how we handle them
1911 IF existing.geometry::text != NEW.geometry::text
1912 AND ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon')
1913 AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
1916 -- Get the version of the geometry actually used (in placex table)
1917 select geometry from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type into existinggeometry;
1919 -- Performance limit
1920 IF st_area(NEW.geometry) < 0.000000001 AND st_area(existinggeometry) < 1 THEN
1922 -- re-index points that have moved in / out of the polygon, could be done as a single query but postgres gets the index usage wrong
1923 update placex set indexed_status = 2 where indexed_status = 0 and
1924 (ST_Contains(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
1925 AND NOT (ST_Contains(existinggeometry, placex.geometry) OR ST_Intersects(existinggeometry, placex.geometry))
1926 AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
1928 update placex set indexed_status = 2 where indexed_status = 0 and
1929 (ST_Contains(existinggeometry, placex.geometry) OR ST_Intersects(existinggeometry, placex.geometry))
1930 AND NOT (ST_Contains(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
1931 AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
1937 -- Special case - if we are just adding extra words we hack them into the search_name table rather than reindexing
1938 IF FALSE AND existingplacex.rank_search < 26
1939 AND coalesce(existing.housenumber, '') = coalesce(NEW.housenumber, '')
1940 AND coalesce(existing.street, '') = coalesce(NEW.street, '')
1941 AND coalesce(existing.isin, '') = coalesce(NEW.isin, '')
1942 AND coalesce(existing.postcode, '') = coalesce(NEW.postcode, '')
1943 AND coalesce(existing.country_code, '') = coalesce(NEW.country_code, '')
1944 AND coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
1947 IF NOT update_location_nameonly(existingplacex.place_id, NEW.name) THEN
1949 IF st_area(NEW.geometry) < 0.5 THEN
1950 UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = existingplacex.place_id
1951 and placex.place_id = place_addressline.place_id and indexed_status = 0
1952 and (rank_search < 28 or name is not null);
1959 -- Anything else has changed - reindex the lot
1960 IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
1961 OR coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '')
1962 OR coalesce(existing.street, '') != coalesce(NEW.street, '')
1963 OR coalesce(existing.isin, '') != coalesce(NEW.isin, '')
1964 OR coalesce(existing.postcode, '') != coalesce(NEW.postcode, '')
1965 OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '') THEN
1967 -- performance, can't take the load of re-indexing a whole country / huge area
1968 IF st_area(NEW.geometry) < 0.5 THEN
1969 -- UPDATE placex set indexed_status = 2 from place_addressline where address_place_id = existingplacex.place_id
1970 -- and placex.place_id = place_addressline.place_id and indexed_status = 0;
1977 IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
1978 OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
1979 OR coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '')
1980 OR coalesce(existing.street, '') != coalesce(NEW.street, '')
1981 OR coalesce(existing.isin, '') != coalesce(NEW.isin, '')
1982 OR coalesce(existing.postcode, '') != coalesce(NEW.postcode, '')
1983 OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
1984 OR existing.geometry::text != NEW.geometry::text
1989 housenumber = NEW.housenumber,
1990 street = NEW.street,
1992 postcode = NEW.postcode,
1993 country_code = NEW.country_code,
1994 extratags = NEW.extratags,
1995 geometry = NEW.geometry
1996 where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
2000 housenumber = NEW.housenumber,
2001 street = NEW.street,
2003 postcode = NEW.postcode,
2004 country_code = NEW.country_code,
2005 parent_place_id = null,
2006 extratags = NEW.extratags,
2008 geometry = NEW.geometry
2009 where place_id = existingplacex.place_id;
2011 -- now done as part of insert
2012 -- partition := get_partition(NEW.geometry, existingplacex.country_code);
2013 -- result := update_location(partition, existingplacex.place_id, existingplacex.country_code, NEW.name, existingplacex.rank_search, existingplacex.rank_address, NEW.geometry);
2017 -- Abort the add (we modified the existing place instead)
2021 $$ LANGUAGE plpgsql;
2023 CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[]) RETURNS TEXT
2030 IF name is null THEN
2034 search := languagepref;
2036 FOR j IN 1..array_upper(search, 1) LOOP
2037 IF name ? search[j] AND trim(name->search[j]) != '' THEN
2038 return trim(name->search[j]);
2045 LANGUAGE plpgsql IMMUTABLE;
2047 CREATE OR REPLACE FUNCTION get_connected_ways(way_ids INTEGER[]) RETURNS SETOF planet_osm_ways
2050 searchnodes INTEGER[];
2055 searchnodes := '{}';
2056 FOR j IN 1..array_upper(way_ids, 1) LOOP
2058 select nodes from planet_osm_ways where id = way_ids[j] LIMIT 1
2060 IF not (ARRAY[location.nodes] <@ searchnodes) THEN
2061 searchnodes := searchnodes || location.nodes;
2066 RETURN QUERY select * from planet_osm_ways where nodes && searchnodes and NOT ARRAY[id] <@ way_ids;
2069 LANGUAGE plpgsql IMMUTABLE;
2071 CREATE OR REPLACE FUNCTION get_address_postcode(for_place_id BIGINT) RETURNS TEXT
2082 search := ARRAY['ref'];
2085 select postcode from placex where place_id = for_place_id limit 1 into for_postcode;
2088 select rank_address,name,distance,length(name::text) as namelength
2089 from place_addressline join placex on (address_place_id = placex.place_id)
2090 where place_addressline.place_id = for_place_id and rank_address in (5,11)
2091 order by rank_address desc,rank_search desc,fromarea desc,distance asc,namelength desc
2093 IF array_upper(search, 1) IS NOT NULL AND array_upper(location.name, 1) IS NOT NULL THEN
2094 FOR j IN 1..array_upper(search, 1) LOOP
2095 FOR k IN 1..array_upper(location.name, 1) LOOP
2096 IF (found > location.rank_address AND location.name[k].key = search[j] AND location.name[k].value != '') AND NOT result @> ARRAY[trim(location.name[k].value)] AND (for_postcode IS NULL OR location.name[k].value ilike for_postcode||'%') THEN
2097 result[(100 - location.rank_address)] := trim(location.name[k].value);
2098 found := location.rank_address;
2105 RETURN array_to_string(result,', ');
2110 CREATE OR REPLACE FUNCTION get_address_by_language(for_place_id BIGINT, languagepref TEXT[]) RETURNS TEXT
2122 FOR location IN select * from get_addressdata(for_place_id) where isaddress order by rank_address desc LOOP
2123 currresult := trim(get_name_by_language(location.name, languagepref));
2124 IF currresult != prevresult AND currresult IS NOT NULL THEN
2125 result[(100 - location.rank_address)] := trim(get_name_by_language(location.name, languagepref));
2126 prevresult := currresult;
2130 RETURN array_to_string(result,', ');
2135 DROP TYPE addressline CASCADE;
2136 create type addressline as (
2143 admin_level INTEGER,
2146 rank_address INTEGER,
2150 CREATE OR REPLACE FUNCTION get_addressdata(in_place_id BIGINT) RETURNS setof addressline
2153 for_place_id BIGINT;
2158 countrylocation RECORD;
2159 searchcountrycode varchar(2);
2160 searchhousenumber TEXT;
2161 searchhousename HSTORE;
2162 searchrankaddress INTEGER;
2163 searchpostcode TEXT;
2170 select parent_place_id,'us', housenumber, 30, postcode, null, 'place', 'house' from location_property_tiger
2171 WHERE place_id = in_place_id
2172 INTO for_place_id,searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode, searchhousename, searchclass, searchtype;
2174 IF for_place_id IS NULL THEN
2175 select parent_place_id,'us', housenumber, 30, postcode, null, 'place', 'house' from location_property_aux
2176 WHERE place_id = in_place_id
2177 INTO for_place_id,searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode, searchhousename, searchclass, searchtype;
2180 IF for_place_id IS NULL THEN
2181 select parent_place_id, country_code, housenumber, rank_address, postcode, name, class, type from placex
2182 WHERE place_id = in_place_id and rank_address = 30
2183 INTO for_place_id, searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode, searchhousename, searchclass, searchtype;
2186 IF for_place_id IS NULL THEN
2187 for_place_id := in_place_id;
2188 select country_code, housenumber, rank_address, postcode, null from placex where place_id = for_place_id
2189 INTO searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode, searchhousename;
2192 --RAISE WARNING '% % % %',searchcountrycode, searchhousenumber, searchrankaddress, searchpostcode;
2195 hadcountry := false;
2197 select placex.place_id, osm_type, osm_id,
2198 CASE WHEN class = 'place' and type = 'postcode' THEN 'name' => postcode ELSE name END as name,
2199 class, type, admin_level, true as fromarea, true as isaddress,
2200 CASE WHEN rank_address = 0 THEN 100 WHEN rank_address = 11 THEN 5 ELSE rank_address END as rank_address,
2201 0 as distance, country_code
2203 where place_id = for_place_id
2205 --RAISE WARNING '%',location;
2206 IF searchcountrycode IS NULL AND location.country_code IS NOT NULL THEN
2207 searchcountrycode := location.country_code;
2209 IF searchpostcode IS NOT NULL and location.type = 'postcode' THEN
2210 location.isaddress := FALSE;
2212 IF location.rank_address = 4 AND location.isaddress THEN
2215 IF location.rank_address < 4 AND NOT hadcountry THEN
2216 select name from country_name where country_code = searchcountrycode limit 1 INTO countryname;
2217 IF countryname IS NOT NULL THEN
2218 countrylocation := ROW(null, null, null, countryname, 'place', 'country', null, true, true, 4, 0)::addressline;
2219 RETURN NEXT countrylocation;
2222 countrylocation := ROW(location.place_id, location.osm_type, location.osm_id, location.name, location.class,
2223 location.type, location.admin_level, location.fromarea, location.isaddress, location.rank_address,
2224 location.distance)::addressline;
2225 RETURN NEXT countrylocation;
2226 found := location.rank_address;
2230 select placex.place_id, osm_type, osm_id,
2231 CASE WHEN class = 'place' and type = 'postcode' THEN 'name' => postcode ELSE name END as name,
2232 class, type, admin_level, fromarea, isaddress,
2233 CASE WHEN address_place_id = for_place_id AND rank_address = 0 THEN 100 WHEN rank_address = 11 THEN 5 ELSE rank_address END as rank_address,
2234 distance,country_code
2235 from place_addressline join placex on (address_place_id = placex.place_id)
2236 where place_addressline.place_id = for_place_id
2237 and (cached_rank_address > 0 AND cached_rank_address < searchrankaddress)
2238 and address_place_id != for_place_id
2239 and (placex.country_code IS NULL OR searchcountrycode IS NULL OR placex.country_code = searchcountrycode OR rank_address < 4)
2240 order by rank_address desc,isaddress desc,fromarea desc,distance asc,rank_search desc
2242 --RAISE WARNING '%',location;
2243 IF searchcountrycode IS NULL AND location.country_code IS NOT NULL THEN
2244 searchcountrycode := location.country_code;
2246 IF searchpostcode IS NOT NULL and location.type = 'postcode' THEN
2247 location.isaddress := FALSE;
2249 IF location.rank_address = 4 AND location.isaddress THEN
2252 IF location.rank_address < 4 AND NOT hadcountry THEN
2253 select name from country_name where country_code = searchcountrycode limit 1 INTO countryname;
2254 IF countryname IS NOT NULL THEN
2255 countrylocation := ROW(null, null, null, countryname, 'place', 'country', null, true, true, 4, 0)::addressline;
2256 RETURN NEXT countrylocation;
2259 countrylocation := ROW(location.place_id, location.osm_type, location.osm_id, location.name, location.class,
2260 location.type, location.admin_level, location.fromarea, location.isaddress, location.rank_address,
2261 location.distance)::addressline;
2262 RETURN NEXT countrylocation;
2263 found := location.rank_address;
2267 select name from country_name where country_code = searchcountrycode limit 1 INTO countryname;
2268 --RAISE WARNING '% % %',found,searchcountrycode,countryname;
2269 IF countryname IS NOT NULL THEN
2270 location := ROW(null, null, null, countryname, 'place', 'country', null, true, true, 4, 0)::addressline;
2271 RETURN NEXT location;
2275 IF searchcountrycode IS NOT NULL THEN
2276 location := ROW(null, null, null, 'ref'=>searchcountrycode, 'place', 'country_code', null, true, false, 4, 0)::addressline;
2277 RETURN NEXT location;
2280 IF searchhousename IS NOT NULL THEN
2281 location := ROW(in_place_id, null, null, searchhousename, searchclass, searchtype, null, true, true, 29, 0)::addressline;
2282 -- location := ROW(in_place_id, null, null, searchhousename, 'place', 'house_name', null, true, true, 29, 0)::addressline;
2283 RETURN NEXT location;
2286 IF searchhousenumber IS NOT NULL THEN
2287 location := ROW(in_place_id, null, null, 'ref'=>searchhousenumber, 'place', 'house_number', null, true, true, 28, 0)::addressline;
2288 RETURN NEXT location;
2291 IF searchpostcode IS NOT NULL THEN
2292 location := ROW(null, null, null, 'ref'=>searchpostcode, 'place', 'postcode', null, true, true, 5, 0)::addressline;
2293 RETURN NEXT location;
2301 CREATE OR REPLACE FUNCTION get_place_boundingbox(search_place_id BIGINT) RETURNS place_boundingbox
2304 result place_boundingbox;
2305 numfeatures integer;
2307 select * from place_boundingbox into result where place_id = search_place_id;
2308 IF result.place_id IS NULL THEN
2309 -- remove isaddress = true because if there is a matching polygon it always wins
2310 select count(*) from place_addressline where address_place_id = search_place_id into numfeatures;
2311 insert into place_boundingbox select place_id,
2312 ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),4)),ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),2)),
2313 ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),1)),ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),3)),
2314 numfeatures, ST_Area(geometry),
2315 geometry as area from location_area where place_id = search_place_id;
2316 select * from place_boundingbox into result where place_id = search_place_id;
2318 IF result.place_id IS NULL THEN
2320 insert into place_boundingbox select address_place_id,
2321 min(ST_Y(ST_Centroid(geometry))) as minlon,max(ST_Y(ST_Centroid(geometry))) as maxlon,
2322 min(ST_X(ST_Centroid(geometry))) as minlat,max(ST_X(ST_Centroid(geometry))) as maxlat,
2323 count(*), ST_Area(ST_Buffer(ST_Convexhull(ST_Collect(geometry)),0.0001)) as area,
2324 ST_Buffer(ST_Convexhull(ST_Collect(geometry)),0.0001) as boundary
2325 from (select * from place_addressline where address_place_id = search_place_id order by cached_rank_address limit 4000) as place_addressline join placex using (place_id)
2326 where address_place_id = search_place_id
2327 -- and (isaddress = true OR place_id = search_place_id)
2328 and (st_length(geometry) < 0.01 or place_id = search_place_id)
2329 group by address_place_id limit 1;
2330 select * from place_boundingbox into result where place_id = search_place_id;
2337 -- don't do the operation if it would be slow
2338 CREATE OR REPLACE FUNCTION get_place_boundingbox_quick(search_place_id BIGINT) RETURNS place_boundingbox
2341 result place_boundingbox;
2342 numfeatures integer;
2345 select * from place_boundingbox into result where place_id = search_place_id;
2346 IF result IS NULL AND rank > 14 THEN
2347 select count(*) from place_addressline where address_place_id = search_place_id and isaddress = true into numfeatures;
2348 insert into place_boundingbox select place_id,
2349 ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),4)),ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),2)),
2350 ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),1)),ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),3)),
2351 numfeatures, ST_Area(geometry),
2352 geometry as area from location_area where place_id = search_place_id;
2353 select * from place_boundingbox into result where place_id = search_place_id;
2355 IF result IS NULL THEN
2356 select rank_search from placex where place_id = search_place_id into rank;
2359 insert into place_boundingbox select address_place_id,
2360 min(ST_Y(ST_Centroid(geometry))) as minlon,max(ST_Y(ST_Centroid(geometry))) as maxlon,
2361 min(ST_X(ST_Centroid(geometry))) as minlat,max(ST_X(ST_Centroid(geometry))) as maxlat,
2362 count(*), ST_Area(ST_Buffer(ST_Convexhull(ST_Collect(geometry)),0.0001)) as area,
2363 ST_Buffer(ST_Convexhull(ST_Collect(geometry)),0.0001) as boundary
2364 from place_addressline join placex using (place_id)
2365 where address_place_id = search_place_id
2366 and (isaddress = true OR place_id = search_place_id)
2367 and (st_length(geometry) < 0.01 or place_id = search_place_id)
2368 group by address_place_id limit 1;
2369 select * from place_boundingbox into result where place_id = search_place_id;
2377 CREATE OR REPLACE FUNCTION update_place(search_place_id BIGINT) RETURNS BOOLEAN
2380 result place_boundingbox;
2381 numfeatures integer;
2385 housenumber = place.housenumber,
2386 street = place.street,
2388 postcode = place.postcode,
2389 country_code = place.country_code,
2390 parent_place_id = null
2392 where placex.place_id = search_place_id
2393 and place.osm_type = placex.osm_type and place.osm_id = placex.osm_id
2394 and place.class = placex.class and place.type = placex.type;
2395 update placex set indexed_status = 2 where place_id = search_place_id;
2396 update placex set indexed_status = 0 where place_id = search_place_id;
2402 CREATE OR REPLACE FUNCTION get_searchrank_label(rank INTEGER) RETURNS TEXT
2408 ELSEIF rank < 4 THEN
2410 ELSEIF rank < 8 THEN
2412 ELSEIF rank < 12 THEN
2414 ELSEIF rank < 16 THEN
2416 ELSEIF rank = 16 THEN
2418 ELSEIF rank = 17 THEN
2419 RETURN 'Town / Island';
2420 ELSEIF rank = 18 THEN
2421 RETURN 'Village / Hamlet';
2422 ELSEIF rank = 20 THEN
2424 ELSEIF rank = 21 THEN
2425 RETURN 'Postcode Area';
2426 ELSEIF rank = 22 THEN
2427 RETURN 'Croft / Farm / Locality / Islet';
2428 ELSEIF rank = 23 THEN
2429 RETURN 'Postcode Area';
2430 ELSEIF rank = 25 THEN
2431 RETURN 'Postcode Point';
2432 ELSEIF rank = 26 THEN
2433 RETURN 'Street / Major Landmark';
2434 ELSEIF rank = 27 THEN
2435 RETURN 'Minory Street / Path';
2436 ELSEIF rank = 28 THEN
2437 RETURN 'House / Building';
2439 RETURN 'Other: '||rank;
2446 CREATE OR REPLACE FUNCTION get_addressrank_label(rank INTEGER) RETURNS TEXT
2452 ELSEIF rank < 2 THEN
2454 ELSEIF rank < 4 THEN
2456 ELSEIF rank = 5 THEN
2458 ELSEIF rank < 8 THEN
2460 ELSEIF rank < 12 THEN
2462 ELSEIF rank < 16 THEN
2464 ELSEIF rank = 16 THEN
2466 ELSEIF rank = 17 THEN
2467 RETURN 'Town / Village / Hamlet';
2468 ELSEIF rank = 20 THEN
2470 ELSEIF rank = 21 THEN
2471 RETURN 'Postcode Area';
2472 ELSEIF rank = 22 THEN
2473 RETURN 'Croft / Farm / Locality / Islet';
2474 ELSEIF rank = 23 THEN
2475 RETURN 'Postcode Area';
2476 ELSEIF rank = 25 THEN
2477 RETURN 'Postcode Point';
2478 ELSEIF rank = 26 THEN
2479 RETURN 'Street / Major Landmark';
2480 ELSEIF rank = 27 THEN
2481 RETURN 'Minory Street / Path';
2482 ELSEIF rank = 28 THEN
2483 RETURN 'House / Building';
2485 RETURN 'Other: '||rank;
2492 CREATE OR REPLACE FUNCTION get_word_suggestion(srcword TEXT) RETURNS TEXT
2499 trigramtoken := regexp_replace(make_standard_name(srcword),E'([^0-9])\\1+',E'\\1','g');
2500 SELECT word FROM word WHERE word_trigram like ' %' and word_trigram % trigramtoken ORDER BY similarity(word_trigram, trigramtoken) DESC, word limit 1 into result;
2507 CREATE OR REPLACE FUNCTION get_word_suggestions(srcword TEXT) RETURNS TEXT[]
2515 trigramtoken := regexp_replace(make_standard_name(srcword),E'([^0-9])\\1+',E'\\1','g');
2517 FOR r IN SELECT word,similarity(word_trigram, trigramtoken) as score FROM word
2518 WHERE word_trigram like ' %' and word_trigram % trigramtoken ORDER BY similarity(word_trigram, trigramtoken) DESC, word limit 4
2520 result[coalesce(array_upper(result,1)+1,1)] := r.word;
2528 CREATE OR REPLACE FUNCTION tigger_create_interpolation(linegeo GEOMETRY, in_startnumber INTEGER,
2529 in_endnumber INTEGER, interpolationtype TEXT,
2530 in_street TEXT, in_isin TEXT, in_postcode TEXT) RETURNS INTEGER
2534 startnumber INTEGER;
2539 numberrange INTEGER;
2540 rangestartnumber INTEGER;
2541 place_centroid GEOMETRY;
2542 out_partition INTEGER;
2543 out_parent_place_id BIGINT;
2545 address_street_word_id INTEGER;
2549 IF in_endnumber > in_startnumber THEN
2550 startnumber = in_startnumber;
2551 endnumber = in_endnumber;
2553 startnumber = in_endnumber;
2554 endnumber = in_startnumber;
2557 numberrange := endnumber - startnumber;
2558 rangestartnumber := startnumber;
2560 IF (interpolationtype = 'odd' AND startnumber%2 = 0) OR (interpolationtype = 'even' AND startnumber%2 = 1) THEN
2561 startnumber := startnumber + 1;
2564 IF (interpolationtype = 'odd' OR interpolationtype = 'even') THEN
2566 ELSE -- everything else assumed to be 'all'
2571 -- Filter out really broken tiger data
2572 IF numberrange > 0 AND (numberrange::float/stepsize::float > 500)
2573 AND ST_length(linegeo)/(numberrange::float/stepsize::float) < 0.000001 THEN
2574 RAISE WARNING 'Road too short for number range % to % on %, % (%)',startnumber,endnumber,in_street,in_isin,
2575 ST_length(linegeo)/(numberrange::float/stepsize::float);
2579 place_centroid := ST_Centroid(linegeo);
2580 out_partition := get_partition(place_centroid, 'us');
2581 out_parent_place_id := null;
2583 address_street_word_id := get_name_id(make_standard_name(in_street));
2584 IF address_street_word_id IS NOT NULL THEN
2585 FOR location IN SELECT * from getNearestNamedRoadFeature(out_partition, place_centroid, address_street_word_id) LOOP
2586 out_parent_place_id := location.place_id;
2590 IF out_parent_place_id IS NULL THEN
2591 FOR location IN SELECT place_id FROM getNearestParellelRoadFeature(out_partition, linegeo) LOOP
2592 out_parent_place_id := location.place_id;
2596 IF out_parent_place_id IS NULL THEN
2597 FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP
2598 out_parent_place_id := location.place_id;
2603 FOR housenum IN startnumber..endnumber BY stepsize LOOP
2604 insert into location_property_tiger (place_id, partition, parent_place_id, housenumber, postcode, centroid)
2605 values (nextval('seq_place'), out_partition, out_parent_place_id, housenum, in_postcode,
2606 ST_Line_Interpolate_Point(linegeo, (housenum::float-rangestartnumber::float)/numberrange::float));
2607 newpoints := newpoints + 1;
2615 CREATE OR REPLACE FUNCTION aux_create_property(pointgeo GEOMETRY, in_housenumber TEXT,
2616 in_street TEXT, in_isin TEXT, in_postcode TEXT, in_countrycode char(2)) RETURNS INTEGER
2621 place_centroid GEOMETRY;
2622 out_partition INTEGER;
2623 out_parent_place_id BIGINT;
2625 address_street_word_id INTEGER;
2630 place_centroid := ST_Centroid(pointgeo);
2631 out_partition := get_partition(place_centroid, in_countrycode);
2632 out_parent_place_id := null;
2634 address_street_word_id := get_name_id(make_standard_name(in_street));
2635 IF address_street_word_id IS NOT NULL THEN
2636 FOR location IN SELECT * from getNearestNamedRoadFeature(out_partition, place_centroid, address_street_word_id) LOOP
2637 out_parent_place_id := location.place_id;
2641 IF out_parent_place_id IS NULL THEN
2642 FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP
2643 out_parent_place_id := location.place_id;
2647 out_postcode := in_postcode;
2648 IF out_postcode IS NULL THEN
2649 SELECT postcode from placex where place_id = out_parent_place_id INTO out_postcode;
2651 IF out_postcode IS NULL THEN
2652 out_postcode := getNearestPostcode(out_partition, place_centroid);
2656 insert into location_property_aux (place_id, partition, parent_place_id, housenumber, postcode, centroid)
2657 values (nextval('seq_place'), out_partition, out_parent_place_id, in_housenumber, out_postcode, place_centroid);
2658 newpoints := newpoints + 1;
2665 CREATE OR REPLACE FUNCTION get_osm_rel_members(members TEXT[], member TEXT) RETURNS TEXT[]
2672 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
2673 IF members[i+1] = member THEN
2674 result := result || members[i];
2683 CREATE OR REPLACE FUNCTION get_osm_rel_members(members TEXT[], memberLabels TEXT[]) RETURNS SETOF TEXT
2689 FOR i IN 1..ARRAY_UPPER(members,1) BY 2 LOOP
2690 IF members[i+1] = ANY(memberLabels) THEN
2691 RETURN NEXT members[i];