harleyqmc

harleyqmc

Elixir / phoenix / sqlite / spatialite error: tracks.geom violates geometry constraint [geom-type or srid not allowed]

Hi,

I’m not sure if this is the appropriate forum for my question. It’s a hybrid question involving Elixir, Phoenix, SpatiaLite, and SQLite. Perhaps someone can point me in the right direction.

Firstly, here’s the error I’m encountering:

error: tracks.geom violates geometry constraint [geom-type or srid not allowed]

Basically trying to get this

%Geo.MultiLineStringZ{
  coordinates: [
    [
      {18.68056, 54.695338, 15.6},
      {18.68061, 54.695331, 15.6},
      {18.68069, 54.695339, 15.6},
      {18.680783, 54.695356, 15.8},
      {18.680868, 54.695368, 15.8},
      {18.680947, 54.695379, 15.8},
      {18.681009, 54.695388, 15.8},
      {18.675521, 54.692955, 22.4}
    ]
  ],
  srid: 3857,
  properties: %{}
}

Into a spatialite GIS table. Created a SpatiaLite SQLite DB a la

  def change do
    create table(:tracks) do
      add :name, :string

      timestamps(type: :utc_datetime)
    end

    execute("SELECT AddGeometryColumn('tracks', 'geom', 3857, 'GEOMETRYZ', 'XYZ');")
  end

I have also tried using MULTILINESTRINGZ, LINESTRING, and other combinations (e.g., SRID 4326). At this point, I feel like I’m just guessing.

I’ve experimented with Ecto.type, but I always get the same error:

error: tracks.geom violates geometry constraint [geom-type or srid not allowed]

I haven’t come across anything like Ecto.Adapters.Spatialite. Postfix/PostGIS seems more straightforward, but for some reason, I’m fixated on SpatiaLite/SQLite.

I even contemplated using:

field(:geom, Geo.PostGIS.Geometry)

But that led to other issues. It seems I might not be creating the column correctly? A direct insert in SpatiaLite gives the same error.

When trying:

spatialite>  INSERT INTO tracks (geom, inserted_at, updated_at) VALUES (X'00A000000500000F11000000010080000002000000084032AE392E1EF73C404B5900D5E8D541402F3333333333334032AE3C74FB54A0404B59009B30728F402F3333333333334032AE41B328B6D8404B5900DE4C5111402F3333333333334032AE47CB70AC3B404B59016CE789E7402F99999999999A4032AE4D5D80E497404B5901D19157AC402F99999999999A4032AE528AE74F2F404B59022DD7A9A0402F99999999999A4032AE569B17481B404B5902795703F3402F99999999999A4032ACEEF1BAC2DF404B58B2BFDB4CC24036666666666666', '2023-10-17', '2023-10-17');

I receive the error:

Error: tracks.geom violates Geometry constraint [geom-type or SRID not allowed]

And when I do manage to insert something, like:

spatialite> INSERT INTO tracks (geom, updated_at, inserted_at) VALUES (st_geomfromtext('multilinestring((10.02 20.01, 10.32 23.98, 11.92 25.64), (9.55 23.75, 15.36 30.11))', 3857), DATETIME('now'), DATETIME('now'));

spatialite> INSERT INTO tracks (geom, inserted_at, updated_at) VALUES (GeomFromText('MULTILINESTRINGZ((18.68056 54.695338 15.6,18.68061 54.695331 15.6,18.68069 54.695339 15.6,18.680783 54.695356 15.8,18.680868 54.695368 15.8,18.680947 54.695379 15.8,18.681009 54.695388 15.8,18.675521 54.692955 22.4))', 3857), DATETIME('now'), DATETIME('now')

( I have encoded WKB and WKT in there too just not listed )

The data doesn’t seem to be retrievable correctly:

iex(90)> t = GPX.get_track!(2)
[debug] QUERY OK source="tracks" db=0.4ms idle=1333.8ms
SELECT t0."id", t0."name", t0."geom", t0."inserted_at", t0."updated_at" FROM "tracks" AS t0 WHERE (t0."id" = ?) [2]
↳ anonymous fn/4 in :elixir.eval_external_handler/1, at: src/elixir.erl:376
%GPX.Track{
  __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
  id: 2,
  name: nil,
  geom: <<0, 1, 17, 15, 0, 0, 154, 153, 153, 153, 153, 25, 35, 64, 195, 245, 40,
    92, 143, 2, 52, 64, 184, 30, 133, 235, 81, 184, 46, 64, 92, 143, 194, 245,
    40, 28, 62, 64, 124, 5, 0, 0, 0, 2, 0, 0, ...>>,
  inserted_at: ~U[2023-10-18 02:18:56Z],
  updated_at: ~U[2023-10-18 02:18:56Z]
}

iex(91)> t.geom |> Geo.WKT.decode
{:error,
 %FunctionClauseError{
   module: Geo.WKT.Decoder,
   function: :do_decode,
   arity: 2,
   kind: nil,
   args: nil,
   clauses: nil
 }}

iex(92)> t.geom |> Geo.WKB.decode
{:error,
 %FunctionClauseError{
   module: Geo.WKB.Decoder,
   function: :do_decode,
   arity: 4,
   kind: nil,
   args: nil,
   clauses: nil
 }}

I’m at a bit of a loss. I’m neither a SpatiaLite/GIS expert nor proficient in Elixir/Phoenix, so any assistance would be greatly appreciated.

Please let me know if I haven’t provided sufficient information or if any part of my query isn’t clear.

Thanks!

Most Liked

harleyqmc

harleyqmc

Thanks for the reply. I meant to reply myself but I guess I forgot

I did manage to at least work something out and you are totally right, had to resort to basically raw SQL for insert

 def build_sql() do
    """
    INSERT INTO tracks(geom, name, updated_at, inserted_at) VALUES (SetSRID(GeomFromText(?), 3857), ?, DATETIME('now'), DATETIME('now'));
    """
  end

You can still use fragments for selects so thats cool

  def get_geom_as_geojson!(id) do
    id = String.to_integer(id)

    from(t in Track,
      where: t.id == ^id,
      select: fragment("AsGeoJSON(?)", t.geom)
    )
  end

Not the most elegant solution but gets the job done for what it is.

SQLiteGIS maybe one day

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement