Search code examples
sqlpostgresqlpostgis

Why is PostGIS stacking all points on top of each other? (Using ST_DWithin to find all results within 1000m radius)


New to PostGIS/PostgreSQL...any help would be greatly appreciated!

I have two tables in a postgres db aliased as gas and ev. I'm trying to choose a specific gas station (gas.site_id=11949) and locate all EV/alternative fuel charging stations within a 1000m radius. When I run the following though, PostGIS returns a number of ev stations that are all stacked on top of each other in the map (see screenshot).

Anyone have any idea why this is happening? How can I get PostGIS to visualize the points within a 1000m radius of the specified gas station?

with myplace as (
  SELECT gas.geom 
  from nj_gas gas 
  where gas.site_id = 11949 limit 1)
select myplace.*, ev.* 
from alt_fuel ev, myplace 
where ST_DWithin(ev.geom1, myplace.geom, 1000)

enter image description here


Solution

  • Resolved by using:

    WITH
      myplace as (
        SELECT geom as g
        FROM   nj_gas  
        WHERE  site_id = 11949 OR site_id = 11099 OR site_id = 11679 or site_id = 480522
      ), myresults AS (
    SELECT ev.* 
    FROM   alt_fuel AS ev
    JOIN   myplace AS mp
      ON   ST_DWithin(ev.geom, mp.g, 0.1))
    select * from myresults```
    
    Thanks so much for your help @ThingumaBob and @JimJones ! Greatly appreciate it.