Monday, 10 September 2018

postgis - Return all results within a 30km radius of a specific lat/long point?


I have a table with a column the_geom which contains data similar to:


0103000020E61000000100000005000000CE473AACFA071E40F27FB23340744740336FE841C6231E40873BED903F744740FC150A0ACE231E40D19E2684637647409C9B443D00081E409A9AF82664764740CE473AACFA071E40F27FB23340744740

Which when applying the function ST_AsEWKT(the_geom) returns:


SRID=4326;POLYGON((7.5077921782085 46.9082092877942,7.53493597966353 46.9081898840296,7.53496566473541 46.9249119938446,7.50781341296434 46.9249314035307,7.5077921782085 46.9082092877942))

I need to select all data that is within a 30km radius of a specific lat/long point, for example:




  • lat = 46.8167

  • lng = 6.9333


However whenever I tried to use ST_Distance(), I always received values less than 1, and using ST_DWithin() always returned true.



Answer



Please Check out the following query for PostgreSQL to get data within certain distance. I hope it will help.


SELECT *
FROM your_table
WHERE ST_Distance_Sphere(the_geom, ST_MakePoint(your_lon,your_lat)) <= radius_mi * 1609.34

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...