I have two array columns in Postgres named lat and long. Both have 1-5 values in their array field. Like the following
Now I need to create geometry from each value based on the array index of lat & long field and update an array geometry column. I have created the geometry from the fields by using the following query
Query:
select
st_SetSrid(st_MakePoint(long[1] :: double precision, lat[1] :: double precision), 4326),
st_SetSrid(st_MakePoint(long[2] :: double precision, lat[2] :: double precision), 4326),
st_SetSrid(st_MakePoint(long[3] :: double precision, lat[3] :: double precision), 4326),
st_SetSrid(st_MakePoint(long[4] :: double precision, lat[4] :: double precision), 4326),
st_SetSrid(st_MakePoint(long[5] :: double precision, lat[5] :: double precision), 4326)
from loc_table;
Output:
It creates separate columns but I need to update an array geometry column. I have tried using that select query first after so many failed attempts as following
Query:
select
string_to_array(
concat(
st_SetSrid(st_MakePoint(long[1] :: double precision, lat[1] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[2] :: double precision, lat[2] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[3] :: double precision, lat[3] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[4] :: double precision, lat[4] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[5] :: double precision, lat[5] :: double precision), 4326)), ',' ) as geom
from loc_table;
Then I tried with the update query with following
Query:
update loc_table
set geom_array = string_to_array(
concat(
st_SetSrid(st_MakePoint(long[1] :: double precision, lat[1] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[2] :: double precision, lat[2] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[3] :: double precision, lat[3] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[4] :: double precision, lat[4] :: double precision), 4326), ',' ,
st_SetSrid(st_MakePoint(long[5] :: double precision, lat[5] :: double precision), 4326)), ',' );
Output:
Note that, the geom_array field/column is the array geometry type column and I know that the query made the geometry into the string. But that’s the best I can able to do. And I have a large amount of data like 5-10 million. So, I have to use an efficient way to do so. Can anyone please help me out with this?
I am using PostGIS 2.5 and Postgres 11.5.