# Load the libraries
import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/ListPestModels"
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request
= requests.get(url,
response =headers,
headers=HTTPBasicAuth(USEREMAIL, APIKEY))
auth
# Convert from response to json
= response.json()
data
# Convert from json to pandas
= pd.DataFrame(data)
df
# Let's see the first 5 agricultural products
df.head()
Pest Models
In this part we will focus on two endpoint:
- “AFS/ListPestModels” endpoint, which allow you to get a zone management map in geojson file format in wgs 84 coordinate reference system
- “AFS/ActualPestModelsRiskLevel” endpoint, which allow you to get a zone management map in zip file format, where within the zip file you will find the shapefile of zone management in wgs 84 coordinate reference system
ListPestModels
Python
In order to use this endpoint you need to set the following parameters:
- USEREMAIL
- APIKEY
R
In order to use this endpoint you need to set the following parameters:
- USEREMAIL
- APIKEY
# import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the url of the API
<- "https://www.api.automaticfarmsolutionwebapp.com/AFS/ListPestModels?"
api_url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Get the start time to API Call
tic()
# Make the GET request
<- GET(
r
api_url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
0.71 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Print out the list of the Phytofarmaceutical products
<- httr::content(r, as = "text", type = "application/json", encoding="UTF-8")
cont <-jsonlite::fromJSON(cont) %>% as.data.frame
cont1:5,] cont[
[1] "gddAlfalfaWeevil" "gddAlmondNavelOrangewormMummy"
[3] "gddAlmondNavelOrangewormNewCrop" "gddAmericanPlumBorer"
[5] "gddAppleMaggotEmergence"
Node.js
In order to use this endpoint you need to set the following parameters:
- USEREMAIL
- APIKEY
// load libraries
const axios = require('axios');
// Set username e apikey
const username = 'XXXXXXXXXXXXXX';
const password = 'XXXXXXXXXXXXXX';
// set the apiurl
const url='https://www.api.automaticfarmsolutionwebapp.com/AFS/ListPestModels';
const options = {
method: 'get',
url: url,
auth: {
username: username,
password: password,
,
};
}
axios(options)
.then(response => {
console.log('Risposta JSON:', response.data);
}).catch(error => {
console.error('Errore:', error.message);
; })
ActualPestModelsRiskLevel
Python
In order to use this endpoint you need to set the following parameters:
- USEREMAIL
- APIKEY
- long, longitude of the request field
- lat, latitude of the request field
- pestmodel, the pest model that you would like to have. Must be on of the list, that ca be obtained by the “AFS/ListPestModels” endpoint
# Load the libraries
import requests
from requests.auth import HTTPBasicAuth
import pandas as pd
# Define the API parameters setting
="13.3123"
longi="42.4560"
lat="gddAlfalfaWeevil"
pestmodel
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/ActualPestModelsRiskLevel?long="+longi+"&lat="+lat+"&pestmodel="+pestmodel
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request
= requests.post(url,
response =headers,
headers=HTTPBasicAuth(USEREMAIL, APIKEY))
auth
# Convert from response to json
= response.json()
data
# Convert from json to pandas
= pd.DataFrame(data)
df
# Let's see the first 5 agricultural products
df.head()
R
In order to use this endpoint you need to set the following parameters:
- USEREMAIL
- APIKEY
- long, longitude of the request field
- lat, latitude of the request field
- pestmodel, the pest model that you would like to have. Must be on of the list, that ca be obtained by the “AFS/ListPestModels” endpoint
# import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the API parameters setting
<-"13.3123"
longi<-"42.4560"
lat<-"gddAlfalfaWeevil"
pestmodel
# Define the url of the API
<- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/ActualPestModelsRiskLevel?long=", longi, "&lat=",lat, "&pestmodel=", pestmodel)
api_url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Get the start time to API Call
tic()
# Make the POST request
<- POST(
r
api_url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
2.94 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Print out the list of the Phytofarmaceutical products
<- httr::content(r, as = "text", type = "application/json", encoding="UTF-8")
cont <-jsonlite::fromJSON(cont) %>% as.data.frame
contgt(cont)
risk_level | current_pest_situation | current_suggested_operation |
---|---|---|
0 | Adult / Eggs | Adults are active. |
0 | Adult / Eggs | Adults have begun to lay eggs. |
Node.js
In order to use this endpoint you need to set the following parameters:
- USEREMAIL
- APIKEY
- long, longitude of the request field
- lat, latitude of the request field
- pestmodel, the pest model that you would like to have. Must be on of the list, that ca be obtained by the “AFS/ListPestModels” endpoint
// load libraries
const axios = require('axios');
// Set username e apikey
const username = 'XXXXXXXXXXXXXX';
const password = 'XXXXXXXXXXXXXX';
// set the apiurl
const apiurl='https://www.api.automaticfarmsolutionwebapp.com/AFS/ActualPestModelsRiskLevel?long=';
// set the product_name parameter
const longi="13.3123";
const lat="42.4560";
const pestmodel="gddAlfalfaWeevil";
// set the apiendpoint
const url = apiurl.concat(longi, "&lat=", lat, "&pestmodel=", pestmodel)
const options = {
method: 'post',
url: url,
auth: {
username: username,
password: password,
,
};
}
axios(options)
.then(response => {
console.log('Risposta JSON:', response.data);
}).catch(error => {
console.error('Errore:', error.message);
; })
Easy - Fast - Customizable