Ag-Analytics® - Cropland Data Layers API

The Cropland Data Layer (CDL), produced by the USDA, provides a raster, geo-referenced, crop-specific land cover map for the continental United States. The CDL also includes a crop mask layer and planting frequency layers, as well as boundary, water and road layers. The Boundary Layer options provided are County, Agricultural Statistics Districts (ASD), State, and Region. The data is created annually using moderate resolution satellite imagery and extensive agricultural ground truth.

CDL Service provides an API for requesting CDL layer as a GeoTIFF of the area of interest(AOI) passed by the user and the summary of the majoy crops planted on the area of interest. To request the CDL layer, the user will pass the AOI as one of the following format: GeoJSON, Shapefile(.SHP, .shp), or GeoTIFF(.TIFF, tif) and the calender year of the CDL layer as parameters in the POST request. The response of POST request includes the crop summary of the AOI and a file name of the GeoTIFF saved on the Ag-Analytics server. Then, user can send a GET request that includes the file name from POST request to download the GeoTIFF of the CDL layer.

Post Request Parameters

1). aoi (GeoJSON String): Area of interest as Geojson string containing boundary coordinates. file(GeoTiff or Shapfile in Zip): Alternative AOI format when GeoJSON is not available. A GeoTiff(.TIFF,.Tif, .etc) or a Shapefile in Zip (must include shp, shx, dbf, but others files such as .prj, .xml, .cpg are recommended for better processing)

2). years(List of integers as String):Call will return CDL or Cultivated Layer GeoTiffs and response from the given years. ex: "['2015','2016']"

3).product(List of integers as String):Get 'CultivatedLayer','CDL', or both products in the response. ex:"['CultivatedLayer','CDL']"

4).Projection(String):Output projection of result GeoTIFF.Provided as EPSG code, ex: "EPSG:4326"

5).Resolution(Float):Output resolution of result GeoTIFF.

Request Spatial Reference Handling

The CDL Service handles requests differently whether they contain a file as an aoi (shapefile/GeoTIFF) or the aoi as a GeoJSON. This is accomplished with a try-catch where the service attempts to extract a file from the request first, and if this is not possible, it falls through to accepting a fully JSON request with GeoJSON as the aoi. Please see the Request Handling Table in CDL API documentation for detailed description.

Library

In [50]:
import requests
import json
import os
from collections import defaultdict
import pandas as pd
from IPython.display import Image

%matplotlib inline
%autosave 0
Autosave disabled

POST Request API Fuction

In [41]:
def cdl_service(values, files, headers):
    try:
        url = 'https://ag-analytics.azure-api.net/cdl-range-read'
        
        if files == None:
            response = requests.post(url, data=values,headers = headers).json()
        else:
            response = requests.post(url, files = files, data = values,headers = headers).json()
            
        print(response)
        
        return response
    
    except Exception as e:
        print(e)
        raise e

Headers and Subscription key

