ruby-on-rails – 使用MIME类型呈现为GeoJSON(或选择性地呈现为W
我有Rails与PostGIS,activerecord-postgis-adapter和rgeo-geojson运行.
目前我可以使用默认的“object.json”URL来获取WKT / WKB格式的JSON字符串.它看起来像这样: {"description":null,"id":1,"position":"POINT (10.0 47.0)"} 但是现在我想要一个自定义MIME类型,所以我可以调用“object.geojson”来获取GeoJSON格式,如下所示: {"description":null,"position":{"type":"Point","coordinates": [10.0,47.0]}} 我发现将JSON编码器设置为GeoJSON的唯一方法是使用RGeo :: ActiveRecord :: GeometryMixin.set_json_generator(:geojson)和RGeo :: ActiveRecord :: GeometryMixin.set_json_generator(:wkt)全局设置它.但我只是想在本地设置它,这可能吗? 我已经将mime :: Type.register“application / json”,:geojson,%w(text / x-json application / jsonrequest)添加到mime_types.rb并且它工作正常:我可以在我的控制器中使用此代码: respond_to do |format| format.json { render json: @object } format.geojson { render text: "test" } end 我希望有人可以告诉我如何在不将全局JSON渲染器设置为:geojson的情况下将一些特定对象渲染到GeoJSON. !? 编辑: 我的对象在Rails控制台中看起来像这样: #< Anchor id:1,description:nil,position:#< RGeo :: Geos :: CAPIPointImpl:0x3fc93970aac0“POINT(10.0 47.0)”>> 解决方法
您可以将这样的工厂用于特定的@object
factory = RGeo::GeoJSON::EntityFactory.instance feature = factory.feature(@object.position,nil,{ desc: @object.description}) 编码: RGeo::GeoJSON.encode feature 它应该输出这样的东西: { "type" => "Feature","geometry" => { "type" => "Point","coordinates"=>[1.0,1.0] },"properties" => { "description" => "something" } } 或者一系列功能: RGeo::GeoJSON.encode factory.feature_collection(features) 赠送: { "type": "FeatureCollection","features": [ { "type": "Feature",# the rest of the feature... },{ "type": "Feature",# another feature... } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |