Skip to main content
2 of 4
Title changed, Content structured
Taras
  • 34.1k
  • 4
  • 73
  • 148

Inserting GeoJSON in Postgres using laravel controller

Actually.. I have GeoJson that I send from javascript into controller. My GeoJSON:

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [101.21719328770531, 3.113987284801912]
        },
        "properties": null
    }, {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": [
                [101.18330230120145, 3.046205311794175],
                [101.13585492009604, 2.985201536087212],
                [101.19008049850223, 2.9174195630794753]
            ]
        },
        "properties": null
    }]
}

and then I found this single heterogeneous GEOMETRYCOLLECTION from this link

So, My SQL something like this:

SELECT public.ST_SetSRID(public.ST_Collect(public.ST_GeomFromGeoJSON(feat->>'geometry')), 4326) as geom
FROM (
      SELECT json_array_elements('{
                      "type":"FeatureCollection",
                      "features":[{
                                 "type":"Feature",
                                 "geometry":{
                                            "type":"Point",
                                            "coordinates":[101.21719328770531,3.113987284801912]
                                  },
                                 "properties":null
                      },{
                      "type":"Feature",
                             "geometry":{
                                        "type":"LineString",
                                        "coordinates":[[101.18330230120145,3.046205311794175],
                                                       [101.13585492009604,2.985201536087212],
                                                       [101.19008049850223,2.9174195630794753]
                                                      ]
                                        },
                             "properties":null
                      }]
                     }'::json->'features') AS feat
) AS f;

It can run it ... but, how can I insert this into my column geom in my table by using something like this 'geom' => db::raw("from my sql")?