vendredi 21 juin 2019

Save postgis geometry type from geojson

I have a Lealfet map in a Laravel Blade view where the user can draw shapes and markers. These features are registered in a GeoJson object, that I stringify and insert in a hidden textarea with name "geojson" to submit it to the server, within a form.

The problem is that I want to save this data as geometry type in my PostgreSQL database, using the PostGis ST_GeomFromGeoJSON() function, but I can't get it to work.

Here is what I tried for now:

$site = new Site;
$data = $request->all();
unset($data['geojson']);

foreach($data as $key=>$d)
{
   $site->$key = $d;
}

$geojson = json_decode($request->geojson);
$site->save();

DB::update('update posha_sites set geom = ST_GeomFromGeoJSON(?)
      WHERE num_site = ?
      AND city_id = ?',
   [$geojson, $request->num_site, $city_id->id]
);

Right now I am saving all my data and then trying to insert the geospatial data, as I don't know how I could use a raw query while saving the rest of my data.

When I do that I am getting this error:

Object of class stdClass could not be converted to string


EDIT

Here is the whole migration for the posha_sites table:

public function up()
{
    Schema::create('posha_sites', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->bigInteger('num_site');
        $table->string('type', 50)->nullable();
        $table->mediumText('adresse')->nullable();
        $table->string('cartes_anciennes')->nullable();
        $table->string('carte_topo')->nullable();
        $table->mediumText('cadastre_remembre')->nullable();
        $table->mediumText('cadastre_moderne')->nullable();
        $table->mediumText('cadastre_ancien')->nullable();
        $table->string('lieu_dit')->nullable();
        $table->mediumText('nature_parcelles')->nullable();
        $table->mediumText('conditions_acces')->nullable();
        $table->string('situation_administrative')->nullable();
        $table->string('altitude')->nullable();
        $table->string('relief')->nullable();
        $table->mediumText('hydrographie')->nullable();
        $table->string('geologie')->nullable();
        $table->string('vestiges_periode')->nullable();
        $table->mediumText('vestiges_nature')->nullable();
        $table->mediumText('vestiges_conservation')->nullable();
        $table->longText('plans_documents_figures')->nullable();
        $table->longText('sources_manuscrites')->nullable();
        $table->longText('sources_imprimees')->nullable();
        $table->longText('renseignement_oral')->nullable();
        $table->longText('bibliographie')->nullable();
        $table->longText('histoire')->nullable();
        $table->longText('historiographie')->nullable();
        $table->longText('description_histoire_monumentale')->nullable();
        $table->geometrycollection('geom')->nullable();
        $table->string('last_author')->nullable();
        $table->integer('tree_id')->unsigned()->nullable();
        $table->integer('parent_id')->unsigned()->nullable();
        $table->integer('city_id')->unsigned();
        $table->timestamps();
    });
}

The function ST_GeomFromGeoJSON() is in fact expecting a string so I didn't decode my $geojson variable:

$geojson = $request->geojson;

instead of:

$geojson = json_decode($request->geojson);

But I still get an error:

SQLSTATE[XX000]: Internal error:

7 ERROR: invalid GeoJson representation (SQL: update posha_sites set geom = ST_GeomFromGeoJSON({"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-0.229114,44.564488]}}]}) WHERE num_site = 248 AND city_id = 5)

Yet I tested my geojson in an online geojson validator and it seems correct.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire