kokjan/backend/modules/gis/controllers/MapController.php

116 lines
3.2 KiB
PHP
Executable File

<?php
namespace backend\modules\gis\controllers;
use common\models\GisGeoFeatures;
use Yii;
use yii\db\Query;
use yii\web\Response;
class MapController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->render('index');
}
public function actionMap()
{
return $this->render('map');
}
public function actionGeojson()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$gis_base_id = Yii::$app->request->get('GisGeoFeatures')['gis_base_id'] ?? null;
$features = [];
// ✅ โหลดข้อมูลจากตาราง gis_tambon เสมอ
$tambonRows = (new Query())
->select(['id', 'tb_name_t', 'tb_code', 'ST_AsGeoJSON(geom) AS geojson'])
->from('gis_tambon')
->where(['id' => 7662])
->all();
foreach ($tambonRows as $row) {
$features[] = [
'type' => 'Feature',
'geometry' => json_decode($row['geojson']),
'properties' => [
'id' => $row['id'],
'name' => $row['tb_name_t'],
'interactive' => false
]
];
}
// ✅ โหลดข้อมูลจาก gis_geo_features ตาม gis_base_id (ถ้ามี)
$geoQuery = (new Query())
->select([
'f.id',
'f.name',
'ST_AsGeoJSON(f.geometry) AS geojson',
'b.color' // 👈 ดึงสีจากตาราง gis_base
])
->from(['f' => 'gis_geo_features'])
->leftJoin(['b' => 'gis_base'], 'f.gis_base_id = b.id'); // 👈 join
if ($gis_base_id) {
$geoQuery->where(['f.gis_base_id' => $gis_base_id]);
}
$geoRows = $geoQuery->all();
foreach ($geoRows as $row) {
$features[] = [
'type' => 'Feature',
'geometry' => json_decode($row['geojson']),
'properties' => [
'id' => $row['id'],
'name' => $row['name'],
'color' => $row['color'] ?? '#3388ff' // ✅ กำหนดสี default ถ้าไม่มี
]
];
}
return [
'type' => 'FeatureCollection',
'features' => $features
];
}
/*public function actionGeojson()
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$gis_base_id = Yii::$app->request->get('GisGeoFeatures')['gis_base_id'] ?? null;
$query = GisGeoFeatures::find();
if ($gis_base_id) {
$query->andWhere(['gis_base_id' => $gis_base_id]);
}
$features = $query->all();
$geojson = [
"type" => "FeatureCollection",
"features" => []
];
foreach ($features as $f) {
$geojson['features'][] = [
"type" => "Feature",
"geometry" => json_decode($f->geometry),
"properties" => [
"name" => $f->name
]
];
}
return $geojson;
}*/
}