Samstag, 30. April 2016

OpenWeatherMap integration

1) Introduction

Some of you may always wanted to show up the current weather at a specific location or connect weather information with your app data.

For example: In case you've got a skiing app, you only want to recommend slopes where the wether and temperature is suitable.



2) The Effort

We want to build an app where we can request weather information by city, longitude/latitude, zipcode and country. We also want to show up a 5 day forecast for a specific city. And as a small bonus, we want to list a number of weather stations nearby a location.
With the first release of our app we want it for free, because we can't estimate the number of users.
The guys from OpenWeatherMap provide some of their data for free under certain conditions.
But for our demo app it supports all features we'll need.

http://openweathermap.org/(*)


The free version got the following restrictions:
  • less equal 60 requests per minute
  • current weather information
  • 5 days / every 3 hour forecast
  • data updates in less then 2 hours
  • API version support: only current version
  • NO 16 days / daily forecast
  • NO SSL
  • NO UV-Index data
  • NO bulk download
When our app works and more and more users join, we could switch to a payment model, to handle an increasing access rate.

3) The Integration

3.1.) Get your account

At first we need to sign up for an account, because we'll need an individual API-Key.
At https://home.openweathermap.org/users/sign_up (*) we can easily register.

Afterwards you'll find the API-Key in your profile information.

3.2.) Download OpenWeatherMapClient component

In the next step you're welcome to download my OpenWeatherMapClient component.
This component is platform independent (Windows 32/64, Android, iOS, MacOS).
It was built with Delphi 10.1 Berlin (should also run with older versions, where anonymous procedures and generics are supported). 
The component works with Firemonkey (FMX) and VCL applications. It uses a REST client for gathering information.
The component was designed to use the free version of OpenWeatherMap API, so only allowed methods are implemented.

DownloadOpenWeatherMap.zip

3.3) Usage of OpenWeatherMap component

Inside the zip-file you can find the API client component files and our firemonkey demo app.
  • OpenWeatherMap.pas: TOpenWeatherMap component
  • OpenWeatherMapClient.pas: Encapsulated TRESTClient with easy API access methods
  • OpenWeatherMapInterfaces.pas: interfaces representing the API structure
  • OpenWeatherMapTypes.pas: Interface-Implementations
  • example/fmx/OpenWeatherMapApp.dproj: demo app
  • example/images/...: external weather images for displaying current weather (seperate licence)

Example: 
procedure TForm1.FormCreate(Sender: TObject);
var lWeather : ICurrentWeather;
begin
  fOpenWeatherMap := TOpenWeatherMap.Create(Self);
  fOpenWeatherMap.client.api_key := '<YOUR-API-KEY-HERE>';
  lWeather := fOpenWeatherMap.client.getCurrentWeather('London', '', '', 'metric', 'en');
end;

The component uses interfaces, so if you're not firm with those, please take a closer look at: http://docwiki.embarcadero.com/RADStudio/Seattle/de/Objekt-Interfaces (*)

The API client was encapsulated inside a TOpenWeatherMap-Component, because we'd like to use the IOpenWeatherMapClient inside an external module.

Every IWeatherObject works as a JSON Data wrapper/helper instance. So we're accessing the JSON data directly. This is quite performant when receiving the complex JSON structure. We do not interprete, instead we're only unmarshaling at first.
The data is interpreted on demand, when using a specific property (also sub-interfaces!).

A disadvantage is: when accessing the same simple property (integer, float, string or boolean) multiple times, the JSON data is requested every time.

But in most cases you're reading/writing property values only once.



4) Conclusion

As always I'd like to thank you for reading my blog. Let me know if there are some bugs, mistakes or problems with this article or component.



(*) These links are being provided as a convenience and for informational purposes only; they do not constitute an endorsement or an approval by this blog of any of the products, services or opinions of the corporation or organization or individual. This blog bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links. Contact the external site for answers to questions regarding its content.

4 Kommentare:

  1. Hi, is there an easy way to expose the Description of IWeatherInfo as well? I see that the interface is already defined, but it could not be loaded in IOpenWeatherMapClient.
    I guess it must be added to OpenWeatherMapClient.pas, but do not know how...
    Thanks,
    Stefan

    AntwortenLöschen
  2. Hi, I'm very sorry for the late response, but blogspot did not noticed this comment to me and this blog is no longer maintained.

    When checking the download folder, I found a newer build incl. station transmission:
    http://fabooking.net/fcknbstrd/dev/OpenWeatherMap/OpenWeatherMap_1.0.1.zip

    Regarding your question:
    + ICurrentWeather and I5DayForecastWeather are inherited from ICustomWeather
    + ICustomWeather has a "weather" property of type TWeatherObjectList
    + this is a TList, in which element needs to be casted to IWeatherInfo
    + then you should have the info you like

    Notice: This library is from 2016. I'm not sure if it still works.

    AntwortenLöschen
  3. Created a repository for it @ https://gitlab.com/diggets/openweathermap

    AntwortenLöschen
  4. Thanks and I have a nifty proposal: How To House Renovation home kitchen remodel

    AntwortenLöschen