Currently making a project in Laravel that uses MapboxGLJS. I've currently got a database server that I'm connected to that contains comments which I need to convert to a GeoJSON FeatureCollection that includes the ID and the spacial data. I've seen an example of the code to do this which I'll provide below but when I try to use said code and try to use the addSource
Mapbox method it comes back with Error: Input data is not a valid GeoJSON object.
.
CommentController.php
...
public function all(){
$comments = Comment::whereNotNull('user_id')->get();
$mapFeatures = array();
$mapFeatures['type'] = 'FeatureCollection';
$mapFeatures['name'] = 'comments';
$mapFeatures['crs'] = array(
'type' => 'name',
'properties' => array(
'name' => 'urn:ogc:def:crs:OGC:1.3:CRS84'
),
);
$mapFeatures['features'] = array();
foreach ($comments as $comment) {
$mapItem = array(
'type' => 'Feature',
'properties' => array(
'id' => $comment->id,
),
'geometry' => $comment->location
);
array_push($mapFeatures['features'], $mapItem);
}
return json_encode($mapFeatures);
}
...
Using Postman I collected the following from the api request:
{
"type": "FeatureCollection",
"name": "comments",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"id": 143
},
"geometry": "0101000020E6100000E17A14AE47E111C085EB51B81E054A40"
},
...
]
}
Running the data through https://geojsonlint.com/ it comes back with Line 1: old-style crs member is not recommended, this object is equivalent to the default and should be removed
. Also stating that geometry was expected as an object but got a string which I assume is to do with the crs
property not decoding the geometry correctly.
Is there a different crs
that I need in order to get the geometry to be correctly decoded?
I unfortunately cannot change the data on the database to include a lat/long geometry as the current data is being used by another project which relies on it being in this format.
Discovered there is a package specifically to fix this issue available here:
https://github.com/mstaack/laravel-postgis
This just needs to be installed and referenced in the Controller that gets the coordinates.