如何将数据从 Laravel DB 连接转换为 GeoJSON

亚当_R

目前在 Laravel 中制作一个使用 MapboxGLJS 的项目。我目前有一个连接到的数据库服务器,其中包含我需要将其转换为包含 ID 和空间数据的 GeoJSON FeatureCollection 的评论。我已经看到了执行此操作的代码示例,我将在下面提供,但是当我尝试使用所述代码并尝试使用addSourceMapbox 方法时,它会返回Error: Input data is not a valid GeoJSON object..

评论控制器.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为了正确解码几何图形,我需要什么不同的东西吗?不幸的是,我无法更改数据库上的数据以包含经纬度几何,因为当前数据正在被另一个依赖于这种格式的项目使用。

亚当_R

发现这里有一个专门用于解决此问题的软件包:

https://github.com/mstaack/laravel-postgis

这只需要在获取坐标的控制器中安装和引用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章