コード例 #1
0
ファイル: AlertsController.php プロジェクト: AnduZhang/nws
 private function getAlertIdInformation($alertId)
 {
     try {
         if ($alertId) {
             $alertData = models\WeatherAlert::findOne($alertId);
             if (!$alertData) {
                 throw new Exception('Record with alertId=' . $alertId . ' not found in our database');
             }
             $WeatherAlertArea = models\WeatherAlertArea::find()->where(['WeatherAlert_id' => $alertData->id])->one();
             $areaForAlertData = models\AreaDefinition::findAll(['WeatherAlertArea_id' => $WeatherAlertArea->id]);
             $alertCircle = new \stdClass();
             $alertPolygonCoordinates = [];
             foreach ($areaForAlertData as $coordinates) {
                 if ($coordinates->radius) {
                     $alertCircle->radius = $coordinates->radius;
                     //Radius can be only one record
                     $alertCircle->centerLat = $coordinates->latitude;
                     //Radius can be only one record
                     $alertCircle->centerLon = $coordinates->longitude;
                     //Radius can be only one record
                 } else {
                     $alertPolygonCoordinates[] = $coordinates->latitude . ' ' . $coordinates->longitude;
                 }
             }
             //                var_dump($alertPolygonCoordinates);die;
             $propertiesList = models\NRESProperty::find()->where(['status' => models\NRESProperty::STATUS_ACTIVE])->all();
             $pointChecker = new PointInPolygon();
             $this->_affectedProperties = [];
             foreach ($propertiesList as $property) {
                 $isPropertyAffectedByAlert = false;
                 if (isset($alertCircle->radius)) {
                     //Checking circle coordinates
                     if ($pointChecker->pointInsideCircle($alertCircle->centerLat, $alertCircle->centerLon, $alertCircle->radius, $property->latitude, $property->longitude)) {
                         $isPropertyAffectedByAlert = true;
                     }
                 }
                 if (!empty($alertPolygonCoordinates)) {
                     if ($pointChecker->pointInPolygon($property->latitude . ' ' . $property->longitude, $alertPolygonCoordinates) !== "outside") {
                         $isPropertyAffectedByAlert = true;
                     }
                 }
                 if ($isPropertyAffectedByAlert) {
                     $this->_affectedProperties[] = $property;
                 }
             }
             if (!$this->_affectedProperties) {
                 throw new Exception("There are no properties affected by this Alert. Nothing to export");
             }
             return $alertData;
         } else {
             throw new Exception('Please choose Alert that you want to export');
         }
     } catch (\Exception $e) {
         echo json_encode(['error' => $e->getMessage()]);
     }
     Yii::$app->end();
 }
コード例 #2
0
ファイル: WeatherAlertArea.php プロジェクト: AnduZhang/nws
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getAreaDefinitions()
 {
     return $this->hasMany(AreaDefinition::className(), ['WeatherAlertArea_id' => 'id']);
 }
コード例 #3
0
ファイル: FtpHelper.php プロジェクト: AnduZhang/nws
 public function insertNewLSRRecords($records)
 {
     foreach ($records['alerts'] as $record) {
         if (in_array(strtolower($record['event']), $this->filterEventType)) {
             $weatherAlertsModel = new models\WeatherAlert();
             switch (strtolower($record['event'])) {
                 case 'hurricane':
                     $weatherAlertsModel->event = 0;
                     break;
                 case 'tornado':
                     $weatherAlertsModel->event = 1;
                     break;
                 default:
                     $weatherAlertsModel->event = 2;
                     break;
             }
             $weatherAlertsModel->status = models\WeatherAlert::STATUS_ACTUAL;
             $weatherAlertsModel->type = 1;
             $weatherAlertsModel->magnitude = $record['magnitude'];
             $weatherAlertsModel->magnitudeUnit = $record['magnitudeUnit'];
             //We need to display date in needed format
             $newTime = substr_replace($record['time'], ':', 2, 0);
             //                $newDate = str_replace('/', '-', $record['date']);
             $dateforConvertionArray = explode('/', $record['date']);
             $finalDateTime = $dateforConvertionArray[2] . '-' . $dateforConvertionArray[0] . '-' . $dateforConvertionArray[1] . 'T' . date("H:i:s", strtotime($newTime));
             $weatherAlertsModel->date = DateTimeHelper::createUnixtimeFromDate($finalDateTime);
             $weatherAlertsModel->save();
             $weatherAlertAreaModel = new models\WeatherAlertArea();
             $weatherAlertAreaModel->WeatherAlert_id = $weatherAlertsModel->id;
             $weatherAlertAreaModel->save();
             $areaDefinitionModel = new models\AreaDefinition();
             $areaDefinitionModel->WeatherAlertArea_id = $weatherAlertAreaModel->id;
             $areaDefinitionModel->latitude = $record['latitude'];
             $areaDefinitionModel->longitude = $record['longitude'];
             $areaDefinitionModel->save();
             $this->insertedAlerts++;
             $this->consoleLog('One alert entry added (ID: ' . $weatherAlertsModel->id . ')');
         } else {
             $this->ignoredAlerts++;
             $this->consoleLog('One alert entry skipped (Filtered by event type)');
         }
     }
 }
