Pergunta

I am having issue with postgis geometry.

Following is my query

SELECT ST_GeomFromText(trim(data))
FROM (SELECT replace(concat(concat('''POINT',concat(trim(TRAILING ')' FROM concat('(',trim(LEADING '(' FROM ea.region::TEXT)::TEXT) ),')')),''''),',',' ') as data from "erpAssets" as ea where ea.id=326816 )as foo;

It is giving error which is as follows: SQL error:

ERROR: parse error - invalid geometry HINT: "'P" <-- parse error at position 2 within geometry In statement: SELECT ST_GeomFromText(trim(data)) FROM (SELECT replace(concat(concat('''POINT',concat(trim(TRAILING ')' FROM concat('(',trim(LEADING '(' FROM ea.region::TEXT)::TEXT) ),')')),''''),',',' ') as data from "erpAssets" as ea where ea.id=326816 )as foo;

When i run simple inner select query it gives me point data when i copy this data and put it in st_GeomFromText() then it is running f9. but when i use above query then it is giving me parse error

Foi útil?

Solução

Based on your previous question, ea.region appears to be text that looks like:

((-79.4609576808001,43.9726680183837))

The parse error or your processed data is obvious if you look at the value of your attempt:

'POINT(-79.4609576808001 43.9726680183837)'

which is invalid because it has single quotes on each side. This is the same as attempting:

select ST_GeomFromText($$'POINT(-79.4609576808001 43.9726680183837)'$$);

Furthermore, the query can be simplified by using || instead of concat, and just using substr if you know to only trim off the first and last characters:

SELECT
  region,
  'POINT' || replace(substr(region, 2, length(region) - 2), ',', ' ') AS correct,
  ST_GeomFromText('POINT' ||
    replace(substr(region, 2, length(region) - 2), ',', ' '), 4326) AS geom
FROM
  (SELECT 326816 AS id,
    '((-79.4609576808001,43.9726680183837))'::text AS region) AS ea
WHERE ea.id=326816;

-[ RECORD 1 ]-----------------------------------------------
region  | ((-79.4609576808001,43.9726680183837))
correct | POINT(-79.4609576808001 43.9726680183837)
geom    | 0101000020E610000018F8A45480DD53C05C69B86280FC4540
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top