0

maybe someone is able to point me to the correct solution for the following problem:

I have a table with coordinates as strings like for example '9.94 54.839' (for some other purposes I need to keep the coordinate values in one column). For the PostGIS function ST_GeomFromText I need them as double precision values.

Example: ST_GeomFromText('POINT(9.94 54.839)',4326)

Another problem is that the coordinate length varies (example: '9.873684 54.63')

9
  • Look at regexp_split_... functions. Commented Oct 4, 2013 at 14:43
  • @PM 77-1 Thanks for the help. With the above command I'm able to split the coordinates but the output is a string again and I'm not able to cast it to get a double precision value... Commented Oct 4, 2013 at 15:04
  • Why not? This one works fine: SELECT split_part('9.873684 54.63',' ',2)::double precision; Commented Oct 4, 2013 at 15:17
  • @TomaszMyrta This statement works for me too but if I try to use SELECT split_part(coordinate,' ',1)::double precision FROM points; I get the ERROR: invalid input syntax for type double precision: "" Commented Oct 4, 2013 at 15:28
  • It won't work for empty or broken strings. You must avoid them in where clause. Commented Oct 4, 2013 at 15:32

4 Answers 4

1

As the function name suggests, ST_GeomFromText requires text—more specifically, well-known text. Picking out double precision values is unnecessary. You just need to glue togther the pieces with a concatenation operator ||:

SELECT ST_GeomFromText('POINT(' || meta_string_value || ')', 4326) AS geom
FROM (
    SELECT '9.94 54.839'::text AS meta_string_value
    UNION ALL SELECT '9.873684 54.63'::text AS meta_string_value
) AS t1;
Sign up to request clarification or add additional context in comments.

Comments

1

I answered your other (extremely similar) question: Obtain double precision values from inconsitent strings for using ST_GeomFromText (PostGIS) and the answer is the same.

Just to Recap

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);

Also, if you have several values (points) that need to be connected to form a line (or polygon) take a look at STRING_AGG(text, text).

Comments

0

Assumes a string "pos" of latitude longitude(lat lon). Split the string and cast it to float, use strip to remove any unwanted whitespaces.

tmp = pos.split()
lat = float(tmp[0].strip())
lon = float(tmp[1].strip())

You could then do something like:

latlonString = 'POINT(%f %f)' % (lat,lon)

Using the variable in a query:

ST_GeomFromText(latlonString,4326)

For example:

SELECT id
FROM features
WHERE pos = ST_GeomFromText(latlonString,4326)

For a string of multiple coordinate pairs; xyString = "x1 y1 x2 y2" NOTE! not separated by comma:

xyString = '3.584731 60.739211 3.590472 60.738030 3.592740 60.736220'
xy = xyString.split()
tuples  = [i+' '+j for i,j in zip(xy[::2],xy[1::2])]
multiPointString = ','.join(map(str,tuples))
polygonString = 'POLYGON((%s))' % (multiPointString)
print polygonString

print:

POLYGON((3.584731 60.739211,3.590472 60.738030,3.592740 60.736220))

If you have a polygon string like this: "x1 y1, x2 y2, ....", just edit the line:

xy = xyString.replace(",", " ").split()

This should be sufficient if you use psycopg2 as postgresql adapter. Maybe it doesn't answer all your questions, but hope this helps.

Comments

-1

With your hints I was able to get a point coordinate with the following syntax:

    SELECT ST_GeomFromText(((('POINT('|| split_part(t1.meta_string_value, ' ', 1)::double
    precision) || ' ') || split_part(t1.meta_string_value, ' ', 2)::double precision) ||
    ')', 4326) AS geom FROM points t1

Now I have a new problem. In the case above only two double precision values (LON, LAT) are needed to create a geometry out of the coordinates. What can I do if I would like to create a line or a polygon out of a list of coordinates like for example:

    9.873684 54.63, 9.8432 54.8792, 9.032 54.32, 8.9 54.987

The proble is that I don't know how much coordinates a line or polygon will have...

Any ideas about it?

2 Comments

Take a look at the geometry constructors, or ask a new question.
These are not double precision values... they are text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.