/** * @param int $timestamp * @param \League\Geotools\Coordinate\CoordinateInterface $coordinate * @param int $offset * @return \Runalyze\Calculation\NightDetector $this-reference * @throws \InvalidArgumentException */ public function setFrom($timestamp, CoordinateInterface $coordinate, $offset = 0) { if (!is_numeric($timestamp)) { throw new \InvalidArgumentException('Provided timestamp must be numerical.'); } $isAfterSunset = $timestamp > date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $coordinate->getLatitude(), $coordinate->getLongitude(), self::ZENITH, $offset); $isBeforeSunrise = $timestamp < date_sunrise($timestamp, SUNFUNCS_RET_TIMESTAMP, $coordinate->getLatitude(), $coordinate->getLongitude(), self::ZENITH, $offset); $this->Value = $isAfterSunset || $isBeforeSunrise; return $this; }
/** * Encode the coordinate via the 10:10 algorithm. * * @param CoordinateInterface $coordinate The coordinate to encode. * @return string */ public function encode(CoordinateInterface $coordinate) { $latitude = floor(($coordinate->getLatitude() + 90.0) * 10000.0); $longitude = floor(($coordinate->getLongitude() + 180.0) * 10000.0); $position = $latitude * 3600000.0 + $longitude; $ttNumber = $position * self::BASE; $checkDigit = 0; for ($i = 1; $i < 10; ++$i) { $checkDigit += $position % self::BASE * $i; $position = floor($position / self::BASE); } $checkDigit %= self::BASE; $ttNumber += $checkDigit; $ttNumber = floor($ttNumber); $tt = ''; for ($i = 0; $i < 10; ++$i) { $digit = $ttNumber % self::BASE; if ($i === 4 || $i === 7) { $tt = ' ' . $tt; } $tt = $this->alphabet[$digit] . $tt; $ttNumber = floor($ttNumber / self::BASE); } return $tt; }
/** * {@inheritDoc} */ public function toUniversalTransverseMercator() { // Convert decimal degrees coordinates to radian. $phi = deg2rad($this->coordinates->getLatitude()); $lambda = deg2rad($this->coordinates->getLongitude()); // Compute the zone UTM zone. $zone = (int) (($this->coordinates->getLongitude() + 180.0) / 6) + 1; // Special zone for South Norway. // On the southwest coast of Norway, grid zone 32V (9° of longitude in width) is extended further west, // and grid zone 31V (3° of longitude in width) is correspondingly shrunk to cover only open water. if ($this->coordinates->getLatitude() >= 56.0 && $this->coordinates->getLatitude() < 64.0 && $this->coordinates->getLongitude() >= 3.0 && $this->coordinates->getLongitude() < 12.0) { $zone = 32; } // Special zone for Svalbard. // In the region around Svalbard, the four grid zones 31X (9° of longitude in width), // 33X (12° of longitude in width), 35X (12° of longitude in width), and 37X (9° of longitude in width) // are extended to cover what would otherwise have been covered by the seven grid zones 31X to 37X. // The three grid zones 32X, 34X and 36X are not used. if ($this->coordinates->getLatitude() >= 72.0 && $this->coordinates->getLatitude() < 84.0) { if ($this->coordinates->getLongitude() >= 0.0 && $this->coordinates->getLongitude() < 9.0) { $zone = 31; } elseif ($this->coordinates->getLongitude() >= 9.0 && $this->coordinates->getLongitude() < 21.0) { $zone = 33; } elseif ($this->coordinates->getLongitude() >= 21.0 && $this->coordinates->getLongitude() < 33.0) { $zone = 35; } elseif ($this->coordinates->getLongitude() >= 33.0 && $this->coordinates->getLongitude() < 42.0) { $zone = 37; } } // Determines the central meridian for the given UTM zone. $lambda0 = deg2rad(-183.0 + $zone * 6.0); $ep2 = (pow($this->coordinates->getEllipsoid()->getA(), 2.0) - pow($this->coordinates->getEllipsoid()->getB(), 2.0)) / pow($this->coordinates->getEllipsoid()->getB(), 2.0); $nu2 = $ep2 * pow(cos($phi), 2.0); $nN = pow($this->coordinates->getEllipsoid()->getA(), 2.0) / ($this->coordinates->getEllipsoid()->getB() * sqrt(1 + $nu2)); $t = tan($phi); $t2 = $t * $t; $l = $lambda - $lambda0; $l3coef = 1.0 - $t2 + $nu2; $l4coef = 5.0 - $t2 + 9 * $nu2 + 4.0 * ($nu2 * $nu2); $l5coef = 5.0 - 18.0 * $t2 + $t2 * $t2 + 14.0 * $nu2 - 58.0 * $t2 * $nu2; $l6coef = 61.0 - 58.0 * $t2 + $t2 * $t2 + 270.0 * $nu2 - 330.0 * $t2 * $nu2; $l7coef = 61.0 - 479.0 * $t2 + 179.0 * ($t2 * $t2) - $t2 * $t2 * $t2; $l8coef = 1385.0 - 3111.0 * $t2 + 543.0 * ($t2 * $t2) - $t2 * $t2 * $t2; // Calculate easting. $easting = $nN * cos($phi) * $l + $nN / 6.0 * pow(cos($phi), 3.0) * $l3coef * pow($l, 3.0) + $nN / 120.0 * pow(cos($phi), 5.0) * $l5coef * pow($l, 5.0) + $nN / 5040.0 * pow(cos($phi), 7.0) * $l7coef * pow($l, 7.0); // Calculate northing. $n = ($this->coordinates->getEllipsoid()->getA() - $this->coordinates->getEllipsoid()->getB()) / ($this->coordinates->getEllipsoid()->getA() + $this->coordinates->getEllipsoid()->getB()); $alpha = ($this->coordinates->getEllipsoid()->getA() + $this->coordinates->getEllipsoid()->getB()) / 2.0 * (1.0 + pow($n, 2.0) / 4.0 + pow($n, 4.0) / 64.0); $beta = -3.0 * $n / 2.0 + 9.0 * pow($n, 3.0) / 16.0 + -3.0 * pow($n, 5.0) / 32.0; $gamma = 15.0 * pow($n, 2.0) / 16.0 + -15.0 * pow($n, 4.0) / 32.0; $delta = -35.0 * pow($n, 3.0) / 48.0 + 105.0 * pow($n, 5.0) / 256.0; $epsilon = 315.0 * pow($n, 4.0) / 512.0; $northing = $alpha * ($phi + $beta * sin(2.0 * $phi) + $gamma * sin(4.0 * $phi) + $delta * sin(6.0 * $phi) + $epsilon * sin(8.0 * $phi)) + $t / 2.0 * $nN * pow(cos($phi), 2.0) * pow($l, 2.0) + $t / 24.0 * $nN * pow(cos($phi), 4.0) * $l4coef * pow($l, 4.0) + $t / 720.0 * $nN * pow(cos($phi), 6.0) * $l6coef * pow($l, 6.0) + $t / 40320.0 * $nN * pow(cos($phi), 8.0) * $l8coef * pow($l, 8.0); // Adjust easting and northing for UTM system. $easting = $easting * AbstractGeotools::UTM_SCALE_FACTOR + 500000.0; $northing = $northing * AbstractGeotools::UTM_SCALE_FACTOR; if ($northing < 0.0) { $northing += 10000000.0; } return sprintf('%d%s %d %d', $zone, $this->latitudeBands[(int) ($this->coordinates->getLatitude() + 80) / 8], $easting, $northing); }
/** * @param \League\Geotools\Coordinate\CoordinateInterface $coordinate */ protected function addCoordinateToCurrentPath(CoordinateInterface $coordinate) { if (abs($coordinate->getLatitude()) > 1.0E-5 || abs($coordinate->getLongitude()) > 1.0E-5) { $this->CurrentPath .= $coordinate->getLongitude() . ',' . $coordinate->getLatitude() . NL; } }
/** * {@inheritDoc} * * @see http://en.wikipedia.org/wiki/Geohash * @see http://geohash.org/ */ public function encode(CoordinateInterface $coordinate, $length = self::MAX_LENGTH) { if ((int) $length < self::MIN_LENGTH || (int) $length > self::MAX_LENGTH) { throw new InvalidArgumentException('The length should be between 1 and 12.'); } $latitudeInterval = $this->latitudeInterval; $longitudeInterval = $this->longitudeInterval; $isEven = true; $bit = 0; $charIndex = 0; while (strlen($this->geohash) < $length) { if ($isEven) { $middle = ($longitudeInterval[0] + $longitudeInterval[1]) / 2; if ($coordinate->getLongitude() > $middle) { $charIndex |= $this->bits[$bit]; $longitudeInterval[0] = $middle; } else { $longitudeInterval[1] = $middle; } } else { $middle = ($latitudeInterval[0] + $latitudeInterval[1]) / 2; if ($coordinate->getLatitude() > $middle) { $charIndex |= $this->bits[$bit]; $latitudeInterval[0] = $middle; } else { $latitudeInterval[1] = $middle; } } if ($bit < 4) { $bit++; } else { $this->geohash = $this->geohash . $this->base32Chars[$charIndex]; $bit = 0; $charIndex = 0; } $isEven = $isEven ? false : true; } $this->latitudeInterval = $latitudeInterval; $this->longitudeInterval = $longitudeInterval; return $this; }
/** * @param CoordinateInterface $coordinate * @return boolean */ public function pointOnVertex(CoordinateInterface $coordinate) { foreach ($this->coordinates as $vertexCoordinate) { if (bccomp($vertexCoordinate->getLatitude(), $coordinate->getLatitude(), $this->getPrecision()) === 0 && bccomp($vertexCoordinate->getLongitude(), $coordinate->getLongitude(), $this->getPrecision()) === 0) { return true; } } return false; }
/** * @param CoordinateInterface $coordinate * @return bool */ public function pointInBoundingBox(CoordinateInterface $coordinate) { if (bccomp($coordinate->getLatitude(), $this->getSouth(), $this->getPrecision()) === -1 || bccomp($coordinate->getLatitude(), $this->getNorth(), $this->getPrecision()) === 1 || bccomp($coordinate->getLongitude(), $this->getEast(), $this->getPrecision()) === 1 || bccomp($coordinate->getLongitude(), $this->getWest(), $this->getPrecision()) === -1) { return false; } return true; }