1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2022 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
7 DROP TABLE IF EXISTS location_property_tiger_import;
8 CREATE TABLE location_property_tiger_import (linegeo GEOMETRY, place_id BIGINT, partition INTEGER, parent_place_id BIGINT, startnumber INTEGER, endnumber INTEGER, interpolationtype TEXT, postcode TEXT);
10 CREATE OR REPLACE FUNCTION tiger_line_import(linegeo GEOMETRY, in_startnumber INTEGER,
11 in_endnumber INTEGER, interpolationtype TEXT,
12 token_info JSONB, in_postcode TEXT) RETURNS INTEGER
19 place_centroid GEOMETRY;
20 out_partition INTEGER;
21 out_parent_place_id BIGINT;
26 IF in_endnumber > in_startnumber THEN
27 startnumber = in_startnumber;
28 endnumber = in_endnumber;
30 startnumber = in_endnumber;
31 endnumber = in_startnumber;
34 IF startnumber < 0 THEN
35 RAISE WARNING 'Negative house number range (% to %)', startnumber, endnumber;
39 numberrange := endnumber - startnumber;
41 IF (interpolationtype = 'odd' AND startnumber % 2 = 0) OR (interpolationtype = 'even' AND startnumber % 2 = 1) THEN
42 startnumber := startnumber + 1;
45 IF (interpolationtype = 'odd' OR interpolationtype = 'even') THEN
47 ELSE -- everything else assumed to be 'all'
52 -- Filter out really broken tiger data
53 IF numberrange > 0 AND (numberrange::float/stepsize::float > 500)
54 AND ST_length(linegeo)/(numberrange::float/stepsize::float) < 0.000001 THEN
55 RAISE WARNING 'Road too short for number range % to % (%)',startnumber,endnumber,
56 ST_length(linegeo)/(numberrange::float/stepsize::float);
60 place_centroid := ST_Centroid(linegeo);
61 out_partition := get_partition('us');
63 out_parent_place_id := getNearestNamedRoadPlaceId(out_partition, place_centroid,
66 IF out_parent_place_id IS NULL THEN
67 SELECT getNearestParallelRoadFeature(out_partition, linegeo)
68 INTO out_parent_place_id;
71 IF out_parent_place_id IS NULL THEN
72 SELECT getNearestRoadPlaceId(out_partition, place_centroid)
73 INTO out_parent_place_id;
76 --insert street(line) into import table
77 insert into location_property_tiger_import (linegeo, place_id, partition, parent_place_id, startnumber, endnumber, interpolationtype, postcode)
78 values (linegeo, nextval('seq_place'), out_partition, out_parent_place_id, startnumber, endnumber, interpolationtype, in_postcode);