# Load the libraries
import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/UserField?"
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the GET request with the GeoJSON data as the request body
= requests.get(url,
response =headers,
headers=HTTPBasicAuth(USEREMAIL, APIKEY))
auth
= bytes(response.content)
b
with fiona.BytesCollection(b) as f:
= f.crs
crs = gpd.GeoDataFrame.from_features(f, crs=crs)
gdf
# Visualize the data
"Zone") gdf.explore(
User data
In this part we will focus on two endpoint:
- “AFS/UserField”, This endpoint allows the user to view all the fields that have been analyzed through our service.
- “AFS/UserHectares” endpoint, This endpoint allows the user to get the analyzed area through our service
UserField
Python
In this example we will test the UserField endpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
With your information and let’s try out the API.
R
In this example we will test the UserField endpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
With your information and let’s try out the API.
# Import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the url of the API
<- "https://www.api.automaticfarmsolutionwebapp.com/AFS/UserField?"
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()
1.23 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Visulize the field boundaries
<- httr::content(r, as = "text", type = "application/geo+json")
mappa <-geojsonio::geojson_sp(mappa)
mappa::mapview(mappa) mapview
Node.js
In this example we will test the UserField endpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
With your information and let’s try out the API.
// Load Libraries
const fs = require('fs');
const axios = require('axios');
// Set the useremail & passowrd
const useremail = 'XXXXXXXXXXXXXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXXXXXXXXXXXXX';
// Set API Url
const apiEndpoint='https://www.api.automaticfarmsolutionwebapp.com/AFS/UserField?';
// Set path to save the geojson prescription map
const outputFilePath = './result.geojson';
async () => {
(try {
const authHeader = `Basic ${Buffer.from(`${useremail}:${apikey}`).toString('base64')}`;
const response = await axios.get(apiEndpoint, {
headers: {
'Content-Type': 'application/json',
'Authorization': authHeader
};
})
const resultGeoJSON = response.data;
console.log('Answer From the API:', resultGeoJSON);
await fs.promises.writeFile(outputFilePath, JSON.stringify(resultGeoJSON, null, 2), 'utf8');
catch (err) {
} console.error('Error:', err.message);
}; })()
UserHectares
Python
In this example we will test the UserHectares endpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
With your information and let’s try out the API.
# 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/UserHectares?"
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the GET 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()
R
In this example we will test the UserHectares endpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
With your information and let’s try out the API.
# Import libraries
library(tictoc)
library(httr)
library(gt)
library(tidyverse)
# Define the url of the API
<- "https://www.api.automaticfarmsolutionwebapp.com/AFS/UserHectares?"
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()
1.14 sec elapsed
# Get the status of the request
::status_code(r) httr
[1] 200
# Get info about the hectares
<- httr::content(r, as ="text", type = "application/json")
cont
head(jsonlite::fromJSON(cont))
[1] 31.0302
Node.js
In this example we will test the UserHectares endpoint which allow the user to get historical climate data
To use the following example you have to replace:
- USEREMAIL
- APIKEY
With your information and let’s try out the API.
// Load Libraries
const fs = require('fs');
const axios = require('axios');
// Set the useremail & passowrd
const useremail = 'XXXXXXXXXXXXXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXXXXXXXXXXXXX';
// Set API Url
const apiEndpoint='https://www.api.automaticfarmsolutionwebapp.com/AFS/UserHectares?';
async () => {
(try {
const authHeader = `Basic ${Buffer.from(`${useremail}:${apikey}`).toString('base64')}`;
const response = await axios.get(apiEndpoint, {
headers: {
'Content-Type': 'application/json',
'Authorization': authHeader
};
})
const resultGeoJSON = response.data;
console.log('Answer From the API:', resultGeoJSON);
catch (err) {
} console.error('Error:', err.message);
}; })()
Easy - Fast - Customizable