Add location and date time filters to ODATA weather data requests

The Visual Crossing Weather API supports requesting data using OData requests. This makes it easy to integrate weather data into tools that support the ODATA specification. In this article we discuss how to extend the basic ODATA requests to create dynamic queries using ODATA filters.

The basic ODATA request

As the introductory article discusses, the  Weather Data Services page can be used to construct a simple weather data request. These weather data requests can retrieve weather forecast or historical weather data.

Here is a sample request:

This request is completely self contained. It includes all the parameters that were used to construct the query including the type of requests (forecast or historical data), the locations and the time frame for the data.

But what happens if you would like to make the requests more dynamic so that you can change the location or dates without recreating the request?

Using ODATA Filters to dynamically specify locations and dates

ODATA supports the concept of a filter parameter. This parameter can be used by Visual Crossing Weather to indicate the locations and dates required. Here’s a simple example:

$filter=Id eq 'London,UK'

When this is added to the end of an existing ODATA request, the locations that were embedded in the request are replaced by the filter location. This will apply to both forecast and history queries.

It is possible to add multiple locations using an ‘or’ operator:

$filter=Id eq 'London,UK' or Id eq 'New York City, NY, USA' or Id eq 'Paris, France'

Now the weather data will be loaded for London, New York City and Paris.

Historical weather data requests also support start and end data filters. You can add dynamic filters using the following filter:

$filter=Date_time gt datetime'2020-09-20T00:00:00' and Date_time lt datetime'2020-10-01T00:00:00'

This will return data between 20th September 2020 and 1st October 2020. This will only apply to historical data requests.

The above filters can also be combined to provide dynamic filter for both locations and dates:

$filter=Id eq 'London,UK' or Id eq 'New York City, NY, USA' or Id eq 'Paris, France' and Date_time gt datetime'2020-09-20T00:00:00' and Date_time lt datetime'2020-10-01T00:00:00'

Note that in all the above cases, we have not URL encoded the filter criteria for clarity. We recommend URL encoding the parameters for correct transmission.