Search code examples
ruby-on-railspostgresqlpostgisgeojson

Convert PostGIS polygon as GeoJSON in rails


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?


Solution

  • 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