Asaf
Asaf

Reputation: 2168

Ruby on Rails PostGIS - insert a polygon record into DB

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.

Upvotes: 6

Views: 1451

Answers (1)

waratuman
waratuman

Reputation: 764

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).

Upvotes: 1

Related Questions