目前在Laravel中制作一个使用MapBoxGljs的项目。目前,我已经连接到该数据库服务器,其中包含我需要将其转换为Geojson featurecollection的注释,该注释包括ID和空间数据。我已经看到了代码执行此操作的示例,但是当我尝试使用上述代码并尝试使用
addSource
mapbox方法时,它会带有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);
}
...
使用Postman,我从API请求中收集了以下内容:
{
"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"
},
...
]
}
通过https://geojsonlint.com/键入数据,它带有Line 1: old-style crs member is not recommended, this object is equivalent to the default and should be removed
。还指出几何形状被期望为一个对象,但我认为这是与
crs
属性无正确解码几何形状有关的字符串。为了使几何形状正确解码,我需要有不同的
crs
? 不幸的是,我无法将数据库上的数据更改为包括LAT/长几何形状,因为另一个项目正在使用此格式的另一个项目。
this:
"0101000020E6100000E17A14AE47E111C085EB51B81E054A40"
不是Geojson几何形状。我不确定是什么。它看起来像Postgis的本地格式(请参阅here),但我不知道那是什么,或者如何从postgis的外面转换。 Geojson几何形状看起来像:
{
"type": "LineString",
"coordinates": [[...]]
}
如果您可以访问GostGIS查询,则应使用
ST_AsGeoJSON
函数。这与CRS的无关 - 消息只是告诉您不要打扰添加
发现有一个专门用于解决此问题的软件包:
https://github.com/mstaack/laravel-postgis