Ag-Analytics - Planting Date API - Demo

In the modern agriculture industry, people often use a crop calendar, Growing Degree Days(GDD) or self-reports to predict or trace back the crop planting date for a given year. However, the information coming from the historical crop calendar or the estimated GDDs are known to be inaccurate for local regions and can be misleading, especially in a year with cooler or warmer spring. Besides, self-reports may lack consistency across different farmers and will not be available in a timely manner. Therefore, there is an urgent need for a product can provide the estimated planting date with higher accuracy and can be delivered on time.

The Ag-Analytics provides the Planting Date API service which can trace back to a given year when the crop was planted on the field. This service is developed from our near real-time ground truth agriculture data, the historical weather and soil data, and weekly high-resolution remote sensing imagery. It provides a near-real-time of estimated planting date of a given year with the spatial resolution at 8m. Users can provide a polygon of their field in specific formats and the year of interest to retrieve the estimated planting date.

Experience the usage of this service on Ag-Analytics website using below link:

Required libraries

In [1]:
import requests
import json
import time
import os
from pandas.io.json import json_normalize
from collections import defaultdict
import pandas as pd
import zipfile, io
from IPython.display import Image

%matplotlib inline
%autosave 0
Autosave disabled

Request Parameter Details

Request URL: 'https://ag-analytics.azure-api.net/plantingdate'

1). SHAPE (geometry, file/text, required): The structure of the geometry can be one of the following.

i. JSON geometry objects returned by the arcgis rest api, (file/text) 

ii. GEOJSON (file/text) 


2). CropSeason(string): The year of interests to retrive the planting date information of the AOI.

Parameter example: "2018"

3). ModelType(string): Classificaiton algorithums. This version of Crop Identification supports: Decision Tree and Neural Networks classification algorithum.

