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

问题描述 投票:0回答:2

目前在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:

php laravel-8 geojson mapbox-gl-js
2个回答
1
投票

"0101000020E6100000E17A14AE47E111C085EB51B81E054A40"

不是Geojson几何形状。我不确定是什么。它看起来像Postgis的本地格式(请参阅
here

),但我不知道那是什么,或者如何从postgis的外面转换。 Geojson几何形状看起来像:

{ "type": "LineString", "coordinates": [[...]] } 如果您可以访问GostGIS查询,则应使用

ST_AsGeoJSON

函数。
这与CRS的无关 - 消息只是告诉您不要打扰添加

crs属性,假设您的数据在EPSG中:4326.

    

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

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


0
投票
只需要在获得坐标的控制器中安装和引用。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.