ryanrborn

ryanrborn

Fetching centroid of geometry objects

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.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Ahh, got it. So you ought to be able to accomplish this with a custom aggregate.

So with your custom aggregate defined:

defmodule StCentroid do
  def dynamic(opts, binding)
    Ecto.Query.dynamic(
      [],
      fragment("ST_centroid(?, ?)", field(as(^binding), ^opts[:field]))
    ) 
  end
end
YourResource
|> Ash.aggregate({:centroid, :custom, implementation: StCentroid})

This is not a commonly used feature, so let me know if you hit any rough edges and we’ll get it worked out :slight_smile:

Also Liked

ryanrborn

ryanrborn

This was it! Thank you Zach!

I will note for posterity that while calling the aggregate the field needs to be passed into the custom module and the type needs to be declared as an option to Ash.aggregate. Like this:

Ash.aggregate(:centroid, :custom, 
  type: AshGeo.Geometry,
  implementation: {StCentroid, [field: :coordinates]}
)

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

We're in Beta

About us Mission Statement