Zone Management

In this part we will focus on two endpoint:

  1. “/AFS/ZoneManagementGeojson” endpoint, which allow you to get a zone management map in geojson file format in wgs 84 coordinate reference system
  2. “/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
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.

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:

  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. 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"

# Define the url of the API

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

# 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

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

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"

# Define the url of the API

url <- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementGeojsonNew?fieldnumber=",fieldnumber,"&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone)

# 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()
2.78 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. 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:

  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. 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"

# Define the url of the API

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

# 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

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

fieldnumber="1"
action="none"
AutoPrescription="False"
NumberZone="3"

# Define the url of the API

url <- paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/ZoneManagementShapefileNew?fieldnumber=",fieldnumber,"&action=",action, "&AutoPrescription=",AutoPrescription,"&NumberZone=",NumberZone)

# 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()
2.69 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. 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',
    });

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

Easy - Fast - Customizable

Back to top