I have Rails with PostGIS
, activerecord-postgis-adapter
and rgeo-geojson
running.
At the moment I can use default "object.json" URLs to get a JSON string with WKT/WKB format. It looks like this:
{"description":null,"id":1,"position":"POINT (10.0 47.0)"}
But now I want to have a custom MIME-Type, so I can call "object.geojson" to get GeoJSON format like this:
{"description":null,"id":1,"position":{"type":"Point","coordinates": [10.0, 47.0]}}
The only way I found to set the JSON-encoder to GeoJSON was to set it globally using RGeo::ActiveRecord::GeometryMixin.set_json_generator(:geojson)
and RGeo::ActiveRecord::GeometryMixin.set_json_generator(:wkt)
. But I just want to set it locally, is this possible?
I already added Mime::Type.register "application/json", :geojson, %w( text/x-json application/jsonrequest )
to mime_types.rb
and it works fine: I can use this code in my controller:
respond_to do |format|
format.json { render json: @object }
format.geojson { render text: "test" }
end
I hope someone can tell me how to render some specific object to GeoJSON without setting the global JSON renderer to :geojson
. !?
EDIT:
My objects look like this in Rails Console:
#<Anchor id: 1, description: nil, position: #<RGeo::Geos::CAPIPointImpl:0x3fc93970aac0 "POINT (10.0 47.0)">>