[英]Laravel Parse `geometry` data type from `MYSQL` database to the original coordinates
我在 MYSQL 表中有一個名為geofences
的geometry
(幾何是數據類型和列名)列,我需要將此geometry
值解析或轉換為可讀的坐標數組。 所以我有一種方法已經完成了我想要的,但我相信有更好的方法來做到這一點,這是我的方法。
這就是我 select 我的數據庫中的幾何值的方法
DB::raw("(ST_AsText(geometry)) AS `geometry`")
查詢 output
"POLYGON((46.646748 24.562727,46.645079 24.561795,46.643556 24.563615,46.64539 24.564747,46.646748 24.562727,46.646748 24.562727))"
我在地理圍欄Geofence
中添加了一個訪問器。
public function getGeometryAttribute($value): array
{
$slice = Str::between($value, '((', '))');
if ($this->shape_type == 'circle') {
$slice = Str::between($value, '(', ')');
}
return collect(explode(',', $slice))->map(function ($point) {
$points = explode(' ', $point);
return [
'longitude' => $points[0],
'latitude' => $points[1],
];
})->toArray();
}
output 正如我所料
array:6 [
0 => array:2 [
"longitude" => "46.646748"
"latitude" => "24.562727"
]
1 => array:2 [
"longitude" => "46.645079"
"latitude" => "24.561795"
]
2 => array:2 [
"longitude" => "46.643556"
"latitude" => "24.563615"
]
3 => array:2 [
"longitude" => "46.64539"
"latitude" => "24.564747"
]
4 => array:2 [
"longitude" => "46.646748"
"latitude" => "24.562727"
]
5 => array:2 [
"longitude" => "46.646748"
"latitude" => "24.562727"
]
]
如果有人知道處理這種情況的更好方法,請在這里給我建議。 提前致謝。
朱爾斯法官在評論中發現了這個問題。
DB::raw("(ST_AsText(geometry)) AS `geometry`")
應該:
DB::raw("(ST_AsGeoJSON(geometry)) AS `geometry`")
output 現在是:
{
"type":"Polygon",
"coordinates":
[
[
[46.646748,24.562727],
[46.645079,24.561795],
[46.643556,24.563615],
[46.64539,24.564747],
[46.646748,24.562727],
[46.646748,24.562727]
]
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.