Skip to content

Instantly share code, notes, and snippets.

@seguelador
Last active July 14, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seguelador/a9c0da75d93ddd4a9320bfb3f3bc83b9 to your computer and use it in GitHub Desktop.
Save seguelador/a9c0da75d93ddd4a9320bfb3f3bc83b9 to your computer and use it in GitHub Desktop.
All points inside a circle POSTGIS Rails
Crea un circulo en base a un punto de 2km
ActiveRecord::Base.connection.execute("SELECT ST_Buffer(ST_GeomFromText('POINT(100 90)'), 2000, 'quad_segs=3')")
###########################
Trae todos los customers ordenados segun distancias, tomando la distancia mas cercana primero respecto al punto
ActiveRecord::Base.connection.execute("SELECT * FROM customers ORDER BY customers.location <-> 'POINT(-70 -33)'")
###########################
Obtiene todos los customers que se encuentren dentro de un rango determinado (2000)
ActiveRecord::Base.connection.execute("SELECT * FROM customers WHERE ST_DistanceSphere(customers.location, ST_MakePoint(-70.70833273338121, -33.429525177615815)) <= 2000")
Customer.where("ST_DistanceSphere(customers.location, ST_MakePoint(-70.70833273338121, -33.429525177615815)) <= 2000").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment