File: geocode_zip.sql

package info (click to toggle)
postgis 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,296 kB
  • sloc: sql: 77,621; ansic: 59,025; xml: 18,553; sh: 11,043; java: 6,061; perl: 2,133; makefile: 981; yacc: 299; python: 192
file content (41 lines) | stat: -rw-r--r-- 994 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
CREATE OR REPLACE FUNCTION geocode_zip(
    parsed NORM_ADDY
) RETURNS REFCURSOR
AS $_$
DECLARE
  result REFCURSOR;
  tempString VARCHAR;
  tempInt INTEGER;
BEGIN
  -- Check to see if the road name can be matched.
  SELECT INTO tempInt count(*)
    FROM zip_lookup_base zip
    JOIN state_lookup sl on (zip.state = sl.name)
    JOIN zt99_d00 zl ON (zip.zip = zl.zcta::integer)
    WHERE zip = parsed.zip;

  IF tempInt = 0 THEN
    RETURN NULL;
  END IF;

  OPEN result FOR
  SELECT
      NULL::varchar(2) as fedirp,
      NULL::varchar(30) as fename,
      NULL::varchar(4) as fetype,
      NULL::varchar(2) as fedirs,
      coalesce(zip.city) as place,
      sl.abbrev as state,
      parsed.zip as zip,
      centroid(wkb_geometry) as address_geom,
      100::integer as rating
  FROM
    zip_lookup_base zip
    JOIN state_lookup sl on (zip.state = sl.name)
    JOIN zt99_d00 zl ON (zip.zip = zl.zcta::integer)
  WHERE
    zip.zip = parsed.zip;

  RETURN result;
END;
$_$ LANGUAGE plpgsql;