123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace App\Handlers;
- use Illuminate\Support\Facades\Log;
- class ConvertHandler
- {
- private $PI = 3.14159265358979324;
- private $x_pi = 0;
- public function __construct()
- {
- $this->x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- }
-
- function is_point_in_circle($point, $circle)
- {
- $distance = $this->distance($point['latitude'], $point['longitude'], $circle['center']['latitude'], $circle['center']['longitude']);
- if ($distance <= $circle['radius']) {
- return true;
- } else {
- return false;
- }
- }
-
- function is_point_in_circles($point, $circles)
- {
- $id = false;
- foreach ($circles as $key => $circle) {
- $distance = $this->distance($point['latitude'], $point['longitude'], $circle['parking_centre']['latitude'], $circle['parking_centre']['longitude']);
- if ($distance <= $circle['parking_radius']) {
- $id = $key;
- break;
- }
- }
- return $id;
- }
-
- function distance($latitudeA, $lonA, $latitudeB, $lonB)
- {
- $earthR = 6371000.;
- $x = cos($latitudeA * $this->PI / 180.) * cos($latitudeB * $this->PI / 180.) * cos(($lonA - $lonB) * $this->PI / 180);
- $y = sin($latitudeA * $this->PI / 180.) * sin($latitudeB * $this->PI / 180.);
- $s = $x + $y;
- if ($s > 1) $s = 1;
- if ($s < -1) $s = -1;
- $alpha = acos($s);
- $distance = $alpha * $earthR;
- return $distance;
- }
-
- function is_point_in_polygons($point, $pts)
- {
- $id = false;
- foreach ($pts as $key => $pt) {
- $is_ok = $this->is_point_in_polygon($point, $pt);
- if ($is_ok) {
- $id = $key;
- break;
- }
- }
- return $id;
- }
-
- function is_point_in_polygon($point, $pts)
- {
- $N = count($pts);
- $boundOrVertex = true;
- $intersectCount = 0;
- $precision = 2e-10;
- $p1 = 0;
- $p2 = 0;
- $p = $point;
- $p1 = $pts[0];
- for ($i = 1; $i <= $N; ++$i) {
-
- if ($p['longitude'] == $p1['longitude'] && $p['latitude'] == $p1['latitude']) {
- return $boundOrVertex;
- }
- $p2 = $pts[$i % $N];
- if ($p['latitude'] < min($p1['latitude'], $p2['latitude']) || $p['latitude'] > max($p1['latitude'], $p2['latitude'])) {
- $p1 = $p2;
- continue;
- }
- if ($p['latitude'] > min($p1['latitude'], $p2['latitude']) && $p['latitude'] < max($p1['latitude'], $p2['latitude'])) {
- if ($p['longitude'] <= max($p1['longitude'], $p2['longitude'])) {
- if ($p1['latitude'] == $p2['latitude'] && $p['longitude'] >= min($p1['longitude'], $p2['longitude'])) {
- return $boundOrVertex;
- }
- if ($p1['longitude'] == $p2['longitude']) {
- if ($p1['longitude'] == $p['longitude']) {
- return $boundOrVertex;
- } else {
- ++$intersectCount;
- }
- } else {
- $xinters = ($p['latitude'] - $p1['latitude']) * ($p2['longitude'] - $p1['longitude']) / ($p2['latitude'] - $p1['latitude']) + $p1['longitude'];
- if (abs($p['longitude'] - $xinters) < $precision) {
- return $boundOrVertex;
- }
- if ($p['longitude'] < $xinters) {
- ++$intersectCount;
- }
- }
- }
- } else {
- if ($p['latitude'] == $p2['latitude'] && $p['longitude'] <= $p2['longitude']) {
- $p3 = $pts[($i + 1) % $N];
- if ($p['latitude'] >= min($p1['latitude'], $p3['latitude']) && $p['latitude'] <= max($p1['latitude'], $p3['latitude'])) {
- ++$intersectCount;
- } else {
- $intersectCount += 2;
- }
- }
- }
- $p1 = $p2;
- }
- if ($intersectCount % 2 == 0) {
- return false;
- } else {
- return true;
- }
- }
- }
|