处理具有点几何,属性的表并转换为GeoJSON

时间:2019-03-04 08:33:23

标签: postgresql gis postgis geojson

我有一张包含点几何及其属性的表格。 该属性以jsonb的形式存储

{
  "id": number,
  "material": [[material_id, in_kgs, quantity]]
}

例如:

{
  "id": 1,
  "material": [[101, false, 2],[102, true, 2],[103, false, 1]]
}

我的任务是编写一个查询该表并将其转换为以下形式的函数:

{
  "id": 1,
  "material": [[101, true, 5],[102, true, 2],[103, true, 20]]
}

即,如果in_kgs为假,则将数量乘以转换因子,乘以表中存储的material_id。例如:101的转换因子为2.5,因此数量变为2 x 2.5 =5。类似地,对于103的转换因子,其数量变为1 x 20 = 20。

进行此操作后,必须将结果转换为GeoJSON。

以下是点表的示例:

create table public.geometry_point
(
  geometry_point_id       integer                                   not null,
  point_metadata          jsonb                                     not null,
  point_geometry          geometry(point)                           not null,
  constraint point_id_pk primary key (geometry_point_id)
);

alter table public.geometry_point
  owner to postgres;

insert into public.geometry_point
    (geometry_point_id, point_metadata, point_geometry)
values (1, 
      ('{"id":1,"material": [[101,false,2],[102,true,2],[103,false,10]]}')::jsonb, 
      ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));

对于转换表:

create table public.material_units_to_kgs_conversion
(
  conversion_id integer                                         not null,
  material_id integer                                           not null,
  conversion_factor float                                       not null,
  constraint conversion_id_pk primary key (conversion_id)
);

alter table public.material_units_to_kgs_conversion
  owner to postgres;

insert into public.material_units_to_kgs_conversion
    (conversion_id, material_id, conversion_factor)
values (1, 101, 2.5);

insert into public.material_units_to_kgs_conversion
    (conversion_id, material_id, conversion_factor)
values (2, 102, 10);

insert into public.material_units_to_kgs_conversion
    (conversion_id, material_id, conversion_factor)
values (3, 103, 20);

0 个答案:

没有答案