I have to convert from lat and long to geom
to use PostGIS. My problem, I have various tables from different locations and I want to pass the table as a parameter t
AS @dezso mentioned, you'll need dynamic SQL in this case.
EXECUTE
So, you're on the right track; forming a dynamic SQL statement using PL/pgSQL, but you just need the finishing touches:
CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, _table text)
RETURNS integer AS $$
BEGIN
RETURN QUERY EXECUTE format('SELECT id FROM %I AS vertices
ORDER BY vertices.geom <->ST_SetSrid(ST_MakePoint(long,lat),4326) LIMIT 1;',_table);
END
$$ LANGUAGE plpgsql;
I believe this should solve your issues.
Note: We've discovered an error with the above solution and using SETOF
, I've attempted to correct the issues below.
A few edits here, hopefully one solution will fix your issue. Also, please excuse any syntax errors in my previous & current solutions; I don't have time to test them right now. :(
1) You could just try returning a SETOF
integers, knowing that of course you'll only return the one. Your return type in this case will then be a single, one-column row containing an integer.
CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, _table text)
RETURNS SETOF integer AS $$
BEGIN
RETURN QUERY EXECUTE format('SELECT id FROM %I AS vertices
ORDER BY vertices.geom <->ST_SetSrid(ST_MakePoint(long,lat),4326) LIMIT 1;',_table);
END
$$ LANGUAGE plpgsql;
and then call as:
SELECT * FROM convert_from_lon_lat(...);
2) To specifically return an integer, I think you can try this:
CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, _table text)
RETURNS integer AS $$
DECLARE
return_id integer;
BEGIN
EXECUTE format('SELECT id FROM %I AS vertices
ORDER BY vertices.geom <->ST_SetSrid(ST_MakePoint(long,lat),4326) LIMIT 1;',_table)
INTO return_id;
RETURN return_id;
END
$$ LANGUAGE plpgsql;