/** Computes a set of initial candidates that cover the given region. */
 private function getInitialCandidates()
 {
     // Optimization: if at least 4 cells are desired (the normal case),
     // start with a 4-cell covering of the region's bounding cap. This
     // lets us skip quite a few levels of refinement when the region to
     // be covered is relatively small.
     if ($this->maxCells >= 4) {
         // Find the maximum level such that the bounding cap contains at most one
         // cell vertex at that level.
         $cap = $this->region->getCapBound();
         $level = min(S2Projections::MIN_WIDTH()->getMaxLevel(2 * $cap->angle()->radians()), min($this->maxLevel(), S2CellId::MAX_LEVEL - 1));
         if ($this->levelMod() > 1 && $level > $this->minLevel()) {
             $level -= ($level - $this->minLevel()) % $this->levelMod();
         }
         // We don't bother trying to optimize the level == 0 case, since more than
         // four face cells may be required.
         if ($level > 0) {
             // Find the leaf cell containing the cap axis, and determine which
             // subcell of the parent cell contains it.
             /** @var S2CellId[] $base */
             $base = array();
             $s2point_tmp = $cap->axis();
             $id = S2CellId::fromPoint($s2point_tmp);
             $id->getVertexNeighbors($level, $base);
             for ($i = 0; $i < count($base); ++$i) {
                 //            printf("(face=%s pos=%s level=%s)\n", $base[$i]->face(), dechex($base[$i]->pos()), $base[$i]->level());
                 //            echo "new S2Cell(base[i])\n";
                 $cell = new S2Cell($base[$i]);
                 //            echo "neighbour cell: " . $cell . "\n";
                 $c = $this->newCandidate($cell);
                 //            if ($c !== null)
                 //            echo "addCandidato getInitialCandidates: " . $c->cell->id() . "\n";
                 $this->addCandidate($c);
             }
             //          echo "\n\n\n";
             return;
         }
     }
     // Default: start with all six cube faces.
     $face_cells = self::FACE_CELLS();
     for ($face = 0; $face < 6; ++$face) {
         $c = $this->newCandidate($face_cells[$face]);
         echo "addCandidato getInitialCandidates_default: " . $c->cell->id() . "\n";
         $this->addCandidate($c);
     }
 }
Esempio n. 2
0
 /**
  * @param $latitude
  * @param $longitude
  * @param $width
  * @return array
  */
 public function getCellIds($latitude, $longitude, $width)
 {
     $latLng = S2LatLng::fromDegrees($latitude, $longitude);
     $cellId = S2CellId::fromLatLng($latLng)->parent(15);
     $index = 0;
     $jindex = 0;
     $level = $cellId->level();
     $size = 1 << S2CellId::MAX_LEVEL - $level;
     $face = $cellId->toFaceIJOrientation($index, $jindex);
     $cells = [];
     $halfWidth = floor($width / 2);
     for ($x = -$halfWidth; $x <= $halfWidth; $x++) {
         for ($y = -$halfWidth; $y <= $halfWidth; $y++) {
             $cells[] = S2CellId::fromFaceIJ($face, $index + $x * $size, $jindex + $y * $size)->parent(15)->id();
         }
     }
     return $cells;
 }