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

ArgumentError in Geo.WKB.Encoder.do_encode/2(geo 3.4.2) #165

Closed
mazz opened this issue Oct 11, 2021 · 2 comments
Closed

ArgumentError in Geo.WKB.Encoder.do_encode/2(geo 3.4.2) #165

mazz opened this issue Oct 11, 2021 · 2 comments

Comments

@mazz
Copy link

mazz commented Oct 11, 2021

I'm adding a ZipCode table and during the process of migrating zipcodes from a CSV that contains coordinate information and inserting them into the db I'm encountering an ArgumentError in Geo.WKB.Encoder .. not sure what this means.

I'm on erlang 24.0.3, elixir 1.12.2. Using geo 3.4.2 and geo_postgis 3.4.0

the migration:

https://github.com/akoutmos/elixir_monitoring_prom/blob/103c868fcc89284ad2ae79b77434ef10fff48f63/priv/repo/migrations/20190821020059_zip_code_table.exs#L42(my source and deps attached).

log:

INSERT INTO "zip_codes" ("city","dst","point","state","timezone","zip_code") VALUES ($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING "id" ["auburn", true, %Geo.Point{coordinates: {"-122.26608", "47.303722"}, properties: %{}, srid: 4326}, "wa", -8, "98001"]
** (ArgumentError) argument error
(geo 3.4.2) lib/geo/wkb/encoder.ex:61: Geo.WKB.Encoder.do_encode/2
(geo 3.4.2) lib/geo/wkb/encoder.ex:48: Geo.WKB.Encoder.encode!/2
(goshen 0.1.0) lib/geo_postgis/extension.ex:76: Goshen.PostgresTypes."Elixir.Geo.PostGIS.Extension"/1
(goshen 0.1.0) deps/postgrex/lib/postgrex/type_module.ex:897: Goshen.PostgresTypes.encode_params/3
(postgrex 0.15.11) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3
(db_connection 2.4.0) lib/db_connection.ex:1205: DBConnection.encode/5
(db_connection 2.4.0) lib/db_connection.ex:1305: DBConnection.run_prepare_execute/5
(db_connection 2.4.0) lib/db_connection.ex:574: DBConnection.parsed_prepare_execute/5
(db_connection 2.4.0) lib/db_connection.ex:566: DBConnection.prepare_execute/4
(postgrex 0.15.11) lib/postgrex.ex:266: Postgrex.query/4
(ecto_sql 3.7.0) lib/ecto/adapters/sql.ex:794: Ecto.Adapters.SQL.struct/10
(ecto 3.7.1) lib/ecto/repo/schema.ex:744: Ecto.Repo.Schema.apply/4
(ecto 3.7.1) lib/ecto/repo/schema.ex:367: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(elixir 1.12.2) lib/enum.ex:930: Enum."-each/2-lists^foreach/1-0-"/2
(ecto_sql 3.7.0) lib/ecto/migration/runner.ex:279: Ecto.Migration.Runner.perform_operation/3
(stdlib 3.15.1) timer.erl:166: :timer.tc/1
(ecto_sql 3.7.0) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/8
(ecto_sql 3.7.0) lib/ecto/migrator.ex:324: Ecto.Migrator.attempt/8
(ecto_sql 3.7.0) lib/ecto/migrator.ex:250: anonymous fn/5 in Ecto.Migrator.do_up/5
(ecto_sql 3.7.0) lib/ecto/migrator.ex:295: anonymous fn/6 in Ecto.Migrator.async_migrate_maybe_in_transaction/7

```
defmodule Goshen.Repo.Migrations.ZipCodeTable do
  use Ecto.Migration

  alias Goshen.ZipCodes.ZipCode

  def up do
    execute("CREATE EXTENSION IF NOT EXISTS postgis")

    create table(:zip_codes) do
      add :zip_code, :string, size: 5, null: false
      add :city, :string, null: false
      add :state, :string, size: 2, null: false
      add :timezone, :integer, null: false
      add :dst, :boolean, null: false
    end

    execute("SELECT AddGeometryColumn('zip_codes', 'point', 4326, 'POINT', 2)")
    execute("CREATE INDEX zip_code_point_index on zip_codes USING gist (point)")

    create unique_index(:zip_codes, [:zip_code])

    flush()

    "#{__DIR__}/../wa_zip_codes.csv"
    |> File.read!()
    |> String.split("\n")
    |> Enum.filter(fn line -> String.trim(line) != "" end)
    |> Enum.map(fn csv_line ->
      [zip, city, state, lat, long, tz, dst] =
        csv_line
        |> String.replace("\"", "")
        |> String.replace("\n", "")
        |> String.split(",")

      city = String.downcase(city)
      state = String.downcase(state)

      attrs = %{
        zip_code: zip,
        city: city,
        state: state,
        point: %Geo.Point{coordinates: {long, lat}, srid: 4326},
        timezone: String.to_integer(tz),
        dst: (dst == "1" && true) || false
      }

      ZipCode.changeset(%ZipCode{}, attrs)
    end)
    |> Enum.each(fn zip_code_changeset ->
      Goshen.Repo.insert(zip_code_changeset, on_conflict: :nothing)
    end)
  end

  def down do
    drop(table(:zip_codes))
    execute("DROP EXTENSION IF EXISTS postgis")
  end
end

```

@bryanjos
Copy link
Contributor

Hey, looking at the error it looks like your coordinates are strings and not numbers. I think that may be the issue

@mazz
Copy link
Author

mazz commented Oct 11, 2021

Hey, looking at the error it looks like your coordinates are strings and not numbers. I think that may be the issue

That was it thanks. This solved my issue.

      {parsed_long, _} = Float.parse(long)
      {parsed_lat, _} = Float.parse(lat)
      attrs = %{
        zip_code: zip,
        city: city,
        state: state,
        point: %Geo.Point{coordinates: {parsed_long, parsed_lat}, srid: 4326},
        timezone: String.to_integer(tz),
        dst: (dst == "1" && true) || false
      }

@mazz mazz closed this as completed Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants