2017年8月20日日曜日

Cakephp : ShellのCron実行

パラメタを自動取得するため、Shellを作成しCronで実行しました。
参考
http://mizunolab.sist.ac.jp/2016/01/cakephp-shell.html
http://mizunolab.sist.ac.jp/2016/01/cakephpcron.html

1.Shellの作成
app]$ vi Console/Command/MapShell.php
<?php
class MapShell extends Shell {
function main () {
App::import('Model','Firedistrict');
$this->Firedistrict = new Firedistrict;
$fires = $this->Firedistrict->find('all',
array(
'conditions' => array('distance' => null),
'limit' => 3,
)
);
foreach($fires as $fire){
$distance = $this->getDirection($fire['Firedistrict']['firelatitude'],$fire['Firedistrict']['firelongitude'],$fire['Firedistrict']['distlatitude'],$fire['Firedistrict']['distlongitude']);
$data = array(
'id' => $fire['Firedistrict']['id'],
'distance' => $distance[0],
'duration' => $distance[1]
);
$this->Firedistrict->save($data);
}
}
public function getDirection($orglat, $orglon, $dstlat, $dstlon){
$datas = file_get_contents("http://maps.googleapis.com/maps/api/directions/json?origin=$orglat,$orglon&destination=$dstlat,$dstlon&sensor=false");
$directions = json_decode($datas);
//print_r ($directions);
$direction = $directions->routes[0]->legs[0];
$distance = $direction->distance->value;
$duration = $direction->duration->value;
$result = array($distance, $duration);
return $result;
}
}
?>
view raw MapShell.php hosted with ❤ by GitHub


2.スクリプトファイルの作成
app]$ vi Vendor/CakeShell.sh
#!/bin/bash
cd ~
cd www/app
Console/cake map
view raw CakeShell.sh hosted with ❤ by GitHub


実行権限を与えます。
app]$ chmod +x Vendor/CakeShell.sh

3.cron設定
$ crontab -e
*/2 * * * * /home/mznqueue/www/app/Vendor/CakeShell.sh
2分に1回実行

2017年8月8日火曜日

凸集合の描画

いくつか点があるときに凸集合を作成して描画をしました。

1.ライブラリの用意
今回は下記のライブラリを使いました。
https://github.com/benpoulson/PHP-ConvexHull/blob/master/convexHull.php
関数になっていたのでAppController.phpに追加しておきました。

2.コントローラでの呼び出し
コントローラから下記のように呼び出します。
public function convex() {
$this->Tsp->recursive = 0;
$this->paginate = array(
'limit'=>10000,
'maxLimit' => 30000,
);
$this->set('tsps', $this->Paginator->paginate());
$tsps = $this->Paginator->paginate();
$input = array(array());
for($i = 0; $i < count($tsps); $i++){
$input[$i][0] = $tsps[$i]['Tsp']['latitude'];
$input[$i][1] = $tsps[$i]['Tsp']['longitude'];
}
$output = $this->convexHull($input);
$this->set('output', $output);
}
view raw Convex hosted with ❤ by GitHub


3.ビューでの描画
ビューで描画します。
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=MyKey&sensor=false"></script>
<div class="tsps index">
<h2><?php echo __('Tsps'); ?></h2>
<?php
$map_options = array(
'id' => 'map1',
'width' => '1200px',
'height' => '800px',
'zoom' => 13,
'type' => 'ROAD',
//'custom' => null,
'localize' => false,
'latitude' => $tsps[0]['Tsp']['latitude'],
'longitude' => $tsps[0]['Tsp']['longitude'],
'marker' => true,
'markerIcon' => 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
'markerShadow' => 'http://google-maps-icons.googlecode.com/files/shadow.png',
'infoWindow' => true,
'windowText' => "自分で指定"
);
?>
<div id="map">
<?php echo $this->GoogleMap->map($map_options); ?>
</div>
<?php $marker_index =1; ?>
<?php foreach ($tsps as $tsp): ?>
<?php
$marker_options = array(
'showWindow' => true,
'windowText' =>$tsp['Tsp']['id'].":".$tsp['Tsp']['name'],
'markerTitle' => 'Title',
'markerIcon' => 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='.$tsp['Tsp']['id'].'|ff7e73|000000',
'markerShadow' => 'http://labs.google.com/ridefinder/images/mm_20_purpleshadow.png'
);
echo $this->GoogleMap->addMarker('map1', $marker_index, array('latitude' => $tsp['Tsp']['latitude'], 'longitude' =>$tsp['Tsp']['longitude'] ),$marker_options);
$marker_index ++;
?>
<?php endforeach; ?>
<?php for($i = 0; $i < count($output); $i++){ ?>
<?php
if($i == count($output)-1){
echo $this->GoogleMap->addPolyline("map1", "polyline", array(
"start" => array("latitude" => $output[$i][0] ,"longitude"=> $output[$i][1]),
"end" => array("latitude" => $output[0][0] ,"longitude"=> $output[0][1])
));
}
else{
echo $this->GoogleMap->addPolyline("map1", "polyline", array(
"start" => array("latitude" => $output[$i][0] ,"longitude"=> $output[$i][1]),
"end" => array("latitude" => $output[$i+1][0] ,"longitude"=> $output[$i+1][1])
));
}
?>
<?php } ?>
view raw Convex View hosted with ❤ by GitHub