I tried to select the id
from the steps
table having their location within 1km from (x,y) some PostGis with Go using the pgx library.
I've tested the following code which returns an error: parse error - invalid geometry (SQLSTATE XX000)
func search(lat float32, lng float32) return (string, error){
searchQuery = "SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT($1 $2)'::geography, location) < 1000"
// GetSession returns *pgxpool.Pool
rows, err := postgres.GetSession().Query(context.Background(), searchQuery,
lat,
lng)
if err != nil {
// ERROR: parse error - invalid geometry (SQLSTATE XX000)
return nil, err
}
defer rows.Close()
...
}
Then, I've just changed the query using ST_SetSRID & ST_MakePoint
searchQuery = "SELECT DISTINCT(id) FROM steps WHERE ST_Distance(ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography, location) < 1000"
... but ...
1) I still don't know why my fist version of the query
"SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT($1 $2)'::geography, location) < 1000"
returns a geometry error whereas it's working when I test it straight into pgadmin replacing $1 and $2 with random coordinate values like
"SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT(0.44 3.40)'::geography, location) < 1000"