0

I am trying to make a query using ST_GeometryFromText. When I run the code I recive the error posted below

fieldGeometry is an object in WKT format

How can I make this query correctly?

Code:

def executeWithFetchallST_GeometryFromText(self, cursor, fieldGeometry):
cursor.execute("SELECT ST_GeometryFromText(ST_SetSRID({fieldGeometry},4326));".format(fieldGeometry=fieldGeometry)) 
return cursor.fetchall()

Error:

cursor.execute("SELECT ST_GeometryFromText(ST_SetSRID({fieldGeometry},4326));".format(fieldGeometry=fieldGeometry))
psycopg2.errors.SyntaxError: FEHLER: Syntaxfehler bei »51.1142127990963«
LINE 1: ...tryFromText(ST_SetSRID(POLYGON ((6.73622172276192 51.1142127...
 
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked May 17, 2021 at 8:38
1
  • Change the order and use SetSRID once you have a geometry. Commented May 17, 2021 at 9:46

2 Answers 2

1

Instead of

def executeWithFetchallST_GeometryFromText(self, cursor, fieldGeometry):
 cursor.execute("SELECT ST_GeometryFromText(ST_SetSRID({fieldGeometry},4326));".format(fieldGeometry=fieldGeometry)) 
 return cursor.fetchall()

do

def executeWithFetchallST_GeometryFromText(self, cursor, fieldGeometry):
 cursor.execute("SELECT ST_GeometryFromText('{fieldGeometry}', 4326);".format(fieldGeometry=fieldGeometry)) 
 return cursor.fetchall()

ST_GeometryFromText expects a string with wrapping single quotes around and you can directly assign an SRID in ST_GeometryFromText.

answered May 17, 2021 at 11:34
0

You can set the srid while constructing the geometry (https://postgis.net/docs/ST_GeometryFromText.html).

SELECT ST_GeometryFromText({fieldGeometry},4326)
answered May 17, 2021 at 9:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.