Prameter example:'TREE' or 'NN'(Optional, default as 'NN)

Request Request in Json

The Planting Date API service takes the request in Json format. Below are examples formatted in Json.

In [2]:
request ={
    "SHAPE": "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-100.953840994,38.5946753571],[-100.953832008,38.5948720599],[-100.953876941,38.5952162884],[-100.953957821,38.5955324152],[-100.953984781,38.5955745654],[-100.954029714,38.5957361407],[-100.954245394,38.5961716896],[-100.954452087,38.5964807873],[-100.95473966,38.5968179832],[-100.954910406,38.596965506],[-100.954910406,38.5969795557],[-100.954982299,38.5970287299],[-100.954982299,38.5970427797],[-100.955359739,38.5973378239],[-100.955377712,38.5973378239],[-100.955629338,38.5975134449],[-100.956042724,38.5977312143],[-100.956357257,38.5978646856],[-100.956707736,38.5979911318],[-100.957175042,38.598110553],[-100.957615388,38.5981878255],[-100.958055734,38.598229974],[-100.958514053,38.5982369988],[-100.958963386,38.5982088998],[-100.959412719,38.5981456769],[-100.959906984,38.5980332805],[-100.96023949,38.5979279087],[-100.960598957,38.5977874128],[-100.960976396,38.5976047676],[-100.961281942,38.597422122],[-100.961560528,38.5972113765],[-100.961578502,38.5972113765],[-100.961848101,38.5969865806],[-100.961982901,38.5968390579],[-100.962018848,38.5968179832],[-100.962189594,38.5966283107],[-100.962315407,38.5964526876],[-100.962405274,38.5964526876],[-100.962405274,38.5963894631],[-100.962369327,38.5963894631],[-100.962369327,38.5963754133],[-100.962513113,38.5961716896],[-100.96262994,38.5959539155],[-100.962800686,38.5955324152],[-100.962908526,38.595054712],[-100.962926499,38.5948580097],[-100.962890553,38.5948509846],[-100.962962446,38.5948650348],[-100.963025352,38.5947947839],[-100.962998393,38.5947877588],[-100.962989406,38.5947666835],[-100.962917513,38.5947526333],[-100.962935486,38.5947245329],[-100.962926499,38.5946894073],[-100.962881566,38.5947034575],[-100.962665886,38.5946823822],[-100.958541013,38.5946753571],[-100.9584152,38.5947175077],[-100.958316347,38.5947175077],[-100.958298374,38.5947034575],[-100.958154587,38.5946753571],[-100.953840994,38.5946753571]]]}}",
    "ScalarVariables": {"CropSeason": "2018"},
    "ModelType":"NN"
}

# Basic Header Pattern.
headers={'Content-Type':'application/json'}

# Header for using a subscription key.
# headers={'content-type': "application/json",'Ocp-Apim-Subscription-Key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

API Function

In [3]:
def PlantingDate(request):
    try:
        url = 'https://ag-analytics.azure-api.net/plantingdate/'
        
        response = requests.post(url, json = request, headers = headers).json()
        
        print(response)
        
        return response
    
    except Exception as e:
        print(e)
        raise e

Calling API Function and Displaying Response

In [4]:
PlantingDateresponse = PlantingDate(request)
{'feature_averages': [{'Key': 'Greenweek18', 'Value': 1138.131570659558}, {'Key': 'Redweek18', 'Value': 1903.1859603449168}, {'Key': 'SWIR2week18', 'Value': 2391.667558326072}, {'Key': 'SWIR1week18', 'Value': 3338.836322033022}, {'Key': 'NIRweek18', 'Value': 2970.0740078383446}, {'Key': 'Blueweek18', 'Value': 779.824844896861}, {'Key': 'Greenweek20', 'Value': 2254.270159289066}, {'Key': 'Redweek20', 'Value': 2480.602444221482}, {'Key': 'SWIR2week20', 'Value': 2977.71718115419}, {'Key': 'SWIR1week20', 'Value': 3808.579113377245}, {'Key': 'NIRweek20', 'Value': 3536.6660938053815}, {'Key': 'Blueweek20', 'Value': 1975.3829592151317}, {'Key': 'Greenweek22', 'Value': 1021.113268742985}, {'Key': 'Redweek22', 'Value': 1324.5397082727213}, {'Key': 'SWIR2week22', 'Value': 2392.3059762302105}, {'Key': 'SWIR1week22', 'Value': 3199.2655615880494}, {'Key': 'NIRweek22', 'Value': 2412.510195847856}, {'Key': 'Blueweek22', 'Value': 733.3521051359988}, {'Key': 'Greenweek24', 'Value': 928.849268847287}, {'Key': 'Redweek24', 'Value': 1069.7853810301349}, {'Key': 'SWIR2week24', 'Value': 1066.0881596873523}, {'Key': 'SWIR1week24', 'Value': 1902.9297659572896}, {'Key': 'NIRweek24', 'Value': 3292.298120071036}, {'Key': 'Blueweek24', 'Value': 643.9180616872748}, {'Key': 'Greenweek26', 'Value': 571.9230231878212}, {'Key': 'Redweek26', 'Value': 512.0971963633662}, {'Key': 'SWIR2week26', 'Value': 577.5711350554554}, {'Key': 'SWIR1week26', 'Value': 1254.004163243775}, {'Key': 'NIRweek26', 'Value': 3535.8988991057713}, {'Key': 'Blueweek26', 'Value': 344.2115098055459}, {'Key': 'Greenweek28', 'Value': 457.9029104885397}, {'Key': 'Redweek28', 'Value': 379.21622211189487}, {'Key': 'SWIR1week28', 'Value': 681.5643129864352}, {'Key': 'SWIR2week28', 'Value': 14.881174763569534}, {'Key': 'NIRweek28', 'Value': 3721.192446778267}, {'Key': 'Blueweek28', 'Value': 236.699024285531}, {'Key': 'Greenweek30', 'Value': 384.65050915319665}, {'Key': 'Redweek30', 'Value': 283.1936926800272}, {'Key': 'SWIR2week30', 'Value': 305.1543742073506}, {'Key': 'SWIR1week30', 'Value': 962.4707162781339}, {'Key': 'NIRweek30', 'Value': 3591.1494805204393}, {'Key': 'Blueweek30', 'Value': 204.9539935971929}, {'Key': 'Greenweek32', 'Value': 375.24875445591255}, {'Key': 'Redweek32', 'Value': 279.2847222138893}, {'Key': 'SWIR2week32', 'Value': 299.5264928749243}, {'Key': 'SWIR1week32', 'Value': 994.7986702873936}, {'Key': 'NIRweek32', 'Value': 3262.5500419977257}, {'Key': 'Blueweek32', 'Value': 216.0730864324732}, {'Key': 'Greenweek34', 'Value': 540.8961853834992}, {'Key': 'Redweek34', 'Value': 467.8558782624387}, {'Key': 'SWIR2week34', 'Value': 805.5621165630723}, {'Key': 'SWIR1week34', 'Value': 1417.8394552018106}, {'Key': 'NIRweek34', 'Value': 3102.557304281368}, {'Key': 'Blueweek34', 'Value': 266.59034645931484}, {'Key': 'Greenweek36', 'Value': 749.1952922510966}, {'Key': 'Redweek36', 'Value': 727.4010197716985}, {'Key': 'SWIR2week36', 'Value': 193.83389703934495}, {'Key': 'SWIR1week36', 'Value': 628.7585803430337}, {'Key': 'NIRweek36', 'Value': 2956.2606778525223}, {'Key': 'Blueweek36', 'Value': 621.9698331028843}, {'Key': 'GDD3', 'Value': 40.4415}, {'Key': 'Precipitation3', 'Value': 11.524}, {'Key': 'GDD4', 'Value': 88.57439999999998}, {'Key': 'Precipitation4', 'Value': 25.41900000000001}, {'Key': 'GDD5', 'Value': 567.5903999999999}, {'Key': 'Precipitation5', 'Value': 70.78399999999999}, {'Key': 'GDD6', 'Value': 808.3584}, {'Key': 'Precipitation6', 'Value': 152.91100000000003}, {'Key': 'GDD7', 'Value': 216.13589999999996}, {'Key': 'Precipitation7', 'Value': 1.4809999999999999}, {'Key': 'GDD8', 'Value': 0.0}, {'Key': 'Precipitation8', 'Value': 0.0}], 'raster_filename': 'result_plantingdateraster_20191204_164311_5681.tif', 'rasterinfo': [{'attributes': {'CellSize': [0.0001, -0.0001], 'CoordinateSystem': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]', 'Extent': '-100.963025352, 38.5946369988, -100.953925352, 38.5982369988', 'Legend': [{'Area': '100.0 %', 'Count': 2645, 'CountAllPixels': 2645, 'Max': 178.87694352071787, 'Mean': 112.13662418820252, 'Min': 45.39630485568716, 'color': '#ff0000'}], 'Matrix': [36, 91], 'Max': 178.87694352071787, 'Mean': 141.2250021675916, 'Min': 45.39630485568716, 'OID': 0, 'Percentile5': 102.71190921927132, 'Percentile95': 178.87694352071787, 'Variety': 'NoVariety', 'pngb64': 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAFsAAAAkCAYAAADmQkaNAAAA80lEQVR4nO3YSQ6DMBAF0cb3vzNZJQIHQ3voz1S1RcLwaBnBZBdqNpsjzjuZTRHnre3Ui4jC9XTGA5AveCZwKRW8ZJErApeKhA/FvhNyXgR6CPadkfNGog/FfhJy3gj0IdhPRs7rQU+9i78J2qzvfpuf0tuQ81omvAn77dDLatCrtxGg19V4VGEDvZ3XxY0N9H4eHxc20L6OnA6xga5rz2sXG+i2Sm5FbKD72vLr/oIkf5vYTPWYcsc/bKDjYhsJbjm8qXSAxsdkC/oOMdjCfr8H2ULiY7KFgS0smbGFqGKyhYEtDGxhYAtLvBx1MdnCwBb2AS+0RgzKp1keAAAAAElFTkSuQmCC'}}]}
In [ ]:
#Response Example 1
PlantingDateresponse = {'feature_averages': [{'Key': 'Greenweek18', 'Value': 1138.131570659558}, {'Key': 'Redweek18', 'Value': 1903.1859603449168}, {'Key': 'SWIR2week18', 'Value': 2391.667558326072}, {'Key': 'SWIR1week18', 'Value': 3338.836322033022}, {'Key': 'NIRweek18', 'Value': 2970.0740078383446}, {'Key': 'Blueweek18', 'Value': 779.824844896861}, {'Key': 'Greenweek20', 'Value': 2254.270159289066}, {'Key': 'Redweek20', 'Value': 2480.602444221482}, {'Key': 'SWIR2week20', 'Value': 2977.71718115419}, {'Key': 'SWIR1week20', 'Value': 3808.579113377245}, {'Key': 'NIRweek20', 'Value': 3536.6660938053815}, {'Key': 'Blueweek20', 'Value': 1975.3829592151317}, {'Key': 'Greenweek22', 'Value': 1021.113268742985}, {'Key': 'Redweek22', 'Value': 1324.5397082727213}, {'Key': 'SWIR2week22', 'Value': 2392.3059762302105}, {'Key': 'SWIR1week22', 'Value': 3199.2655615880494}, {'Key': 'NIRweek22', 'Value': 2412.510195847856}, {'Key': 'Blueweek22', 'Value': 733.3521051359988}, {'Key': 'Greenweek24', 'Value': 928.849268847287}, {'Key': 'Redweek24', 'Value': 1069.7853810301349}, {'Key': 'SWIR2week24', 'Value': 1066.0881596873523}, {'Key': 'SWIR1week24', 'Value': 1902.9297659572896}, {'Key': 'NIRweek24', 'Value': 3292.298120071036}, {'Key': 'Blueweek24', 'Value': 643.9180616872748}, {'Key': 'Greenweek26', 'Value': 571.9230231878212}, {'Key': 'Redweek26', 'Value': 512.0971963633662}, {'Key': 'SWIR2week26', 'Value': 577.5711350554554}, {'Key': 'SWIR1week26', 'Value': 1254.004163243775}, {'Key': 'NIRweek26', 'Value': 3535.8988991057713}, {'Key': 'Blueweek26', 'Value': 344.2115098055459}, {'Key': 'Greenweek28', 'Value': 457.9029104885397}, {'Key': 'Redweek28', 'Value': 379.21622211189487}, {'Key': 'SWIR1week28', 'Value': 681.5643129864352}, {'Key': 'SWIR2week28', 'Value': 14.881174763569534}, {'Key': 'NIRweek28', 'Value': 3721.192446778267}, {'Key': 'Blueweek28', 'Value': 236.699024285531}, {'Key': 'Greenweek30', 'Value': 384.65050915319665}, {'Key': 'Redweek30', 'Value': 283.1936926800272}, {'Key': 'SWIR2week30', 'Value': 305.1543742073506}, {'Key': 'SWIR1week30', 'Value': 962.4707162781339}, {'Key': 'NIRweek30', 'Value': 3591.1494805204393}, {'Key': 'Blueweek30', 'Value': 204.9539935971929}, {'Key': 'Greenweek32', 'Value': 375.24875445591255}, {'Key': 'Redweek32', 'Value': 279.2847222138893}, {'Key': 'SWIR2week32', 'Value': 299.5264928749243}, {'Key': 'SWIR1week32', 'Value': 994.7986702873936}, {'Key': 'NIRweek32', 'Value': 3262.5500419977257}, {'Key': 'Blueweek32', 'Value': 216.0730864324732}, {'Key': 'Greenweek34', 'Value': 540.8961853834992}, {'Key': 'Redweek34', 'Value': 467.8558782624387}, {'Key': 'SWIR2week34', 'Value': 805.5621165630723}, {'Key': 'SWIR1week34', 'Value': 1417.8394552018106}, {'Key': 'NIRweek34', 'Value': 3102.557304281368}, {'Key': 'Blueweek34', 'Value': 266.59034645931484}, {'Key': 'Greenweek36', 'Value': 749.1952922510966}, {'Key': 'Redweek36', 'Value': 727.4010197716985}, {'Key': 'SWIR2week36', 'Value': 193.83389703934495}, {'Key': 'SWIR1week36', 'Value': 628.7585803430337}, {'Key': 'NIRweek36', 'Value': 2956.2606778525223}, {'Key': 'Blueweek36', 'Value': 621.9698331028843}, {'Key': 'GDD3', 'Value': 40.4415}, {'Key': 'Precipitation3', 'Value': 11.524}, {'Key': 'GDD4', 'Value': 88.57439999999998}, {'Key': 'Precipitation4', 'Value': 25.41900000000001}, {'Key': 'GDD5', 'Value': 567.5903999999999}, {'Key': 'Precipitation5', 'Value': 70.78399999999999}, {'Key': 'GDD6', 'Value': 808.3584}, {'Key': 'Precipitation6', 'Value': 152.91100000000003}, {'Key': 'GDD7', 'Value': 216.13589999999996}, {'Key': 'Precipitation7', 'Value': 1.4809999999999999}, {'Key': 'GDD8', 'Value': 0.0}, {'Key': 'Precipitation8', 'Value': 0.0}], 'raster_filename': 'result_plantingdateraster_20191204_164311_5681.tif', 'rasterinfo': [{'attributes': {'CellSize': [0.0001, -0.0001], 'CoordinateSystem': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]', 'Extent': '-100.963025352, 38.5946369988, -100.953925352, 38.5982369988', 'Legend': [{'Area': '100.0 %', 'Count': 2645, 'CountAllPixels': 2645, 'Max': 178.87694352071787, 'Mean': 112.13662418820252, 'Min': 45.39630485568716, 'color': '#ff0000'}], 'Matrix': [36, 91], 'Max': 178.87694352071787, 'Mean': 141.2250021675916, 'Min': 45.39630485568716, 'OID': 0, 'Percentile5': 102.71190921927132, 'Percentile95': 178.87694352071787, 'Variety': 'NoVariety', 'pngb64': 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAFsAAAAkCAYAAADmQkaNAAAA80lEQVR4nO3YSQ6DMBAF0cb3vzNZJQIHQ3voz1S1RcLwaBnBZBdqNpsjzjuZTRHnre3Ui4jC9XTGA5AveCZwKRW8ZJErApeKhA/FvhNyXgR6CPadkfNGog/FfhJy3gj0IdhPRs7rQU+9i78J2qzvfpuf0tuQ81omvAn77dDLatCrtxGg19V4VGEDvZ3XxY0N9H4eHxc20L6OnA6xga5rz2sXG+i2Sm5FbKD72vLr/oIkf5vYTPWYcsc/bKDjYhsJbjm8qXSAxsdkC/oOMdjCfr8H2ULiY7KFgS0smbGFqGKyhYEtDGxhYAtLvBx1MdnCwBb2AS+0RgzKp1keAAAAAElFTkSuQmCC'}}]}

Displaying Output

In [7]:
df=defaultdict(list)
for value in PlantingDateresponse['rasterinfo'][0]['attributes']['Legend']:
    df['PlantingDate(Mean)'].append(round(value['Mean']))
    df['AreaPercent'].append(value['Area'])
    df['Color'].append(value['color'])
plantingdf=pd.DataFrame.from_dict(df).style.applymap(lambda x:"background-color: %s"%x, subset=['Color'])
plantingdf
Out[7]:
PlantingDate(Mean) AreaPercent Color
0 112 100.0 % #ff0000
In [6]:
#Image
PlantingImage = PlantingDateresponse["rasterinfo"][0]['attributes']["pngb64"]

Image(url = PlantingImage, width= 400, height =200)
Out[6]:

GET Request

GET Request for downloading the POST requested file to local machine.

In [ ]:
# Specify local path where file will be downloaded.
# local_path = r"C:\<Path to download directory>" # E.g., r"C:\Users\John_Doe\Documents\rasters"
local_path = r"C:\Users\Alex\Documents" # E.g., r"C:\Users\John_Doe\Documents\rasters"

# Create GET request payload.
values = {'filename': PlantingDateresponse['raster_filename']}
In [ ]:
def cropID_get(values, local_path):
    try:
        url = 'https://ag-analytics.azure-api.net/plantingdate/'
    
        download_path = os.path.join(local_path, values['filename'])
     
        response = requests.get(url, params=values)
        open(download_path, 'wb').write(response.content)
        
        print(response.url)

        return response
    
    except Exception as e:
        print(e)
        raise e
In [ ]:
cropID_get(values, local_path)