<funcsynopsis>
<funcprototype>
<funcdef>setof record <function>geocode</function></funcdef>
- <paramdef><type>address </type> <parameter>varchar</parameter></paramdef>
+ <paramdef><type>varchar </type> <parameter>address</parameter></paramdef>
+ <paramdef><type choice='opt'>integer </type> <parameter>max_results=10</parameter></paramdef>
<paramdef><type>norm_addy </type> <parameter>OUT addy</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>OUT geomout</parameter></paramdef>
<paramdef><type>integer </type> <parameter>OUT rating</parameter></paramdef>
<funcprototype>
<funcdef>setof record <function>geocode</function></funcdef>
<paramdef><type>norm_addy </type> <parameter>in_addy</parameter></paramdef>
+ <paramdef><type choice='opt'>integer </type> <parameter>max_results=10</parameter></paramdef>
<paramdef><type>norm_addy </type> <parameter>OUT addy</parameter></paramdef>
<paramdef><type>geometry </type> <parameter>OUT geomout</parameter></paramdef>
<paramdef><type>integer </type> <parameter>OUT rating</parameter></paramdef>
<para>Takes in an address as a string (or already normalized address) and outputs a set of possible locations which include a point geometry in NAD 83 long lat, a <varname>normalized_address</varname> (addy) for each, and the rating. The lower the rating the more likely the match.
Results are sorted by lowest rating first. Uses Tiger data (edges,faces,addr), PostgreSQL fuzzy string matching (soundex,levenshtein) and PostGIS line interpolation functions to interpolate address along the Tiger edges. The higher the rating the less likely the geocode is right.</para>
- <para>Enhanced: 2.0.0 to support Tiger 2010 structured data and revised some logic to improve speed.</para>
-
-
+ <para>Enhanced: 2.0.0 to support Tiger 2010 structured data and revised some logic to improve speed and accuracy of geocoding. New parameter max_results useful for specifying ot just return the best result.</para>
+
</refsection>
70 | POINT(-71.0646 42.35105) | 31 | Stuart | St | Boston | MA | 02116
(11 rows) </programlisting>
-<para>Using to do a batch geocode of addresses. USE DISTINCT ON to return the best address match (lowest rating), when more than one match is returned. Only process those not yet geocoded (have no rating).</para>
+<para>Using to do a batch geocode of addresses. Easiest is to set <varname>max_results=1</varname>. Only process those not yet geocoded (have no rating).</para>
<programlisting>CREATE TABLE addresses_to_geocode(addid serial PRIMARY KEY, address text,
lon numeric, lat numeric, new_address text, rating integer);
('124 Mount Auburn St, Cambridge, Massachusetts 02138'),
('950 Main Street, Worcester, MA 01610');
--- only update the first two addresses (850 ms) --
+-- only update the first two addresses (323-704 ms - there are caching and shared memory effects so first geocode you do is always slower) --
-- for large numbers of addresses you don't want to update all at once
-- since the whole geocode must commit at once
UPDATE addresses_to_geocode
SET (rating, new_address, lon, lat)
- = (g.rating, pprint_addy(g.addy),
- ST_X(g.geomout)::numeric(8,5), ST_Y(g.geomout)::numeric(8,5) )
-FROM (SELECT DISTINCT ON (addid) addid, (g1.geo).*
- FROM (SELECT addid, (geocode(address)) As geo
-FROM addresses_to_geocode As ag
- WHERE ag.rating IS NULL ) As g1
-ORDER BY addid, rating LIMIT 2) As g
+ = ( (g.geo).rating, pprint_addy((g.geo).addy),
+ ST_X((g.geo).geomout)::numeric(8,5), ST_Y((g.geo).geomout)::numeric(8,5) )
+FROM (SELECT addid, (geocode(address,1)) As geo
+ FROM addresses_to_geocode As ag
+ WHERE ag.rating IS NULL ORDER BY addid LIMIT 5) As g
WHERE g.addid = addresses_to_geocode.addid;
result
-----
-2 rows affected, 850 ms execution time.
+5 rows affected, 345 ms execution time.
SELECT * FROM addresses_to_geocode WHERE rating is not null;
-
- addid | address | lon | lat | new_address | rating
- ------+----------------------------------------------+-----------+----------+-------------------------------------------+--------
- 1 | 529 Main Street, Boston MA, 02129 | -71.07187 | 42.38351 | 529 Main St, Boston, MA 02129 | 0
- 2 | 77 Massachusetts Avenue, Cambridge, MA 02139 | -71.09436 | 42.35981 | 77 Massachusetts Ave, Cambridge, MA 02139 | 0
+ addid | address | lon | lat | new_address | rating
+-------+-----------------------------------------------------+-----------+----------+-------------------------------------------+--------
+ 1 | 529 Main Street, Boston MA, 02129 | -71.07187 | 42.38351 | 529 Main St, Boston, MA 02129 | 0
+ 2 | 77 Massachusetts Avenue, Cambridge, MA 02139 | -71.09436 | 42.35981 | 77 Massachusetts Ave, Cambridge, MA 02139 | 0
+ 3 | 28 Capen Street, Medford, MA | -71.12370 | 42.41108 | 28 Capen St, Medford, MA 02155 | 0
+ 4 | 124 Mount Auburn St, Cambridge, Massachusetts 02138 | -71.12298 | 42.37336 | 124 Mount Auburn St, Cambridge, MA 02138 | 0
+ 5 | 950 Main Street, Worcester, MA 01610 | -71.82361 | 42.24948 | 950 Main St, Worcester, MA 01610 | 0
</programlisting>
</refsection>