Search code examples
postgresqlgeospatialpostgisspatial-indexspatial-query

postgresql query with ST_GeomFromText not working


I am trying to execute the Postgres query:

SELECT a.a_geom
FROM a
WHERE 
ST_within(a.a_geom::geometry,ST_GeomFromText('Polygon((1,1),(4,1),(4,4),(4,1))'));

but this query gives error :

ERROR: parse error - invalid geometry
SQL state: XX000
Hint: "Polygon((1,1" <-- parse error at position 12 within geometry


Solution

  • Coordinates are space separated, points are comma separated, and the whole list of points is included in parens.

    Separately from the syntax issues, your polygon needs to be closed, i.e. you need to repeat the starting point and the end of the list of points.

    This generates your poly:

    select ST_GeomFromText('Polygon((1 1,4 1,4 4,4 1,1 1))')