What is the best way to do reverse geocoding with PostGIS?

Finally, I understand the way to do geocode and reverse geocode with my PostGIS database. I made a stored procedure that finds geometry types that are near a point. This stored procedure uses the distance function to get the nearest points. After that, I've created a RESTful API that calls this stored procedure to resolve the reverse geocoding requests.

Correct me if I'm wrong, but I've understood that Nominatim project creates an API for databases that have been imported from OSM. So you don't have to create any stored procedure for reverse geocoding and geocoding. Also you don't have to make any webservice to resolve the requests through the web.

Hope this helps someone.

CREATE OR REPLACE FUNCTION reverse_geocode (lat double precision, lon double precision)
RETURNS text AS 
$BODY$ 
declare  point geometry;  rec record;  geocode text; 
begin  
    point := geomfromtext('POINT('||lat||' '||lon||')', 4326);  
    select name, distance(way, point) as dist  
    into rec  from mapserverdb_point  
    order by dist asc limit 1;  
    geocode := rec.name;  
    return geocode; 
end; 
$BODY$   
LANGUAGE plpgsql; 

PostGIS 2.0 tiger geocoder has a reverse geocoder utilizing Tiger data. The geocoder will install fine on PostGIS 1.5 8.4+ or higher.

If you are in US, might be the easiest to use since it's all packaged as plpgsql functions.

http://www.postgis.org/documentation/manual-svn/Extras.html#Tiger_Geocoder http://www.postgis.org/documentation/manual-svn/Reverse_Geocode.html