Prescription Map

In this part we will focus on two endpoint:

  1. “AFS/AutoPrescriptionMapGEOjsonNew” endpoint, which allow you to get a zone management map in geojson file format in wgs 84 coordinate reference system
  2. “AFS/AutoPrescriptionShapefileNew” 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
  3. “AFS/TotalAmountFertilizerNew” endpoint, which allow you to get the total amount of fertilizer that you need to provided to the crop for the prescription maps setted.
Zone Management vs Prescription Map

The zone management and prescription map are quite different:

  1. 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
  2. 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.

AutoPrescriptionMapGEOjsonNew

Python

In this example we will test the AutoPrescriptionMapGEOjson endpoint which allow the user to get an Prescription map in geojson format.

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. Userfertilizer, set the amount of agronomic input to provide on avarage to the field
  8. Strategy, can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. ProductType, Set the type of the product type that you want to provide (e.g. Solid, Fluid, Liquid)
  10. ProductName, Set the name of the product name that you want to provide (e.g. Urea, AmmoniumNitrate, the word don’t have to have space)
  11. GeographicReference, Must be WGS84 or report the number of the UTM zone (e.g. 33, 32, 22)
  12. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
  13. 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

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"
userfertilizer="200"
Strategy="highwherehigh"
ProductType="Solid"
ProductName="Urea"
GeographicReference="WGS84"
DoseDifference="0"

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionMapGEOjsonNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription="+AutoPrescription+"&NumberZone="+NumberZone+"&userfertilizer="+userfertilizer+"&Strategy="+Strategy+"&ProductType="+ProductType+"&ProductName="+ProductName+"&GeographicReference="+GeographicReference+"&DoseDifference="+DoseDifference

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey""

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Read the GeoJSON file contents

with open(file_path_to_geojson, "r") as file:
    geojson_data = file.read()

# Set the headers for the request

headers = {
    "Content-Type": "application/json"
}

# Make the POST request with the GeoJSON data as the request body

response = requests.post(url, 
                         data=geojson_data, 
                         headers=headers,
                         auth=HTTPBasicAuth(USEREMAIL, APIKEY))

b = bytes(response.content)

with fiona.BytesCollection(b) as f:
    crs = f.crs
    gdf = gpd.GeoDataFrame.from_features(f, crs=crs)

#  Visualize the data

gdf.explore("Zone")

R

In this example we will test the AutoPrescriptionMapGEOjson endpoint which allow the user to get an Prescription map in geojson format.

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. Userfertilizer, set the amount of agronomic input to provide on avarage to the field
  8. Strategy, can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. ProductType, Set the type of the product type that you want to provide (e.g. Solid, Fluid, Liquid)
  10. ProductName, Set the name of the product name that you want to provide (e.g. Urea, AmmoniumNitrate, the word don’t have to have space)
  11. GeographicReference, Must be WGS84 or report the number of the UTM zone (e.g. 33, 32, 22)
  12. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
  13. 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

library(tictoc)
library(httr)
library(geojsonio)
library(mapview)

# Define the API call parameters

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"
userfertilizer="200"
Strategy="highwherehigh"
ProductType="Solid"
ProductName="Urea"
GeographicReference="WGS84"
DoseDifference="0"

# Define the url of the API

url = paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionMapGEOjsonNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone,"&userfertilizer=",userfertilizer,"&Strategy=",Strategy,"&ProductType=",ProductType,"&ProductName=",ProductName,"&GeographicReference=",GeographicReference, "&DoseDifference=", DoseDifference)

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey"

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Get the start time to API Call

tic()

# Make the POST request

api_call <- POST(
  url,
  httr::authenticate(
    user = 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()
3.75 sec elapsed
# Visualize the prescription map

prscription_map <- content(api_call, as = "text", type = "application/geo+json")
prscription_map<-geojson_sp(prscription_map)
mapview(prscription_map, zcol="Zone", layer.name="Prescription Map")

Node.js

Here to test the API connection you have to replace:

  1. useremail
  2. apikey
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. Userfertilizer, set the amount of agronomic input to provide on avarage to the field
  8. Strategy, can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. ProductType, Set the type of the product type that you want to provide (e.g. Solid, Fluid, Liquid)
  10. ProductName, Set the name of the product name that you want to provide (e.g. Urea, AmmoniumNitrate, the word don’t have to have space)
  11. GeographicReference, Must be WGS84 or report the number of the UTM zone (e.g. 33, 32, 22)
  12. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
  13. 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 = 'XXXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXXX';

// Set the API Parameter

const fieldnumber="1";
const action="none";
const AutoPrescription = "False";
const NumberZone = "4";
const Userfertilizer="200";
const Strategy = "highwherehigh";
const ProductType = "Solid";
const ProductName = "Urea";
const GeographicReference = "WGS84";
const DoseDifference="0";

// Set API Url

const api_url='https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionMapGEOjsonNew?fieldnumber=';

// Set endpoint Url

const apiEndpoint = api_url.concat(fieldnumber, "&action=",action, "&AutoPrescription=", AutoPrescription, "&NumberZone=", NumberZone, "&userfertilizer=", Userfertilizer, "&Strategy=",Strategy,"&ProductType=",ProductType,"&ProductName=",ProductName,"&GeographicReference=",GeographicReference, "&DoseDifference=",DoseDifference);

// 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);
  }
})();

