cmfcmf/OpenWeatherMap-PHP-API

Weather location coordinates are different in response from requested ones

valjar opened this issue · 1 comments

Hi there!!

First of all, thank you for this API!
I got two questions regarding the requesting weather.

First one is:
I'm requesting the weather by location coordinates and the input is the center of Ljubljana / Slovenia
So this is my code:

private function getWeatherData($selectedLanguage, $latitude, $longitude) {
        if ($selectedLanguage != 'en' || $selectedLanguage != 'sl') {
            $selectedLanguage = 'en';
        }
        $units = 'metric';
        $owm = new OpenWeatherMap($this->config->config['open_weather_api_key']);
        try {
            $weather = $owm->getWeatherForecast(['lat' => '46.0524865', 'lon' => '14.5019355'], $units, 'en', '', '5');
            return $weather;
    }

And this is the response
screen shot 2018-07-18 at 15 40 19

Here the problem is not so obvious, because you get the weather from somewhere north of Ljubljana, but If i request weather from our Doljenska region, I will get Croatian weather.

My question is why the response coordinates are different, then the ones I requested.
Somehow they get runded. The problem is that Slovenia is really small and rounding coordinates just a little bit means requesting weather data from different country :D

And the second question is how do I get 5 days forecast? As you can see im my response there is no 5 days forecast..

Thank you for all the answers! ;)

Hi @valjar
, sorry for getting back to you so late.

My question is why the response coordinates are different, then the ones I requested.
Somehow they get runded. The problem is that Slovenia is really small and rounding coordinates just a little bit means requesting weather data from different country :D

I can only assume that OpenWeatherMap has no better / nearer data for the location you provieded. Your example request with the coordinates of Ljubljana seems to be pretty close though, when I type in both your coordinates and the coordinates returned by the API, it's just a four minute walk.
grafik

I can't really provide further insights into how OpenWeatherMap handles coordinates internally :/ For reference, this is the URL this library calls when executing the request you posted above: http://api.openweathermap.org/data/2.5/forecast?lat=46.0524865&lon=14.5019355&units=metric&lang=en&mode=xml&APPID=XXXXXXXXXXXXXXX (as expected)

And the second question is how do I get 5 days forecast? As you can see im my response there is no 5 days forecast..

I modified the Examples/WeatherForecast.php file locally to test this:

use Cmfcmf\OpenWeatherMap;

require_once __DIR__ . '/bootstrap.php';

// Language of data (try your own language here!):
$lang = 'de';

// Units (can be 'metric' or 'imperial' [default]):
$units = 'metric';

// Get OpenWeatherMap object. Don't use caching (take a look into Example_Cache.php to see how it works).
$owm = new OpenWeatherMap($myApiKey);

// Example 1: Get forecast for the next 5 days for Berlin.
$forecast = $owm->getWeatherForecast(['lat' => '46.0524865', 'lon' => '14.5019355'], $units, 'en', '', '5');
echo "EXAMPLE 1<hr />\n\n\n";

echo "City: " . $forecast->city->name;
echo "<br />\n";
echo "LastUpdate: " . $forecast->lastUpdate->format('d.m.Y H:i');
echo "<br />\n";
echo "Sunrise : " . $forecast->sun->rise->format("H:i:s (e)") . " Sunset : " . $forecast->sun->set->format("H:i:s (e)");
echo "<br />\n";
echo "<br />\n";

foreach ($forecast as $weather) {
    // Each $weather contains a Cmfcmf\ForecastWeather object which is almost the same as the Cmfcmf\Weather object.
    // Take a look into 'Examples_Current.php' to see the available options.
    echo "Weather forecast at " . $weather->time->day->format('d.m.Y') . " from " . $weather->time->from->format('H:i') . " to " . $weather->time->to->format('H:i');
    echo "<br />\n";
    echo $weather->temperature;
    echo "<br />\n";
    echo "Sun rise: " . $weather->sun->rise->format('d.m.Y H:i (e)');
    echo "<br />\n";
    echo "---";
    echo "<br />\n";
}
die;

This code gives me a five-day forecast as expected.

Let me know if there is anything else I can do.