Skip to content

Instantly share code, notes, and snippets.

@joseluistorres
Created July 24, 2015 17:44
Show Gist options
  • Save joseluistorres/7b5cccda1eba5ef0a92b to your computer and use it in GitHub Desktop.
Save joseluistorres/7b5cccda1eba5ef0a92b to your computer and use it in GitHub Desktop.
defmodule Ph_14Version.Field do
use Ph_14Version.Web, :model
use Ecto.Model.Callbacks
before_insert :set_map_coords
schema "fields" do
field :name, :string
field :map_coords, Geo.MultiPolygon
timestamps
end
@required_fields ~w(name map_coords)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
def set_map_coords(changeset) do
map_coords = Ecto.Changeset.get_field(changeset, :map_coords)
change(changeset, %{map_coords: Geo.JSON.decode(map_coords)})
end
end
@joseluistorres
Copy link
Author

I need to parse this to insert a record

{"type":"MultiPolygon","crs":{"type":"name","properties":{"name":"EPSG4326"}},"coordinates":[[[[-85.62224328517914,42.09954388363239],[-85.62217891216278,42.092203790811375],[-85.62700688838957,42.09218786777775],[-85.6271356344223,42.098970718243926],[-85.62554776668549,42.09892295422769],[-85.62529027462006,42.099496120047895],[-85.62224328517914,42.09954388363239]]]]}

This will decode it to be a Geo.MultiPolygon
Geo.JSON.decode(map_coords)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment