I want to create a function to calculate the distance and sort between user and geolayer. I do the following:
CREATE OR REPLACE FUNCTION search_geolayer_near_user(location text)
RETURNS TABLE(id int, layer_name text, gps_to_center_layer numeric, gps_belong_province boolean) AS $func$
SELECT gl.id, gl.layer_name, ST_Distance(
ST_AsText(ST_Centroid(ST_GeometryFromText(ST_AsText(ST_MakeEnvelope(gl.minx, gl.miny, gl.maxx, gl.maxy, 4326))))),
location
) AS gps_to_center_layer,
ST_AsText(ST_Centroid(ST_GeometryFromText(ST_AsText(ST_MakeEnvelope(r.minx, r.miny, r.maxx, r.maxy, 4326))))),
ST_contains(
ST_GeometryFromText(ST_AsText(ST_MakeEnvelope(r.minx, r.miny, r.maxx, r.maxy, 4326))),
ST_GeometryFromText(location))
AS gps_belong_province
--
FROM "GeoLayers" gl JOIN "Regions" r ON gl.province_id = r.id
WHERE gl.deleted_flg = false
GROUP BY gl.id , gl.layer_name, r.minx, r.miny, r.maxx, r.maxy, gl.minx, gl.miny, gl.maxx, gl.maxy
ORDER BY gps_belong_province DESC, gps_to_center_layer ASC
$func$ LANGUAGE sql STABLE;
But error
"SQL Execution Failed postgres-error : return type mismatch in function declared to return record"
The normal query would look like this:
select gl.id , gl.layer_name, ST_Distance(
ST_AsText(ST_Centroid(ST_GeometryFromText(ST_AsText(ST_MakeEnvelope(gl.minx, gl.miny, gl.maxx, gl.maxy, 4326))))),
'POINT(109.189083 12.272397)'
) as "GPS to center Layer",
ST_AsText(ST_Centroid(ST_GeometryFromText(ST_AsText(ST_MakeEnvelope(r.minx, r.miny, r.maxx, r.maxy, 4326))))),
ST_contains(
ST_GeometryFromText(ST_AsText(ST_MakeEnvelope(r.minx, r.miny, r.maxx, r.maxy, 4326))),
ST_GeometryFromText('POINT(109.189083 12.272397)'))
as "GPS belong Province"
--
from "GeoLayers" gl join "Regions" r ON gl.province_id = r.id
where gl.deleted_flg = false
group by gl.id , gl.layer_name, r.minx, r.miny, r.maxx, r.maxy, gl.minx, gl.miny, gl.maxx, gl.maxy
-- having st_contains = false
order by "GPS belong Province" desc, "GPS to center Layer" asc
-- limit 10
;
Data return:
id layer_name GPS to center Layer st_astext GPS belong Province
"539" Bản đồ địa chính VIP - Phường Vĩnh Phước, Thành phố Nha Trang, Tỉnh Khánh Hòa" "0.009276001515402381" "POINT(112.2530484505 10.3798475845001)" "t"