clickbar / laravel-magellan

A modern PostGIS toolbox for Laravel
MIT License
203 stars 12 forks source link

Invalid Geometry Type Error #27

Closed tanabi closed 1 year ago

tanabi commented 1 year ago

Hey there;

When trying to use magellanGeography as a column type, such as:

        Schema::create('units', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('host_id')
                  ->nullable(false);
            $table->text('title')
                  ->nullable(false);
            $table->magellanGeography('location', 4326); // 4326 is srid
            $table->text('address');
            $table->timestamps();

            $table->spatialIndex('location');
        });

I get an error such as:

  SQLSTATE[22023]: Invalid parameter value: 7 ERROR:  Invalid geometry type modifier: geography at character 257 (Connection: pgsql, SQL: create table "units" ("id" bigserial not null primary key, "title" text not null, "location" public.GEOGRAPHY(GEOGRAPHY, 4326) not null, "address" text not null, "created_at" timestamp(0) without time zone null, "updated_at" timestamp(0) without time zone null))

I believe the problem is that it should be public.GEOGRAPHY(POINT, 4326) not null (replace POINT with whatever object you wish to use) instead of public.GEOGRAPHY(GEOGRAPHY, 4326). When I take the SQL generated above and replace it with my recommendation, the create table works.

Therefore, I believe magellanGeography should take a third parameter which is the geometry type you want to use with the geography.

tanabi commented 1 year ago

If anyone gets this error and needs a workaround for right now, this code will do it:

            // Workaround for MagellanGeography bug
            // https://github.com/clickbar/laravel-magellan/issues/27
            Grammar::macro('typeMagellanFix', function() {
                return 'GEOGRAPHY(POINT, 4326)';
            });

            $table->addColumn('magellanFix', 'location');
            //$table->magellanGeography('location', 4326); // 4326 is srid
saibotk commented 1 year ago

Oh, seems like an issue with the grammar function indeed.

Thank you very much for reporting the issue!

We will take a closer look asap and get a fix out.

And your workaround should be equal to

$table->magellanPoint('location', 4326, 'GEOGRAPHY');

If you want to allow any geography for the column, i believe you will need to do this:

// Workaround for MagellanGeography bug
// https://github.com/clickbar/laravel-magellan/issues/27
Grammar::macro('typeMagellanFix', function() {
    return 'GEOGRAPHY';
});

$table->addColumn('magellanFix', 'location');
tanabi commented 1 year ago

I just need points at the moment. I think your suggestion of using magellanPoint will work for me and avoid having to do a kind of weird workaround, thanks for showing me how to do that :) And thanks for jumping on the issue!