Пример #1
0
 public static function getStops(Agency $agency, $version = 0)
 {
     /**
      * @var DB
      */
     $dbObj = DBPool::getInstance();
     $version = $version == 0 ? TableUpdate::getVersion() : $version;
     $dbObj->bindParams(array($agency->getId(), TableUpdate::getVersion()));
     $stops = $dbObj->get_results("SELECT * FROM stop WHERE agency_id=?\n            AND version = ?");
     if ($dbObj->num_rows > 0) {
         $stopArray = array();
         foreach ($stops as $s) {
             $stopObj = new Stop();
             $stopObj->setId($s->id);
             $stopObj->setAgency($agency);
             $stopObj->setFlipStopTag($s->flip_stop_tag);
             $stopObj->setTag($s->tag);
             $stopObj->setTitle($s->title);
             $stopObj->setPrettyTitle($s->pretty_title);
             $stopObj->setLatitude($s->latitude);
             $stopObj->setLongitude($s->longitude);
             $stopArray[$s->tag] = $stopObj;
         }
         return $stopArray;
     } else {
         //TODO: Don't use a generic exception
         throw new Exception("No data available - Stop::getStops");
     }
 }
 public function updateStops()
 {
     $routeArray = Route::getRoutes($this->agency);
     $stopArray = array();
     //Final list of Stop objects
     $bartApiKey = $this->appConfig['BART_API_KEY'];
     //Platform mapping
     $xmlStr = '<?xml version="1.0"?><agency title="BART" shortTitle="bart"></agency>';
     $platformXml = new SimpleXMLElement($xmlStr);
     foreach ($this->xml->stations->station as $s) {
         $stationTag = (string) $s->abbr;
         //Fetch the station details
         $apiURL = str_replace(BartApiEndPoints::ORIG, $stationTag, BartApiEndPoints::STATION_INFO . $bartApiKey);
         $stationInfoXmlBuilder = new XmlObjBuilder($apiURL);
         $stationInfoXml = $stationInfoXmlBuilder->getXmlObj();
         $stopObj = new Stop();
         $stopObj->setAgency($this->agency);
         $stopObj->setTag($stationTag);
         $stopObj->setTitle((string) $stationInfoXml->stations->station->name);
         $stopObj->setLatitude((string) $stationInfoXml->stations->station->gtfs_latitude);
         $stopObj->setLongitude((string) $stationInfoXml->stations->station->gtfs_longitude);
         $stopArray[$stationTag] = $stopObj;
         /************* Add to platform XML object ****************/
         //Add stop node
         $stopNode = $platformXml->addChild("stop");
         $stopNode->addAttribute("tag", $stationTag);
         //Add platform node as a child of the stop node (north)
         $platformNode = $stopNode->addChild("platform");
         $northPlatforms = array();
         foreach ($stationInfoXml->stations->station->north_platforms as $p) {
             foreach ($p as $pNum) {
                 $northPlatforms[] = trim((string) $pNum);
             }
         }
         $platformNode->addAttribute("number", implode(",", $northPlatforms));
         //Add directions as a children of the platform node
         foreach ($stationInfoXml->stations->station->north_routes as $r) {
             foreach ($r->route as $direction) {
                 $dirStr = trim((string) $direction);
                 $dirTagArray = explode(" ", $dirStr);
                 $dirTag = $dirTagArray[1];
                 $platformNode->addChild("direction", $dirTag);
             }
         }
         //Add platform node as a child of the stop node (south)
         $platformNode = $stopNode->addChild("platform");
         $southPlatforms = array();
         foreach ($stationInfoXml->stations->station->south_platforms as $p) {
             foreach ($p as $pNum) {
                 $southPlatforms[] = trim((string) $pNum);
             }
         }
         $platformNode->addAttribute("number", implode(",", $southPlatforms));
         //Add directions as a children of the platform node
         foreach ($stationInfoXml->stations->station->south_routes as $r) {
             foreach ($r as $direction) {
                 $dirStr = trim((string) $direction);
                 $dirTagArray = explode(" ", $dirStr);
                 $dirTag = $dirTagArray[1];
                 $platformNode->addChild("direction", $dirTag);
             }
         }
     }
     //Write platform mapping XML to file
     $fileName = Util::getBaseDirectoryPath(Util::XML_FILE) . "bart-platforms.xml";
     Util::prettyPrintXml($platformXml, $fileName);
     //Write the stops to the database
     try {
         //Pass it to the base RouteUpdate class
         $stopUpdate = new StopUpdate($this->agency, $stopArray);
         $stopUpdate->updateDB();
     } catch (Exception $ex) {
         throw new Exception($ex->getMessage());
     }
 }
 public function updateStops()
 {
     $routeArray = Route::getRoutes($this->agency);
     $flipStopMap = $this->getFlipStopOverrides();
     $stopTitleMap = array();
     $stopLatLonMap = array();
     $uniqueStopsAcrossRoutes = array();
     //Generate maps of stopTag => StopTitle and stopTag => [lat, lon] for
     //later look up
     foreach ($this->xml->route as $r) {
         foreach ($r->stop as $s) {
             $stopTag = (string) $s['tag'];
             $stopTitle = html_entity_decode((string) $s['title']);
             $lat = (string) $s['lat'];
             $lon = (string) $s['lon'];
             $stopTitleMap[$stopTag] = $stopTitle;
             $stopLatLonMap[$stopTag] = array("lat" => $lat, "lon" => $lon);
             $uniqueStopsAcrossRoutes[] = $stopTag;
         }
         //Stops
     }
     //Routes
     //We'll only consider the stops in the directions with show=true
     foreach ($this->xml->route as $r) {
         $routeTag = (string) $r['tag'];
         //if($routeTag != "25") {continue;}
         $routeObj = $routeArray[$routeTag];
         $dirArray = Direction::getDirections($routeObj);
         $agencyName = $routeObj->getAgency()->getShortTitle();
         $trackDuplicatesInDir = array();
         /**
          * @var Array $routeDirDetails - Array representation of the
          * directions and stops in a route
          */
         $routeDirDetails = array();
         foreach ($r->direction as $d) {
             $dirTag = (string) $d['tag'];
             $dirObj = $dirArray[$dirTag];
             $dirStops = array();
             //if ($dirObj->getShow()) {
             foreach ($d->stop as $s) {
                 $stopTag = (string) $s['tag'];
                 $stopTitle = $stopTitleMap[$stopTag];
                 $dirStops[$stopTag] = $stopTitle;
             }
             //Stops
             $routeDirDetails[$dirObj->getTag()] = $dirStops;
             //} //Check for show=true
         }
         //Directions
         //Now that we have the details of the directions for this group,
         //let's find the flip stops
         foreach ($routeDirDetails as $fDirTag => $fDirStops) {
             foreach ($routeDirDetails as $fDirTagDiffDir => $fDirStopsDiffDir) {
                 if ($fDirTag == $fDirTagDiffDir) {
                     continue;
                 }
                 foreach ($fDirStops as $fStopTag => $fStopTitle) {
                     //Check if we have one or more matching flip stops in the
                     //direction we are checking against
                     $fFlipStopInOppDir = $this->getFlipStopsInOppDir($fStopTag, $fStopTitle, $fDirStopsDiffDir);
                     //If we don't have any flip stops continue to the next stop
                     if (count($fFlipStopInOppDir) == 0) {
                         continue;
                     }
                     if (count($fFlipStopInOppDir) > 1) {
                         //We have encountered more than one stop at the
                         //same intersection in a different direction
                         //TODO: This has to go in the e-mail report
                         //Generate the (stopTag, lat, lon) string
                         $fstopLatLonMap = array();
                         $fstopLatLonMap[] = "{$fStopTag}," . implode(",", $stopLatLonMap[$fStopTag]);
                         foreach ($fFlipStopInOppDir as $tempStopForOppDir) {
                             $fstopLatLonMap[] = "{$tempStopForOppDir}," . implode(",", $stopLatLonMap[$tempStopForOppDir]);
                         }
                         $logStr = "More than one stop in diff. direction [agency: " . $agencyName . "] [route: " . $routeTag . "] [current stop|dir: {$fStopTag}|{$fDirTag}] [other stops|dir: " . implode(",", $fFlipStopInOppDir) . "|{$fDirTagDiffDir}] [stop title: " . $fStopTitle . "] [" . implode("|", $fstopLatLonMap) . "]";
                         //$this->logger->log($logStr, Logger::WARN, NxtbusStop::PACKAGE);
                     } else {
                         if (!array_key_exists($fStopTag, $flipStopMap)) {
                             $tempFlipStopTag = $fFlipStopInOppDir[0];
                             $flipStopMap[$fStopTag] = $tempFlipStopTag;
                             $flipStopMap[$tempFlipStopTag] = $fStopTag;
                         }
                     }
                 }
             }
             // Inner loop
         }
         //Find flip stops
     }
     //Routes
     //Check if any of the stops don't have a flip stop
     $uniqueStopsAcrossRoutes = array_unique($uniqueStopsAcrossRoutes);
     foreach ($uniqueStopsAcrossRoutes as $finalCheckStopTag) {
         if (!array_key_exists($finalCheckStopTag, $flipStopMap)) {
             $flipStopMap[$finalCheckStopTag] = "";
         }
     }
     //Create the Stop objects
     $stopArray = array();
     foreach ($this->xml->route as $r) {
         foreach ($r->stop as $s) {
             $stopTag = (string) $s['tag'];
             $stopTitle = html_entity_decode($s['title']);
             if (array_key_exists($stopTag, $flipStopMap) && !array_key_exists($stopTag, $stopArray)) {
                 $stopObj = new Stop();
                 $stopObj->setAgency($this->agency);
                 $stopObj->setTag($stopTag);
                 $stopObj->setTitle((string) $stopTitle);
                 $stopObj->setFlipStopTag($flipStopMap[$stopTag]);
                 $stopObj->setLatitude((string) $s['lat']);
                 $stopObj->setLongitude((string) $s['lon']);
                 $stopArray[$stopTag] = $stopObj;
             }
         }
         //Stops
     }
     //Routes
     try {
         //Pass it to the base RouteUpdate class
         $stopUpdate = new StopUpdate($this->agency, $stopArray);
         $stopUpdate->updateDB();
     } catch (Exception $ex) {
         throw new Exception($ex->getMessage());
     }
 }