# Load the libraries
import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth
# Define the API call parameters
="1"
fieldnumber="none"
action="False"
AutoPrescription="3"
NumberZone
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementGeojsonNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription="+AutoPrescription+"&NumberZone="+NumberZone
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey""
APIKEY
# Path to the GeoJSON file
= "county.geojson"
file_path_to_geojson
# Read the GeoJSON file contents
with open(file_path_to_geojson, "r") as file:
= file.read()
geojson_data
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request with the GeoJSON data as the request body
= requests.post(url,
response =geojson_data,
data=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(
Zone Management
In this part we will focus on two endpoint:
- “/AFS/ZoneManagementGeojson” endpoint, which allow you to get a zone management map in geojson file format in wgs 84 coordinate reference system
- “/AFS/ZoneManagementShapefile” 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
The zone management and prescription map are quite different:
- Zone Managament is the map where you differentiate the different zones of the field, but those zones do not have any dose or agronomic input set, so when you receive this file you should then associate to cada zone a dose
- Prescription Map is the map where for each set zone the dose value and agronomic input is then associated, and then this map is ready to be used directly by tractors.
ZoneManagementGeojson
Python
In this example we will test the ZoneManagementGeojson endpoint which allow the user to get an Zone Management map in geojson format.
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
- action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
- AutoPrescription, Must be set as True or False. If True means that number and position of zone management are setted automatically; If False means that number of zone management are setted by the user by definig the NumberZone parameter
- NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
- file_path_to_geojson (you can find it here county.geojson the county file of the example)
With your information and let’s try out the API.
R
Here to test the API connection you have to replace:
- USEREMAIL
- APIKEY
- fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
- action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
- AutoPrescription, Must be set as True or False. If True means that number and position of zone management are setted automatically; If False means that number of zone management are setted by the user by definig the NumberZone parameter
- NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
- file_path_to_geojson (you can find it here county.geojson the county file of the example)
with your account information.
# Load the libraries
library(tictoc)
library(httr)
library(geojsonio)
library(mapview)
# Define the API call parameters
="1"
fieldnumber="none"
action="False"
AutoPrescription="3"
NumberZone
# Define the url of the API
<- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementGeojsonNew?fieldnumber=",fieldnumber,"&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone)
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Path to the GeoJSON file
= "county.geojson"
file_path_to_geojson
# Get the start time to API Call
tic()
# Make the POST request
<- POST(
api_call
url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
),body=httr::upload_file(file_path_to_geojson)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
2.78 sec elapsed
# Visualize the prescription map
<- content(api_call, as = "text", type = "application/geo+json")
prscription_map <-geojson_sp(prscription_map)
prscription_mapmapview(prscription_map, zcol="Zone", layer.name="Prescription Map")
Node.js
Here to test the API connection you have to replace:
- USEREMAIL
- APIKEY
- fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
- action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
- AutoPrescription, Must be set as True or False. If True means that number and position of zone management are setted automatically; If False means that number of zone management are setted by the user by definig the NumberZone parameter
- NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
- file_path_to_geojson (you can find it here county.geojson the county file of the example)
with your account information.
// Load Libraries
const fs = require('fs');
const axios = require('axios');
// Set the useremail & passowrd
const useremail = 'XXXXXXXXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXXXXXXXX';
// Set the API Parameter
const fieldnumber="1";
const action="none";
const AutoPrescription = "False";
const NumberZone = "4";
// Set API Url
const api_url='https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementGeojsonNew?fieldnumber=';
// Set endpoint Url
const apiEndpoint = api_url.concat(fieldnumber,"&action=",action,"&AutoPrescription=", AutoPrescription,"&NumberZone=",NumberZone);
// Set path to load the geojson to send as body of POST request to the API
const file_path_to_geojson = './county.geojson';
// Set path to save the geojson prescription map
const outputFilePath = './result.geojson';
async () => {
(try {
const geojsonContent = await fs.promises.readFile(file_path_to_geojson, 'utf8');
const geojsonObject = JSON.parse(geojsonContent);
const authHeader = `Basic ${Buffer.from(`${useremail}:${apikey}`).toString('base64')}`;
const response = await axios.post(apiEndpoint, geojsonObject, {
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);
}; })()
ZoneManagementShapefile
Python
In this example we will test the ZoneManagementShapefile endpoint which allow the user to get an Zone Management map in shapefile format
To use the following example you have to replace:
- USEREMAIL
- APIKEY
- fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
- action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
- AutoPrescription, Must be set as True or False. If True means that number and position of zone management are setted automatically; If False means that number of zone management are setted by the user by definig the NumberZone parameter
- NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
- file_path_to_geojson (you can find it here county.geojson the county file of the example)
With your information and let’s try out the API.
# Load the libraries
import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth
# Define the API call parameters
="1"
fieldnumber="none"
action="False"
AutoPrescription="3"
NumberZone
# Define the url of the API
= "https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementShapefileNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription="+AutoPrescription+"&NumberZone="+NumberZone
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Path to the GeoJSON file
= "county.geojson"
file_path_to_geojson
# Read the GeoJSON file contents
with open(file_path_to_geojson, "r") as file:
= file.read()
geojson_data
# Set the headers for the request
= {
headers "Content-Type": "application/json"
}
# Make the POST request with the GeoJSON data as the request body
= requests.post(url,
response =geojson_data,
data=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(
R
Here to test the API connection you have to replace:
- USEREMAIL
- APIKEY
- fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
- action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
- AutoPrescription, Must be set as True or False. If True means that number and position of zone management are setted automatically; If False means that number of zone management are setted by the user by definig the NumberZone parameter
- NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
- file_path_to_geojson (you can find it here county.geojson the county file of the example)
with your account information.
# Load the libraries
library(tictoc)
library(httr)
library(geojsonio)
library(mapview)
# Define the API call parameters
="1"
fieldnumber="none"
action="False"
AutoPrescription="3"
NumberZone
# Define the url of the API
<- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementShapefileNew?fieldnumber=",fieldnumber,"&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone)
url
# Set the useremail & Password
="useremail"
USEREMAIL="apikey"
APIKEY
# Path to the GeoJSON file
= "county.geojson"
file_path_to_geojson
# Get the start time to API Call
tic()
# Make the POST request
<- POST(
api_call
url,::authenticate(
httruser = Sys.getenv(USEREMAIL),
password = Sys.getenv(APIKEY)
),body=httr::upload_file(file_path_to_geojson)
)
# Print out the seconds needed to create the prescription map
print("The API needed:")
[1] "The API needed:"
toc()
2.69 sec elapsed
# Visualize the prescription map
<-readBin(api_call$content, what = "raw", n=length(api_call$content))
bin_shape
writeBin(bin_shape,
con="zone_managament.zip")
unzip("zone_managament.zip")
<-sf::st_read(dsn = ".",
mappalayer = "shapefile")
Reading layer `shapefile' from data source
`C:\Users\Utente\Desktop\APIdocumentation' using driver `ESRI Shapefile'
Simple feature collection with 3 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 13.38873 ymin: 43.61031 xmax: 13.39692 ymax: 43.61283
Geodetic CRS: GCS_unknown
::mapview(mappa, zcol="Zone") mapview
Node.js
Here to test the API connection you have to replace:
- USEREMAIL
- APIKEY
- fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
- action, the action must be setted as none, new, edit. If you want to receive the data based on the polygons saved in the past set none. If you want to add a new field set as new. If you want to change the shape of the polygons set edit.
- AutoPrescription, Must be set as True or False. If True means that number and position of zone management are setted automatically; If False means that number of zone management are setted by the user by definig the NumberZone parameter
- NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
- file_path_to_geojson (you can find it here county.geojson the county file of the example)
with your account information.
// Load Libraries
const fs = require('fs');
const axios = require('axios');
// Set the useremail & passowrd
const useremail = 'XXXXXXXXXXX';
const apikey = 'XXXXXXXXXXX';
// Set the API Parameter
const fieldnumber="1";
const action="none";
const AutoPrescription = "False";
const NumberZone = "4";
// Set API Url
const api_url='https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementShapefileNew?fieldnumber=';
// Set endpoint Url
const apiEndpoint = api_url.concat(fieldnumber,"&action=",action,"&AutoPrescription=", AutoPrescription,"&NumberZone=",NumberZone);
// Set path to load the geojson to send as body of POST request to the API
const file_path_to_geojson = './county.geojson';
// Salva il file shapefile localmente
const shapefilePath = './result.zip';
async function main() {
try {
const geojson = JSON.parse(fs.readFileSync(file_path_to_geojson, 'utf8'));
const authHeader = `Basic ${Buffer.from(`${useremail}:${apikey}`).toString('base64')}`;
const response = await axios.post(apiEndpoint, geojson, {
headers: {
'Authorization': authHeader,
'Content-Type': 'application/json',
,
}responseType: 'stream',
;
})
.data.pipe(fs.createWriteStream(shapefilePath));
response
await new Promise((resolve) => {
.data.on('end', resolve);
response;
})
console.log('Shapefile saved with sucess!');
catch (error) {
} console.error('An error was created:', error.message);
}
}
main();
Easy - Fast - Customizable