sql - Obtain double precision values from inconsitent strings for using ST_GeomFromText (PostGIS) -


i have postgresql table column containing strings in following format:

    '3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309'      '3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309, 4.5632 58.32423'     'and on...' 

as can see column contains coordinates in different lengths strings in one line. use postgis function:

    st_geomfromtext('linestring(3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982      59.309)', 4326)   

the following questions appears:

the postgis functions requires double precision values in right format shown above. how can generate double precision values strings when don't know how long strings , how many characters each coordinate has?

thanks in advance!

jann

according docs, st_geomfromtext requires (text, integer), not (double precision).

all need concat() , should work.

solution

st_geomfromtext(concat('linestring(', "yourtable"."yourstring", ')'), 4326); 

for testing

select     st_geomfromtext('linestring(3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309)', 4326),     concat('linestring(', "t1"."c1", ')'),     st_geomfromtext(concat('linestring(', "t1"."c1", ')'), 4326)     (         select '3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309, 4.5632 58.32423' "c1"     ) "t1"; 

just fun

i decided convert string requested double precision , string... end result same above, lot more work. nevertheless, asked for.

select     st_geomfromtext(concat('linestring(', (select string_agg(array_to_string("line"."points", ' '), ',') regexp_matches(array_to_string(regexp_split_to_array("t1"."c1", e', | ')::double precision[], ' '), '(\d*\.\d*) (\d*\.\d*)', 'g') "line"("points")), ')'), 4326)     (         select '3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309, 4.5632 58.32423'::text "c1"     ) "t1"; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -