This blog post shows a very simple FME process which connects to OpenWeatherMap to get the weather for a city and writes it to an Esri shapefile.
It’s a very simple script but highlights connecting to a web api, getting a JSON file, extracting some of it’s contents and then writing to a simple output format.
OpenWeatherMap is a great service, it offers both free and paid subscriptions and is a useful resource when you require programmatically available weather information for your systems, for example you need to forecast the demand on your electric utility or some other requirement.
So, how does the script work.
Well, you need a Creator, because that is simply how FME works to kick things off.
The HTTPCaller is the transformer that we are going to use in order to access the OpenWeatherMap API.
We insert the API call Request URL https://api.openweathermap.org/data/2.5/weather and then you can insert the parameters. Of course it is possible to hardcode the parameter values, but it’s always better to make them public (if you want people to enter values) or private and then you only have one place to update when you need to change values.
For this example, we added three public parameters
q – This is the parameter that takes the city name, OpenWeatherMap supports other location modes, but the city is an easy example.
APPID – this is the actual coded authentication key that OpenWeatherMap will give you when you register for their system.
DestDataset_ESRISHAPE – this is the folder location for the Shapefile. Most of you will probably connect the script to a database system, or ArcGIS Online, but for simplicity we used the shapefile.
In the HTTPCaller then, you set the Query String Parameters ‘q’ and ‘APPID’ which are required for the API call and set the value to the Published Parameters that you specify.
The _response_body will contain the JSON document with all of the current weather information for the city specified.
Running the system with Toronto as the city, the OpenWeatherMap system gives us this JSON output
Clean and easy to read, but of course we need a way to get the data out – JSONExtractor to the rescue.
Connecting the JSONExtractor to the HTTPCaller will give you access to the _response_body which of course will contain the data of interest. Looking through the JSON file, there is a lot of data we can extract. As a sample, we’ll pull out the Latitude and Longitude values as well as the Temperature in the City.
To do this, we create the LAT, LON and TEMP attributes and leverage JSON queries to pull the data out.
In order to write the Shapefile we need to create the vertex in order to create the point geometry for the Shapefile. The VertexCreator serves our needs beautifully.
To test our work we used FME’s Data Inspector on the Shapefile. To take advantage of Data Inspector and leveraging Esri’s ArcGIS Online base maps we threw in a reprojector to take the vertex from Lat/Long WGS84 to the standard Web Mercator EPSG 3857 projection.
Finally we connect the Reprojector to the Shapefile writer
We used the default values of the ShapefileWriter and changed the Attribute Definition to Manual and added the Temp attribute. This gives us the slot to put in the temp value.
After running the script we can see the output
We have a shapefile we can work with and the current temperature. The temp of course is in Kelvin, but you can simply subtract 273.15 to convert to Celsius. Basically, on this April 18th it’s only 1.49 degrees celcius.
Enjoy FME and stay warm.
P.S. Of course you can then publish this to FME Server or FME Cloud and call it as a web service. You’ll probably want to adjust the output, maybe write to ArcGIS Online, or simply return a set of values. We’ll cover that in a future post.