0
我有這個模式具有GeoGeo.Point
:JSON編碼GEO.Point
defmodule Api.Shop do
use Ecto.Schema
import Ecto.Changeset
import Api.Repo
import Ecto.Query
@derive {Poison.Encoder, only: [:name, :place_id, :geo_json, :distance]}
schema "shops" do
field :name, :string
field :place_id, :string
field :point, Geo.Point
field :geo_json, :string, virtual: true
field :distance, :float, virtual: true
timestamps()
end
def encode_model(shop) do
%Api.Shop{shop | geo_json: Geo.JSON.encode(shop.point) }
end
defimpl Poison.Encoder, for: Api.Shop do
def encode(shop, options) do
shop = Api.Shop.encode_model(shop)
Poison.Encoder.Map.encode(Map.take(shop, [:id, :name, :geo_json]), options)
end
end
def changeset(shop, params \\ %{}) do
shop
|> cast(params, [:name, :place_id, :point])
|> validate_required([:name, :place_id, :point])
|> unique_constraint(:place_id)
end......
end
當我返回查詢的shop.point
領域:
def create_query_no_keyword(categories, shop_ids) do
products_shops_categories = from p in Product,
distinct: p.id,
join: ps in ProductShop, on: p.id == ps.p_id,
join: s in Shop, on: s.id == ps.s_id,
join: pc in ProductCategory, on: p.id == pc.p_id,
join: c in Subcategory, on: c.id == pc.c_id,
where: c.id in ^categories,
where: s.id in ^shop_ids,
group_by: [p.id, p.name, p.brand],
select: %{product: p, categories: fragment("json_agg(DISTINCT (?, ?)) AS category", c.id, c.name), shops: fragment("json_agg(DISTINCT (?, ?, ?)) AS shop", s.id, s.name, s.point)}
end
實際返回的是0101000020E6100000A3BDB0EB0DD9654030AC2C1BE76D42C0
,這是錯誤的格式 - WKB。我正在尋找編碼爲具有可讀座標的WKT。
如何獲得s.point
爲WKT
的格式,並且在查詢返回時具有座標?
怎麼樣' 「01 ...」 |> Geo.WKB.decode |> Geo.WKT.encode'? – Dogbert
@Dogbert只是想知道我在哪寫代碼? – BeniaminoBaggins
你可以發佈'IO.inspect(create_query_no_keyword(categories,shop_ids)|> Api.Repo.all)'的輸出嗎?我不完全清楚WKB值在返回值中。 – Dogbert