[GIS] SQL Query to Reverse Geocoding in PostGIS

geocodingpostgisreverse-geocodingshapefilespatialite

I try to do Reverse Geocode from my shapefile on the database.
I'd already try with Spatialite and PostGIS to put shapefile (.shp) into it. But I'm still can't delivered my objection about reverse geocode from my shapefile on the database.
My shapefile (.shp) is content with district polygon, all I want is simple: Pass some coordinate, and check that coordinate is in what district polygon from shapefile, then return the district name. That's all.
Does anybody know the SQL Query to do that reverse geocode in PostGIS or Spatialite?

Thanks before. 🙁

Best Answer

Suppose your district table looks like this

districts(name text, the_geom geometry)

Then you would select the district a point falls into using

SELECT name 
FROM districts
WHERE ST_Within(ST_SetSRID(ST_MakePoint(lon,lat),4326),
                the_geom);

Replace lon and lat with your values.

If your districts are in a different projection than WGS84, use ST_Transform.