├── Angkot.php ├── Count_Bobot_Tambah_Simpul.php ├── Dijkstra.php ├── DistanceTo.php ├── Get_koordinat_awal_akhir.php ├── GraphToArray.php ├── Koneksi.php ├── Main.php ├── README.md ├── Tambah_simpul.php └── index.php /Angkot.php: -------------------------------------------------------------------------------- 1 | koneksi = $k->connect(); 13 | } 14 | 15 | /** 16 | * MENENTUKAN JENIS ANGKUTAN UMUM YANG MELEWATI JALUR SHORTEST PATH 17 | * @RETURN JSON KOORDINAT ANGKOT 18 | */ 19 | public function angkot_shortest_path($exp_dijkstra, $old_simpul_awal, $old_simpul_akhir, $maxRow0, $maxRow1){ 20 | 21 | // misal exp_dijkstra[] = 1->5->6->7 22 | 23 | $m = 0; 24 | $old_awal = explode('-', $old_simpul_awal); // misal 4-5 25 | $old_akhir = explode('-', $old_simpul_akhir); // misal 8-7 26 | 27 | $ganti_a = 0; 28 | $ganti_b = 0; 29 | $simpulAwalDijkstra = $exp_dijkstra[0]; // 1 30 | 31 | $gabungSimpul_all = ""; 32 | $listAngkutanUmum = array(); 33 | $listSimpulAngkot = array(); 34 | 35 | // CARI SIMPUL_OLD (misal 4->6->5) SEBELUM KOORDINAT DIPECAH 36 | // misal 4-5 dipecah menjadi 4-6-5, berarti simpul_old awal = 4, simpul_old akhir = 5 37 | // total perulangannnya dikurang 1 38 | for($e = 0; $e < (count($exp_dijkstra) - 1); $e++) 39 | { 40 | if($e == 0) // awal 41 | { 42 | // dijalankan jika hasil algo hanya 2 simpul, example : 4->5 43 | if(count($exp_dijkstra) == 2 /* 2 simpul (4->5)*/) 44 | { 45 | // ada simpul baru di awal (10) dan di akhir (11), example 10->11 46 | if( $exp_dijkstra[0] == $maxRow0 && $exp_dijkstra[1] == $maxRow1 ){ 47 | 48 | $ganti_b = ($maxRow0 == $old_akhir[0]) ? $old_akhir[1] : $old_akhir[0]; 49 | 50 | $ganti_a = ($ganti_b == $old_awal[0]) ? $old_awal[1] : $old_awal[0]; 51 | } 52 | else{ 53 | // ada simpul baru di awal (10), example 10->5 54 | // maka cari simpul awal yg oldnya 55 | if( $exp_dijkstra[0] == $maxRow0){ 56 | 57 | $ganti_a = ($exp_dijkstra[1] == $old_awal[1]) ? $old_awal[0] : $old_awal[1]; 58 | 59 | $ganti_b = $exp_dijkstra[1]; 60 | } 61 | // ada simpul baru di akhir (10), example 5->10 62 | // maka cari simpul akhir yg oldnya 63 | else if( $exp_dijkstra[1]== $maxRow0 ){ 64 | 65 | $ganti_b = ($exp_dijkstra[0] == $old_akhir[0]) ? $old_akhir[1] : $old_akhir[0]; 66 | 67 | $ganti_a = $exp_dijkstra[0]; 68 | } 69 | // tidak ada penambahan simpul sama sekali 70 | else{ 71 | $ganti_a = $exp_dijkstra[0]; 72 | $ganti_b = $exp_dijkstra[1]; 73 | } 74 | } 75 | } 76 | // hasil algo lebih dr 2 : 4->5->8->7->9 etc .. 77 | else{ 78 | 79 | // ada simpul baru di awal (10), example 10->5 80 | // maka cari simpul awal yg oldnya 81 | /*5 == 5*/ 82 | if( $exp_dijkstra[0] == $maxRow0){ 83 | $ganti_a = ($exp_dijkstra[1] == $old_awal[1]) ? $old_awal[0]/*4*/ : $old_awal[1] /*5*/; 84 | } 85 | // tidak ada simpul baru di awal 86 | else{ 87 | $ganti_a = $exp_dijkstra[0]; 88 | } 89 | 90 | $ganti_b = $exp_dijkstra[++$m]; 91 | } 92 | } 93 | // akhir 94 | else if($e == count($exp_dijkstra) - 2) 95 | { 96 | // simpul terkhir dijkstra 97 | $simpul_akhir_dijkstra = $exp_dijkstra[(count($exp_dijkstra) - 1)]; 98 | 99 | // simpul sebelum terakhir dijkstra 100 | $simpul_sblm_akhir_dijkstra = $exp_dijkstra[(count($exp_dijkstra) - 2)]; 101 | 102 | // ga ada simpul baru 103 | // array_search() sama seperti in_array(), 104 | // bedanya return-nya $key/index yg dicari. 105 | if (false !== $key = array_search( $simpul_akhir_dijkstra, $old_akhir )) 106 | { 107 | $ganti_b = $old_akhir[$key]; // hasil 8 or 7 108 | } 109 | // ada simpul baru 110 | else if($simpul_akhir_dijkstra == $maxRow0 || $simpul_akhir_dijkstra == $maxRow1) 111 | { 112 | $ganti_b = ($simpul_sblm_akhir_dijkstra == $old_akhir[0]) ? $old_akhir[1] : $old_akhir[0]; 113 | } 114 | /*else 115 | { 116 | $ganti_b = $old_akhir[1]; // hasil 7 117 | }*/ 118 | 119 | $ganti_a = $exp_dijkstra[$m]; 120 | 121 | }else // tengah tengah 122 | { 123 | $ganti_a = $exp_dijkstra[$m]; 124 | $ganti_b = $exp_dijkstra[++$m]; 125 | } 126 | 127 | // GABUNG SIMPUL GAK BOLEH SAMA! --> ,5-5, 128 | //if($ganti_a != $ganti_b){ 129 | $gabung_a_b = "," . $ganti_a . "-" . $ganti_b . ","; // ,1-5, 130 | //}else{ 131 | // $gabung_a_b = ""; 132 | //} 133 | // GABUNG SIMPUL 134 | $gabungSimpul_all .= $gabung_a_b; 135 | $gabungSimpul = $gabung_a_b; 136 | 137 | // GET NOMOR TRAYEK ANGKOT 138 | $select = "SELECT * FROM angkutan_umum where simpul like '%" . $gabungSimpul . "%'"; 139 | $query = mysqli_query($this->koneksi, $select); 140 | //$fetch = mysqli_fetch_array($query, MYSQLI_ASSOC); 141 | 142 | $listAngkutan = array(); 143 | while($fetch = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 144 | array_push($listAngkutan, $fetch['no_trayek']); 145 | } 146 | 147 | // add no_trayek angkot 148 | $listAngkutanUmum["angkutan".$e] = $listAngkutan; 149 | // add simpul angkot 150 | array_push($listSimpulAngkot, $exp_dijkstra[$e]); 151 | } 152 | 153 | $replace_jalur = str_replace(',,', ',', $gabungSimpul_all); // ,1-5,,5-6,,6-7, => ,1-5,5-6,6-7, 154 | $select1 = "SELECT count(*) as jml_angkot, no_trayek FROM angkutan_umum where simpul like '%" . $replace_jalur . "%'"; 155 | $query1 = mysqli_query($this->koneksi, $select1); 156 | $fetch1 = mysqli_fetch_array($query1); 157 | 158 | 159 | // SEKALI NAIK ANGKOT 160 | // ADA 1/LEBIH ANGKOT YG MELEWATI JALUR DARI AWAL SAMPEK AKHIR 161 | // cukup gambar 1 koordinat angkot saja 162 | // die() sampai if ini 163 | if($fetch1['jml_angkot'] >= 1){ 164 | 165 | $siAngkot = $fetch1['no_trayek']; 166 | 167 | // get coordinate 168 | $select2 = "SELECT jalur FROM graph where simpul_awal = '" . $simpulAwalDijkstra . "'"; 169 | $query2 = mysqli_query($this->koneksi, $select2); 170 | $fetch2 = mysqli_fetch_array($query2); 171 | 172 | $json_coordinate = $fetch2['jalur']; 173 | 174 | // manipulate JSON 175 | $jObject = json_decode($json_coordinate, true); 176 | $jArrCoordinates = $jObject["coordinates"]; 177 | $latlngs = $jArrCoordinates[0]; 178 | 179 | // first latlng 180 | $lats = $latlngs[0]; 181 | $lngs = $latlngs[1]; 182 | 183 | $return_array = [['koordinat_angkot'=>['lat'=>$lats, 'lng'=>$lngs], 'no_angkot'=>$siAngkot]]; 184 | 185 | return $return_array; 186 | die(); 187 | } 188 | 189 | // BERKALI-KALI GANTI ANGKOT 190 | // ADA 1/LEBIH ANGKOT YG MELEWATI JALUR DARI AWAL SAMPEK AKHIR 191 | // PERINGKAS NOMOR TRAYEK 192 | $banyakAngkot = 0; 193 | $indexUrut = 0; 194 | $indexSimpulAngkot = 1; 195 | $lengthAngkutan = count($listAngkutanUmum); 196 | $angkotFix = array(); 197 | 198 | for($en = 0; $en < $lengthAngkutan; $en++ ) 199 | { 200 | // FIRST LOOPING 201 | // temporary sementara sebelum di array_intersect() 202 | $temps = array(); 203 | for($u = 0; $u < count($listAngkutanUmum['angkutan0']); $u++){ 204 | array_push($temps, $listAngkutanUmum['angkutan0'][$u]); 205 | } 206 | 207 | // SENCOND LOOPING 208 | if($en > 0 ){ 209 | $listSekarang = $listAngkutanUmum['angkutan0']; 210 | $listSelanjutnya = $listAngkutanUmum['angkutan'.$en]; 211 | 212 | // INTERSECTION 213 | // cari elemen yg ada di kedua array, yg tidak ada dihapus di $listSekarang 214 | // http://php.net/manual/en/function.array-intersect.php 215 | $listSekarang = array_intersect($listSekarang, $listSelanjutnya); 216 | $listSekarang = array_values($listSekarang); // 'reindex' array 217 | 218 | if(count($listSekarang) > 0){ 219 | 220 | unset($listSimpulAngkot[$indexSimpulAngkot]); 221 | $listSimpulAngkot = array_values($listSimpulAngkot); // 'reindex' array 222 | --$indexSimpulAngkot; 223 | 224 | unset($listAngkutanUmum['angkutan'.$en]); 225 | 226 | // sebelum akhir 227 | if($en == ($lengthAngkutan - 1)){ 228 | $tempDalam = array(); 229 | 230 | for($es = 0; $es < count($listSekarang); $es++){ 231 | array_push( $tempDalam, $listSekarang[$es] ); 232 | } 233 | 234 | $angkotFix['angkutanFix'.$indexUrut] = $tempDalam; 235 | ++$indexUrut; 236 | } 237 | } 238 | else if(count($listSekarang) == 0) 239 | { 240 | $angkotFix['angkutanFix'.$indexUrut] = $temps; 241 | 242 | $tempDalam = array(); 243 | for($es = 0; $es < count($listSelanjutnya); $es++){ 244 | array_push($tempDalam, $listSelanjutnya[$es]); 245 | } 246 | 247 | $listAngkutanUmum['angkutan0'] = $tempDalam; 248 | unset($listAngkutanUmum['angkutan'.$en]); 249 | 250 | ++$indexUrut; 251 | 252 | if($en == ($lengthAngkutan - 1)) 253 | { 254 | $tempDalam2 = array(); 255 | for($es = 0; $es < count($listSelanjutnya); $es++){ 256 | array_push($tempDalam2, $listSelanjutnya[$es]); 257 | } 258 | 259 | $angkotFix['angkutanFix'.$indexUrut] = $tempDalam2; 260 | ++$indexUrut; 261 | } 262 | } 263 | ++$indexSimpulAngkot; 264 | } 265 | } 266 | 267 | // buat return 268 | $return_array = []; 269 | 270 | // GAMBAR 2 ATAU LEBIH KOORDINAT ANGKUTAN UMUM 271 | foreach($listSimpulAngkot as $r => $simpulx) 272 | { 273 | // get coordinate simpulAngkutan 274 | $select = "SELECT jalur FROM graph where simpul_awal = '" . $simpulx . "'"; 275 | $query = mysqli_query($this->koneksi, $select); 276 | $fetch = mysqli_fetch_array($query, MYSQLI_ASSOC); 277 | 278 | // dapatkan koordinat Lat,Lng dari field koordinat (3) 279 | $json_db = $fetch['jalur']; 280 | 281 | // get JSON 282 | $jObject = json_decode($json_db, true); 283 | $jArrCoordinates = $jObject['coordinates']; 284 | 285 | // get first coordinate JSON 286 | $latlngs = $jArrCoordinates[0]; 287 | $lats = $latlngs[0]; 288 | $lngs = $latlngs[1]; 289 | 290 | $json['lat'] = $lats; 291 | $json['lng'] = $lngs; 292 | $angkot = $angkotFix['angkutanFix'.$r]; 293 | 294 | $gabung_array = ['koordinat_angkot'=>$json, 'no_angkot'=>$angkot]; 295 | array_push($return_array, $gabung_array); 296 | } 297 | 298 | return $return_array; 299 | } 300 | } -------------------------------------------------------------------------------- /Count_Bobot_Tambah_Simpul.php: -------------------------------------------------------------------------------- 1 | distanceTo($lat_0, $lng_0, $lat_1, $lng_1); 26 | }else{ 27 | for($i = 0; $i < 1; $i++){ 28 | 29 | // get JSON coordinate 30 | $latlngs = $jArrCoordinates[$index]; 31 | 32 | $lat_0 = $latlngs[0]; 33 | $lng_0 = $latlngs[1]; 34 | 35 | // get coordinate again 36 | $latlngs1 = $jArrCoordinates[++$index]; 37 | 38 | $lat_1 = $latlngs1[0]; 39 | $lng_1 = $latlngs1[1]; 40 | 41 | // simpan jarak 42 | // @extends 43 | $bobot += $this->distanceTo($lat_0, $lng_0, $lat_1, $lng_1); 44 | 45 | if($index == $limit) break; //jika dah smpe ke tengah, break; misal 0-72 46 | else --$i; 47 | } 48 | } 49 | 50 | return $bobot; 51 | } 52 | } 53 | ?> -------------------------------------------------------------------------------- /Dijkstra.php: -------------------------------------------------------------------------------- 1 | 10'; 8 | [0][1] = '2->11'; 9 | [0][2] = '3->40'; 10 | 11 | [1][0] = '0->10'; 12 | [1][1] = '2->55'; 13 | [1][2] = '4->20'; 14 | 15 | [2][0] = '0->11'; 16 | [2][1] = '1->55'; 17 | [2][3] = '1->54'; 18 | 19 | [3][0] = '4->89'; 20 | [3][0] = '4->89'; 21 | 22 | [4][0] = '0->90'; 23 | [4][3] = '3->89'; 24 | * GRAPH ARRAY YANG SUDAH DIKERJAKAN DALAM MPERHITUNGAN ALGORITMA DIJKSTRA : 25 | [0][0] = '1->10->y'; 26 | [0][1] = '2->11->y'; 27 | [0][2] = '3->40->y'; 28 | [1][0] = '0->10->y'; 29 | ... 30 | * @param arg_graph array[][] 31 | * @param simpulAwal int 32 | * @param simpulTujuan int 33 | * @return string, contoh 0->1->4 34 | */ 35 | function jalurTerpendek($arg_graph, $simpulAwal, $simpulTujuan){ 36 | 37 | // SIMPUL_AWAL == SIMPUL_TUJUAN, MAKA DIE() 38 | // 0 == 0 39 | if($simpulAwal == $simpulTujuan){ 40 | return json_encode(['status'=>'error','error'=>'lokasi_anda_sudah_dekat','teks'=>'Lokasi Anda Sudah Dekat','content'=>'']); 41 | } 42 | 43 | // SIMPUL_AWAL OR SIMPUL_TUJUAN NOT FOUND 44 | if(!array_key_exists($simpulAwal, $arg_graph) || !array_key_exists($simpulTujuan, $arg_graph)){ 45 | return print_r(json_encode(['status'=>'error','error'=>'simpul_input_tidak_ditemukan','teks'=>"could not find the input : $simpulAwal or $simpulTujuan", 'content'=>''])); 46 | } 47 | 48 | $graph = $arg_graph; 49 | $simpul_awal = $simpulAwal; 50 | $simpul_maju = $simpulAwal; 51 | $simpul_tujuan = $simpulTujuan; 52 | $jml_simpul = count($arg_graph); 53 | 54 | /** 55 | * TANDAI SIMPUL YANG AKAN DIKERJAKAN DENGAN TANDA BINTANG (*) 56 | * MISAL SOAL : CARI JALUR TERPENDEK DARI SIMPUL 0 KE SIMPUL 4 ! 57 | --------- ---------- --------- --------- --------- 58 | | 0(*) | 1(*) | 2(*) | 3 | 4(*) | <-- KOLOM SIMPUL 59 | --------- ---------- --------- --------- --------- 60 | | 0->1=10 | 1->0=10 | 2->0=11 | 3->4=89 | 4->0=90 | <-- BARIS; BOBOT = 90 61 | | 0->2=11 | 1->2=55 | 2->1=55 | | 4->3=89 | 62 | | 0->3=40 | 1->4=20 | 2->3=54 | | | 63 | --------- ---------- --------- --------- --------- 64 | * MAKA HASILNYA $simpulYangDikerjakan = array(0, 1, 2, 4); 65 | */ 66 | $simpulYangDikerjakan = array(); 67 | 68 | // UNTUK MENYIMPAN NILAI-NILAI * YANG DITANDAI 69 | $simpulYangSudahDikerjakan_bawah = array(); 70 | 71 | $nilaiSimpulYgDitandai = 0; 72 | $nilaiSimpulFixYgDitandai = 0; 73 | 74 | 75 | // #HANDLE PERULANGAN 76 | // PERULANGAN INI TIDAK AKAN BERHENTI (--$perulangan;) SAMPAI ALGORITMA DIJKSTRA MENEMUKAN 1 JALUR TERPENDEK 77 | // 78 | for($perulangan = 0; $perulangan < 1; $perulangan++) 79 | { 80 | // UNTUK MNDAPATKAN 1 BOBOT PALING MINIMUM DARI SETIAP SIMPUL 81 | $perbandinganSemuaBobot = array(); 82 | 83 | // DAFTARKAN SIMPUL pertama YANG AKAN DIKERJAKAN KE DALAM ARRAY 84 | if(!in_array($simpul_maju, $simpulYangDikerjakan)){ 85 | array_push($simpulYangDikerjakan, $simpul_maju); 86 | } 87 | 88 | /** 89 | * PERULANGAN (KOLOM) SIMPUL-SIMPUL YANG DITANDAI 90 | --------- ---------- --------- --------- --------- 91 | | 0(*) | 1(*) | 2(*) | 3 | 4(*) | <-- KOLOM SIMPUL 92 | --------- ---------- --------- --------- --------- 93 | | 0->1=10 | 1->0=10 | 2->0=11 | 3->4=89 | 4->0=90 | 94 | | 0->2=11 | 1->2=55 | 2->1=55 | | 4->3=89 | 95 | | 0->3=40 | 1->4=20 | 2->3=54 | | | 96 | --------- ---------- --------- --------- --------- 97 | * PERULANGANNYA : array(0, 1, 2, 4); 98 | */ 99 | for($perulanganSimpul = 0; $perulanganSimpul < count($simpulYangDikerjakan); $perulanganSimpul++) 100 | { 101 | // HITUNG JUMLAH BARIS PER KOLOM SIMPUL 102 | // 0(*) = 3 BARIS; (0->1=10; 0->2=11; 0->3=40) 103 | // 1(*) = 3 BARIS; 104 | // 2(*) = 3 BARIS; 105 | // 4(*) = 2 BARIS; 106 | $jumlah_baris = count($graph[ $simpulYangDikerjakan[$perulanganSimpul] ]); 107 | 108 | // TAMPUNG BOBOT MINIMUM DARI SETIAP KOLOM SIMPUL BERDASARKAN BARIS SCR URUT[0][0],[0][1] DST 109 | $bobot_baris = array(); 110 | 111 | // JUMLAH BARIS YANG BELUM DIKERJAKAN 112 | $baris_belum_dikerjakan = 0; 113 | 114 | 115 | /** 116 | * PERULANGAN BARIS TABEL, CARI BOBOT DARI 1 KOLOM SIMPUL 117 | --------- 118 | | 0(*) | <-- KOLOM SIMPUL 119 | --------- 120 | | 0->1=10 | <-- baris; bobot = 10; 121 | | 0->2=11 | <-- baris; bobot = 11; 122 | | 0->3=40 | <-- baris; bobot = 40; 123 | --------- 124 | */ 125 | for($start_baris = 0; $start_baris < $jumlah_baris; $start_baris++) 126 | { 127 | // AMBIL VALUE ARRAY graph[][] 128 | // MISAL graph[0][0] = '1->10'; VALUENYA BERARTI '1->10' 129 | $ruas_dan_bobot = $graph[ $simpulYangDikerjakan[$perulanganSimpul] ][$start_baris];//pasti berurutan [0][0],[0][1] dst 130 | 131 | // PECAH RUAS & BOBOT BERDASARKAN '->' 132 | // RUAS : explode[0] = 1 133 | // BOBOT : explode[1] = 10 134 | $explode = explode('->', $ruas_dan_bobot); 135 | 136 | /** 137 | * CARI BOBOT YG BELUM DIKERJAKAN (YG TIDAK ADA TANDA ->Y) 138 | * MISAL : 139 | [0][0] = '1->10->y'; 140 | [0][1] = '2->11'; <---- ini yang belum dikerjakan / tidak ada tanda y 141 | */ 142 | if(count($explode) == 2) 143 | { 144 | // TOTAL BARIS YG BELUM ->Y 145 | $baris_belum_dikerjakan += 1; 146 | 147 | // CEK KOLOM SIMPUL APAKAH SUDAH DITANDAI (*) APA BLOM, KLO UDH BERARTI NILAI * TIDAK DITAMBAH LAGI / 0 148 | // KALO BLM DITANDAI, BERARTI NILAI * BERNILAI $nilaiSimpulYgDitandai 149 | if(!empty($simpulYangSudahDikerjakan_bawah)) 150 | { 151 | if(in_array($simpulYangDikerjakan[$perulanganSimpul], $simpulYangSudahDikerjakan_bawah)){ 152 | $nilaiSimpulYgDitandai = 0; 153 | }else{ 154 | $nilaiSimpulYgDitandai = $nilaiSimpulFixYgDitandai; 155 | } 156 | } 157 | 158 | /** 159 | * MASUKKAN BOBOT BARIS YANG SUDAH DIUPDATE KE DALAM ARRAY 160 | * ILUSTRASI MENGUPDATE BOBOT DALAM TABEL : 161 | -------------- 162 | | 0(*) | <-- KOLOM SIMPUL 163 | -------------- 164 | | 0->1=10 (13) | <-- bobot diupdate = 13; 165 | | 0->2=11 (12) | <-- bobot diupdate = 12; 166 | | 0->3=40 (45) | <-- bobot diupdate = 45; 167 | -------------- 168 | */ 169 | array_push($bobot_baris, ($explode[1]+$nilaiSimpulYgDitandai)); // (bobot+0) or (bobot+232) 170 | 171 | // UPDATE JUGA BOBOT BARIS PADA graph[][] 172 | // MISAL : '1->13' 173 | $graph[ $simpulYangDikerjakan[$perulanganSimpul] ][$start_baris] = $explode[0] . "->" . $explode[1] . $nilaiSimpulYgDitandai; 174 | } 175 | } 176 | 177 | 178 | // JIKA ADA BARIS DI KOLOM BELUM ->Y SEMUA, MAKA LAKUKAN IF DI BAWAH INI : 179 | if($baris_belum_dikerjakan > 0) 180 | { 181 | // DAPATKAN BOBOT MINIMUM 182 | for($index_bobot = 0; $index_bobot < count($bobot_baris); $index_bobot++){ 183 | if($bobot_baris[$index_bobot] <= $bobot_baris[0]){ 184 | $bobot_baris[0] = $bobot_baris[$index_bobot]; 185 | } 186 | } 187 | 188 | // BOBOT TERKECIL DARI SETIAP KOLOM SIMPUL 189 | array_push($perbandinganSemuaBobot, $bobot_baris[0]); 190 | 191 | }// end if jika ->y atau ->t belum semua dikerjakan 192 | else{ 193 | // Jika baris di kolom sudah ->y semua, maka lakukan else di bawah ini 194 | // System.out.println("=======||Baris sudah ->y semua||======="); 195 | } 196 | 197 | // DAFTARKAN SIMPUL SIMPUL YANG baru selesai DIKERJAKAN 198 | if(!in_array($simpulYangDikerjakan[$perulanganSimpul], $simpulYangSudahDikerjakan_bawah)){ 199 | array_push( $simpulYangSudahDikerjakan_bawah, $simpulYangDikerjakan[$perulanganSimpul] ); 200 | } 201 | }// end for perulanganSimpul 202 | 203 | 204 | /** 205 | * DAPATKAN 1 BOBOT PALING MINIMUM DARI SIMPUL YG DITANDAI (*) 206 | ------------- ------------ --------- 207 | | 0(*) | 1(*) | 2(*) | 208 | ------------- ------------ --------- 209 | | 0->1=10(*) | 1->0=10 | 2->0=11 | 210 | | 0->2=11(*) | 1->2=55 | 2->1=55 | 211 | | 0->3=40 | 1->4=20(*) | 2->3=54 | 212 | ------------- ------------ --------- 213 | */ 214 | for($min_indexAntarBobotYgDitandai = 0; $min_indexAntarBobotYgDitandai < count($perbandinganSemuaBobot); $min_indexAntarBobotYgDitandai++) 215 | { 216 | if($perbandinganSemuaBobot[$min_indexAntarBobotYgDitandai] <= $perbandinganSemuaBobot[0]){ 217 | $perbandinganSemuaBobot[0] = $perbandinganSemuaBobot[$min_indexAntarBobotYgDitandai]; 218 | } 219 | } 220 | 221 | // DAPATKAN INDEX SIMPUL+BOBOTNYA YG ASLI DARI SIMPUL YG DITANDAI 222 | $indexAwalAsli = 0; // index simpulnya 223 | $baris_belum_dikerjakan1 = 0; 224 | $dapat_indexAsliBobot = 0; 225 | $simpul_lama = 0; 226 | //$length_baris = count($graph[$simpulYangDikerjakan[$indexAwalAsli]]); 227 | 228 | foreach($simpulYangDikerjakan as $idx => $v) 229 | { 230 | /** 231 | JUMLAH BARIS per KOLOM SIMPUL 232 | -------------- 233 | | 0(*) | <-- KOLOM SIMPUL 234 | -------------- 235 | | 0->1=10 (13) | <-- baris 1 236 | | 0->2=11 (12) | <-- baris 2 237 | | 0->3=40 (45) | <-- baris 3 238 | -------------- 239 | */ 240 | $length_baris = $graph[$simpulYangDikerjakan[$idx]]; 241 | 242 | for($baris1 = 0; $baris1 < $length_baris; $baris1++) 243 | { 244 | if( isset($graph[ $simpulYangDikerjakan[$indexAwalAsli] ][$baris1]) ) 245 | { 246 | $bobot_baris_dan_ruas1 = $graph[ $simpulYangDikerjakan[$indexAwalAsli] ][$baris1]; 247 | $explode1 = array(); 248 | $explode1 = explode('->', $bobot_baris_dan_ruas1); 249 | if(count($explode1) == 2) 250 | { 251 | if($perbandinganSemuaBobot[0] == $explode1[1]) 252 | { 253 | $dapat_indexAsliBobot = $baris1; 254 | $simpul_lama = $simpulYangDikerjakan[$indexAwalAsli]; 255 | $simpul_maju = $explode1[0]; 256 | $baris_belum_dikerjakan1 += 1; 257 | } 258 | }// end if cek ->y atau ->t 259 | }// end if cek baris != null 260 | else{ 261 | break; 262 | } 263 | }// end for limit baris = 100 264 | 265 | $indexAwalAsli++; // index simpul di tambah 1 266 | }// end for simpul yang dikerjakan 267 | 268 | 269 | // BULETIN BOBOT MINIMUM YANG UDH DIDAPAT dan HAPUS RUAS YANG BERHUBUNGAN 270 | if($baris_belum_dikerjakan1 > 0){ 271 | $graph[$simpul_lama][$dapat_indexAsliBobot] = $graph[$simpul_lama][$dapat_indexAsliBobot] . "->y"; 272 | 273 | // HAPUS RUAS LAIN 274 | for($min_kolom = 0; $min_kolom < $jml_simpul; $min_kolom++) 275 | { 276 | // JUMLAH BARIS PER KOLOM SIMPUL 277 | $length_baris1 = count($graph[$min_kolom]); 278 | 279 | for($min_baris = 0; $min_baris < $length_baris1; $min_baris++) 280 | { 281 | if(isset($graph[$min_kolom][$min_baris])) 282 | { 283 | $ruasYgAkanDihapus = $graph[$min_kolom][$min_baris]; 284 | $explode3 = explode('->', $ruasYgAkanDihapus); 285 | if(count($explode3) == 2){ 286 | if($explode3[0] == $simpul_maju){ 287 | $graph[$min_kolom][$min_baris] = $graph[$min_kolom][$min_baris]+"->t"; 288 | } 289 | } 290 | }// end if cek baris != null 291 | }// end for baris 292 | }// end for kolom 293 | }// end if cek baris_belum_dikerjakan sudah ->y atau ->t semua apa belum 294 | 295 | // ====================================================== 296 | // # JIKA ALUR GRAPH ANDA SALAH, MAKA DIE() SAMPAI IF INI 297 | // ====================================================== 298 | if(!isset($perbandinganSemuaBobot[0])) 299 | return json_encode(['status'=>'error', 'error'=>'alur_graph_anda_salah', 'teks'=>'Alur graph Anda Salah', 'content'=>'']); 300 | 301 | 302 | // NILAI * YG DITANDAI 303 | $nilaiSimpulFixYgDitandai = $perbandinganSemuaBobot[0]; 304 | 305 | // LOOPING $perulangan lagi jika SIMPUL_MAJU != SIMPUL_TUJUAN 306 | if($simpul_maju != $simpul_tujuan){ 307 | --$perulangan; 308 | } 309 | else{ 310 | break; // akhiri perulangan 311 | } 312 | }// end for handle perulangan 313 | 314 | 315 | 316 | // TARUH SIMPUL GABUNGAN KE ARRAY; MISAL : SIMPUL 6-10 317 | $gabungSimpulPilihan = array(); 318 | for($h = 0; $h < $jml_simpul; $h++) 319 | { 320 | // JUMLAH BARIS PER KOLOM SIMPUL 321 | $length_baris2 = count($graph[$h]); 322 | 323 | for($n = 0; $n < $length_baris2; $n++) 324 | { 325 | if(isset($graph[$h][$n])) 326 | { 327 | $str_graph = $graph[$h][$n]; 328 | if( substr($str_graph, (strlen($str_graph)-1), strlen($str_graph)) == "y" ){ 329 | $explode4 = explode('->', $graph[$h][$n]); 330 | $simpulGabung = $h . "-" . $explode4[0]; 331 | 332 | array_push($gabungSimpulPilihan, $simpulGabung); 333 | } 334 | }// end if cek isi graph != null 335 | }// end for looping baris 336 | }// end looping kolom (simpul) 337 | 338 | 339 | // UNTUK MEMASUKKAN SIMPUL YG SUDAH DIURUTKAN (DARI SIMPUL TUJUAN KE SIMPUL AWAL). (NANTI DIREVERSE ARRAYNYA) 340 | $simpulFix_finish = array(); 341 | 342 | // MASUKKAN PERTAMA KALI SIMPUL TUJUAN (SIMPUL AKHIR) KE ARRAY DGN INDEX 0. (NANTI DIBALIK(REVERSE) ARRAYNYA) 343 | array_push($simpulFix_finish, $simpul_tujuan); 344 | 345 | $simpul_explode = $simpul_tujuan; 346 | for($v = 0; $v < 1; $v++) 347 | { 348 | for($w = 0; $w < count($gabungSimpulPilihan); $w++) 349 | { 350 | $explode_simpul = $gabungSimpulPilihan[$w]; 351 | $explode5 = explode('-', $explode_simpul); 352 | if($simpul_explode == $explode5[1]) 353 | { 354 | array_push($simpulFix_finish, $explode5[0]); 355 | $simpul_explode = $explode5[0]; 356 | } 357 | if($simpul_explode == $simpul_awal){ 358 | break; 359 | } 360 | } 361 | 362 | if($simpul_awal != $simpul_explode){ 363 | --$v; 364 | }else{ 365 | break; 366 | } 367 | }// end for cari simpul yang dibuletin lalu dibandingkan dgn simpul_tujuan 368 | 369 | 370 | // ARRAY DI BALIK INDEXNYA; JADI SIMPUL TUJUAN DI PINDAH POSISI KE AKHIR INDEX ARRAY 371 | $simpulFix_finish_reverse = array_reverse($simpulFix_finish); 372 | 373 | $jalur_terpendek = ""; 374 | for($x = 0; $x < count($simpulFix_finish_reverse); $x++) 375 | { 376 | if($x == (count($simpulFix_finish_reverse)-1)) 377 | { 378 | $jalur_terpendek .= $simpulFix_finish_reverse[$x]; 379 | }else{ 380 | $jalur_terpendek .= $simpulFix_finish_reverse[$x] . "->"; 381 | } 382 | } 383 | 384 | $json['status'] = 'success'; 385 | $json['success'] = 'generate_jalur_terpendek'; 386 | $json['teks'] = 'Jalur berhasil dibuat'; 387 | $json['content'] = $jalur_terpendek; 388 | 389 | return json_encode($json); 390 | 391 | }// end function jalurTerpendek 392 | } 393 | ?> -------------------------------------------------------------------------------- /DistanceTo.php: -------------------------------------------------------------------------------- 1 | koneksi = $k->connect(); 13 | } 14 | 15 | /** 16 | * @FUNGSI 17 | * MENYELEKSI SIMPUL YG AKAN DIKERJAKAN 18 | * jika ada simpul 1-0 dan 0-1 maka yg dikerjakan hanya 1-0 ( karena koordinat 1-0 sama dengan 0-1 (koordinat hanya dibalik) ) 19 | * @PARAMETER 20 | * latx : latitude user atau SMK 21 | * lngx : longitude user atau destination 22 | * context : MainActivity context 23 | * @RETURN 24 | * JSON (index coordinates, nodes0, nodes1) 25 | */ 26 | public function Get_simpul($latx, $lngx, $id_buang) 27 | { 28 | // your coordinate 29 | $user_lat = $latx; 30 | $user_lng = $lngx; 31 | 32 | // TAMPUNG NODE DARI FIELD SIMPUL_AWAL DAN TUJUAN DI TABEL GRAPH, TRS DIGABUNG; contoh 1,0 DAN MASUKKAN KE ARRAY 33 | $barisDobel = array(); 34 | $indexBarisYgDikerjakan = array(); 35 | 36 | // filter simpul yg akan dikerjakan 37 | $select = mysqli_query($this->koneksi, "SELECT * FROM graph where id not in($id_buang) and simpul_awal != '' and simpul_tujuan != '' and jalur != '' and bobot != ''"); 38 | 39 | // AMBIL SIMPUL DARI FIELD SIMPUL_AWAL DAN SIMPUL_TUJUAN DI TABEL GRAPH, TRS DIGABUNG; contoh 1,0 TRS MASUKKAN KE ARRAY 40 | // LOOPING DI BAWAH INI UNTUK MEMERIKSA BARIS DOBEL {SIMPULNYA YG DOBEL} 1,0 -> 0,1 {1,0 DIHITUNG TAPI 0,1 GAK DIHITUNG} 41 | $i = 0; 42 | while($field = mysqli_fetch_array($select, MYSQLI_ASSOC)) 43 | { 44 | // node dari field simpul_awal 45 | $fieldSimpulAwal = $field['simpul_awal']; 46 | 47 | // node dari field simpul_akhir 48 | $fieldSimpulTujuan = $field['simpul_tujuan']; 49 | 50 | $gabungSimpul = $fieldSimpulAwal . "," . $fieldSimpulTujuan; 51 | $gabung_balikSimpul = $fieldSimpulTujuan . "," . $fieldSimpulAwal; 52 | 53 | // SELEKSI RUAS YANG DOBEL; CONTOH : 1,0 == 0,1 54 | // PILIH SALAH SATU, MISAL : 1,0 55 | if(empty($barisDobel)) 56 | { 57 | array_push($barisDobel, $gabung_balikSimpul); 58 | 59 | // field id pada tabel graph 60 | array_push($indexBarisYgDikerjakan, $field['id']); 61 | }else 62 | { 63 | if(!in_array($gabungSimpul, $barisDobel)) 64 | { 65 | array_push($barisDobel, $gabung_balikSimpul); 66 | 67 | // field id pada tabel graph 68 | array_push($indexBarisYgDikerjakan, $field['id']); 69 | } 70 | } 71 | } 72 | 73 | // QUERY BARIS YANG GAK DOBEL 74 | $id_where_in = implode(',', $indexBarisYgDikerjakan);//echo $id_where_in; 75 | $selectRow = mysqli_query($this->koneksi, "SELECT * FROM graph where id in($id_where_in)"); 76 | 77 | // CARI JARAK 78 | // LOOPING SEMUA RECORD 79 | $obj = new stdClass(); 80 | for($index_obj = 0; $kolom = mysqli_fetch_array($selectRow, MYSQLI_ASSOC); $index_obj++){ 81 | 82 | // VARIABEL BUAT CARI 1 JARAK DALAM 1 RECORD (1 record isinya banyak koordinat) 83 | // simpan jarak user ke koordinat simpul dalam meter 84 | $jarakUserKeKoordinatSimpul = array(); 85 | 86 | // dapatkan koordinat Lat,Lng dari field koordinat (3) 87 | $json = $kolom['jalur']; 88 | 89 | // decode JSON coordinate 90 | $jsonOneRoute = json_decode($json, true); 91 | 92 | // data json 93 | $dataNode = $jsonOneRoute['nodes'][0]; 94 | $nodeSplit = explode('-', $dataNode); 95 | $node0 = $nodeSplit[0]; 96 | $node1 = $nodeSplit[1]; 97 | $dataBobot = $jsonOneRoute['distance_metres'][0]; 98 | $countCoordinate= (count($jsonOneRoute['coordinates']) - 1); // dikurang 1 karena buat index array 99 | 100 | // hitung jarak user ke koordinat angkutan umum 101 | foreach($jsonOneRoute['coordinates'] as $coordinate){ 102 | $json_lat = $coordinate[0]; 103 | $json_lng = $coordinate[1]; 104 | 105 | $jarak = $this->distanceTo($user_lat, $user_lng, $json_lat, $json_lng); 106 | array_push($jarakUserKeKoordinatSimpul, $jarak); 107 | } 108 | 109 | // CARI bobot yg paling kecil 110 | $index_koordinatSimpul = 0; 111 | for($m = 0; $m < count($jarakUserKeKoordinatSimpul); $m++) 112 | { 113 | if($jarakUserKeKoordinatSimpul[$m] <= $jarakUserKeKoordinatSimpul[0]) 114 | { 115 | $jarakUserKeKoordinatSimpul[0] = $jarakUserKeKoordinatSimpul[$m]; 116 | 117 | // index array dari value yg terkecil 118 | $index_koordinatSimpul = $m; 119 | } 120 | } 121 | 122 | // field id dari table graph 123 | $row_id = $kolom['id']; 124 | 125 | // masukkan index koordinat array, bobot terkecil dan jumlah koordinat ke JSON 126 | $list = array(); 127 | $list['row_id'] = (int)$row_id; 128 | $list['index'] = $index_koordinatSimpul; 129 | $list['bobot'] = $jarakUserKeKoordinatSimpul[0]; 130 | $list['nodes'] = $dataNode; 131 | $list['count_koordinat']= $countCoordinate; 132 | 133 | // Create json 134 | // example output : 135 | // {"0" : [{"row_id":17, "index":"7", "bobot":"427.66", "count_koordinat":"15", "nodes":"0-1"}]} 136 | $obj->$index_obj = [$list]; 137 | 138 | }//end looping baris DB 139 | 140 | //echo "
"; 141 | //print_r($obj); 142 | //echo ""; 143 | $x = 0; 144 | $y = 0; 145 | $stdCount = count((array)$obj); 146 | 147 | // cari bobot terkecil dari JSON 148 | for($s = 0; $s < $stdCount; $s++){ 149 | 150 | if($s == 0){ 151 | // std Object 152 | $x = $obj->{$s}[0]['bobot']; 153 | $rowId = $obj->{$s}[0]['row_id']; 154 | $indexCoordinate = $obj->{$s}[0]['index']; 155 | $countCoordinate = $obj->{$s}[0]['count_koordinat']; 156 | $nodes = $obj->{$s}[0]['nodes']; 157 | }else 158 | { 159 | $y = $obj->{$s}[0]['bobot']; 160 | // dapatkan value terkecil (bobot) 161 | if($y <= $x){ 162 | $x = $y; 163 | // std Object 164 | $rowId = $obj->{$s}[0]['row_id']; 165 | $indexCoordinate = $obj->{$s}[0]['index']; 166 | $countCoordinate = $obj->{$s}[0]['count_koordinat']; 167 | $nodes = $obj->{$s}[0]['nodes']; 168 | } 169 | } 170 | } 171 | 172 | 173 | // nodes : 0-1 174 | $exp_nodes = explode('-', $nodes); 175 | $field_simpul_awal = $exp_nodes[0]; 176 | $field_simpul_tujuan = $exp_nodes[1]; 177 | 178 | // Koordinat yg didapat di awal atau diakhir, maka gak perlu nambah simpul 179 | if($indexCoordinate == 0 || $indexCoordinate == $countCoordinate){ 180 | 181 | //tentukan simpul awal atau akhir yg dekat dgn posisi user 182 | if($indexCoordinate == 0) 183 | { 184 | // nodes di field simpul_awal 185 | $fix_simpul_awal = $field_simpul_awal; 186 | }else if($indexCoordinate == $countCoordinate) 187 | { 188 | // nodes di field simpul_akhir 189 | $fix_simpul_awal = $field_simpul_tujuan; 190 | } 191 | 192 | $jadi_json['status'] = "tidak_tambah_simpul"; 193 | } 194 | //Koordinat yang didapat berada ditengah2 simpul 0 - 1 (misal) 195 | else 196 | { 197 | // cari simpul dobel, simpulnya dibalik 198 | $select = "SELECT count(id) as jum_id FROM graph where simpul_awal = ".$field_simpul_tujuan." and simpul_tujuan = ".$field_simpul_awal; 199 | $query = mysqli_query($this->koneksi, $select); 200 | $dobel = mysqli_fetch_array($query, MYSQLI_ASSOC); 201 | 202 | //ada simpul yg dobel (1,0) dan (0,1) 203 | if($dobel['jum_id'] == 1){ 204 | $jadi_json['status'] = "tambah_simpul_double"; 205 | } 206 | //gak dobel, hanya (1,0) 207 | else if($dobel['jum_id'] == 0){ 208 | $jadi_json['status'] = "tambah_simpul_single"; 209 | } 210 | } 211 | 212 | // get ID berdasarkan simpul yg dobel 213 | $selectx= "SELECT count(id) as simpul_dobel, id FROM graph where simpul_awal = ".$field_simpul_tujuan." and simpul_tujuan = ".$field_simpul_awal; 214 | $queryx = mysqli_query($this->koneksi, $selectx); 215 | $dobelx = mysqli_fetch_array($queryx, MYSQLI_ASSOC); 216 | // jika ada IDnya dan ada simpul baru ditengah2 jalur 217 | if($dobelx['simpul_dobel'] == 1 && ($jadi_json['status'] == 'tambah_simpul_single' || $jadi_json['status'] == 'tambah_simpul_double')){ 218 | $rowId .= ','.$dobelx['id']; 219 | }else{ 220 | // tidak ada id yg dibuang 221 | $rowId = 0; 222 | } 223 | 224 | // JSON 225 | $jadi_json['row_id'] = $rowId; 226 | $jadi_json['node_simpul_awal0'] = $field_simpul_awal; 227 | $jadi_json['node_simpul_awal1'] = $field_simpul_tujuan; 228 | $jadi_json['index_coordinate_json'] = $indexCoordinate; 229 | 230 | 231 | return json_encode($jadi_json); 232 | }//public 233 | } 234 | ?> -------------------------------------------------------------------------------- /GraphToArray.php: -------------------------------------------------------------------------------- 1 | 1=10 | 1->0=10 | 2->0=11 | 3->4=89 | 4->0=90 | 12 | | 0->2=11 | 1->2=55 | 2->1=55 | | 4->3=89 | 13 | | 0->3=40 | 1->4=20 | 2->3=54 | | | 14 | --------- ---------- --------- --------- --------- 15 | * DARI TABEL DI ATAS DIKONVERSI MENJADI : 16 | [0][0] = '=> 1->10'; 17 | [0][1] = '=> 2->11'; 18 | [0][2] = '=> 3->40'; 19 | 20 | [1][0] = '=> 0->10'; 21 | [1][1] = '=> 2->55'; 22 | [1][2] = '=> 4->20'; 23 | 24 | [2][0] = '=> 0->11'; 25 | [2][1] = '=> 1->55'; 26 | [2][3] = '=> 1->54'; 27 | 28 | [3][0] = '=> 4->89'; 29 | [3][0] = '=> 4->89'; 30 | 31 | [4][0] = '=> 0->90'; 32 | [4][3] = '=> 3->89'; 33 | */ 34 | 35 | class GraphToArray 36 | { 37 | // menampung graph array 2 dimensi 38 | public $graph = array(array()); 39 | 40 | // koneksi DB 41 | public $koneksi; 42 | 43 | /** 44 | * before Action 45 | * set CONNECTION 46 | */ 47 | function __construct(){ 48 | $k = new Koneksi(); 49 | $this->koneksi = $k->connect(); 50 | } 51 | 52 | /** 53 | * konversi graph tabel ke graph array 54 | * @return array[][] 55 | */ 56 | public function graphArray() 57 | { 58 | // set index baris dan kolom array 59 | $temp_baris = ""; 60 | $kolom = 0; 61 | 62 | $select = mysqli_query($this->koneksi, "SELECT * FROM graph order by CONVERT(simpul_awal, SIGNED INTEGER), CONVERT(simpul_tujuan, SIGNED INTEGER) asc"); 63 | while($field = mysqli_fetch_array($select, MYSQL_ASSOC)){ 64 | 65 | // baris array 66 | $baris = $field['simpul_awal']; 67 | 68 | // cek index baris; graph[index_baris][..] 69 | if($temp_baris == ""){ 70 | // pertama kali dijalankan 71 | $temp_baris = $baris; 72 | }else{ 73 | // baris berikutnya tidak sama dgn sebelumnya ( graph[0][..] -> graph[1][..] ) 74 | // maka reset kolom = 0 ( graph[0][..] -> graph[1][0] ) 75 | if($temp_baris != $baris){ 76 | $kolom = 0; 77 | $temp_baris = $baris; 78 | } 79 | } 80 | 81 | //tidak ada derajat keluar 82 | $value = ""; 83 | if($field['simpul_tujuan'] == "" && $field['jalur'] == "" && $field['bobot'] == ""){ 84 | // masukkan ke graph array 85 | $value = ";"; 86 | } 87 | // ada derajat keluar 88 | else{ 89 | // example output : 2->789.98 90 | // masukkan ke graph array 91 | $value = $field['simpul_tujuan'] . "->" . $field['bobot']; //simpul_tujuan dan bobot 92 | } 93 | 94 | $this->graph[$baris][$kolom] = $value; 95 | $kolom++; 96 | } 97 | 98 | return $this->graph; 99 | } 100 | } 101 | ?> -------------------------------------------------------------------------------- /Koneksi.php: -------------------------------------------------------------------------------- 1 | db); 7 | } 8 | 9 | public function connectPDO(){ 10 | return $conn = new PDO("mysql:host=localhost;dbname=$this->db", 'root', 'abcd.123'); 11 | } 12 | } 13 | ?> -------------------------------------------------------------------------------- /Main.php: -------------------------------------------------------------------------------- 1 | koneksi = $koneksi->connect(); 31 | 32 | // DELETE TEMPORARY ID 33 | $k = $koneksi->connect(); 34 | mysqli_query($k, "DELETE FROM graph where temp = 'Y'"); 35 | 36 | // MEMPREDIKSI 2 SIMPUL BARU 37 | $this->maxRowDB(); 38 | 39 | // GET ARRAY GRAPH[..][..] 40 | $graph = new GraphToArray(); 41 | $graphArray = $graph->graphArray(); 42 | $this->graph = $graphArray; 43 | 44 | // id_old yg gak dikerjakan pas Get_simpul() didalam fungsi getSimpulAwalAkhirJalur() 45 | $this->id_buang = 0; 46 | 47 | } 48 | 49 | public function core($lat0, $lng0, $lat1, $lng1) 50 | { 51 | $this->getSimpulAwalAkhirJalur($lat0, $lng0, 'awal', /*tambahan-->*/ $this->id_buang); 52 | $this->getSimpulAwalAkhirJalur($lat1, $lng1, 'akhir', /*tambahan-->*/ $this->id_buang); 53 | 54 | // ALGORITMA DIJKSTRA 55 | // GET SHORTEST PATH ( ex : 1->0->4 ) 56 | $dijkstra = new Dijkstra(); 57 | // {"status":"error","error":"simpul_input_tidak_ditemukan","teks":"could not find the input : 10 or ","content":""} 58 | // {"status":"error","error":"lokasi_anda_sudah_dekat","teks":"Lokasi Anda Sudah Dekat","content":""} 59 | // {"status":"success","success":"generate_jalur_terpendek","content":"10->3->2->11"} 60 | 61 | $json = $dijkstra->jalurTerpendek($this->graph, $this->simpul_awal, $this->simpul_akhir); 62 | $decode = json_decode($json, true); 63 | $status = $decode['status']; 64 | $content = $decode['content']; 65 | 66 | // ERROR ALGORITMA DIJKSTRA 67 | if($status == 'error'){ 68 | //echo 'ERROR :: [' . $decode['error'] .']'; 69 | $jsonPolyline = json_encode(['jalur_shortest_path'=>[], 'error'=>$decode]); 70 | return $jsonPolyline; 71 | } 72 | // ALGORITMA DIJKSTRA LANCAR JAYA 73 | else{ 74 | $jsonPolyline = $this->drawRoute($content); 75 | return $jsonPolyline; 76 | } 77 | } 78 | 79 | public function getSimpulAwalAkhirJalur($lat, $lng, $kerjain, $id_buang) 80 | { 81 | // DAPATKAN KOORDINAT ANGKUTAN UMUM TERDEKAT DARI POSISI KITA / POSISI TUJUAN 82 | $get = new Get_koordinat_awal_akhir(); 83 | $jsonPosisi = $get->Get_simpul($lat, $lng, $id_buang); 84 | 85 | // DECODE JSON 86 | // {"status":"tidak_tambah_simpul","node_simpul_awal0":"5","node_simpul_awal1":"6","index_coordinate_json":0} 87 | // {"status":"tambah_simpul_double","node_simpul_awal0":"5","node_simpul_awal1":"8","index_coordinate_json":3} 88 | // {"status":"tidak_tambah_simpul","node_simpul_awal0":"10","node_simpul_awal1":"3","index_coordinate_json":0} 89 | $j = json_decode($jsonPosisi, true); 90 | $status = $j['status']; 91 | $this->id_buang = $j['row_id']; // id_lama yg gak dikerjakan pas getSimpulAwalAkhirJalur('akhir') jika ada simpul baru : #4->5 --> 4->6->5 92 | $node_simpul_awal0 = $j['node_simpul_awal0']; 93 | $node_simpul_awal1 = $j['node_simpul_awal1']; 94 | $index_coordinate = $j['index_coordinate_json']; 95 | 96 | // CEK JALUR ANGKUTAN UMUM 97 | // tidak perlu tambah simpul 98 | if( $status == 'tidak_tambah_simpul' ) 99 | { 100 | // tentukan simpul awal atau akhir yg dekat dgn posisi user 101 | ($index_coordinate == 0) ? /*awal*/$fix_simpul_awal = $node_simpul_awal0 : /*akhir*/$fix_simpul_awal = $node_simpul_awal1; 102 | 103 | // kerjain simpul awal 104 | if($kerjain == "awal"){ 105 | 106 | // return 107 | $this->old_simpul_awal = $node_simpul_awal0 . "-" . $node_simpul_awal1; 108 | $this->simpul_awal = $fix_simpul_awal; // misal 0 109 | } 110 | // kerjain simpul akhir 111 | else{ 112 | 113 | // return 114 | $this->old_simpul_akhir = $node_simpul_awal0 . "-" . $node_simpul_awal1; 115 | $this->simpul_akhir = $fix_simpul_awal; // misal 0 116 | } 117 | } 118 | // perlu tambah simpul karena double 119 | else if( $status == 'tambah_simpul_double' ) 120 | { 121 | // kerjain simpul awal 122 | if($kerjain == "awal"){ 123 | 124 | // cari simpul (5,4) dan (4-5) di Tambah_simpul.php 125 | $tb = new Tambah_simpul(); 126 | //echo $node_simpul_awal0 .', '.$node_simpul_awal1 .','. $index_coordinate; 127 | $jadi_json = $tb->dobelSimpul($node_simpul_awal0, $node_simpul_awal1, $index_coordinate, $this->graph); 128 | 129 | // decode json 130 | $d = json_decode($jadi_json, true); 131 | 132 | // return 133 | $this->old_simpul_awal = $d['simpul_lama']; 134 | $this->simpul_awal = $d['simpul_baru']; // misal 6 135 | $this->graph = json_decode($d['graph'], true); // graph[][] 136 | 137 | }else{ 138 | 139 | // cari simpul (5,4) dan (4-5) di Tambah_simpul.php 140 | $tb = new Tambah_simpul(); 141 | $jadi_json = $tb->dobelSimpul($node_simpul_awal0, $node_simpul_awal1, $index_coordinate, $this->graph); 142 | 143 | // decode json 144 | $d = json_decode($jadi_json, true); 145 | 146 | // return 147 | $this->old_simpul_akhir = $d['simpul_lama']; 148 | $this->simpul_akhir = $d['simpul_baru']; // misal 4 149 | $this->graph = json_decode($d['graph'], true); // graph[][] 150 | 151 | } 152 | } 153 | // perlu tambah simpul karena single 154 | else if( $status == 'tambah_simpul_single' ) 155 | { 156 | if($kerjain == "awal"){ 157 | 158 | // cari simpul (5,4) dan (4-5) di Tambah_simpul.php 159 | $tb = new Tambah_simpul(); 160 | $jadi_json = $tb->singleSimpul($node_simpul_awal0, $node_simpul_awal1, $index_coordinate, $this->graph); 161 | 162 | // decode json 163 | $d = json_decode($jadi_json, true); 164 | 165 | // return 166 | $this->old_simpul_awal = $d['simpul_lama']; 167 | $this->simpul_awal = $d['simpul_baru']; // misal 6 168 | $this->graph = json_decode($d['graph'], true); // graph[][] 169 | 170 | }else{ 171 | 172 | // cari simpul (5,4) di Tambah_simpul.php 173 | $tb = new Tambah_simpul(); 174 | $jadi_json = $tb->singleSimpul($node_simpul_awal0, $node_simpul_awal1, $index_coordinate, $this->graph); 175 | 176 | // decode json 177 | $d = json_decode($jadi_json, true); 178 | 179 | // return 180 | $this->old_simpul_akhir = $d['simpul_lama']; 181 | $this->simpul_akhir = $d['simpul_baru']; // misal 4 182 | $this->graph = json_decode($d['graph'], true); // graph[][] 183 | } 184 | } 185 | } 186 | 187 | /** 188 | * GAMBAR JALUR SHORTEST PATH & ANGKOTNYA 189 | * @PARAM $shortest_path string; misal 1->9->0 190 | * @RETURN $semua_latlng json; misal [{'lat':6, 'lng':10},{dst..}] 191 | */ 192 | public function drawRoute($shortest_path) 193 | { 194 | $exp_shortest_path = explode("->", $shortest_path); 195 | $start = 0; 196 | $semua_latlng = array(); 197 | 198 | for($i = 0; $i < (count($exp_shortest_path)-1); $i++){ 199 | 200 | $select = "SELECT jalur FROM graph where simpul_awal =" . $exp_shortest_path[$start] . " and simpul_tujuan =" . $exp_shortest_path[(++$start)]; 201 | $query = mysqli_query($this->koneksi, $select); 202 | $fetch = mysqli_fetch_array($query, MYSQLI_ASSOC); 203 | 204 | $json = json_decode($fetch['jalur'], true); 205 | $koordinat = $json['coordinates']; 206 | 207 | // DAPATKAN KOORDINAT LAT,LNG DARI FIELD JALUR 208 | // get coordinate JSON 209 | for($w = 0; $w < count($koordinat); $w++){ 210 | 211 | $latlngs = $koordinat[$w]; 212 | $lats = $latlngs[0]; 213 | $lngs = $latlngs[1]; 214 | 215 | $lat_lng['lat'] = $lats; 216 | $lat_lng['lng'] = $lngs; 217 | 218 | array_push($semua_latlng, $lat_lng); 219 | } 220 | } 221 | 222 | // CARI ANGKOT YANG LEWAT JALUR SHORTEST PATH 223 | $a = new Angkot(); 224 | // [{"koordinat_angkot":{"lat":-6.2880200009082,"lng":106.91497564316},"no_angkot":["T04"]}] 225 | $angkot_array = $a->angkot_shortest_path($exp_shortest_path, $this->old_simpul_awal, $this->old_simpul_akhir, /*tambahan-->*/ $this->maxRow0, $this->maxRow1); 226 | 227 | // return 228 | $return_json = ['jalur_shortest_path'=>$semua_latlng, 'angkot'=>$angkot_array]; 229 | return json_encode($return_json); 230 | } 231 | 232 | /** 233 | * MEMPREDIKSI 2 SIMPUL BARU SEBELUM DILAKUKAN PENAMBAHAN SIMPUL 234 | * @RETURN $maxRow0, $maxRow1 : int 235 | */ 236 | public function maxRowDB(){ 237 | 238 | $select = "SELECT max(CONVERT(simpul_awal, SIGNED INTEGER)) as max_sa, max(CONVERT(simpul_tujuan, SIGNED INTEGER)) as max_st FROM graph"; 239 | $query = mysqli_query($this->koneksi, $select); 240 | $fetch = mysqli_fetch_array($query, MYSQLI_ASSOC); 241 | 242 | $max_simpul_db = 0; 243 | $max_simpulAwal_db = $fetch['max_sa']; 244 | $max_simpulTujuan_db = $fetch['max_st']; 245 | 246 | if( $max_simpulAwal_db >= $max_simpulTujuan_db ){ 247 | $max_simpul_db = $max_simpulAwal_db; 248 | }else{ 249 | $max_simpul_db = $max_simpulTujuan_db; 250 | } 251 | 252 | // return 253 | $this->maxRow0 = ($max_simpul_db+1); 254 | $this->maxRow1 = ($max_simpul_db+2); 255 | } 256 | }// end CLASS 257 | 258 | if(isset($_POST['koord_user'], $_POST['koord_destination'])){ 259 | 260 | $koord_user = json_decode($_POST['koord_user'], true); 261 | $koord_destination = json_decode($_POST['koord_destination'], true); 262 | 263 | //print_r($koord_user); 264 | //echo $koord_destination; 265 | //die(); 266 | $a = new Main(); 267 | $shortest_path = $a->core($koord_user['lat'], $koord_user['lng'], $koord_destination['lat'], $koord_destination['lng']); 268 | //$shortest_path = $a->core(-6.338843516774621, 106.85749053955078, -6.35914593243388, 106.86967849731445);# terakhir 269 | //$shortest_path = $a->core(-6.365543586486009, 106.87787532806396, -6.35914593243388, 106.86967849731445);#fix/25-08 270 | //$shortest_path = $a->core(-6.297895039696718, 106.86504364013672, -6.2927762525228745, 106.8698501586914);#fix/25-08 271 | //$shortest_path = $a->core(-6.355136695311239, 106.8585205078125, -6.35914593243388, 106.86967849731445); 272 | 273 | // return 274 | echo $shortest_path; 275 | //echo "ere"; 276 | } 277 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dijkstra-algorithm 2 | Implementation of dijkstra algorithm using php on google map 3 | -------------------------------------------------------------------------------- /Tambah_simpul.php: -------------------------------------------------------------------------------- 1 | koneksi = $k->connect(); 20 | $this->koneksiPDO = $k->connectPDO(); 21 | } 22 | 23 | /** 24 | * @FUNGSI 25 | * MEYISIPKAN SIMPUL BARU 26 | * misal simpul 5-4, disisipkan menjadi 5-6-4 27 | * dan simpul 4-5, disisipkan menjadi 4-6-5 28 | * @PARAMETER 29 | * nodes0 : misal {"nodes": "5-4"} maka nodes_awal0 = 5 30 | * nodes1 : misal {"nodes": "5-4"} maka nodes_awal1 = 4 31 | * index_koordinat_json : index array koordinat di JSON 32 | * graph[][] : array untuk menampung graph dari DB 33 | * example output : graph[5][0] = 4->439.281 34 | * graph[6][0] = 1->216.281 35 | * @RETURN json 36 | * simpul_lama = nodes0 + "-" + nodes1 37 | * simpul_baru = simpul awal 38 | * new_id 39 | * graph[][] 40 | */ 41 | public function dobelSimpul($nodes0, $nodes1, $index_koordinat_json, $graph){ 42 | 43 | // HITUNG SIMPUL YANG ASLI DULU (5-4), BUKAN YANG DOBEL (4-5) 44 | //============================================================== 45 | $index_kolom_graph = (count($graph[$nodes0])); // new index --> graph[][new_index] 46 | $index_kolom_graph = ""; 47 | 48 | // cari index kolomnya nodes1 (4) dari graph[baris][index kolom] 49 | // JUMLAH BARIS PER KOLOM SIMPUL 50 | $jumlah_baris = count($graph[$nodes0]); 51 | 52 | for($l = 0; $l < $jumlah_baris; $l++){ 53 | 54 | if(isset($graph[$nodes0][$l])){ 55 | 56 | // [5][0] = 4->721.666 57 | $simpulAwal = $graph[$nodes0][$l]; 58 | 59 | // 4->721.666 60 | $explode = explode('->', $simpulAwal); 61 | $simpul_tujuan_ = $explode[0]; // 4 62 | 63 | // jika 4 == 4 (node1) 64 | if(trim($simpul_tujuan_) == trim($nodes1)) 65 | { 66 | // index kolom; example graph[baris][kolom] 67 | $index_kolom_graph = $l; 68 | } 69 | } 70 | else break; 71 | }// for 72 | 73 | 74 | // index dari graph[baris][kolom] yang akan di edit 75 | $baris = $nodes0; 76 | $kolom = $index_kolom_graph; 77 | 78 | // ambil koordinatnya dari simpul 5-4 79 | $select = "SELECT jalur FROM graph where simpul_awal = ".$nodes0." and simpul_tujuan = ".$nodes1; 80 | $query = mysqli_query($this->koneksi, $select); 81 | $fetch = mysqli_fetch_array($query, MYSQLI_ASSOC); 82 | 83 | // -- 84 | // get coordinates JSON 85 | $json_coordinates = $fetch['jalur']; 86 | $jsonOneRoute = json_decode($json_coordinates, true); 87 | // data json 88 | $dataNode = $jsonOneRoute['nodes'][0]; 89 | $nodeSplit = explode('-', $dataNode); 90 | $node0 = $nodeSplit[0]; 91 | $node1 = $nodeSplit[1]; 92 | $dataBobot = $jsonOneRoute['distance_metres'][0]; 93 | $countCoordinate = count($jsonOneRoute['coordinates']); 94 | $jArrCoordinates = $jsonOneRoute['coordinates']; 95 | // -- 96 | 97 | // cari maksimal simpul, (buat penomoran simpul baru) 98 | $select1 = "SELECT max(CONVERT(simpul_awal, SIGNED INTEGER)) as max_sa, max(CONVERT(simpul_tujuan, SIGNED INTEGER)) as max_st FROM graph"; 99 | $query1 = mysqli_query($this->koneksi, $select1); 100 | $fetch1 = mysqli_fetch_array($query1, MYSQLI_ASSOC); 101 | 102 | $max_simpul_db = 0; 103 | $max_simpulAwal_db = $fetch1['max_sa']; 104 | $max_simpulTujuan_db = $fetch1['max_st']; 105 | if($max_simpulAwal_db >= $max_simpulTujuan_db){ 106 | $max_simpul_db = $max_simpulAwal_db; 107 | }else{ 108 | $max_simpul_db = $max_simpulTujuan_db; 109 | } 110 | 111 | // pecah koordinat dari AWAL->TENGAH 112 | $limit = $index_koordinat_json; 113 | // @extends 114 | $bobot = $this->Count_Bobot_Tambah_Simpul(0, $limit, $jArrCoordinates); // 0, koordinat tengah, jSON coordinates 115 | 116 | //replace array graph[5][0] = 6->888.6 117 | $graph[$baris][$kolom] = ($max_simpul_db+1) . "->" . $bobot; 118 | 119 | // buat dan simpan (new record) json koordinat yang baru ke DB 120 | $start_loop = 0; 121 | // @local 122 | $this->createAndSave_NewJsonCoordinate($start_loop, $limit, $jArrCoordinates, $nodes0, ($max_simpul_db + 1), $bobot); 123 | 124 | // reset bobot 125 | $bobot = 0; 126 | 127 | // pecah koordinat dari TENGAH->AKHIR 128 | $start_loop1 = $index_koordinat_json; 129 | $limit1 = (count($jArrCoordinates) - 1); // - 1 karena array mulai dari 0 130 | // @extends 131 | $bobot = $this->Count_Bobot_Tambah_Simpul($index_koordinat_json, $limit1, $jArrCoordinates); // coordinate tengah sampai akhir 132 | 133 | 134 | // new array graph[6][0] = 4->777.4 135 | $graph[($max_simpul_db+1)][0] = $nodes1 . "->" . $bobot; //didefinisikan [0] karena index baru di graph[][] 136 | 137 | // buat dan simpan (new record) json koordinate yang baru ke DB 138 | $this->createAndSave_NewJsonCoordinate($start_loop1, $limit1, $jArrCoordinates, ($max_simpul_db + 1), $nodes1, $bobot); 139 | 140 | // reset bobot 141 | $bobot = 0; 142 | 143 | 144 | // HITUNG SIMPUL YANG DOBEL (4-5), BUKAN YANG ASLI (5-4) 145 | //============================================================== 146 | // dibalik, nodes0 jadi nodes1; example (5-4) jadi (4-5) 147 | $t_nodes0 = $nodes1; // 4 148 | $t_nodes1 = $nodes0; // 5 149 | 150 | $index_kolom_graph1 = count($graph[$t_nodes0]); // new index --> graph[][new_index] 151 | $index_kolom_graph1 = ""; 152 | $nodes_inside_kolom = ""; 153 | 154 | // cari index kolomnya dari graph[4][index kolomnya] 155 | // JUMLAH BARIS PER KOLOM SIMPUL 156 | $jumlah_baris1 = count($graph[$t_nodes0]); 157 | for($l = 0; $l < $jumlah_baris1; $l++){ 158 | 159 | if(isset($graph[$t_nodes0][$l])){ 160 | 161 | // == dapatkan simpul tujuan, example : 5->9585.340 162 | $simpulAwal = $graph[$t_nodes0][$l]; 163 | $explode1 = explode('->', $simpulAwal); 164 | 165 | $nodes_inside_kolom = $explode1[0]; 166 | 167 | if(trim($nodes_inside_kolom) == $t_nodes1){ 168 | $index_kolom_graph1 = $l; 169 | } 170 | 171 | }else break; 172 | }//for 173 | 174 | 175 | // index dari graph[baris1][kolom1] yang akan di edit 176 | $baris1 = $t_nodes0; 177 | $kolom1 = $index_kolom_graph1; 178 | 179 | // ambil koordinatnya dari simpul 4-5 180 | $select2 = "SELECT jalur FROM graph where simpul_awal = " . $t_nodes0 . " and simpul_tujuan = " . $t_nodes1; 181 | $query2 = mysqli_query($this->koneksi, $select2); 182 | $fetch2 = mysqli_fetch_array($query2, MYSQLI_ASSOC); 183 | 184 | // -- 185 | // get coordinates JSON 186 | $json_coordinates1 = $fetch2['jalur']; 187 | $jsonOneRoute1 = json_decode($json_coordinates1, true); 188 | // data json 189 | $dataNode1 = $jsonOneRoute1['nodes'][0]; 190 | $nodeSplit1 = explode('-', $dataNode1); 191 | $node01 = $nodeSplit1[0]; 192 | $node11 = $nodeSplit1[1]; 193 | $dataBobot1 = $jsonOneRoute1['distance_metres'][0]; 194 | $countCoordinate1 = count($jsonOneRoute1['coordinates']); 195 | $jArrCoordinates1 = $jsonOneRoute1['coordinates']; 196 | // -- 197 | 198 | // pecah koordinat dari AWAL->TENGAH 199 | $index_dobel_koordinat_json = ( (count($jArrCoordinates1)-1) - $index_koordinat_json ); 200 | // @extends 201 | $bobot = $this->Count_Bobot_Tambah_Simpul(0, $index_dobel_koordinat_json, $jArrCoordinates1); // 0, koordinat awal ke tengah, JSONArray coordinate 202 | 203 | //replace array graph[4][0] = 6->777.4 204 | $graph[$baris1][$kolom1] = ($max_simpul_db+1) . "->" . $bobot; 205 | 206 | 207 | // buat dan simpan (new record) json koordinate yang baru ke DB 208 | $start_loop2 = 0; 209 | // @local 210 | $this->createAndSave_NewJsonCoordinate($start_loop2, $index_dobel_koordinat_json, $jArrCoordinates1, $baris1, ($max_simpul_db + 1), $bobot); 211 | 212 | 213 | // reset bobot 214 | $bobot = 0; 215 | 216 | // pecah koordinat dari TENGAH->AKHIR 217 | $limit2 = (count($jArrCoordinates1) - 1); // - 1 karena array mulai dari 0 218 | // @extends 219 | $bobot = $this->Count_Bobot_Tambah_Simpul($index_dobel_koordinat_json, $limit2, $jArrCoordinates1); // koordinat tengah sampai akhir 220 | 221 | //replace array graph[6][1] = 5->888.6 222 | $graph[($max_simpul_db+1)][1] = $t_nodes1 . "->" . $bobot; // didefinisikan [1] karena sdh ada index 0 di graph[][] 223 | 224 | // buat dan simpan (new record) json koordinate yang baru ke DB 225 | // @local 226 | $this->createAndSave_NewJsonCoordinate($index_dobel_koordinat_json, $limit2, $jArrCoordinates1, ($max_simpul_db + 1), $t_nodes1, $bobot); 227 | 228 | // return 229 | $simpul_lama = $nodes0 . "-" . $nodes1; 230 | $simpul_baru = ($max_simpul_db + 1); 231 | 232 | $jadi_json['simpul_lama'] = $simpul_lama; 233 | $jadi_json['simpul_baru'] = $simpul_baru; 234 | $jadi_json['temporary_id']= $this->temporary_id; 235 | $jadi_json['graph'] = json_encode($graph); 236 | 237 | return json_encode($jadi_json); 238 | }// function dobelSimpul 239 | 240 | 241 | 242 | /* 243 | * @FUNGSI 244 | * MEYISIPKAN SIMPUL BARU 245 | * misal simpul 5-4, disisipkan menjadi 5-6-4 246 | * @PARAMETER 247 | * nodes_awal0 : misal {"nodes": "5-4"} maka nodes_awal0 = 5 248 | * nodes_awal1 : misal {"nodes": "5-4"} maka nodes_awal1 = 4 249 | * index_koordinat_json : index array koordinat di JSON 250 | * graph[][] : array untuk menampung graph dari DB 251 | * example output : graph[5][0] = 4->439.281 252 | * graph[6][0] = 1->216.281 253 | * @RETURN 254 | * simpul_lama 255 | * simpul_baru 256 | * new_id 257 | */ 258 | 259 | public function singleSimpul($nodes0, $nodes1, $index_koordinat_json, $graph){ 260 | 261 | // HITUNG SIMPUL YANG ASLI (5-4) 262 | //============================================================== 263 | $index_kolom_graph = count($graph[$nodes0]); // new index --> graph[][new_index] 264 | $index_kolom_graph = ""; 265 | 266 | // cari index kolomnya nodes_akhir1 (4) dari graph[baris][kolom] 267 | // JUMLAH BARIS PER KOLOM SIMPUL 268 | $jumlah_baris = count($graph[$nodes0]); 269 | for($l = 0; $l < $jumlah_baris; $l++){ 270 | 271 | if(isset($graph[$nodes0][$l])){ 272 | 273 | $simpulAwal = $graph[$nodes0][$l]; // [5][0] = 4->721.666 274 | $explode = explode('->', $simpulAwal); 275 | 276 | // 6->721.666 277 | $value_node_array = $explode[0]; 278 | 279 | // jika 4 == 4 (node_akhir1) 280 | if( trim($value_node_array) == trim($nodes1) ){ 281 | 282 | // index kolom; example graph[baris][kolom] 283 | $index_kolom_graph = $l; 284 | } 285 | }else break; 286 | }//for 287 | 288 | 289 | // index dari graph[baris][kolom] yang akan di edit 290 | $baris = $nodes0; 291 | $kolom = $index_kolom_graph; 292 | 293 | // ambil koordinatnya dari simpul 5-4 294 | $select = "SELECT jalur FROM graph where simpul_awal = ".$nodes0." and simpul_tujuan = ".$nodes1; 295 | $query = mysqli_query($this->koneksi, $select); 296 | $fetch = mysqli_fetch_array($query, MYSQLI_ASSOC); 297 | 298 | // cocokan jalur, misal : 299 | // ada jalur 10-3, tapi di input 3-10, maka die() 300 | if($fetch['jalur'] == "") die(json_encode(['status'=>'jalur_not_found'])); 301 | 302 | // -- 303 | // get coordinates JSON 304 | $json_coordinates = $fetch['jalur']; 305 | $jsonOneRoute = json_decode($json_coordinates, true); 306 | // data json 307 | $dataNode = $jsonOneRoute['nodes'][0]; 308 | $nodeSplit = explode('-', $dataNode); 309 | $node0 = $nodeSplit[0]; 310 | $node1 = $nodeSplit[1]; 311 | $dataBobot = $jsonOneRoute['distance_metres'][0]; 312 | $countCoordinate = count($jsonOneRoute['coordinates']); 313 | $jArrCoordinates = $jsonOneRoute['coordinates']; 314 | // -- 315 | 316 | // cari maksimal simpul, (buat penomoran simpul baru) 317 | $select1 = "SELECT max(CONVERT(simpul_awal, SIGNED INTEGER)) as max_sa FROM graph"; 318 | $query1 = mysqli_query($this->koneksi, $select1); 319 | $fetch1 = mysqli_fetch_array($query1, MYSQLI_ASSOC); 320 | 321 | // max nodes field['simpul_awal'] 322 | $max_simpul_db = $fetch1['max_sa']; 323 | 324 | // pecah koordinat dari AWAL->TENGAH 325 | $limit = $index_koordinat_json; 326 | // @extends 327 | $bobot = $this->Count_Bobot_Tambah_Simpul(0, $limit, $jArrCoordinates); // 0, koordinat tengah, jSON coordinates 328 | 329 | //replace array graph[5][0] = 6->888.6 330 | $graph[$baris][$kolom] = ($max_simpul_db+1) . "->" . $bobot; 331 | 332 | 333 | // buat dan simpan (new record) json koordinat yang baru ke DB 334 | $start_loop = 0; 335 | // @local 336 | $this->createAndSave_NewJsonCoordinate($start_loop, $limit, $jArrCoordinates, $nodes0, ($max_simpul_db + 1), $bobot); 337 | 338 | 339 | // reset bobot 340 | $bobot = 0; 341 | 342 | // pecah koordinat dari TENGAH->AKHIR 343 | $start_loop1 = $index_koordinat_json; // - 1 karena array mulai dari 0 344 | $limit1 = (count($jArrCoordinates) - 1); // - 1 karena array mulai dari 0 345 | // @extends 346 | $bobot = $this->Count_Bobot_Tambah_Simpul($index_koordinat_json, $limit1, $jArrCoordinates); // coordinate tengah sampai akhir 347 | 348 | 349 | // new array graph[6][0] = 4->777.4 350 | $graph[($max_simpul_db+1)][0] = $nodes1 . "->" . $bobot; //didefinisikan [0] karena index baru di graph[][] 351 | 352 | // buat dan simpan (new record) json koordinate yang baru ke DB 353 | // @local 354 | $this->createAndSave_NewJsonCoordinate($start_loop1, $limit1, $jArrCoordinates, ($max_simpul_db + 1), $nodes1, $bobot); 355 | 356 | // return 357 | $simpul_lama = $nodes0 . "-" . $nodes1; 358 | $simpul_baru = ($max_simpul_db + 1); 359 | 360 | $jadi_json['simpul_lama'] = $simpul_lama; 361 | $jadi_json['simpul_baru'] = $simpul_baru; 362 | $jadi_json['temporary_id']= $this->temporary_id; 363 | $jadi_json['graph'] = json_encode($graph); 364 | 365 | return json_encode($jadi_json); 366 | } 367 | 368 | /* @FUNGSI 369 | * MEMBUAT DAN MENYIMPAN COORDINATES BARU DALAM BENTUK JSON KE DB 370 | * @PARAMETER 371 | * mulai : mulai looping, misal 0 372 | * limit : index array koordinat, misal i[7] maka limit = 7 373 | * jArrCoordinates : Koordinat dari DB dalam bentuk JSONArray 374 | * new_id : id record baru 375 | * //baris : baris multidimensi array, misal i[baris][kolom] 376 | * //max_simpul_db : jumlah max record pada tabel graph 377 | * new_bobot : bobot baru dari pemecahan koordinat jalur 378 | * dbInsert : insert ke database 379 | * dbRead : baca record database 380 | * @RETURN 381 | * no return 382 | */ 383 | public function createAndSave_NewJsonCoordinate($mulai, $limit, $jArrCoordinates, $field_simpul_awal, $field_simpul_akhir, $new_bobot){ 384 | 385 | // JSON for save new coordinate 386 | $json_baru = array(); 387 | $new_root_coordinates = array(); 388 | 389 | /** 390 | # looping dari coordinate awal sampai ke coordinate tengah 391 | # atau 392 | # looping dari coordinate tengah sampai ke coordinate akhir 393 | # then, move old coordinate to new coordinate 394 | */ 395 | for($ne = $mulai; $ne <= $limit; $ne++){ 396 | $latlng = $jArrCoordinates[$ne]; 397 | // cut coordinates 398 | array_push($new_root_coordinates, $latlng); 399 | } 400 | 401 | // gabung nodes 402 | $gabung_nodes = $field_simpul_awal . '-' . $field_simpul_akhir; 403 | 404 | // create new JSON 405 | $json_baru['nodes'] = [$gabung_nodes]; 406 | $json_baru['coordinates'] = $new_root_coordinates; 407 | $json_baru['distance_metres'] = [$new_bobot]; 408 | 409 | // json coordinates 410 | $jalur_baru = json_encode($json_baru); 411 | 412 | // INSERT USING PDO 413 | try { 414 | $conn = $this->koneksiPDO; 415 | 416 | // set the PDO error mode to exception 417 | $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 418 | 419 | // get MAX row ID 420 | $gid = $conn->prepare("SELECT MAX(id) as newid FROM graph"); 421 | $gid->execute(); 422 | $new_id = $gid->fetch(PDO::FETCH_ASSOC); 423 | 424 | $sql = "INSERT INTO graph values(($new_id[newid]+1), '$field_simpul_awal', '$field_simpul_akhir', '$jalur_baru', '$new_bobot', 'Y')"; 425 | 426 | // Prepare statement 427 | $stmt = $conn->prepare($sql); 428 | 429 | // execute the query 430 | $stmt->execute(); 431 | 432 | // echo a message to say the UPDATE succeeded 433 | //echo $stmt->rowCount() . " records INSERTED successfully"; 434 | 435 | // save new id 436 | array_push($this->temporary_id, $conn->lastInsertId('id')); 437 | } 438 | catch(PDOException $e) 439 | { 440 | echo $e->getMessage(); 441 | } 442 | } 443 | } 444 | ?> -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | 16 | 17 | 18 | 217 | 218 | koneksi; 224 | 225 | // query 226 | $sql = "SELECT * FROM sekolah"; 227 | $query = mysqli_query($koneksi, $sql); 228 | 229 | // select option 230 | echo 'TUJUAN : '; 241 | ?> 242 | 243 | 244 | --------------------------------------------------------------------------------