CREATE OR REPLACE FUNCTION geocode_location( parsed NORM_ADDY, restrict_geom @extschema:postgis@.geometry DEFAULT null, OUT ADDY NORM_ADDY, OUT GEOMOUT @extschema:postgis@.GEOMETRY, OUT RATING INTEGER ) RETURNS SETOF RECORD AS $_$ DECLARE result RECORD; in_statefp VARCHAR; stmt VARCHAR; var_debug boolean := false; BEGIN in_statefp := statefp FROM tiger.state WHERE state.stusps = parsed.stateAbbrev; IF var_debug THEN RAISE NOTICE 'geocode_location starting: %', clock_timestamp(); END IF; FOR result IN SELECT coalesce(zip.city)::varchar as place, zip.zip as zip, @extschema:postgis@.ST_Centroid(tiger.zcta5.the_geom) as address_geom, stusps as state, 100::integer + coalesce( tiger.levenshtein_ignore_case(coalesce(zip.city), parsed.location),0) as in_rating FROM tiger.zip_lookup_base zip JOIN tiger.zcta5 ON (zip.zip = tiger.zcta5.zcta5ce AND zip.statefp = tiger.zcta5.statefp) JOIN tiger.state ON (state.statefp=zip.statefp) WHERE parsed.zip = zip.zip OR (@extschema:fuzzystrmatch@.soundex(zip.city) = @extschema:fuzzystrmatch@.soundex(parsed.location) and zip.statefp = in_statefp) ORDER BY tiger.levenshtein_ignore_case(coalesce(zip.city), parsed.location), zip.zip LOOP ADDY.location := result.place; ADDY.stateAbbrev := result.state; ADDY.zip := result.zip; ADDY.parsed := true; GEOMOUT := result.address_geom; RATING := result.in_rating; RETURN NEXT; IF RATING = 100 THEN RETURN; END IF; END LOOP; IF parsed.location IS NULL THEN parsed.location := city FROM tiger.zip_lookup_base WHERE tiger.zip_lookup_base.zip = parsed.zip ORDER BY tiger.zip_lookup_base.zip LIMIT 1; in_statefp := statefp FROM tiger.zip_lookup_base WHERE tiger.zip_lookup_base.zip = parsed.zip ORDER BY tiger.zip_lookup_base.zip LIMIT 1; END IF; stmt := 'SELECT ' || ' pl.name as place, ' || ' state.stusps as stateAbbrev, ' || ' @extschema:postgis@.ST_Centroid(pl.the_geom) as address_geom, ' || ' 100::integer + tiger.levenshtein_ignore_case(coalesce(pl.name), ' || quote_literal(coalesce(parsed.location,'')) || ') as in_rating ' || ' FROM (SELECT * FROM tiger.place WHERE statefp = ' || quote_literal(coalesce(in_statefp,'')) || ' ' || COALESCE(' AND @extschema:postgis@.ST_Intersects(' || quote_literal(restrict_geom::text) || '::@extschema:postgis@.geometry, the_geom)', '') || ') AS pl ' || ' INNER JOIN tiger.state ON(pl.statefp = state.statefp)' || ' WHERE @extschema:fuzzystrmatch@.soundex(pl.name) = @extschema:fuzzystrmatch@.soundex(' || quote_literal(coalesce(parsed.location,'')) || ') and pl.statefp = ' || quote_literal(COALESCE(in_statefp,'')) || ' ORDER BY tiger.levenshtein_ignore_case(coalesce(pl.name), ' || quote_literal(coalesce(parsed.location,'')) || ');' ; IF var_debug THEN RAISE NOTICE 'geocode_location stmt: %', stmt; END IF; FOR result IN EXECUTE stmt LOOP ADDY.location := result.place; ADDY.stateAbbrev := result.stateAbbrev; ADDY.zip = parsed.zip; ADDY.parsed := true; GEOMOUT := result.address_geom; RATING := result.in_rating; RETURN NEXT; IF RATING = 100 THEN RETURN; IF var_debug THEN RAISE NOTICE 'geocode_location ending hit 100 rating result: %', clock_timestamp(); END IF; END IF; END LOOP; IF var_debug THEN RAISE NOTICE 'geocode_location ending: %', clock_timestamp(); END IF; RETURN; END; $_$ LANGUAGE plpgsql STABLE COST 20 ROWS 5 PARALLEL SAFE;