Ruby on Rails PostGIS - insert a polygon entry into the database

I am using RoR with PostGIS to store location data. I am trying to save a calculated location with a circle (e.g. a center point with a radius).

I tried something like this, but it does not work:

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

I also tried using RGeo and factory, but don't know how to use it.

Any help would 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 this raises the following exception:

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

Again, any help would be appreciated.

+6
source share
1 answer

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

You might also want to set your SRID.

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

Another, and probably the best, option is to simply save the exact location and location request with a specific distance. You can use either the distance operator ( http://postgis.net/docs/manual-2.1/geometry_distance_centroid.html ) or the overlap operator ( http://postgis.net/docs/manual-2.1/geometry_overlaps.html ).

+1
source

All Articles