6

I'm using RoR with PostGIS to store locations data. I'm trying to store an estimated location using circle (e.g. center point with radius).

I've tried something like that, but it doesn't work:

@location = Location.new(:place_id => place.id,
                         :circle => %{ST_Buffer(ST_MakePoint(#{latitude}, #{longitude})::geography, #{accuracy})})

I've also tried using RGeo and it's factory but not sure how to use it exactly.

Any help will be appreciated. Thanks.

Edit 1: I made some progress.

factory = RGeo::Cartesian.factory
center_point = factory.point(latitude, longitude)
circle = center_point.buffer(accuracy)

@location = Location.new(:place_id => place.id,
                         :circle => circle)

BUT - now it throws the following exception:

can't cast RGeo::Cartesian::Polygon Impl to string

Again, any help will be appreciated.

Asaf
  • 2,158
  • 2
  • 25
  • 40
  • 1
    Be aware that PostGIS uses the (x y) axis order, which for WGS84 is (longitude latitude). – Mike T Mar 16 '14 at 22:03
  • @MikeT Thanks. I've changed it but it wasn't the problem. Can you tell if it looks like a code problem or a configuration problem (maybe something with the adapter)? – Asaf Mar 17 '14 at 06:51
  • This looks borked to me: %{ST_Buffer(ST_MakePoint(#{latitude}, #{longitude})::geography, #{accuracy})} -- ST_MakePoint is presumarly trying to return an RGeo::Cartesian::Polygon and %{..} is trying to force that into a string. Seems like you need to fix that. – Steve Midgley Mar 21 '14 at 20:53

1 Answers1

1

It looks like the column named circle in the locations table is a text column and not a geometry column. What does you schema look like?

You will probably want to set your SRID as well.

circle = RGeo::Cartesian.factory.point(0, 0).buffer(20)
@location = Location.new(:place_id => place.id, :circle => circle)
@locaiton.save

Another, and probably better option would be to just store the exact location and query for the location with a certain distance. You can use either the distance operator (http://postgis.net/docs/manual-2.1/geometry_distance_centroid.html) or the overlaps operator (http://postgis.net/docs/manual-2.1/geometry_overlaps.html).

waratuman
  • 734
  • 1
  • 5
  • 3