/**
  * test grabbing a Trail Relationship by segmentType
  **/
 public function testGetValidTrailRelationshipBySegmentType()
 {
     // count the number of rows and save it for later
     $numRows = $this->getConnection()->getRowCount("trailRelationship");
     //create a new Trail Relationship and insert it into mySQL
     $trailRelationship = new TrailRelationship($this->segment->getSegmentId(), $this->trail->getTrailId(), $this->VALID_SEGMENTTYPE);
     $trailRelationship->insert($this->getPDO());
     // grab the data from mySQL and enforce the fields match our expectations
     $pdoTrailRelationship = TrailRelationship::getTrailRelationshipBySegmentType($this->getPDO(), $this->VALID_SEGMENTTYPE);
     $this->assertSame($numRows + 1, $this->getConnection()->getRowCount("trailRelationship"));
     $this->assertSame($pdoTrailRelationship->getSegmentId(), $this->segment->getSegmentId());
     $this->assertSame($pdoTrailRelationship->getTrailId(), $this->trail->getTrailId());
     $this->assertSame($pdoTrailRelationship->getSegmentType(), $this->VALID_SEGMENTTYPE);
 }
Example #2
0
 /**
  *
  * Decodes geoJson file, converts to string, sifts through the string and inserts the data into the database
  *
  * @param string $url
  * @throws PDOException PDO related errors
  * @throws Exception catch-all exception
  **/
 public static function readTrailSegmentsGeoJson($url)
 {
     $context = stream_context_create(array("http" => array("ignore_errors" => true, "method" => "GET")));
     try {
         $pdo = connectToEncryptedMySQL("/var/www/trailquail/encrypted-mysql/trailquail.ini");
         if (($jsonData = file_get_contents($url, null, $context)) !== false) {
             if (($jsonFd = @fopen("php://memory", "wb+")) === false) {
                 throw new RuntimeException("Memory Error: I can't remember");
             }
             //decode the geoJson file
             $jsonConverted = json_decode($jsonData);
             $jsonFeatures = $jsonConverted->features;
             // create array from converted geoJson file
             $properties = new SplFixedArray(count($jsonFeatures));
             //loop through array to get to json properties
             foreach ($jsonFeatures as $jsonFeature) {
                 $properties[$properties->key()] = $jsonFeature->properties;
                 $properties->next();
             }
             //create an array of trails and a SplObjectStorage for the trail guide
             $trails = [];
             $trailGuide = new SplObjectStorage();
             //loop through array to get trail name and trail use
             foreach ($properties as $property) {
                 $trailName = $property->name;
                 $trailUse = "";
                 //set $trailUse string based on information in geoJson properties field.
                 if ($property->bicycle === "yes") {
                     $trailUse = $trailUse . "bicycle: yes, ";
                 } else {
                     $trailUse = $trailUse . "bicycle: no, ";
                 }
                 if ($property->foot === "yes") {
                     $trailUse = $trailUse . "foot: yes, ";
                 } else {
                     $trailUse = $trailUse . "foot: no, ";
                 }
                 if ($property->wheelchair === "yes") {
                     $trailUse = $trailUse . "wheelchair: yes ";
                 } else {
                     $trailUse = $trailUse . "wheelchair: no ";
                 }
                 //get the trail by name and set the trail use
                 try {
                     $candidateTrails = Trail::getTrailByTrailName($pdo, $trailName);
                     foreach ($candidateTrails as $trail) {
                         $trail->setTrailUse($trailUse);
                         $trail->update($pdo);
                         $trails[] = $trail;
                     }
                 } catch (PDOException $pdoException) {
                     $sqlStateCode = "23000";
                     $errorInfo = $pdoException->errorInfo;
                     if ($errorInfo[0] === $sqlStateCode) {
                     } else {
                         throw new PDOException($pdoException->getMessage(), 0, $pdoException);
                     }
                 } catch (Exception $exception) {
                     throw new Exception($exception->getMessage(), 0, $exception);
                 }
             }
             try {
                 $trailIndex = 0;
                 foreach ($jsonFeatures as $jsonFeature) {
                     $jsonCoordinates = $jsonFeature->geometry->coordinates;
                     if ($jsonFeature->geometry->type === "LineString") {
                         $coordinates = new SplFixedArray(count($jsonCoordinates));
                         foreach ($jsonCoordinates as $coordinate) {
                             $coordinates[$coordinates->key()] = $coordinate;
                             $coordinates->next();
                         }
                         $trailGuide[$trails[$trailIndex]] = $coordinates;
                         $trailIndex++;
                     } else {
                         if ($jsonFeature->geometry->type === "MultiLineString") {
                             $trailClones = [];
                             $trails[$trailIndex]->delete($pdo);
                             for ($i = 1; $i <= count($jsonCoordinates); $i++) {
                                 $trail = clone $trails[$trailIndex];
                                 $trail->setTrailId(null);
                                 $trail->setTrailName($trail->getTrailName() . " {$i}");
                                 $trail->insert($pdo);
                                 $trailClones[] = $trail;
                             }
                             array_splice($trails, $trailIndex, 1, $trailClones);
                             foreach ($jsonCoordinates as $lineCoordinates) {
                                 $trailGuide[$trails[$trailIndex]] = $lineCoordinates;
                                 $trailIndex++;
                             }
                         }
                     }
                 }
                 $trailGuide->rewind();
                 foreach ($trailGuide as $map) {
                     $trail = $trailGuide->current();
                     $geo = $trailGuide->getInfo();
                     for ($indexTwo = 0; $indexTwo < count($geo) - 1; $indexTwo++) {
                         $segmentStartX = $geo[$indexTwo][0];
                         $segmentStartY = $geo[$indexTwo][1];
                         //							$segmentStartElevation = $geo[$indexTwo][2];
                         $segmentStopX = $geo[$indexTwo + 1][0];
                         $segmentStopY = $geo[$indexTwo + 1][1];
                         //							$segmentStopElevation = $geo[$indexTwo + 1][2];
                         $segmentStart = new Point($segmentStartX, $segmentStartY);
                         $segmentStop = new Point($segmentStopX, $segmentStopY);
                         try {
                             $segment = new Segment(null, $segmentStart, $segmentStop, 0, 0);
                             $segment->insert($pdo);
                             $relationship = new TrailRelationship($segment->getSegmentId(), $trail->getTrailId(), "T");
                             $relationship->insert($pdo);
                         } catch (PDOException $pdoException) {
                             $sqlStateCode = "23000";
                             $errorInfo = $pdoException->errorInfo;
                             if ($errorInfo[0] === $sqlStateCode) {
                             } else {
                                 throw new PDOException($pdoException->getMessage(), 0, $pdoException);
                             }
                         } catch (Exception $exception) {
                             throw new Exception($exception->getMessage(), 0, $exception);
                         }
                     }
                 }
             } catch (PDOException $pdoException) {
                 $sqlStateCode = "23000";
                 $errorInfo = $pdoException->errorInfo;
                 if ($errorInfo[0] === $sqlStateCode) {
                 } else {
                     throw new PDOException($pdoException->getMessage(), 0, $pdoException);
                 }
             } catch (Exception $exception) {
                 throw new Exception($exception->getMessage(), 0, $exception);
             }
         }
         fclose($jsonFd);
     } catch (PDOException $pdoException) {
         throw new PDOException($pdoException->getMessage(), 0, $pdoException);
     } catch (Exception $exception) {
         throw new Exception($exception->getMessage(), 0, $exception);
     }
 }