我已经创建了这两个函数,我想知道是否有更好的方法将 json 数组转换为多边形,而无需安装 PostGIS 扩展。
原始类型:
((-34.888733,-57.956764),(-34.92303,-57.99367),(-34.953579,-57.95255))
多边形到json:
["-34.888733,-57.956764","-34.92303,-57.99367","-34.953579,-57.95255"]
json_到_多边形:
((-34.888733,-57.956764),(-34.92303,-57.99367),(-34.953579,-57.95255))
create or replace function json_to_polygon(p_poly json, out p_out polygon) returns polygon
parallel safe
returns null on null input
immutable
language plpgsql
as $$
begin
if(p_poly is null) then
return;
end if;
select
polygon(concat('((', string_agg(x, '),('), '))'))
from
json_array_elements_text(p_poly) x
into
p_out;
end;
$$;
create or replace function polygon_to_json(p_poly polygon) returns json
parallel safe
returns null on null input
immutable
language plpgsql
as $$
begin
if(p_poly is null) then
return null;
end if;
return to_json(string_to_array(replace(replace(p_poly::text, '((', ''), '))', ''), '),('));
end;
$$;