Из выгрузки берем features->geometry->coordinates Массив вида
|
[[ ['23,23', '23,2323'], ['24,23', '24,2323'], ]]; |
Меняем местами координаты
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public function actionTest() { $array = [ ['34,43', '34,2323'], ['34,43', '34,2323'], ['34,43', '34,2323'], ]; $string = "[["; foreach ($array as $key) { $string .= '['; $string .= str_replace(',', '.', $key[1]) . ',' . str_replace(',', '.', $key[0]); $string .= '],'; } if (substr($string, -1) == ',') { $string = substr($string, 0, -1); } $string .= ']]'; $file = Yii::$app->otherFiles->getBucket('dump'); $file->saveFileContent('zona1.txt', $string); } |
Пример из жизни:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public function actionUpdateBaseZones($baseId) { $path = 'file.json'; $data = file_get_contents($path); $data = json_decode($data); foreach ($data->features as $features) { if ($features->geometry->type == 'Polygon') { $cords = []; $zona = intval(preg_replace("/[^0-9]/", '', $features->properties->description)); foreach ($features->geometry->coordinates as $coordinate) { foreach ($coordinate as $cord) { $cords[] = [$cord[1], $cord[0]]; } } $model = BaseZone::modify($baseId, $zona, '[' . json_encode($cords) . ']'); $model->save(); } } } |