In [42]:
# Basic Header Pattern.
# headers=None
# Header for using a subscription key.
headers={'Content-Type':'application/x-www-form-urlencoded','Ocp-Apim-Subscription-Key': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

Raster (.tif) as AOI for CDL

In [44]:
#Path to raster in your local machine
rasterpath = r"raster path on your local machine"
gtiffs_files = [('file',(open(rasterpath, 'rb')))]
#Parameters to call cdl API
request_with_tif = {
    "years":"['2015']",
    "Resolution":"0.0001",
    "Projection":"EPSG:4326"
}
In [45]:
cdl_response_raster = cdl_service(request_with_tif, gtiffs_files, headers)
{'2015': {'attributes': {'CellSize': [9.999999999999224e-05, -9.999999999996996e-05], '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': '-95.89404159769204, 46.32639173298982, -95.87354159769204, 46.340391732989815', 'Legend': [{'Acres': 166.78235027740627, 'Area': '27.47 %', 'Count': 7885, 'CountAllPixels': 28700, 'CropCode': 1, 'CropName': 'Corn', 'color': '#6af121'}, {'Acres': 225.56334601880283, 'Area': '37.16 %', 'Count': 10664, 'CountAllPixels': 28700, 'CropCode': 5, 'CropName': 'Soybeans', 'color': '#73fbcd'}, {'Acres': 0.06345555495652742, 'Area': '0.01 %', 'Count': 3, 'CountAllPixels': 28700, 'CropCode': 23, 'CropName': 'SpringWheat', 'color': '#d369b7'}, {'Acres': 6.81089623200061, 'Area': '1.12 %', 'Count': 322, 'CountAllPixels': 28700, 'CropCode': 36, 'CropName': 'Alfalfa', 'color': '#440111'}, {'Acres': 1.6498444288697132, 'Area': '0.27 %', 'Count': 78, 'CountAllPixels': 28700, 'CropCode': 37, 'CropName': 'OtherHay/NonAlfalfa', 'color': '#4c6a6e'}, {'Acres': 2.3055518300871634, 'Area': '0.38 %', 'Count': 109, 'CountAllPixels': 28700, 'CropCode': 61, 'CropName': 'Fallow/IdleCropland', 'color': '#025d4a'}, {'Acres': 31.664321923307185, 'Area': '5.22 %', 'Count': 1497, 'CountAllPixels': 28700, 'CropCode': 111, 'CropName': 'OpenWater', 'color': '#7956c0'}, {'Acres': 27.582014554437254, 'Area': '4.54 %', 'Count': 1304, 'CountAllPixels': 28700, 'CropCode': 121, 'CropName': 'Developed/OpenSpace', 'color': '#fe061d'}, {'Acres': 0.12691110991305485, 'Area': '0.02 %', 'Count': 6, 'CountAllPixels': 28700, 'CropCode': 123, 'CropName': 'Developed/MedIntensity', 'color': '#c84602'}, {'Acres': 80.27127702000719, 'Area': '13.22 %', 'Count': 3795, 'CountAllPixels': 28700, 'CropCode': 141, 'CropName': 'DeciduousForest', 'color': '#c74184'}, {'Acres': 3.1516258961741954, 'Area': '0.52 %', 'Count': 149, 'CountAllPixels': 28700, 'CropCode': 142, 'CropName': 'EvergreenForest', 'color': '#77ba69'}, {'Acres': 8.397285105913797, 'Area': '1.38 %', 'Count': 397, 'CountAllPixels': 28700, 'CropCode': 152, 'CropName': 'Shrubland', 'color': '#472192'}, {'Acres': 36.21197002852499, 'Area': '5.97 %', 'Count': 1712, 'CountAllPixels': 28700, 'CropCode': 176, 'CropName': 'Grassland/Pasture', 'color': '#7b0e2c'}, {'Acres': 16.477292437044955, 'Area': '2.71 %', 'Count': 779, 'CountAllPixels': 28700, 'CropCode': 195, 'CropName': 'HerbaceousWetlands', 'color': '#f1b525'}], 'pngb64': 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAM0AAACMCAYAAADfuC2gAAAJcklEQVR4nO3dT2gVRxzA8Y08bWyqIiG9iKUgeAgFhQRyMAURCa2IjRCkUkWoVaHYg/gHr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l5mvZRrJclqifYD5p/XB98VL7g8ev915/3k0ly+olxZvWFXcoQWhMrlbE8NXXJeR08DKBE0gBLpWY7qKe1Kw14SbObJcNHruua/FnS/ntm/3CcXrI8PB9buD7qfzZeSSfQ0gBJBAygRNIASY5oqV2vrnhnT7B3muY2O90x47id3q1hpVWiETulrdvgLQU8DKBE0gFLhnpiqW/XsRgWbgnpnbwz2nG+NBHmuf7W1ObJjo9q80dMASgQNoETQAEqFrkXL4hczFWxIo0m79Kycgq726WdbqZsb2+vItQ8+LXqd3GA5D/Q0gBJBAygV5A5ntbb0bCg7FZLVx0uXtwTd4/HD6UzbJNtgt08+DJV3qpE1mTJmvVOdnd5t2ZtMQV8dPJ/pZ/nQ0wBKBA2g1LR/9s5kpRuhEbo8z8SaPbm1IfThMt8MmSwiPLxjl3HOl5LV2oyZ1Nya/F63W3fHx/2eb/b7Hyx2nnOlZKEPk6VFTwMoETSAEkEDKFVllbNvqtI1VpHrY9mv8xzfRJE5djHWzgp8+KnWppXTkrvurZDro427n4XsFItzyHFQJdHTAEoEDaBk9IsyVUsr7xRPpj+GW7l+rME3lWykWm3ue2yaHM+wRbVH7vB2osdz4VRy2NlqprHy39r5d5EDehpAiaABlAoynTr34bHgN+77eiCP9rxUaJec9YyZ/Gba9w12FssFyYoDNqpN2EWz7ePF///t1JclnIAKI2gAJYIGUCrIytPovTcq1xLBV8krqwXk+KYrMisCXGtspdUvHoDyjaWGu8MqoItvOoEs2P8/o0Olj2nkOImeBlAiaAClwr1t6+IXGzwXfv/k1/xbE8CVuvk2MbUdupIs/WPv1uUiu/wj3XPO62S1QKPvhJY3V6Fr2uqA0CoNehpAiaABlJrmbd8QtEZApSoAQtlrB/y0Nlma6t/ZE8Y5ObM2MPOH857HmpfEx1kv4RSKioBE1ktCpUVPAyj9B5bgxB5ge1BVAAAAAElFTkSuQmCC'}, 'filename': 'result_raster_cdl_2015_epsg_4326_resolution_0.0001_89977.tif'}, 'status': 'SUCCEED'}

GeoJSON as AOI for CDL

In [59]:
#Parameters to call cdl API
aoi = "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-95.9148931503296,46.322452681104124],[-95.91476440429689,46.31674720419029],[-95.91442108154298,46.31682130510615],[-95.91399192810059,46.31673238399508],[-95.91360569000246,46.31648044006236],[-95.91345548629762,46.31630259658792],[-95.91388463974,46.3157838831528],[-95.91392755508424,46.31532444714701],[-95.91435670852661,46.314924290063686],[-95.91478586196901,46.31456859242209],[-95.91474294662476,46.31356076988237],[-95.91439962387085,46.313545948824185],[-95.91382026672365,46.31390165311263],[-95.91313362121583,46.314227713346064],[-95.91306924819948,46.314538950847584],[-95.91345548629762,46.31474644153189],[-95.9135413169861,46.31517624116103],[-95.91176033020021,46.315131779286965],[-95.91109514236452,46.31465751704923],[-95.91105222702028,46.314227713346064],[-95.91070890426637,46.3137682642754],[-95.9101939201355,46.31356076988237],[-95.90980768203735,46.31317542106524],[-95.90974330902101,46.312819712056545],[-95.90978622436523,46.31253810745169],[-95.91015100479126,46.31222685857106],[-95.90978622436523,46.31221203715164],[-95.90882062911987,46.3126270353782],[-95.90843439102173,46.312849354562246],[-95.90768337249756,46.312849354562246],[-95.90699672698975,46.312730784443126],[-95.90633153915405,46.31256775010992],[-95.90598821640015,46.31236025116524],[-95.90577363967897,46.311470961062994],[-95.90521574020387,46.31133756630139],[-95.9045934677124,46.311722928059],[-95.90403556823732,46.311722928059],[-95.90332746505739,46.31170810650312],[-95.90231895446779,46.311574712319505],[-95.90182542800903,46.3116191770835],[-95.90128898620607,46.31193042942004],[-95.90113878250122,46.31221203715164],[-95.90113878250122,46.31238989391975],[-95.90146064758301,46.31261221406715],[-95.9018898010254,46.31253810745169],[-95.9025549888611,46.31249364343426],[-95.90337038040163,46.31224167998646],[-95.90403556823732,46.31249364343426],[-95.90397119522096,46.312893818290675],[-95.9036707878113,46.31305685165256],[-95.9031128883362,46.313130957565605],[-95.90270519256593,46.31336809581297],[-95.90229749679567,46.31378308527336],[-95.90195417404176,46.31399057882352],[-95.90126752853395,46.31511695865424],[-95.90075254440308,46.31529480598191],[-95.90075254440308,46.31553193485307],[-95.90090274810792,46.315754242236565],[-95.90094566345216,46.31594690790508],[-95.90068817138673,46.31622849496964],[-95.90025901794435,46.31653972109205],[-95.90002298355103,46.316850945444386],[-95.90002298355103,46.31722144831783],[-95.90023756027223,46.31762158860345],[-95.89991569519044,46.31811064497894],[-95.89944362640382,46.31885163115864],[-95.89989423751832,46.3191035641739],[-95.9040141105652,46.319073925055825],[-95.9043574333191,46.31870343472544],[-95.9043574333191,46.31843668013423],[-95.90418577194215,46.31827366279941],[-95.90369224548341,46.318258843017624],[-95.90296268463136,46.318081005323016],[-95.90251207351685,46.317932806802666],[-95.90225458145143,46.317799427791165],[-95.90317726135254,46.315872805805164],[-95.90356349945068,46.31581352405298],[-95.90362787246706,46.31603583029254],[-95.90414285659791,46.31622849496964],[-95.90470075607301,46.316258135628985],[-95.9052586555481,46.316287776272304],[-95.90555906295778,46.31928139854511],[-95.90843439102173,46.31932585704758],[-95.90858459472658,46.322482318391906],[-95.9148931503296,46.322452681104124]]]}}"
request_with_GeoJSON = {"years": "['2015']",
                        "Resolution":"0.0001",
                        "Projection":"EPSG:4326",
                         "aoi":aoi,}
In [60]:
cdl_response_geojson = cdl_service(request_with_GeoJSON, None, headers)
{'2015': {'attributes': {'CellSize': [9.999999999999763e-05, -0.000100000000000055], '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': '-95.914846577811, 46.31137964386316, -95.899446577811, 46.32247964386317', 'Legend': [{'Acres': 5.268237736283184, 'Area': '2.55 %', 'Count': 249, 'CountAllPixels': 9783, 'CropCode': 1, 'CropName': 'Corn', 'color': '#79c024'}, {'Acres': 167.9700377042257, 'Area': '81.15 %', 'Count': 7939, 'CountAllPixels': 9783, 'CropCode': 5, 'CropName': 'Soybeans', 'color': '#f01e16'}, {'Acres': 4.252673835312931, 'Area': '2.05 %', 'Count': 201, 'CountAllPixels': 9783, 'CropCode': 36, 'CropName': 'Alfalfa', 'color': '#52ca3e'}, {'Acres': 0.3808364628638446, 'Area': '0.18 %', 'Count': 18, 'CountAllPixels': 9783, 'CropCode': 37, 'CropName': 'OtherHay/NonAlfalfa', 'color': '#23eab1'}, {'Acres': 0.12694548762128155, 'Area': '0.06 %', 'Count': 6, 'CountAllPixels': 9783, 'CropCode': 111, 'CropName': 'OpenWater', 'color': '#c4b536'}, {'Acres': 14.85262205168994, 'Area': '7.18 %', 'Count': 702, 'CountAllPixels': 9783, 'CropCode': 121, 'CropName': 'Developed/OpenSpace', 'color': '#f8a041'}, {'Acres': 0.6347274381064076, 'Area': '0.31 %', 'Count': 30, 'CountAllPixels': 9783, 'CropCode': 122, 'CropName': 'Developed/LowIntensity', 'color': '#e54ef9'}, {'Acres': 7.1724200506024065, 'Area': '3.47 %', 'Count': 339, 'CountAllPixels': 9783, 'CropCode': 141, 'CropName': 'DeciduousForest', 'color': '#2090f5'}, {'Acres': 2.940903796559689, 'Area': '1.42 %', 'Count': 139, 'CountAllPixels': 9783, 'CropCode': 176, 'CropName': 'Grassland/Pasture', 'color': '#7563ce'}, {'Acres': 3.385213003234174, 'Area': '1.64 %', 'Count': 160, 'CountAllPixels': 9783, 'CropCode': 195, 'CropName': 'HerbaceousWetlands', 'color': '#efaeca'}], 'pngb64': 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAJoAAABvCAYAAAAUoNm9AAAGjklEQVR4nO2dz2scZRjHn8Q9BANhY5kcRJcIgmIoiTnsUZJbheJh0UOCSGkOvS7RUqRQ0UNDGi37D0RCDvag7KnFQoVIEA85xBQRLLY0rHrZoSQIgfUg9aAzfWcyMzs/3vd55533+znNzv6Yh32/87zv+zzv+8zIYGvRHVz7hWx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as AOI for Cultivated Layer

In [61]:
#Parameters to call Cultivated Layer
aoi = "{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-95.9148931503296,46.322452681104124],[-95.91476440429689,46.31674720419029],[-95.91442108154298,46.31682130510615],[-95.91399192810059,46.31673238399508],[-95.91360569000246,46.31648044006236],[-95.91345548629762,46.31630259658792],[-95.91388463974,46.3157838831528],[-95.91392755508424,46.31532444714701],[-95.91435670852661,46.314924290063686],[-95.91478586196901,46.31456859242209],[-95.91474294662476,46.31356076988237],[-95.91439962387085,46.313545948824185],[-95.91382026672365,46.31390165311263],[-95.91313362121583,46.314227713346064],[-95.91306924819948,46.314538950847584],[-95.91345548629762,46.31474644153189],[-95.9135413169861,46.31517624116103],[-95.91176033020021,46.315131779286965],[-95.91109514236452,46.31465751704923],[-95.91105222702028,46.314227713346064],[-95.91070890426637,46.3137682642754],[-95.9101939201355,46.31356076988237],[-95.90980768203735,46.31317542106524],[-95.90974330902101,46.312819712056545],[-95.90978622436523,46.31253810745169],[-95.91015100479126,46.31222685857106],[-95.90978622436523,46.31221203715164],[-95.90882062911987,46.3126270353782],[-95.90843439102173,46.312849354562246],[-95.90768337249756,46.312849354562246],[-95.90699672698975,46.312730784443126],[-95.90633153915405,46.31256775010992],[-95.90598821640015,46.31236025116524],[-95.90577363967897,46.311470961062994],[-95.90521574020387,46.31133756630139],[-95.9045934677124,46.311722928059],[-95.90403556823732,46.311722928059],[-95.90332746505739,46.31170810650312],[-95.90231895446779,46.311574712319505],[-95.90182542800903,46.3116191770835],[-95.90128898620607,46.31193042942004],[-95.90113878250122,46.31221203715164],[-95.90113878250122,46.31238989391975],[-95.90146064758301,46.31261221406715],[-95.9018898010254,46.31253810745169],[-95.9025549888611,46.31249364343426],[-95.90337038040163,46.31224167998646],[-95.90403556823732,46.31249364343426],[-95.90397119522096,46.312893818290675],[-95.9036707878113,46.31305685165256],[-95.9031128883362,46.313130957565605],[-95.90270519256593,46.31336809581297],[-95.90229749679567,46.31378308527336],[-95.90195417404176,46.31399057882352],[-95.90126752853395,46.31511695865424],[-95.90075254440308,46.31529480598191],[-95.90075254440308,46.31553193485307],[-95.90090274810792,46.315754242236565],[-95.90094566345216,46.31594690790508],[-95.90068817138673,46.31622849496964],[-95.90025901794435,46.31653972109205],[-95.90002298355103,46.316850945444386],[-95.90002298355103,46.31722144831783],[-95.90023756027223,46.31762158860345],[-95.89991569519044,46.31811064497894],[-95.89944362640382,46.31885163115864],[-95.89989423751832,46.3191035641739],[-95.9040141105652,46.319073925055825],[-95.9043574333191,46.31870343472544],[-95.9043574333191,46.31843668013423],[-95.90418577194215,46.31827366279941],[-95.90369224548341,46.318258843017624],[-95.90296268463136,46.318081005323016],[-95.90251207351685,46.317932806802666],[-95.90225458145143,46.317799427791165],[-95.90317726135254,46.315872805805164],[-95.90356349945068,46.31581352405298],[-95.90362787246706,46.31603583029254],[-95.90414285659791,46.31622849496964],[-95.90470075607301,46.316258135628985],[-95.9052586555481,46.316287776272304],[-95.90555906295778,46.31928139854511],[-95.90843439102173,46.31932585704758],[-95.90858459472658,46.322482318391906],[-95.9148931503296,46.322452681104124]]]}}"
request_with_GeoJSON_cl = {"years": "['2015']",
                             "aoi":aoi,
                           "Resolution":"0.0001",
                        "Projection":"EPSG:4326",
                        "product":"['CultivatedLayer']"}
In [62]:
CultivatedLayer_response_geojson = cdl_service(request_with_GeoJSON_cl, None, headers)
{'CultivatedLayer': {'attributes': {'CellSize': [0.00010000000000006881, -9.999999999996073e-05], '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"]]', 'Cultivated_Percent': 0.9381999999999999, 'Extent': '-95.91482400108805, 46.31142004905899, -95.89952400108804, 46.322420049058984', 'Legend': [{'Acres': 194.0148787962576, 'Area': '93.82 %', 'Count': 9170, 'CountAllPixels': 9774, 'Cultivated': 'Yes', 'color': '#97b0ec'}], 'pngb64': 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAJkAAABuCAYAAAA0y7EbAAAC9ElEQVR4nO3c4XHqMBBF4cWTEqAv2qCitJG+kiLyI2OGEAdLsq600p6vged5PlnJMnAyM3v/+Pq0YG7X86X3NUSx9L4AzC9sZBGndy9hI0M7RAY5IoMckUGOyCBHZJAjMsgRGeSIDHJEBjkigxyRQY7IIEdkkCMyyBEZ5IgMcgufEIUakwxyRAY5IoMckUGOyCBHZJALHRnHN22EjgxtEBnkiAxyJ/YlWvxEFZMMDbz1vgDkG2H1eZzgLJcNHV06R7xXt+v5wnI5iBEDM/u5biIbwKiBrYgMckTW0PvH1+foUykXezJIrQ86RNZBtGnGOdkAarw1aB324zUzyVDd8x8FkUGOyFDV1tLOniyI55tfY4+WuldkkkGOyCBHZJAjMsgRWSeRTv2JDHILX3SAGpMMckQGOU78g+j5oHGPbGtvFukJCDqL2f/voG7X84UHAxyVtFyuoTHZxuPhnmVt/JlqY/EQmFnB0yWhIRdHGCiSM2xOJf+AlzE8M8+/m5F7bdmTjMD62/uSsKfAzDIjIzBfen7NLUfyiT+BtVX6/+3xPiXtyTxeONornWQ8XSJZ6bAhMsgRGeSIDHK7kbHpx1FZH1p89XSRE2PqU4riq/T80eSp8a662idjeXE+n1r3lD0Z5IgMm2quTL9O/BX7qt7Yg+1T38v7JMu9Gdw8pFrMjr2MJTbsYU8GOSKDHJFB7nBkozxlop9qr5W84rVSfyyXwbUYHMmRjTjF8Fqre7q7XI4YF0via63v6b+RjRgX9vW4r5vLJYHNqdd9LfqZAu9YLv/qOTh4ugyg98pEZJPrHZgZP0w8LQ9xraaPLMKJv6egtkwf2ay8h/WIyAYzUlyr0JGV3LAZl1u1cOdkiknQMjwmmXOqGxTh4eKI6SdZz798VWyjTbMpI/OqdnR7sSl+S6REqOVyNs8T2+syzWulSXgNzIzI0ACRQY49GTbVfIJlkuGP2kckTDKHWpyDtTxL5JysoZQnwNEOWlMwyZyYMa7VN8CKBP8aHqItAAAAAElFTkSuQmCC'}, 'filename': 'result_raster_cdl_CultivatedLayer_epsg_4326_resolution_0.0001_57351.tif'}, 'status': 'SUCCEED'}

Visualize the response png

Visualize CDL

In [63]:
# pngb64
attributes =cdl_response_geojson['2015']['attributes']
pngb64cdl = attributes['pngb64']
#Image Legend
df=defaultdict(list)

Legend = attributes['Legend']
    
for val in Legend:
    df['Acres'].append(float(val['Acres']))
    df['Area'].append(val['Area'])
    df['Count'].append(float(val['Count']))
    df['CountAllPixels'].append(val['CountAllPixels'])
    df['CropCode'].append(val['CropCode'])
    df['CropName'].append(val['CropName'])
    df['color'].append(val['color'])

    
legenddf = pd.DataFrame.from_dict(df)    
legenddf.style.applymap(lambda x:"background-color: %s"%x, subset=['color'])
Out[63]:
Acres Area Count CountAllPixels CropCode CropName color
0 5.26824 2.55 % 249 9783 1 Corn #79c024
1 167.97 81.15 % 7939 9783 5 Soybeans #f01e16
2 4.25267 2.05 % 201 9783 36 Alfalfa #52ca3e
3 0.380836 0.18 % 18 9783 37 OtherHay/NonAlfalfa #23eab1
4 0.126945 0.06 % 6 9783 111 OpenWater #c4b536
5 14.8526 7.18 % 702 9783 121 Developed/OpenSpace #f8a041
6 0.634727 0.31 % 30 9783 122 Developed/LowIntensity #e54ef9
7 7.17242 3.47 % 339 9783 141 DeciduousForest #2090f5
8 2.9409 1.42 % 139 9783 176 Grassland/Pasture #7563ce
9 3.38521 1.64 % 160 9783 195 HerbaceousWetlands #efaeca
In [66]:
# Visualize CDL
Image(url = pngb64cdl, width = 400, height = 400)
Out[66]:

Visualize Cultivated Layer

In [72]:
# pngb64 for cultivated Layer
attributes_cl =CultivatedLayer_response_geojson['CultivatedLayer']['attributes']
pngb64cl = attributes_cl['pngb64']

#Image Legend
df_cl=defaultdict(list)
Legend_cl = attributes['Legend']
    
for val in Legend_cl:
    df_cl['Acres'].append(float(val['Acres']))
    df_cl['Area'].append(val['Area'])
    df_cl['Count'].append(float(val['Count']))
    df_cl['CountAllPixels'].append(val['CountAllPixels'])
    df_cl['Cultivated'].append(val['Cultivated'])
    df_cl['color'].append(val['color'])

    
legenddf_cl = pd.DataFrame.from_dict(df_cl)    
legenddf_cl.style.applymap(lambda x:"background-color: %s"%x, subset=['color'])
Out[72]:
Acres Area Count CountAllPixels Cultivated color
0 194.015 93.82 % 9170 9774 Yes #97b0ec
In [73]:
# Visualize cultivated Layer
Image(url = pngb64cl, width = 400, height = 400)
Out[73]:

GET Request

In [82]:
# 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:'
# Create GET request payload.
values = {'filename': [cdl_response_geojson['2015']['filename'],]}
print(values)
{'filename': ['result_raster_cdl_2015_epsg_4326_resolution_0.0001_15442.tif']}
In [83]:
# Get CDL file 
def CDL_get(values, local_path):
    try:
        url = 'https://ag-analytics.azure-api.net/cdl-range-read'
    
        download_path = os.path.join(local_path, values['filename'][0])
        print(values)
     
        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 [84]:
CDL_get(values, local_path)
{'filename': ['result_raster_cdl_2015_epsg_4326_resolution_0.0001_15442.tif']}
https://agflaskdev.eastus2.cloudapp.azure.com/flask_api/api/v1.0/CDL_Service_API/cdl_rr?filename=result_raster_cdl_2015_epsg_4326_resolution_0.0001_15442.tif
Out[84]:
<Response [200]>
In [ ]: