1 -- Functions for address interpolation objects in location_property_osmline.
3 -- Splits the line at the given point and returns the two parts
4 -- in a multilinestring.
5 CREATE OR REPLACE FUNCTION split_line_on_node(line GEOMETRY, point GEOMETRY)
9 RETURN ST_Split(ST_Snap(line, point, 0.0005), point);
12 LANGUAGE plpgsql IMMUTABLE;
15 CREATE OR REPLACE FUNCTION get_interpolation_address(in_address HSTORE, wayid BIGINT)
22 IF akeys(in_address) != ARRAY['interpolation'] THEN
26 SELECT nodes INTO waynodes FROM planet_osm_ways WHERE id = wayid;
28 SELECT placex.address, placex.osm_id FROM placex
29 WHERE osm_type = 'N' and osm_id = ANY(waynodes)
30 and placex.address is not null
31 and (placex.address ? 'street' or placex.address ? 'place')
32 and indexed_status < 100
34 -- mark it as a derived address
35 RETURN location.address || in_address || hstore('_inherited', '');
41 LANGUAGE plpgsql STABLE;
45 -- find the parent road of the cut road parts
46 CREATE OR REPLACE FUNCTION get_interpolation_parent(token_info JSONB,
48 centroid GEOMETRY, geom GEOMETRY)
52 parent_place_id BIGINT;
55 parent_place_id := find_parent_for_address(token_info, partition, centroid);
57 IF parent_place_id is null THEN
58 FOR location IN SELECT place_id FROM placex
59 WHERE ST_DWithin(geom, placex.geometry, 0.001) and placex.rank_search = 26
60 ORDER BY (ST_distance(placex.geometry, ST_LineInterpolatePoint(geom,0))+
61 ST_distance(placex.geometry, ST_LineInterpolatePoint(geom,0.5))+
62 ST_distance(placex.geometry, ST_LineInterpolatePoint(geom,1))) ASC limit 1
64 parent_place_id := location.place_id;
68 IF parent_place_id is null THEN
72 RETURN parent_place_id;
75 LANGUAGE plpgsql STABLE;
78 CREATE OR REPLACE FUNCTION osmline_reinsert(node_id BIGINT, geom GEOMETRY)
84 SELECT w.id FROM planet_osm_ways w, location_property_osmline p
85 WHERE p.linegeo && geom and p.osm_id = w.id and p.indexed_status = 0
86 and node_id = any(w.nodes) INTO existingline;
88 IF existingline.id is not NULL THEN
89 DELETE FROM location_property_osmline WHERE osm_id = existingline.id;
90 INSERT INTO location_property_osmline (osm_id, address, linegeo)
91 SELECT osm_id, address, geometry FROM place
92 WHERE osm_type = 'W' and osm_id = existingline.id;
101 CREATE OR REPLACE FUNCTION osmline_insert()
105 NEW.place_id := nextval('seq_place');
106 NEW.indexed_date := now();
108 IF NEW.indexed_status IS NULL THEN
109 IF NEW.address is NULL OR NOT NEW.address ? 'interpolation'
110 OR NEW.address->'interpolation' NOT IN ('odd', 'even', 'all') THEN
111 -- other interpolation types than odd/even/all (e.g. numeric ones) are not supported
115 NEW.indexed_status := 1; --STATUS_NEW
116 NEW.country_code := lower(get_country_code(NEW.linegeo));
118 NEW.partition := get_partition(NEW.country_code);
119 NEW.geometry_sector := geometry_sector(NEW.partition, NEW.linegeo);
128 CREATE OR REPLACE FUNCTION osmline_update()
132 place_centroid GEOMETRY;
142 interpol_postcode TEXT;
146 IF OLD.indexed_status = 100 THEN
147 delete from location_property_osmline where place_id = OLD.place_id;
151 IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
155 NEW.interpolationtype = NEW.address->'interpolation';
157 place_centroid := ST_PointOnSurface(NEW.linegeo);
158 NEW.parent_place_id = get_interpolation_parent(NEW.token_info, NEW.partition,
159 place_centroid, NEW.linegeo);
161 interpol_postcode := token_normalized_postcode(NEW.address->'postcode');
163 NEW.token_info := token_strip_info(NEW.token_info);
164 IF NEW.address ? '_inherited' THEN
165 NEW.address := hstore('interpolation', NEW.interpolationtype);
168 -- if the line was newly inserted, split the line as necessary
169 IF OLD.indexed_status = 1 THEN
170 select nodes from planet_osm_ways where id = NEW.osm_id INTO waynodes;
172 IF array_upper(waynodes, 1) IS NULL THEN
176 linegeo := NEW.linegeo;
179 FOR nodeidpos in 1..array_upper(waynodes, 1) LOOP
181 select osm_id, address, geometry
182 from place where osm_type = 'N' and osm_id = waynodes[nodeidpos]::BIGINT
183 and address is not NULL and address ? 'housenumber' limit 1 INTO nextnode;
184 --RAISE NOTICE 'Nextnode.place_id: %s', nextnode.place_id;
185 IF nextnode.osm_id IS NOT NULL THEN
186 --RAISE NOTICE 'place_id is not null';
187 IF nodeidpos > 1 and nodeidpos < array_upper(waynodes, 1) THEN
188 -- Make sure that the point is actually on the line. That might
189 -- be a bit paranoid but ensures that the algorithm still works
190 -- should osm2pgsql attempt to repair geometries.
191 splitline := split_line_on_node(linegeo, nextnode.geometry);
192 sectiongeo := ST_GeometryN(splitline, 1);
193 linegeo := ST_GeometryN(splitline, 2);
195 sectiongeo = linegeo;
197 endnumber := substring(nextnode.address->'housenumber','[0-9]+')::integer;
199 IF startnumber IS NOT NULL AND endnumber IS NOT NULL
200 AND startnumber != endnumber
201 AND ST_GeometryType(sectiongeo) = 'ST_LineString' THEN
203 IF (startnumber > endnumber) THEN
204 housenum := endnumber;
205 endnumber := startnumber;
206 startnumber := housenum;
207 sectiongeo := ST_Reverse(sectiongeo);
210 -- determine postcode
211 postcode := coalesce(interpol_postcode,
212 token_normalized_postcode(prevnode.address->'postcode'),
213 token_normalized_postcode(nextnode.address->'postcode'),
216 IF postcode is NULL THEN
217 SELECT token_normalized_postcode(placex.postcode)
218 FROM placex WHERE place_id = NEW.parent_place_id INTO postcode;
220 IF postcode is NULL THEN
221 postcode := get_nearest_postcode(NEW.country_code, nextnode.geometry);
224 IF NEW.startnumber IS NULL THEN
225 NEW.startnumber := startnumber;
226 NEW.endnumber := endnumber;
227 NEW.linegeo := sectiongeo;
228 NEW.postcode := postcode;
230 insert into location_property_osmline
231 (linegeo, partition, osm_id, parent_place_id,
232 startnumber, endnumber, interpolationtype,
233 address, postcode, country_code,
234 geometry_sector, indexed_status)
235 values (sectiongeo, NEW.partition, NEW.osm_id, NEW.parent_place_id,
236 startnumber, endnumber, NEW.interpolationtype,
237 NEW.address, postcode,
238 NEW.country_code, NEW.geometry_sector, 0);
242 -- early break if we are out of line string,
243 -- might happen when a line string loops back on itself
244 IF ST_GeometryType(linegeo) != 'ST_LineString' THEN
248 startnumber := substring(nextnode.address->'housenumber','[0-9]+')::integer;
249 prevnode := nextnode;
254 -- marking descendants for reparenting is not needed, because there are
255 -- actually no descendants for interpolation lines