Postgres zipcode - listings for same mile radius not showing up

85 Views Asked by At

Using Postgres and extensions cube and earthdistance I do this query for zipcode 78705 with 50 miles radius range:

  WITH query1 AS(SELECT * FROM listings
    WHERE earth_box(ll_to_earth(30.2961708, -97.73895429999999), 80467.2)
    @> ll_to_earth(listings.lat, listings.lng)

and I get back result foo listing, which is great.

If I do a query for zipcode 78232 with a 50 miles radius range:

 WITH query1 AS(SELECT * FROM listings
    WHERE earth_box(ll_to_earth(29.59442259999999, -98.4568128), 80467.2)
    @> ll_to_earth(listings.lat, listings.lng)

I get back the expected results, including the listing foo.

My question is.... if foo(which is zipcode 78705) is returned for a 50 mile radius of 78232 with other listings that are 78232, why aren't all the listings from zipcode 78232 being included in a 50 mile radius of 78705?

1

There are 1 best solutions below

0
JGH On

The two 50 miles buffer partially overlap each others. Entries located in the common area - such as Foo in your example - are within 50 miles of the two post codes.Elsewhere, entries are less than 50 miles from one postcode but are further away than 50 miles of the other postcode.

In the image below, only the green area is within 50 miles of both postcodes, so entries located in this area will be returned by both queries.

enter image description here