I need to execute the PostGIS function st_intersection within an SQL SELECT clause in Ruby. At the moment I am doing it as raw SQL query:
sql_query = "SELECT id, ST_ASEWKT(ST_INTERSECTION(geometry, ?)) FROM trips WHERE status='active';"
intersections = Trip.execute_sql(sql_query, self[:geometry].to_s)
This way has the disadvantage that I receive the result as text and I need to parse the objects out of the strings. Much nicer would be the use of the ActiveRecord interface to make queries. However, I could not find any solution yet to run PostGIS functions (e.g. st_intersection) within ActiveRecord.
An earlier version of the activerecord-postgis-adapter's README showed a nice example using the gem squeel:
my_polygon = get_my_polygon # Obtain the polygon as an RGeo geometry
MySpatialTable.where{st_intersects(lonlat, my_polygon)}.first
As this is not part of the current README anymore, I am wondering whether this is not recommended or if there are any better alternatives.
There are two problems to solve here.
The first is using an SQL function within a
.selectclause. Ordinarily this is pretty easy—you just useASto give the result a name. Here's an example from the ActiveRecord Rails Guide:The resulting Order objects would have
ordered_dateandtotal_priceattributes.This brings us to the second problem, which is that Rails doesn't give us an easy way to parameterize a
select(i.e. use a?placeholder), so (as far as I can tell) you'll need to do it yourself withsanitize_sql_array:This will return a sanitized SQL fragment like
ST_ASEWKT(ST_INTERSECTION(geometry, '...')), which you can then use to specify a field inselect:The resulting query will return Trip objects with
idandintersectionattributes.