Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions in Prepared Statements #3

Open
ejc123 opened this issue May 8, 2013 · 2 comments
Open

Functions in Prepared Statements #3

ejc123 opened this issue May 8, 2013 · 2 comments

Comments

@ejc123
Copy link

ejc123 commented May 8, 2013

I've been trying to use geom.asGeoJson() in a query that gets turned into a prepared statement. The problem is that I get

ERROR: function st_asgeojson(bytea, integer) is not unique at character 31
HINT: Could not choose a best candidate function. You might need to add explicit type casts.

in my Postgres logs.

Here's what Postgres is trying to do:

STATEMENT: select x2."name", x2."state", ST_AsGeoJson(ST_AsEWKB(x2."geom"),1), x2."country", x3."email", x4."term", x5."career" from "term" x4, "career" x5, "cities" x2, "students" x3 where (((x4."term" = $1) and (x5."career" = $2)) and (x2."id" = $3)) and (((x3."city" = x2."id") and (x3."term" = x4."id")) and (x3."career" = x5."id"))

The query works wonderfully if it's not in a prepared statement.

@ahinz
Copy link
Owner

ahinz commented May 8, 2013

Ah, it looks like PostGIS helpfully defines two 2-input ST_AsGeoJson functions:

CREATE OR REPLACE FUNCTION ST_AsGeoJson(int4, geometry)

and

CREATE OR REPLACE FUNCTION ST_AsGeoJson(geometry, int4)

So, as the actually helpful hint mentions, all we should need to do it typecast "integer" to a int4. Unfortunately, this is going to happen for pretty much all of the PostGIS functions. I'm not sure if there is an easy way to add type information to function arguments.

I'll try to tackle this over the weekend.

@ejc123
Copy link
Author

ejc123 commented Jul 5, 2013

Actually it's because of these two:

text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits=15, integer options=0);

text ST_AsGeoJSON(geography geog, integer maxdecimaldigits=15, integer options=0);

and the fact that postgres can't tell if we have geom or geog when GeoSlick returns the geometry column as bytea ala ST_AsEWKB()

As an experiment I tried this directly from psql:
gis=# SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');
st_asgeojson


{"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}
(1 row)

gis=# SELECT ST_AsGeoJSON(ST_AsEWKB('LINESTRING(1 2 3, 4 5 6)'));
ERROR: function st_asgeojson(bytea) is not unique
LINE 1: SELECT ST_AsGeoJSON(ST_AsEWKB('LINESTRING(1 2 3, 4 5 6)'));
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.

As expected, this works:

gis=# SELECT ST_AsGeoJSON(ST_AsEWKB('LINESTRING(1 2 3, 4 5 6)')::GEOMETRY);
st_asgeojson


{"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}
(1 row)

I tried to add the cast to GEOMETRY in GeoSlick, but I could not figure out how to do it. As an experiment I commented out the call to wrapEWKB in postgis.scala and it works for my purposes -- not sure what it breaks. Any hints on casting would be appreciated. I think it should be possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants