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

依然范特西╮ 提交于 2019-12-13 04:45:34

问题


I have a PostgreSQL table with a column containing strings in the 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 so on...'

As you can see the column contains coordinates in different lengths as strings in one line. I would like to use the 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 now appears:

The PostGIS functions requires double precision values in the right format as shown above. How can I generate double precision values from strings when I don't know how long the strings will be and how many characters each coordinate has?

Thanks for your help in advance!

Jann


回答1:


According to the docs, ST_GeomFromText requires (text, integer), not (double precision).

All you need to do is CONCAT() and it should work.

Solution

ST_GeomFromText(CONCAT('LINESTRING(', "YourTable"."YourString", ')'), 4326);

For your 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)
FROM
    (
        SELECT '3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309, 4.5632 58.32423' AS "C1"
    ) AS "T1";

Just for fun

I decided to convert your string into the originally requested DOUBLE PRECISION and then back into a string... the end result is the same as above, but it does it with a lot more work. Nevertheless, it is what you asked for.

SELECT
    ST_GeomFromText(CONCAT('LINESTRING(', (SELECT STRING_AGG(ARRAY_TO_STRING("Line"."Points", ' '), ',') FROM REGEXP_MATCHES(ARRAY_TO_STRING(REGEXP_SPLIT_TO_ARRAY("T1"."C1", E', | ')::DOUBLE PRECISION[], ' '), '(\d*\.\d*) (\d*\.\d*)', 'g') AS "Line"("Points")), ')'), 4326)
FROM
    (
        SELECT '3.985 58.934, 4.56 61.2323, 5.4 63.234355, 3.25982 59.309, 4.5632 58.32423'::TEXT AS "C1"
    ) AS "T1";


来源:https://stackoverflow.com/questions/19220875/obtain-double-precision-values-from-inconsitent-strings-for-using-st-geomfromtex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!