rgeo/rgeo-geojson

DatatypeMismatch with db queries using multipolygons

Siggs2000 opened this issue · 2 comments

I'm fairly certain that I'm doing something wrong here but when using a the active-record rgeo gem and encoding a single polygon, I'm able to make a db query like this:

This works:
MyModel.where("latlon && ?", poly) where the latlon column is a point type and the poly represents a regular polygon that I generated from a linestring of coordinates using this factory: factory = RGeo::Geographic.spherical_factory(:srid => 4326)

This doesn't:
Using this gem, I encode a geojson multipolygon successfully using: poly = RGeo::GeoJSON.decode(mp.to_json, :json_parser => :json) Then I extract the geometry using:
poly = poly.geometry and that also seems to be successful. Now I try to run that same query from above:
MyModel.where("latlon && ?", poly) but this time it fails with this error:

PG::DatatypeMismatch at /api/v1/listings/polygon_search.json
============================================================

> ERROR:  argument of WHERE must be type boolean, not type record
LINE 1: SELECT "my_models".* FROM "my_model" WHERE (latlon && '002000...
            

I think I've figured this out. I suppose I just have to iterate through each of the polygons in the multi_polygon and check results for each one. Unless anyone knows of a better way?

The exception was being raised because the multi_polygon is actually multiple polygon types.... Obvious in hindsight!

I think I've figured this out. I suppose I just have to iterate through each of the polygons in the multi_polygon and check results for each one. Unless anyone knows of a better way?

That's the way indeed!

Here's a snippet I use to do so.

Since it is from 2017 and you found a solution anyway, I'm closing this issue :)