コード例 #4
0
 public function actionExportrest()
 {
     $startTime = time();
     $this->_writeLogLine('Start Date/Time - ' . date('Y-m-d\\TH:i:s', $startTime));
     header('Content-type: application/json');
     try {
         $data = file_get_contents("php://input");
         $data = json_decode($data);
         if ($data && isset($data->alertId)) {
             $this->format = isset($data->format) && $data->format ? $data->format : $this->format;
             $this->tmpPath = isset($data->tmpPath) && $data->tmpPath ? $data->tmpPath : $this->tmpPath;
             if (!is_dir(Yii::$app->basePath . '/' . $this->tmpPath . '/')) {
                 throw new Exception('Temp directory path is invalid.');
             }
             $alertData = models\WeatherAlert::findOne($data->alertId);
             if (!$alertData) {
                 throw new Exception('Record with alertId=' . $data->alertId . ' not found in our database');
             }
             $areaForAlertData = models\AreaDefinition::findAll(['WeatherAlertArea_id' => $alertData->id]);
             $alertCircle = new \stdClass();
             $alertPolygonCoordinates = [];
             foreach ($areaForAlertData as $coordinates) {
                 if ($coordinates->radius) {
                     $alertCircle->radius = $coordinates->radius;
                     //Radius can be only one record
                     $alertCircle->centerLat = $coordinates->latitude;
                     //Radius can be only one record
                     $alertCircle->centerLon = $coordinates->longitude;
                     //Radius can be only one record
                 } else {
                     $alertPolygonCoordinates[] = $coordinates->latitude . ' ' . $coordinates->longitude;
                 }
             }
             //                var_dump($alertPolygonCoordinates);die;
             $propertiesList = models\NRESProperty::find()->all();
             $pointChecker = new PointInPolygon();
             $this->_affectedProperties = [];
             foreach ($propertiesList as $property) {
                 $isPropertyAffectedByAlert = false;
                 if (isset($alertCircle->radius)) {
                     //Checking circle coordinates
                     if ($pointChecker->pointInsideCircle($alertCircle->centerLat, $alertCircle->centerLon, $alertCircle->radius, $property->latitude, $property->longitude)) {
                         $isPropertyAffectedByAlert = true;
                     }
                 }
                 if (!empty($alertPolygonCoordinates)) {
                     if ($pointChecker->pointInPolygon($property->latitude . ' ' . $property->longitude, $alertPolygonCoordinates) !== "outside") {
                         $isPropertyAffectedByAlert = true;
                     }
                 }
                 if ($isPropertyAffectedByAlert) {
                     $this->_affectedProperties[] = $property;
                 }
             }
             if (!$this->_affectedProperties) {
                 throw new Exception("There are no properties affected by this Alert (" . $alertData->id . ")");
             }
             switch ($this->format) {
                 case 'csv':
                     $generatedFileURL = $this->generateCSV($alertData);
                     break;
                 case 'pdf':
                     $generatedFileURL = $this->generatePDF($alertData);
                     break;
                 default:
                     throw new Exception("Invalid format of output file is defined");
             }
             $this->_writeLogLine('Affected Properties - ' . count($this->_affectedProperties));
             $this->_writeLogLine('Generated File URL - ' . $generatedFileURL);
             $this->_writeLogLine('End Date/Time - ' . date('Y-m-d\\TH:i:s', time()));
             //Now we need to check if some properties are located inside this coordinates
             $responce = ['fileUrl' => $generatedFileURL, 'affectedProperties' => count($this->_affectedProperties)];
             header('Content-type: application/json');
             echo json_encode($responce);
             //                if ($this->_debug == 'true') {
             //
             //                    echo json_encode(array_merge(['start'=>date('Y-m-d\TH:i:s',$startTime),'end'=>date('Y-m-d\TH:i:s',$endTime)],(array)$atomGeneralInformation));
             //                } else {
             //                    echo json_encode(['result'=>'Job Done']);
             //                }
         } else {
             throw new Exception('Missing alertId parameter');
         }
     } catch (\Exception $e) {
         echo json_encode(['error' => $e->getMessage()]);
         $this->_writeLogLine($e->getMessage());
     }
     Yii::$app->end();
 }
