0
votes

I have some polygons saved in postgres using postgis and now I need some way to convert this data to a GeoJSON file so I can display it over google maps.

Here is an example of a polygon in postgis

"POLYGON ((-40 145, -40 145, -40 145, -40 145, -40 145, -40 145))"

How could I convert this to the equivalent polygon in GeoJSON?

2
PostGIS provides the ST_AsGeoJSON function. - Dmitri Goldring
@DmitriGoldring Could you give an example how this is used in rails? - Qwertie
I'm afraid I'm not a Rails person but this package might get you started. - Dmitri Goldring

2 Answers

1
votes

I ended up just creating some JSON with .exterior_ring.points.map{|point|{lng:point.x, lat:point.y}} and displaying that on google maps in the same way that flightPlanCoordenants are used in the example https://developers.google.com/maps/documentation/javascript/examples/polyline-simple

0
votes

https://github.com/rgeo/rgeo-geojson is a specific gem for the purpose.

Your code should look like follow:

RGeo::GeoJSON.encode(<your_spatial_variable>, json_parser: :json)