AutoPrescriptionShapefileNew

Python

In this example we will test the AutoPrescriptionShapefile endpoint which allow the user to get an Prescription map in shapefile format.

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. Userfertilizer, set the amount of agronomic input to provide on avarage to the field
  8. Strategy, can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. ProductType, Set the type of the product type that you want to provide (e.g. Solid, Fluid, Liquid)
  10. ProductName, Set the name of the product name that you want to provide (e.g. Urea, AmmoniumNitrate, the word don’t have to have space)
  11. GeographicReference, Must be WGS84 or report the number of the UTM zone (e.g. 33, 32, 22)
  12. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
  13. 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

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"
userfertilizer="200"
Strategy="highwherehigh"
ProductType="Solid"
ProductName="Urea"
GeographicReference="WGS84"
DoseDifference="0"

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionShapefileNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription="+AutoPrescription+"&NumberZone="+NumberZone+"&userfertilizer="+userfertilizer+"&Strategy="+Strategy+"&ProductType="+ProductType+"&ProductName="+ProductName+"&GeographicReference="+GeographicReference+"&DoseDifference="+DoseDifference

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey""

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Read the GeoJSON file contents

with open(file_path_to_geojson, "r") as file:
    geojson_data = file.read()

# Set the headers for the request

headers = {
    "Content-Type": "application/json"
}

# Make the POST request with the GeoJSON data as the request body

response = requests.post(url, 
                         data=geojson_data, 
                         headers=headers,
                         auth=HTTPBasicAuth(USEREMAIL, APIKEY))

b = bytes(response.content)

with fiona.BytesCollection(b) as f:
    crs = f.crs
    gdf = gpd.GeoDataFrame.from_features(f, crs=crs)

#  Visualize the data

gdf.explore("Zone")

R

In this example we will test the AutoPrescriptionShapefile endpoint which allow the user to get an Prescription map in shapefile format.

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. Userfertilizer, set the amount of agronomic input to provide on avarage to the field
  8. Strategy, can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. ProductType, Set the type of the product type that you want to provide (e.g. Solid, Fluid, Liquid)
  10. ProductName, Set the name of the product name that you want to provide (e.g. Urea, AmmoniumNitrate, the word don’t have to have space)
  11. GeographicReference, Must be WGS84 or report the number of the UTM zone (e.g. 33, 32, 22)
  12. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
  13. 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

library(tictoc)
library(httr)
library(geojsonio)
library(mapview)

# Define the API call parameters

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"
userfertilizer="200"
Strategy="highwherehigh"
ProductType="Solid"
ProductName="Urea"
GeographicReference="WGS84"
DoseDifference="0"

# Define the url of the API

url = paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionShapefileNew?fieldnumber=",fieldnumber,"&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone,"&userfertilizer=",userfertilizer,"&Strategy=",Strategy,"&ProductType=",ProductType,"&ProductName=",ProductName,"&GeographicReference=",GeographicReference, "&DoseDifference=",DoseDifference)

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey"

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Get the start time to API Call

tic()

# Make the POST request

api_call <- POST(
  url,
  httr::authenticate(
    user = 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()
4.21 sec elapsed
# Visualize the prescription map

bin_shape<-readBin(api_call$content, what = "raw", n=length(api_call$content))

writeBin(bin_shape,
         con="zone_managament.zip")

unzip("zone_managament.zip")

mappa<-sf::st_read(dsn = ".",
                   layer = "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::mapview(mappa, zcol="Zone")

Node.js

Here to test the API connection you have to replace:

  1. useremail
  2. apikey
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. Userfertilizer, set the amount of agronomic input to provide on avarage to the field
  8. Strategy, can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. ProductType, Set the type of the product type that you want to provide (e.g. Solid, Fluid, Liquid)
  10. ProductName, Set the name of the product name that you want to provide (e.g. Urea, AmmoniumNitrate, the word don’t have to have space)
  11. GeographicReference, Must be WGS84 or report the number of the UTM zone (e.g. 33, 32, 22)
  12. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
  13. 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 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Set the API Parameter

const fieldnumber="1";
const action="none";
const AutoPrescription = "False";
const NumberZone = "4";
const Userfertilizer="200";
const Strategy = "highwherehigh";
const ProductType = "Solid";
const ProductName = "Urea";
const GeographicReference = "WGS84";
const DoseDifference="0";

// Set API Url

const api_url='https://www.api.automaticfarmsolutionwebapp.com/AFS/AutoPrescriptionShapefileNew?fieldnumber=';

// Set endpoint Url

const apiUrl = api_url.concat(fieldnumber, "&action=",action ,"&AutoPrescription=", AutoPrescription, "&NumberZone=", NumberZone, "&userfertilizer=", Userfertilizer, "&Strategy=",Strategy,"&ProductType=",ProductType,"&ProductName=",ProductName,"&GeographicReference=",GeographicReference, "&DoseDifference=",DoseDifference);

// 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(apiUrl, geojson, {
      headers: {
        'Authorization': authHeader,
        'Content-Type': 'application/json',
      },
      responseType: 'stream',
    });

    response.data.pipe(fs.createWriteStream(shapefilePath));

    await new Promise((resolve) => {
      response.data.on('end', resolve);
    });

    console.log('Shapefile saved with sucess!');
  } catch (error) {
    console.error('An error was created:', error.message);
  }
}

