You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
12 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use DB;
  4. use Log;
  5. use Quartz;
  6. use p3k\Multipart;
  7. use App\Jobs\Job;
  8. use Illuminate\Contracts\Bus\SelfHandling;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use DateTime, DateTimeZone;
  11. class TripComplete extends Job implements SelfHandling, ShouldQueue
  12. {
  13. private $_dbid;
  14. private $_data;
  15. public function __construct($dbid, $data) {
  16. $this->_dbid = $dbid;
  17. $this->_data = $data;
  18. }
  19. public function handle() {
  20. // echo "Job Data\n";
  21. // echo json_encode($this->_data)."\n";
  22. if(!is_array($this->_data)) return;
  23. $db = DB::table('databases')->where('id','=',$this->_dbid)->first();
  24. Log::info("Starting job for ".$db->name);
  25. Log::debug(json_encode($this->_data));
  26. if(!$db->micropub_endpoint) {
  27. Log::info('No micropub endpoint configured for database ' . $db->name);
  28. return;
  29. }
  30. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
  31. // Load the data from the start and end times
  32. $start = new DateTime($this->_data['properties']['start']);
  33. $end = new DateTime($this->_data['properties']['end']);
  34. $results = $qz->queryRange($start, $end);
  35. $features = [];
  36. foreach($results as $id=>$record) {
  37. // Don't include app action tracking data
  38. if(!property_exists($record->data->properties, 'action')) {
  39. // Ignore locations with accuracy worse than 5000m
  40. if(property_exists($record->data->properties, 'horizontal_accuracy') && $record->data->properties->horizontal_accuracy <= 5000) {
  41. $record->data->properties = array_filter((array)$record->data->properties, function($k){
  42. // Remove some of the app-specific tracking keys from each record
  43. return !in_array($k, ['locations_in_payload','desired_accuracy','significant_change','pauses','deferred']);
  44. }, ARRAY_FILTER_USE_KEY);
  45. $features[] = $record->data;
  46. }
  47. }
  48. }
  49. // Build the GeoJSON for this trip
  50. $geojson = [
  51. 'type' => 'FeatureCollection',
  52. 'features' => $features
  53. ];
  54. $file_path = tempnam(sys_get_temp_dir(), 'compass');
  55. file_put_contents($file_path, json_encode($geojson));
  56. // If there are no start/end coordinates in the request, use the first and last coordinates
  57. if(count($features)) {
  58. if(!array_key_exists('start-coordinates', $this->_data['properties'])) {
  59. $this->_data['properties']['start-coordinates'] = $features[0]->geometry->coordinates;
  60. }
  61. if(!array_key_exists('end-coordinates', $this->_data['properties'])) {
  62. $this->_data['properties']['end-coordinates'] = $features[count($features)-1]->geometry->coordinates;
  63. }
  64. }
  65. $startAdr = false;
  66. if(array_key_exists('start-coordinates', $this->_data['properties'])) {
  67. // Reverse geocode the start and end location to get an h-adr
  68. $startAdr = [
  69. 'type' => 'h-adr',
  70. 'properties' => [
  71. 'latitude' => $this->_data['properties']['start-coordinates'][1],
  72. 'longitude' => $this->_data['properties']['start-coordinates'][0],
  73. ]
  74. ];
  75. Log::info('Looking up start location');
  76. $start = self::geocode($this->_data['properties']['start-coordinates'][1], $this->_data['properties']['start-coordinates'][0]);
  77. if($start) {
  78. $startAdr['properties']['locality'] = $start->locality;
  79. $startAdr['properties']['region'] = $start->region;
  80. $startAdr['properties']['country'] = $start->country;
  81. Log::info('Found start: '.$start->full_name.' '.$start->timezone);
  82. }
  83. } else {
  84. $start = false;
  85. }
  86. $endAdr = false;
  87. if(array_key_exists('end-coordinates', $this->_data['properties'])) {
  88. $endAdr = [
  89. 'type' => 'h-adr',
  90. 'properties' => [
  91. 'latitude' => $this->_data['properties']['end-coordinates'][1],
  92. 'longitude' => $this->_data['properties']['end-coordinates'][0],
  93. ]
  94. ];
  95. Log::info('Looking up end location');
  96. $end = self::geocode($this->_data['properties']['end-coordinates'][1], $this->_data['properties']['end-coordinates'][0]);
  97. if($end) {
  98. $endAdr['properties']['locality'] = $end->locality;
  99. $endAdr['properties']['region'] = $end->region;
  100. $endAdr['properties']['country'] = $end->country;
  101. Log::info('Found end: '.$end->full_name.' '.$end->timezone);
  102. }
  103. } else {
  104. $end = false;
  105. }
  106. // Set the timezone of the dates based on the location
  107. $startDate = new DateTime($this->_data['properties']['start']);
  108. if($start && $start->timezone) {
  109. $startDate->setTimeZone(new DateTimeZone($start->timezone));
  110. }
  111. $endDate = new DateTime($this->_data['properties']['end']);
  112. if($end && $end->timezone) {
  113. $endDate->setTimeZone(new DateTimeZone($end->timezone));
  114. }
  115. $params = [
  116. 'h' => 'entry',
  117. 'published' => $endDate->format('c'),
  118. 'trip' => [
  119. 'type' => 'h-trip',
  120. 'properties' => [
  121. 'mode-of-transport' => $this->_data['properties']['mode'],
  122. 'start' => $startDate->format('c'),
  123. 'end' => $endDate->format('c'),
  124. 'route' => 'route.json'
  125. // TODO: avgpace for runs
  126. // TODO: avgspeed for bike rides
  127. // TODO: avg heart rate if available
  128. ]
  129. ]
  130. ];
  131. if($startAdr) {
  132. $params['trip']['properties']['start-location'] = $startAdr;
  133. }
  134. if($endAdr) {
  135. $params['trip']['properties']['end-location'] = $endAdr;
  136. }
  137. if(array_key_exists('distance', $this->_data['properties'])) {
  138. $params['trip']['properties']['distance'] = [
  139. 'type' => 'h-measure',
  140. 'properties' => [
  141. 'num' => round($this->_data['properties']['distance']),
  142. 'unit' => 'meter'
  143. ]
  144. ];
  145. }
  146. if(array_key_exists('duration', $this->_data['properties'])) {
  147. $params['trip']['properties']['duration'] = [
  148. 'type' => 'h-measure',
  149. 'properties' => [
  150. 'num' => round($this->_data['properties']['duration']),
  151. 'unit' => 'second'
  152. ]
  153. ];
  154. }
  155. if(array_key_exists('cost', $this->_data['properties'])) {
  156. $params['trip']['properties']['cost'] = [
  157. 'type' => 'h-measure',
  158. 'properties' => [
  159. 'num' => round($this->_data['properties']['cost'], 2),
  160. 'unit' => 'USD'
  161. ]
  162. ];
  163. }
  164. // If there is trip data, recalculate the distance and duration based on the actual data
  165. if(count($features)) {
  166. $startTime = strtotime($features[0]->properties['timestamp']);
  167. $endTime = strtotime($features[count($features)-1]->properties['timestamp']);
  168. $duration = $endTime - $startTime;
  169. $params['trip']['properties']['duration']['type'] = 'h-measure';
  170. $params['trip']['properties']['duration']['properties']['num'] = $duration;
  171. $params['trip']['properties']['duration']['properties']['unit'] = 'second';
  172. Log::debug("Overriding duration to $duration");
  173. $points = array_map(function($f){
  174. return $f->geometry->coordinates;
  175. }, $features);
  176. $simple = $this->_ramerDouglasPeucker($points, 0.0001);
  177. $last = false;
  178. $distance = 0;
  179. foreach($simple as $p) {
  180. if($last) {
  181. $distance += $this->_gc_distance($p[1], $p[0], $last[1], $last[0]);
  182. }
  183. $last = $p;
  184. }
  185. if($distance) {
  186. $params['trip']['properties']['distance']['type'] = 'h-measure';
  187. $params['trip']['properties']['distance']['properties']['num'] = $distance;
  188. $params['trip']['properties']['distance']['properties']['unit'] = 'meter';
  189. Log::debug("Overriding distance to $distance");
  190. }
  191. }
  192. // echo "Micropub Params\n";
  193. // print_r($params);
  194. $multipart = new Multipart();
  195. $multipart->addArray($params);
  196. $multipart->addFile('route.json', $file_path, 'application/json');
  197. $httpheaders = [
  198. 'Authorization: Bearer ' . $db->micropub_token,
  199. 'Content-type: ' . $multipart->contentType()
  200. ];
  201. Log::info('Sending to the Micropub endpoint: '.$db->micropub_endpoint);
  202. // Post to the Micropub endpoint
  203. $ch = curl_init($db->micropub_endpoint);
  204. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  205. curl_setopt($ch, CURLOPT_POST, true);
  206. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
  207. curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());
  208. curl_setopt($ch, CURLOPT_HEADER, true);
  209. $response = curl_exec($ch);
  210. Log::info("Done!");
  211. Log::info($response);
  212. // echo "========\n";
  213. // echo $response."\n========\n";
  214. //
  215. // echo "\n";
  216. }
  217. public static function geocode($lat, $lng) {
  218. $ch = curl_init(env('ATLAS_BASE').'api/geocode?latitude='.$lat.'&longitude='.$lng);
  219. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  220. curl_setopt($ch, CURLOPT_TIMEOUT, 8);
  221. $response = curl_exec($ch);
  222. if($response) {
  223. return json_decode($response);
  224. }
  225. }
  226. // TODO: move this to a library p3k/Geo
  227. // http://www.loughrigg.org/rdp/
  228. //The author has placed this work in the Public Domain, thereby relinquishing all copyrights.
  229. //You may use, modify, republish, sell or give away this work without prior consent.
  230. //This implementation comes with no warranty or guarantee of fitness for any purpose.
  231. //=========================================================================
  232. //An implementation of the Ramer-Douglas-Peucker algorithm for reducing
  233. //the number of points on a polyline
  234. //see http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
  235. //=========================================================================
  236. //Finds the perpendicular distance from a point to a straight line.
  237. //The coordinates of the point are specified as $ptX and $ptY.
  238. //The line passes through points l1 and l2, specified respectively with their
  239. //coordinates $l1x and $l1y, and $l2x and $l2y
  240. public function _perpendicularDistance($ptX, $ptY, $l1x, $l1y, $l2x, $l2y)
  241. {
  242. $result = 0;
  243. if ($l2x == $l1x)
  244. {
  245. //vertical lines - treat this case specially to avoid divide by zero
  246. $result = abs($ptX - $l2x);
  247. }
  248. else
  249. {
  250. $slope = (($l2y-$l1y) / ($l2x-$l1x));
  251. $passThroughY = (0-$l1x)*$slope + $l1y;
  252. $result = (abs(($slope * $ptX) - $ptY + $passThroughY)) / (sqrt($slope*$slope + 1));
  253. }
  254. return $result;
  255. }
  256. //RamerDouglasPeucker
  257. //Reduces the number of points on a polyline by removing those that are closer to the line
  258. //than the distance $epsilon.
  259. //The polyline is provided as an array of arrays, where each internal array is one point on the polyline,
  260. //specified by easting (x-coordinate) with key "0" and northing (y-coordinate) with key "1".
  261. //It is assumed that the coordinates and distance $epsilon are given in the same units.
  262. //The result is returned as an array in a similar format.
  263. //Each point returned in the result array will retain all its original data, including its E and N
  264. //values along with any others.
  265. public function _ramerDouglasPeucker($pointList, $epsilon)
  266. {
  267. if(count($pointList) == 0)
  268. return array();
  269. // Find the point with the maximum distance
  270. $dmax = 0;
  271. $index = 0;
  272. $totalPoints = count($pointList);
  273. for ($i = 1; $i < ($totalPoints - 1); $i++)
  274. {
  275. $d = $this->_perpendicularDistance($pointList[$i][0], $pointList[$i][1],
  276. $pointList[0][0], $pointList[0][1],
  277. $pointList[$totalPoints-1][0], $pointList[$totalPoints-1][1]);
  278. if ($d > $dmax)
  279. {
  280. $index = $i;
  281. $dmax = $d;
  282. }
  283. }
  284. $resultList = array();
  285. // If max distance is greater than epsilon, recursively simplify
  286. if ($dmax >= $epsilon)
  287. {
  288. // Recursive call
  289. $recResults1 = $this->_ramerDouglasPeucker(array_slice($pointList, 0, $index + 1), $epsilon);
  290. $recResults2 = $this->_ramerDouglasPeucker(array_slice($pointList, $index, $totalPoints - $index), $epsilon);
  291. // Build the result list
  292. $resultList = array_merge(array_slice($recResults1, 0, count($recResults1) - 1),
  293. array_slice($recResults2, 0, count($recResults2)));
  294. }
  295. else
  296. {
  297. $resultList = array($pointList[0], $pointList[$totalPoints-1]);
  298. }
  299. // Return the result
  300. return $resultList;
  301. }
  302. function _gc_distance($lat1, $lng1, $lat2, $lng2) {
  303. return ( 6378100 * acos( cos( deg2rad($lat1) ) * cos( deg2rad($lat2) ) * cos( deg2rad($lng2) - deg2rad($lng1) ) + sin( deg2rad($lat1) ) * sin( deg2rad($lat2) ) ) );
  304. }
  305. }