[GIS] How to cast geom to geog in PostGis to find points within a given distance

postgis

I have a Geo table with geometry column storing point data SRD=4326

I need to find all points within a given distance in meters of a Longitude, Latitude

Do I create a geography column from the geometry column or the Longitude, Latitude stored in the table

or

Do the conversion on the fly within the query

Could someone suggest how to perform both options and there pro & cons?

Best Answer

You can do it both ways, storing geography or storing geometry and convert on the fly. If you create a geom column not based on SRID 4326, you can convert it to a geography type before the computation as follows (using the ST_DWithin function)

SELECT geom FROM my_points WHERE ST_DWithin(geom::geography, my_latlon);

Of course, this second approach is slower in use because of the need to cast geom to geography type.