main();

TotalAmountFertilizerNew

Python

In this example we will test the TotalAmountFertilizer endpoint which allow the user to get the total amount of fertilizer to provide to the crop for the prescription maps setted.

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. userfertilizer, set the amount of fertilizer to provide on avarage to the field
  8. Strategy, Can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
# Load the libraries
import requests
import json
import pandas as pd
from requests.auth import HTTPBasicAuth

# Define the API call parameters

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"
userfertilizer="200"
Strategy="highwherehigh"
DoseDifference="0"

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/TotalAmountFertilizerNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription="+AutoPrescription+"&NumberZone="+NumberZone+"&userfertilizer="+userfertilizer+"&Strategy="+Strategy+"&DoseDifference="+DoseDifference

# Set the useremail & Password

USEREMAIL="XXXXXXXXXXXX"
APIKEY="XXXXXXXXXXXXX"

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Read the GeoJSON file contents

with open(file_path_to_geojson, "r") as file:
    geojson_data = file.read()

# Set the headers for the request

headers = {
    "Content-Type": "application/json"
}

# Make the POST request with the GeoJSON data as the request body

response = requests.post(url, 
                         data=geojson_data, 
                         headers=headers,
                         auth=HTTPBasicAuth(USEREMAIL, APIKEY))

response_bytes=response.content

response_str = response_bytes.decode('utf-8')

# Parse the JSON string
data = json.loads(response_str)

# Create a DataFrame
df = pd.DataFrame(data)

df.head()

R

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. userfertilizer, set the amount of fertilizer to provide on avarage to the field
  8. Strategy, Can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
# Load the libraries

library(tictoc)
library(httr)
library(geojsonio)
library(mapview)

# Define the API call parameters

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"
userfertilizer="200"
Strategy="highwherehigh"
DoseDifference="0"

# Define the url of the API

url = paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/TotalAmountFertilizerNew?fieldnumber=",fieldnumber, "&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone,"&userfertilizer=",userfertilizer,"&Strategy=",Strategy,"&DoseDifference=",DoseDifference)

# Set the useremail & Password

USEREMAIL="useremail"
APIKEY="apikey"

# Path to the GeoJSON file

file_path_to_geojson = "county.geojson"

# Get the start time to API Call

tic()

# Make the POST request

api_call <- POST(
  url,
  httr::authenticate(
    user = 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:")
toc()

# Visualize the prescription map
cont <- httr::content(api_call, as = "text", type = "application/json", encoding="UTF-8")
cont<-jsonlite::fromJSON(cont) %>% as.data.frame
cont

Node.js

To use the following example you have to replace:

  1. USEREMAIL
  2. APIKEY
  3. fieldnumber, The field number. You have to refereed to the field that you can get from the gid column of the UserField endpoint response
  4. 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.
  5. 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
  6. NumberZone, if AutoPrescription is setted False, here you can specify the number of zone
  7. userfertilizer, set the amount of fertilizer to provide on avarage to the field
  8. Strategy, Can be set as highwherehigh or highwherelow. If highwherehigh means provide an higher amout of fertilizer where the vegetation indexes is higher. If highwherelow means provide an higher amout of fertilizer where the vegetation indexes is lower
  9. DoseDifference, Set the difference percentange of the dose that you want to apply between the zones. you can set a value between 0 to 100. if you set 0 an automatic process will define the difference of the dose based on vegetation index values.
// load libraries
const fs = require('fs');
const axios = require('axios');


// set the username and apikey
const useremail = 'XXXXXXXXXXXX';
const apikey = 'XXXXXXXXXXXX';

// set the api parameters
const fieldnumber="1";
const action="none";
const AutoPrescription = 'False';
const NumberZone = '4';
const Userfertilizer = '200';
const Strategy = 'highwherehigh';
const DoseDifference = '20';

// set the api url
const api_url = 'https://www.api.automaticfarmsolutionwebapp.com/AFS/TotalAmountFertilizerNew?fieldnumber=';

const apiUrl = api_url.concat(
  fieldnumber,
  "&action=",
  action,
  '&AutoPrescription=',
  AutoPrescription,
  '&NumberZone=',
  NumberZone,
  '&userfertilizer=',
  Userfertilizer,
  '&Strategy=',
  Strategy,
  '&DoseDifference=',
  DoseDifference
);

const file_path_to_geojson = './county.geojson';

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(apiUrl, geojson, {
      headers: {
        Authorization: authHeader,
        'Content-Type': 'application/json',
      },
    });

    // Print the formatted JSON response to the console
    console.log(JSON.stringify(response.data, null, 2));
  } catch (error) {
    console.error('An error occurred:', error.message);
  }
}

main();

Easy - Fast - Customizable

Back to top