So basically my problem is this. I have an sql statement which is obtained from a table in my database. This SQL statement hasd placeholders in it for prepared statement variables. I am using a wrapper class to take care of replacing the variables. The wrapper class deals with ensuring they are the correct type when passed through to PostgreSQL. I am getting syntax errors in my statement which are driving me mad. Part of the prepared statement is a string used in a ST_GeomFromText() function. This string has its values populated by way of prepared statement variables. Oddly the variables are not just being concatenated into the string but are being treated as individual entities by the looks of the errors I receive. See below for examples of queries I have tried and error messages.
Can anyone shed any light on this? It is driving me mad. I have to be able to build this geometry from text and the parameters must be able to be passed via a prepared statement. I have followed the trail in C# and it ends passing the values into my command object parameters as string so there is not conversion going on in the code that I am able to view that would make these be treated as anything but strings.
Statements (as returned from the table)
Number 1. With values
select st_xmin(a) minx, st_ymin(a) miny, st_xmax(a) maxx, st_ymax(a) maxy
from
ST_ENVELOPE(
ST_TRANSFORM(
ST_GeomFromText('POLYGON(('||138082||' '||12907||','||207179||' '||12907||','||207179||' '||88647||','||138082||' '||88647'||','||138082||' '||12907||'))',27700),4326))
as a
Number 1. Without values
select st_xmin(a) minx, st_ymin(a) miny, st_xmax(a) maxx, st_ymax(a) maxy
from
ST_ENVELOPE(
ST_TRANSFORM(
ST_GeomFromText('POLYGON(('||:0||' '||:1||','||:2||' '||:3||','||:4||' '||:5||','||:6||' '||:7'||','||:8||' '||:9||'))',27700),:10))
as a
Number 2. With values
select st_xmin(a) minx, st_ymin(a) miny, st_xmax(a) maxx, st_ymax(a) maxy
from
ST_ENVELOPE(
ST_TRANSFORM(
ST_GeomFromText($$POLYGON((138082 12907, 207179 12907, 207179 88647, 138082 88647, 138082 12907))$$,27700), 4326))
as a
Number 2. With values
select st_xmin(a) minx, st_ymin(a) miny, st_xmax(a) maxx, st_ymax(a) maxy
from
ST_ENVELOPE(
ST_TRANSFORM(
ST_GeomFromText($$POLYGON((:0 :1, :2 :3, :4 :5, :6 :7, :8 :9))$$,27700),:10))
as a;
Errors from PostgreSQL log:
Number 1
2012-04-18 09:39:41 BST ERROR: syntax error at or near "'||'" at character 283
2012-04-18 09:39:41 BST STATEMENT: select st_xmin(a) minx, st_ymin(a) miny, st_xmax(a) maxx, st_ymax(a) maxy from ST_ENVELOPE(ST_TRANSFORM(ST_GeomFromText('POLYGON(('||((E'138082'))||' '||((E'12907'))||','||((E'207179'))||' '||((E'12907'))||','||((E'207179'))||' '||((E'88647'))||','||((E'138082'))||' '||((E'88647'))'||','||((E'138082'))||' '||((E'12907'))||'))',27700),((E'4326')))) as a
Number 2
09:39:27 BST ERROR: parse error - invalid geometry
2012-04-18 09:39:27 BST HINT: "POLYGON(((" <-- parse error at position 10 within geometry
2012-04-18 09:39:27 BST STATEMENT: select st_xmin(a) minx, st_ymin(a) miny, st_xmax(a) maxx, st_ymax(a) maxy ,1, 1 from ST_ENVELOPE(ST_TRANSFORM(ST_GeomFromText($$POLYGON((((E'464217')) ((E'133902')), ((E'591014')) ((E'133902')), ((E'591014')) ((E'261209')), ((E'464217')) ((E'261209')), ((E'464217')) ((E'133902'))))$$,27700),((E'4326')))) as a;
Your first error is a simple syntax error with an extra
'character, at character row 283. Change88647'||to88647||, and:7'||to:7||for your without value version.I can’t reproduce your second error.
On the topic of MBR, have you seen the box functions/types? They could help you.