1 DROP TABLE IF EXISTS location_property_tiger_import;
2 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);
4 CREATE OR REPLACE FUNCTION tiger_line_import(linegeo GEOMETRY, in_startnumber INTEGER,
5 in_endnumber INTEGER, interpolationtype TEXT,
6 token_info JSONB, in_postcode TEXT) RETURNS INTEGER
13 place_centroid GEOMETRY;
14 out_partition INTEGER;
15 out_parent_place_id BIGINT;
20 IF in_endnumber > in_startnumber THEN
21 startnumber = in_startnumber;
22 endnumber = in_endnumber;
24 startnumber = in_endnumber;
25 endnumber = in_startnumber;
28 IF startnumber < 0 THEN
29 RAISE WARNING 'Negative house number range (% to %)', startnumber, endnumber;
33 numberrange := endnumber - startnumber;
35 IF (interpolationtype = 'odd' AND startnumber % 2 = 0) OR (interpolationtype = 'even' AND startnumber % 2 = 1) THEN
36 startnumber := startnumber + 1;
39 IF (interpolationtype = 'odd' OR interpolationtype = 'even') THEN
41 ELSE -- everything else assumed to be 'all'
46 -- Filter out really broken tiger data
47 IF numberrange > 0 AND (numberrange::float/stepsize::float > 500)
48 AND ST_length(linegeo)/(numberrange::float/stepsize::float) < 0.000001 THEN
49 RAISE WARNING 'Road too short for number range % to % (%)',startnumber,endnumber,
50 ST_length(linegeo)/(numberrange::float/stepsize::float);
54 place_centroid := ST_Centroid(linegeo);
55 out_partition := get_partition('us');
57 out_parent_place_id := getNearestNamedRoadPlaceId(out_partition, place_centroid,
60 IF out_parent_place_id IS NULL THEN
61 SELECT getNearestParallelRoadFeature(out_partition, linegeo)
62 INTO out_parent_place_id;
65 IF out_parent_place_id IS NULL THEN
66 SELECT getNearestRoadPlaceId(out_partition, place_centroid)
67 INTO out_parent_place_id;
70 --insert street(line) into import table
71 insert into location_property_tiger_import (linegeo, place_id, partition, parent_place_id, startnumber, endnumber, interpolationtype, postcode)
72 values (linegeo, nextval('seq_place'), out_partition, out_parent_place_id, startnumber, endnumber, interpolationtype, in_postcode);