Climatic variables spatial plotting

Here we are going through step by step on how to spatially place the responses from API requests on a plot using Python

Open you preferable Python IDE or Python console and initially import the required libraries. This example is quite more complex than the previous example Simple climatic variables plotting. Beside the standard libraries also imported in the previous example, we are going to need a couple of geospatial related libraries to better handle coordinates.

import requests
import json
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
import shapely.geometry

After importing the libraries, we define all the needed variables to proceed. Email, password, climatic_variable_1 and coordinates are the essential variables needed to get your responses from the API. The input coordinates of the API are single points, i.e. to time a call is made on the API it refers to specific location requested. Thus to have a collection of spatially distributed points, multiple requests are needed.

It's a good idea to bring all variables that you are going to use up, in order to avoid "hard coding" values later in the code

# Define the user credentials
email = 'YOUR ACCOUNT EMAIL'
password = 'YOUR PASSWORD'
# Define the climatic variable to plot
climatic_variable_1 = 'a-frost-days'  # Average Frost Days
# Define the coordinates of the polygon
lat_point_list = [50.854457, 52.518172, 50.072651, 48.853033, 50.854457]
lon_point_list = [4.377184, 13.407759, 14.435935, 2.349553, 4.377184]
# Create a geopandas structure with the coordinates
polygon_geom = shapely.geometry.Polygon(zip(lon_point_list, lat_point_list))
gdf = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry=[polygon_geom])
# total area for the grid
xmin, ymin, xmax, ymax= gdf.total_bounds
# Define the grid
n_cells=5
cell_size = (xmax-xmin)/n_cells
# Projection of the grid
crs = "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"

So far, the polygon, bounding box and projection are defined. Following a grid is defined within this polygon. This grid contains the point coordinates to be used in the API calls

Following the process, the user needs to generate the temporally authkey to interact with the rest endpoints.

A for loop is used to loop over all points, make the calls to the API and store the responses into a list.

Finally, we can plot the points to spatially understand the variation of the climate statistics.

In case you want to run the entire process with a single command, bellow you can find the entire code.

Last updated