I have a table business_locations
with a postGIS location column and I try to fetch the rows that are within a bounding box (function arguments).
I am trying to run this function in Hasura:
CREATE OR REPLACE FUNCTION search_businesses_near_project(lngW integer, latS integer, lngE integer, latN integer)
RETURNS SETOF business_locations AS $$
SELECT A.location, A.route, A.locality, A.administrative_area_level_1, A.administrative_area_level_2, A.country, A.business_id
FROM business_locations A where ST_Intersects(A.location::geography, ST_MakeEnvelope(lngW, latS, lngE, latN, 4326)::geography, 4326)
$$ LANGUAGE sql STABLE;
The error returned:
{
"internal": {
"statement": "CREATE OR REPLACE FUNCTION search_businesses_near_project(project_location geography, lngW integer, latS integer, lngE integer, latN integer)\r\nRETURNS SETOF business_locations AS $$\r\n SELECT A.location, A.route, A.locality, A.administrative_area_level_1, A.administrative_area_level_2, A.country, A.business_id\r\n FROM business_locations A where ST_Intersects(project_location, ST_MakeEnvelope(lngW, latS, lngE, latN, 4326)::geography, 4326)\r\n$$ LANGUAGE sql STABLE;",
"prepared": false,
"error": {
"exec_status": "FatalError",
"hint": "No function matches the given name and argument types. You might need to add explicit type casts.",
"message": "function st_intersects(geography, geography, integer) does not exist",
"status_code": "42883",
"description": null
},
"arguments": []
},
"path": "$.args[0].args",
"error": "query execution failed",
"code": "postgres-error"
}
st_intersects
takes only two arguments. Omit the superfluous SRID:
CREATE OR REPLACE FUNCTION search_businesses_near_project(
lngW integer,
latS integer,
lngE integer,
latN integer
) RETURNS SETOF business_locations AS $$
SELECT *
FROM business_locations A
WHERE st_intersects(
A.location::geography,
ST_MakeEnvelope(lngW, latS, lngE, latN, 4326)::geography
)
$$ LANGUAGE sql STABLE;