コード例 #5
0
ファイル: CronController.php プロジェクト: AnduZhang/nws
 public function parseAtomFeed()
 {
     $url = Yii::$app->params['AtomFeedParser']['url'];
     $startTime = time();
     $needToUpdateCAP = false;
     try {
         $atomGeneralInformation = Yii::$app->CAPParser->getAtomGeneralContent($url);
         $atomEntriesContent = Yii::$app->CAPParser->getEntriesContent($url);
         if (!isset($atomGeneralInformation['updated']) || !isset($atomGeneralInformation['id'])) {
             throw new Exception("Can't parse AtomFeed file by given URL");
         }
         $processedFeedsModel = new models\ProcessedATOMFeeds();
         $processedFeedsModelSearch = new models\ProcessedATOMFeedsSearch();
         $atomRecordInDb = $processedFeedsModelSearch->findOne(['feedURL' => $atomGeneralInformation['id']]);
         if (!$atomRecordInDb) {
             //We don't have records at all for this file
             $processedFeedsModel->updated = helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated']);
             $processedFeedsModel->feedURL = $atomGeneralInformation['id'];
             $processedFeedsModel->save();
             $this->consoleLog('Added ProcessedATOMFeeds Record, id - ' . $processedFeedsModel->feedURL . ', updated - ' . helpers\DateTimeHelper::createDateFromUnixtime($processedFeedsModel->updated) . '(' . $atomGeneralInformation['updated'] . ')');
             $needToUpdateCAP = true;
         } else {
             if ($atomRecordInDb->updated != helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated'])) {
                 $this->consoleLog('We need to update records in WeatherAlert and WeatherAlertArea tables.Updated time in the database -' . helpers\DateTimeHelper::createDateFromUnixtime($atomRecordInDb->updated) . ', from file - ' . $atomGeneralInformation['updated'] . ' (' . helpers\DateTimeHelper::createDateFromUnixtime(helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated'])) . ')');
                 $atomRecordInDb->updated = helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated']);
                 $atomRecordInDb->save();
                 $needToUpdateCAP = true;
             }
         }
         if (!$needToUpdateCAP) {
             $this->consoleLog('We DON\'T need to update records in WeatherAlert and WeatherAlertArea tables.');
         }
         if ($needToUpdateCAP) {
             //change 1 to $needToUpdateCAP
             foreach ($atomEntriesContent['entries'] as $entry) {
                 $entryContent = $this->_getCapAreaInformationInDatabse($entry);
                 if ($entryContent) {
                     // Now we try to find this alert and check if we have it already in our database
                     $alertModel = models\WeatherAlert::find()->where(['identifier' => $entryContent['identifier'], 'date' => helpers\DateTimeHelper::createUnixtimeFromDate($entryContent['date'])])->one();
                     if (!$alertModel) {
                         $alertModel = new models\WeatherAlert();
                     } else {
                         $this->skippedAlertsCount++;
                         $this->consoleLog('One alert entry skipped (Information about this alert is actual in our database, ID = ' . $alertModel->id . ')');
                         continue;
                     }
                     $alertModel->attributes = $entryContent;
                     $alertModel->date = helpers\DateTimeHelper::createUnixtimeFromDate($alertModel->date);
                     $alertModel->save();
                     $this->consoleLog('One alert entry added (ID: ' . $alertModel->id . ')');
                     $this->addedAlertsCount++;
                     if ($alertModel->updates) {
                         //WE need to change status of old alert
                         //TODO: this part of code should be checked on real data
                         $alertModel = models\WeatherAlert::find()->where(['identifier' => $alertModel->updates])->one();
                         $alertModel->status = models\WeatherAlert::STATUS_UPDATED;
                         $alertModel->save();
                     }
                     $weatherAlertAreaModel = new models\WeatherAlertArea();
                     $weatherAlertAreaModel->WeatherAlert_id = $alertModel->id;
                     $weatherAlertAreaModel->save();
                     if (isset($entryContent['polygon']['0'])) {
                         $coordinatePairs = explode(' ', $entryContent['polygon']['0']);
                         foreach ($coordinatePairs as $pair) {
                             $areaDefinitionModel = new models\AreaDefinition();
                             $areaDefinitionModel->WeatherAlertArea_id = $weatherAlertAreaModel->id;
                             $latLonFromPair = explode(',', $pair);
                             $areaDefinitionModel->latitude = $latLonFromPair[0];
                             $areaDefinitionModel->longitude = $latLonFromPair[1];
                             $areaDefinitionModel->save();
                         }
                     }
                     if (isset($entryContent['circle']['0'])) {
                         $coordinatePair = explode(' ', $entryContent['polygon']['0']);
                         $areaDefinitionModel = new models\AreaDefinition();
                         $areaDefinitionModel->WeatherAlertArea_id = $weatherAlertAreaModel->id;
                         $latLonFromPair = explode(',', $coordinatePair);
                         $areaDefinitionModel->latitude = $latLonFromPair[0];
                         $areaDefinitionModel->longitude = $latLonFromPair[1];
                         $areaDefinitionModel->radius = $coordinatePair[1];
                         $areaDefinitionModel->save();
                     }
                 }
             }
         }
         $endTime = time();
         $this->consoleLog('Start date - ' . date('Y-m-d\\TH:i:s', $startTime));
         $this->consoleLog('End date - ' . date('Y-m-d\\TH:i:s', $endTime));
         $this->consoleLog('Execution Time - ' . ($endTime - $startTime) . ' seconds.');
         $this->consoleLog('Count of added alerts - ' . $this->addedAlertsCount);
         $this->consoleLog('Count of skipped alerts - ' . $this->skippedAlertsCount);
         $atomGeneralInformation = array_merge(['start' => date('Y-m-d\\TH:i:s', $startTime), 'end' => date('Y-m-d\\TH:i:s', $endTime)], $atomGeneralInformation);
         $atomGeneralInformation['entries'] = isset($atomEntriesContent['entries']) ? $atomEntriesContent['entries'] : [];
         return $atomGeneralInformation;
     } catch (\Exception $e) {
         $this->consoleLog('Process ERROR -  ' . $e->getMessage());
         Yii::$app->end();
     }
 }
コード例 #6
0
ファイル: MockForm.php プロジェクト: AnduZhang/nws
 private function _createAffectedProperties($weatherAlertAreaId)
 {
     $areaDefinition = AreaDefinition::find()->where(['WeatherAlertArea_id' => $weatherAlertAreaId])->all();
     $pointChecker = new PointInPolygon();
     if (count($areaDefinition) && count($areaDefinition) == 1) {
         $needToInsertProperties = (int) $this->countPostAffected;
         $alertCircle = $areaDefinition[0];
         while ($needToInsertProperties) {
             $property = new \stdClass();
             $property->latitude = $alertCircle->latitude + $this->_generateRand(false, rand(300, 1000));
             $property->longitude = $alertCircle->longitude + $this->_generateRand(false, rand(300, 1000));
             if ($pointChecker->pointInsideCircle($alertCircle->latitude, $alertCircle->longitude, $alertCircle->radius, $property->latitude, $property->longitude)) {
                 //insert property
                 $newProperty = new NRESProperty();
                 $newProperty->attributes = (array) $property;
                 $newProperty->name = 'Generated';
                 $newProperty->zipcode = '12345-6789';
                 $newProperty->city = 'Test';
                 $newProperty->state = 'CA';
                 $newProperty->streetAddress = 'Test';
                 $newProperty->client = 'Test';
                 $newProperty->status = 2;
                 $newProperty->save();
                 if ($newProperty->getErrors()) {
                     var_dump($newProperty->getErrors());
                     die;
                 }
                 $needToInsertProperties--;
             }
         }
     } else {
         $allCoordinates = [];
         $needToInsertProperties = $this->isStormPath ? $this->countAffectedInStormPath : (int) $this->countPreAffected;
         foreach ($areaDefinition as $point) {
             $allCoordinates[] = $point->latitude . ' ' . $point->longitude;
         }
         while ($needToInsertProperties) {
             $propertyToGet = rand(0, count($areaDefinition) - 1);
             $property = new \stdClass();
             $property->latitude = $areaDefinition[$propertyToGet]->latitude + $this->_generateRand();
             $property->longitude = $areaDefinition[$propertyToGet]->longitude + $this->_generateRand();
             if ($pointChecker->pointInPolygon($property->latitude . ' ' . $property->longitude, $allCoordinates) !== "outside") {
                 //insert property
                 $newProperty = new NRESProperty();
                 $newProperty->attributes = (array) $property;
                 $newProperty->name = 'Generated';
                 $newProperty->zipcode = '12345-6789';
                 $newProperty->city = 'Test';
                 $newProperty->state = 'CA';
                 $newProperty->streetAddress = 'Test';
                 $newProperty->client = 'Test';
                 $newProperty->status = 2;
                 $newProperty->save();
                 $needToInsertProperties--;
             }
         }
     }
 }