Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. function generateRandomPoint($centre, $radius)
  2. {
  3. $radius_earth = 3959; //miles
  4.  
  5. //Pick random distance within $distance;
  6. $distance = lcg_value() * $radius;
  7.  
  8. //Convert degrees to radians.
  9. $centre_rads = array_map('deg2rad', $centre);
  10.  
  11. //First suppose our point is the north pole.
  12. //Find a random point $distance miles away
  13. $lat_rads = (pi() / 2) - $distance / $radius_earth;
  14. $lng_rads = lcg_value() * 2 * pi();
  15.  
  16.  
  17. //($lat_rads,$lng_rads) is a point on the circle which is
  18. //$distance miles from the north pole. Convert to Cartesian
  19. $x1 = cos($lat_rads) * sin($lng_rads);
  20. $y1 = cos($lat_rads) * cos($lng_rads);
  21. $z1 = sin($lat_rads);
  22.  
  23.  
  24. //Rotate that sphere so that the north pole is now at $centre.
  25.  
  26. //Rotate in x axis by $rot = (pi()/2) - $centre_rads[0];
  27. $rot = (pi() / 2) - $centre_rads[0];
  28. $x2 = $x1;
  29. $y2 = $y1 * cos($rot) + $z1 * sin($rot);
  30. $z2 = -$y1 * sin($rot) + $z1 * cos($rot);
  31.  
  32. //Rotate in z axis by $rot = $centre_rads[1]
  33. $rot = $centre_rads[1];
  34. $x3 = $x2 * cos($rot) + $y2 * sin($rot);
  35. $y3 = -$x2 * sin($rot) + $y2 * cos($rot);
  36. $z3 = $z2;
  37.  
  38.  
  39. //Finally convert this point to polar co-ords
  40. $lng_rads = atan2($x3, $y3);
  41. $lat_rads = asin($z3);
  42.  
  43. return array_map('rad2deg', array($lat_rads, $lng_rads));
  44. }
  45.  
  46. for ($j = 0; $j < 12; $j++) {
  47. $data = [];
  48.  
  49. for ($i = 0; $i < 10000; $i++) {
  50. $point = generateRandomPoint([37.3295584, -57.98016319], 16);
  51. $randDate = new DateTime();
  52. $randDate->setTime(mt_rand(0, 23), mt_rand(0, 59));
  53.  
  54. $data[] = [
  55. 'user_id' => rand(1, 2000),
  56. 'location' => \DB::raw('ST_MakePoint(' . $point[0] . ',' . $point[1] . ')'),
  57. 'timestamp' => $randDate->format('Y-m-d H:i:s'),
  58. 'is_moving' => true,
  59. 'activity_type' => 'walking',
  60. 'accuracy' => 7.5,
  61. ];
  62. }
  63. \App\Location::insert(
  64. $data
  65. );
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement