ryanrborn
I have a resource (MyApp.MapPin) that has an attribute :coordinates of type :geometry. I’m trying to implement a clustering feature that will cluster pins that are close together for easier viewing. These MapPins also have filter policies associated with them. It makes the most sense to use functionality from PostGIS for this, so the query I’d like to create is this:
SELECT COUNT(*), ST_centroid(ST_collect(mp.coordinates))
FROM map_pins mp
WHERE ST_makeenvelope(?, ?, ?, ?, 4326) ~ mp.coordinates
-- Additional filters from applied policies
Is there a way to create an Ash query that could return the result I’m looking for? Or perhaps a way to get the filter commands from policies to inject into a raw query? Or even a better way that I haven’t thought of?
I know AshGeo has the st_centroid function exposed for Ash expressions, but expressions can’t be used in select statements.
The best I’ve come up with so far is a separate query that fetches the ids of the map_pins and then doing a Repo.query with an in clause on those ids.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
Not quite true. Calculations can use expressions.
Then you can load that calculation.
ryanrborn
True, calculations can use expressions. Though,
ST_centroidis an aggregate function likecountoraverage, so postgres returns an error saying that all the other columns need to appear in theGROUP BYclause.As I understand calculations, they’re meant to operate individually on each record rather than collectively on a group of records.
There might be a solution using Ash aggregates, though I don’t understand them as well as I should. Don’t they require to be used with a relationship? Would I need to create another resource that somehow relates to the MapPin without a foreign key or something?
zachdaniel
Ahh, got it. So you ought to be able to accomplish this with a custom aggregate.
So with your custom aggregate defined:
This is not a commonly used feature, so let me know if you hit any rough edges and we’ll get it worked out
ryanrborn
This was it! Thank you Zach!
I will note for posterity that while calling the aggregate the
fieldneeds to be passed into the custom module and thetypeneeds to be declared as an option toAsh.aggregate. Like this: