Asynchronous

In this part we will focus on the batch processing endpoint aka, “AFS/Asynchronous”.

This endpoint allows the user to be able to request the processing of many fields with a single post request.

It is an asynchronous request i.e., as soon as the request is executed, the process begins and it will take some time before everything is readily available for further requests. Once the technical time has passed, all the data will be available, quickly This is to reduce making a request for each new field and wait for the response.

Asynchronous

Python

In this example we will test the Asynchronous which allow the user to perform a batch processing on several fields.

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

fieldnumber="1"
action="new"

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/Asynchronous?"

# Define the API call

api_url = url+"fieldnumber="+fieldnumber+"&action="+action

# 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(api_url, 
                         data=geojson_data, 
                         headers=headers,
                         auth=HTTPBasicAuth(USEREMAIL, APIKEY))

R

In this example we will test the BatchProcessing which allow the user to get the latest vegetation index available for the Area of Interest in gejson 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. 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 parameters
fieldnumber="1"
action="new"

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/Asynchronous?"

# Define the API endpoint

url_endpoint<-paste0(url, "fieldnumber=",fieldnumber,"&action=",action)

# 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_endpoint,
  httr::authenticate(
    user = Sys.getenv(USEREMAIL),
    password = Sys.getenv(APIKEY)
  ),
  body=httr::upload_file(file_path_to_geojson)
)

Node.js

In this example we will test the BatchProcessing which allow the user to get the latest vegetation index available for the Area of Interest in gejson 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. 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 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="new";

// Set API Url

const apiUrl='https://www.api.automaticfarmsolutionwebapp.com/AFS/Asynchronous?';

// Set the API endpoint

ApiEndpoint=apiUrl.concat("fieldnumber=",fieldnumber,"&action=",action)

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

  } catch (err) {
    console.error('Error:', err.message);
  }
})();

Java

Work in progress

Easy - Fast - Customizable

Back to top