]> git.openstreetmap.org Git - nominatim.git/blob - sql/tiger_import_start.sql
clean up docs for lookup call
[nominatim.git] / sql / tiger_import_start.sql
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);
3
4 CREATE OR REPLACE FUNCTION tiger_line_import(linegeo GEOMETRY, in_startnumber INTEGER, 
5   in_endnumber INTEGER, interpolationtype TEXT, 
6   in_street TEXT, in_isin TEXT, in_postcode TEXT) RETURNS INTEGER
7   AS $$
8 DECLARE
9   startnumber INTEGER;
10   endnumber INTEGER;
11   stepsize INTEGER;
12   numberrange INTEGER;
13   place_centroid GEOMETRY;
14   out_partition INTEGER;
15   out_parent_place_id BIGINT;
16   location RECORD;
17   address_street_word_id INTEGER;  
18
19 BEGIN
20
21   IF in_endnumber > in_startnumber THEN
22     startnumber = in_startnumber;
23     endnumber = in_endnumber;
24   ELSE
25     startnumber = in_endnumber;
26     endnumber = in_startnumber;
27   END IF;
28
29   numberrange := endnumber - startnumber;
30
31   IF (interpolationtype = 'odd' AND startnumber%2 = 0) OR (interpolationtype = 'even' AND startnumber%2 = 1) THEN
32     startnumber := startnumber + 1;
33     stepsize := 2;
34   ELSE
35     IF (interpolationtype = 'odd' OR interpolationtype = 'even') THEN
36       stepsize := 2;
37     ELSE -- everything else assumed to be 'all'
38       stepsize := 1;
39     END IF;
40   END IF;
41
42   -- Filter out really broken tiger data
43   IF numberrange > 0 AND (numberrange::float/stepsize::float > 500) 
44                     AND ST_length(linegeo)/(numberrange::float/stepsize::float) < 0.000001 THEN
45     RAISE WARNING 'Road too short for number range % to % on %, % (%)',startnumber,endnumber,in_street,in_isin,
46                   ST_length(linegeo)/(numberrange::float/stepsize::float);    
47     RETURN 0;
48   END IF;
49
50   place_centroid := ST_Centroid(linegeo);
51   out_partition := get_partition('us');
52   out_parent_place_id := null;
53
54   address_street_word_id := get_name_id(make_standard_name(in_street));
55   IF address_street_word_id IS NOT NULL THEN
56     FOR location IN SELECT * from getNearestNamedRoadFeature(out_partition, place_centroid, ARRAY[address_street_word_id]) LOOP
57       out_parent_place_id := location.place_id;
58     END LOOP;
59   END IF;
60
61   IF out_parent_place_id IS NULL THEN
62     FOR location IN SELECT place_id FROM getNearestParellelRoadFeature(out_partition, linegeo) LOOP
63       out_parent_place_id := location.place_id;
64     END LOOP;    
65   END IF;
66
67   IF out_parent_place_id IS NULL THEN
68     FOR location IN SELECT place_id FROM getNearestRoadFeature(out_partition, place_centroid) LOOP
69       out_parent_place_id := location.place_id;
70     END LOOP;    
71   END IF;
72
73 --insert street(line) into import table
74 insert into location_property_tiger_import (linegeo, place_id, partition, parent_place_id, startnumber, endnumber, interpolationtype, postcode)
75 values (linegeo, nextval('seq_place'), out_partition, out_parent_place_id, startnumber, endnumber, interpolationtype, in_postcode);
76
77   RETURN 1;
78 END;
79 $$
80 LANGUAGE plpgsql;