Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode / parse wkt #262

Closed
tomaszantas opened this issue Sep 7, 2017 · 1 comment
Closed

Encode / parse wkt #262

tomaszantas opened this issue Sep 7, 2017 · 1 comment

Comments

@tomaszantas
Copy link

How can I parse / encode wkt?

I've got simple model of Places:

class CreatePlaces < ActiveRecord::Migration[5.1]
  def change

    create_table :places do |t|
      t.string :name
      t.st_point :coords, :geographic => true

      t.timestamps
    end

    change_table :places do |t|
      t.index :coords, using: :gist
    end

  end
end

I get all places in following way:

class PlacesController < ApplicationController

  def index
    @places = Place.all

    render json: @places.to_json
  end

end

But my JSON in response contains WKT:

[
    {
        "id": 1,
        "name": "test name 1",
        "coords": "POINT (50.324192 19.037805)",
        "created_at": "2017-09-07T20:29:19.203Z",
        "updated_at": "2017-09-07T20:29:19.203Z"
    }
]

I can map @places and encode coords like this:

class PlacesController < ApplicationController

  def index
    @places = Place.all

    @places.map { |k,v| k.coords = RGeo::GeoJSON.encode(k.coords, json_parser: :json) }

    render json: @places.to_json
  end

end

And then I get what I wanted - encoded/parsed coords:

[
    {
        "id": 1,
        "name": "test name 1",
        "coords": {
            "type": "Point",
            "coordinates": [
                50.324192,
                19.037805
            ]
        },
        "created_at": "2017-09-07T20:29:19.203Z",
        "updated_at": "2017-09-07T20:29:19.203Z"
    }
]

Is it right way to encode POINT?

@keithdoggett
Copy link
Member

Should be fixed by #309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants