Skip to content

Commit 67d89a0

Browse files
authored
added Air Pollution API for Carbon Monoxide (#138)
added Air Pollution API for Carbon Monoxide
2 parents 3abb656 + c872cab commit 67d89a0

15 files changed

+526
-5
lines changed

Cmfcmf/OpenWeatherMap.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
namespace Cmfcmf;
2020

21+
use Cmfcmf\OpenWeatherMap\AirPollution;
2122
use Cmfcmf\OpenWeatherMap\CurrentWeather;
22-
use Cmfcmf\OpenWeatherMap\UVIndex;
2323
use Cmfcmf\OpenWeatherMap\CurrentWeatherGroup;
2424
use Cmfcmf\OpenWeatherMap\Exception as OWMException;
25+
use Cmfcmf\OpenWeatherMap\NotFoundException as OWMNotFoundException;
26+
use Cmfcmf\OpenWeatherMap\UVIndex;
2527
use Cmfcmf\OpenWeatherMap\WeatherForecast;
2628
use Psr\Cache\CacheItemPoolInterface;
2729
use Psr\Http\Client\ClientInterface;
@@ -67,6 +69,11 @@ class OpenWeatherMap
6769
*/
6870
private $uvIndexUrl = 'https://api.openweathermap.org/data/2.5/uvi';
6971

72+
/**
73+
* @var string The basic api url to fetch air pollution data from.
74+
*/
75+
private $airPollutionUrl = 'https://api.openweathermap.org/pollution/v1/';
76+
7077
/**
7178
* @var CacheItemPoolInterface|null $cache The cache to use.
7279
*/
@@ -336,6 +343,45 @@ public function getHistoricUVIndex($lat, $lon, $start, $end)
336343
}, $data);
337344
}
338345

346+
/**
347+
* Returns air pollution data
348+
*
349+
* @param string $type One of CO, O3, SO2, and NO2.
350+
* @param string $lat The location's latitude.
351+
* @param string $lon The location's longitude.
352+
* @param string $date The date to gather data from. If you omit this parameter or supply "current", returns current data.
353+
*
354+
* @return AirPollution\COAirPollution|AirPollution\NO2AirPollution|AirPollution\O3AirPollution|AirPollution\SO2AirPollution|null The air pollution data or null if no data was found.
355+
*
356+
* We use strings as $lat and $lon, since the exact number of digits in $lat and $lon determines the search range.
357+
* For example, there is a difference between using "1.5" and "1.5000".
358+
* We also use a string for $date, since it may either be "current" or an (abbreviated) ISO 8601 timestamp like 2016Z.
359+
*
360+
* @throws OWMException|\Exception
361+
*
362+
* @api
363+
*/
364+
public function getAirPollution($type, $lat, $lon, $date = "current")
365+
{
366+
$answer = $this->getRawAirPollutionData($type, $lat, $lon, $date);
367+
if ($answer === null) {
368+
return null;
369+
}
370+
$json = $this->parseJson($answer);
371+
switch ($type) {
372+
case "O3":
373+
return new AirPollution\O3AirPollution($json);
374+
case "NO2":
375+
return new AirPollution\NO2AirPollution($json);
376+
case "SO2":
377+
return new AirPollution\SO2AirPollution($json);
378+
case "CO":
379+
return new AirPollution\COAirPollution($json);
380+
default:
381+
throw new \LogicException();
382+
}
383+
}
384+
339385
/**
340386
* Directly returns the xml/json/html string returned by OpenWeatherMap for the current weather.
341387
*
@@ -471,6 +517,40 @@ public function getRawUVIndexData($mode, $lat, $lon, $cnt = null, $start = null,
471517
return $this->cacheOrFetchResult($url);
472518
}
473519

520+
/**
521+
* Fetch raw air pollution data
522+
*
523+
* @param string $type One of CO, O3, SO2, and NO2.
524+
* @param string $lat The location's latitude.
525+
* @param string $lon The location's longitude.
526+
* @param string $date The date to gather data from. If you omit this parameter or supply "current", returns current data.
527+
*
528+
* @return string|null The air pollution data or null if no data was found.
529+
*
530+
* We use strings as $lat and $lon, since the exact number of digits in $lat and $lon determines the search range.
531+
* For example, there is a difference between using "1.5" and "1.5000".
532+
* We also use a string for $date, since it may either be "current" or an (abbreviated) ISO 8601 timestamp like 2016Z.
533+
*
534+
* @api
535+
*/
536+
public function getRawAirPollutionData($type, $lat, $lon, $date = "current")
537+
{
538+
if (!in_array($type, ["CO", "O3", "SO2", "NO2"])) {
539+
throw new \InvalidArgumentException('Invalid $type received.');
540+
}
541+
if (!is_string($lat) || !is_string($lon) || !is_string($date)) {
542+
throw new \InvalidArgumentException('$lat, $lon and $date all must be strings.');
543+
}
544+
545+
$url = $this->airPollutionUrl . strtolower($type) . "/$lat,$lon/$date.json?appid=" . $this->apiKey;
546+
547+
try {
548+
return $this->cacheOrFetchResult($url);
549+
} catch (OWMNotFoundException $e) {
550+
return null;
551+
}
552+
}
553+
474554
/**
475555
* Returns whether or not the last result was fetched from the cache.
476556
*
@@ -505,7 +585,10 @@ private function cacheOrFetchResult($url)
505585
$response = $this->httpClient->sendRequest($this->httpRequestFactory->createRequest("GET", $url));
506586
$result = $response->getBody()->getContents();
507587
if ($response->getStatusCode() !== 200) {
508-
throw new OWMException('OpenWeatherMap returned a response with status code ' . $response->getStatusCode() . ' and the following content '. $result);
588+
if ($result === "{\"message\":\"not found\"}\n" && $response->getStatusCode() === 404) {
589+
throw new OWMNotFoundException();
590+
}
591+
throw new OWMException('OpenWeatherMap returned a response with status code ' . $response->getStatusCode() . ' and the following content `'. $result . '`');
509592
}
510593

511594
if ($this->cache !== null) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\AirPollution;
20+
21+
use Cmfcmf\OpenWeatherMap\Util\Location;
22+
23+
abstract class BaseAirPollution
24+
{
25+
/**
26+
* @var \DateTime
27+
*/
28+
public $time;
29+
30+
/**
31+
* @var Location
32+
*/
33+
public $location;
34+
35+
/**
36+
* @param object $json
37+
*
38+
* @throws \Exception
39+
* @internal
40+
*/
41+
public function __construct($json)
42+
{
43+
$this->time = new \DateTime($json->time, new \DateTimeZone('UTC'));
44+
$this->location = new Location($json->location->latitude, $json->location->longitude);
45+
}
46+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\AirPollution;
20+
21+
class COAirPollution extends PrecisionPressureValueAirPollution
22+
{
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\AirPollution;
20+
21+
use Cmfcmf\OpenWeatherMap\Util\Unit;
22+
23+
class NO2AirPollution extends BaseAirPollution
24+
{
25+
26+
/**
27+
* @var Unit
28+
*/
29+
public $value;
30+
31+
/**
32+
* @var Unit
33+
*/
34+
public $valueStratosphere;
35+
36+
/**
37+
* @var Unit
38+
*/
39+
public $valueTroposphere;
40+
41+
public function __construct($json)
42+
{
43+
parent::__construct($json);
44+
45+
$this->value = new Unit($json->data->no2->value, "g/m³", "", $json->data->no2->precision);
46+
$this->valueStratosphere = new Unit($json->data->no2_strat->value, "g/m³", "", $json->data->no2_strat->precision);
47+
$this->valueTroposphere = new Unit($json->data->no2_trop->value, "g/m³", "", $json->data->no2_trop->precision);
48+
}
49+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\AirPollution;
20+
21+
use Cmfcmf\OpenWeatherMap\Util\Unit;
22+
23+
class O3AirPollution extends BaseAirPollution
24+
{
25+
26+
/**
27+
* @var Unit
28+
*/
29+
public $value;
30+
31+
public function __construct($json)
32+
{
33+
parent::__construct($json);
34+
35+
$this->value = new Unit($json->data, "DU");
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\AirPollution;
20+
21+
use Cmfcmf\OpenWeatherMap\Util\Unit;
22+
23+
class PrecisionPressureValueAirPollution extends BaseAirPollution
24+
{
25+
26+
/**
27+
* @var object[]
28+
*/
29+
public $values;
30+
31+
public function __construct($json)
32+
{
33+
parent::__construct($json);
34+
35+
$this->values = [];
36+
foreach ($json->data as $data) {
37+
$this->values[] = [
38+
"value" => new Unit($data->value, "g/m³", "", $data->precision),
39+
"pressure" => new Unit($data->pressure, "hPa"),
40+
];
41+
}
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5+
*
6+
* @license MIT
7+
*
8+
* Please see the LICENSE file distributed with this source code for further
9+
* information regarding copyright and licensing.
10+
*
11+
* Please visit the following links to read about the usage policies and the license of
12+
* OpenWeatherMap data before using this library:
13+
*
14+
* @see https://OpenWeatherMap.org/price
15+
* @see https://OpenWeatherMap.org/terms
16+
* @see https://OpenWeatherMap.org/appid
17+
*/
18+
19+
namespace Cmfcmf\OpenWeatherMap\AirPollution;
20+
21+
class SO2AirPollution extends PrecisionPressureValueAirPollution
22+
{
23+
}

0 commit comments

Comments
 (0)