Support for ST_Covers

#439 · open · 4 comments

View on GitHub ↗

pablobm

Hello, newcomer to PostGIS here, I hope I'm making sense. I was trying to use run the following query, but it fails with `ActiveRecord::StatementInvalid: PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1`: ```ruby GeoblockZone.exists?( [ <<~SQL.squish, ST_Covers( "geoblock_zones"."zone", ST_GeomFromText('POINT(? ?)', 4326) ) SQL lon, lat ] ) ``` Where `geoblock_zones.zone` is a `t.geometry :zone, :geographic => true`. Tried with `lon` and `lat` as floats and as strings. Same result with both. Relevant version numbers: ``` ruby 3.4.8 activerecord (8.1.2) activerecord-postgis-adapter (11.1.1) rgeo (3.0.1) rgeo-activerecord (8.1.0) ``` I also looked if I could do it with Arel, but `ST_Covers` doesn't appear to be implemented, as it's not listed in [rgeo-activerecord's lib/rgeo/active_record/spatial_expressions.rb](https://github.com/rgeo/rgeo-activerecord/blob/main/lib/rgeo/active_record/spatial_expressions.rb)... but I'm guessing that's an issue for that repo? (Incidentally: where does the file `lib/rgeo/active_record/spatial_expressions.rb` live? I see it in the gem, but not in this repo).

Comments

keithdoggett

Hey @pablobm not sure exactly why the query is failing, but you probably need to use `ST_GeogFromText` (https://postgis.net/docs/ST_GeogFromText.html) rather than `ST_GeomFromText` since your `zone` column is geographic. If you want to go with the Arel route, you could define your own [function](https://github.com/rgeo/rgeo-activerecord/blob/3e52adeb51fb2270a24edbcbaef8ddc23d040db5/lib/rgeo/active_record/spatial_expressions.rb#L19-L22), but I'd say try the geography fix first before looking at that. RGeo ActiveRecord is a generic activerecord connector that this gem requires, so that file would be part of the rgeo-activerecord gem.

pablobm

Thank you. I tried with `ST_GeogFromText` now and got the same error: ``` ActiveRecord::StatementInvalid: PG::IndeterminateDatatype: ERROR: could not determine data type of parameter $1 ``` For reference I used this code: ``` GeoblockZone.exists?( [ <<~SQL.squish, ST_Covers( "geoblock_zones"."zone", ST_GeogFromText('POINT(? ?)') ) SQL lon, lat ] ) ``` I have also checked that the following works on the Postgres cli: ``` SELECT * FROM geoblock_zones WHERE ST_Covers(geoblock_zones.zone, ST_GeogFromText('POINT(1 2)')); ``` I'll go and define my own function as suggested 👍

pablobm

Re: the file I couldn't find... sorry, bad copy&paste on my side. I meant `lib/active_record/relation/query_methods_ext.rb`. I see it when I open the gem with `bundle open`, but I don't see it in the repo.

pablobm

Had another look and I'm now thinking that the problem is not in this gem, as I can run exactly the same code in a new Rails app. Something in my app must be interacting badly. Relevant code at https://github.com/openstreetmap/openstreetmap-website/pull/6713