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...
-
Change the order and use SetSRID once you have a geometry.user30184– user301842021年05月17日 09:46:57 +00:00Commented May 17, 2021 at 9:46
2 Answers 2
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
.
You can set the srid while constructing the geometry (https://postgis.net/docs/ST_GeometryFromText.html).
SELECT ST_GeometryFromText({fieldGeometry},4326)