Search code examples
sqlpostgresqlpostgis

PostgreSQL/PostGIS unable to search inside a rectangle polygon


I want to be able to query points inside a rectangle surface efficiently. Unfortunately it doesn't seem to work.

eykache=# SELECT * FROM colonies;
 id |                  location                  
----+--------------------------------------------
  1 | 010100000000000000000000000000000000000000
  2 | 010100000000000000000050400000000000005040
  3 | 010100000000000000000050400000000000000000
  4 | 0101000000000000000000504000000000000050C0
  5 | 0101000000000000000000000000000000000050C0
  6 | 010100000000000000000050C000000000000050C0
  7 | 010100000000000000000050C00000000000000000
(7 rows)

eykache=# SELECT id FROM colonies WHERE ST_Contains(colonies.location, ST_MakeEnvelope(-64, -64, 64, 64));
 id 
----
(0 rows)

As you can see the second response is empty.


Solution

  • I just had to reverse the arguments within ST_Contains:

    SELECT id FROM colonies WHERE ST_Contains(ST_MakeEnvelope(-64, -64, 64, 64), colonies.location);