AI Field Boundaries

In this part we will focus on the “AFS/AIBoundaries” endpoint, which allow the user to get the AI generated boundary of the field in geojson format

Python

in order to use the endpoint you have to set the following parameter:

  1. USEREMAIL
  2. APIKEY
  3. lat, latitude coordinate in WGS84 of the field (e.g. 43.60990243471787)
  4. longi, longitude coordinate in WGS84 of the field (e.g. 13.393299188945429)
# Load the libraries

import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth

# Define the API call parameters

Latitidue="43.60990243471787"
Longitude="13.393299188945429"

# Define the url of the API

url = "https://www.api.automaticfarmsolutionwebapp.com/AFS/AIBoundaries?lat="+Latitidue+"&longi="+Longitude

# 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 order to use the endpoint you have to set the following parameter:

  1. USEREMAIL
  2. APIKEY
  3. lat, latitude coordinate in WGS84 of the field (e.g. 43.60990243471787)
  4. longi, longitude coordinate in WGS84 of the field (e.g. 13.393299188945429)
# Load the libraries

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

# Define the API call parameters

Latitidue="43.60990243471787"
Longitude="13.393299188945429"

# Define the url of the API

url = paste0("https://www.api.automaticfarmsolutionwebapp.com/AFS/AIBoundaries?lat=",Latitidue,"&longi=",Longitude)

# 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()
159.5 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, layer.name="AI Segmentation Map")

Node.js

in order to use the endpoint you have to set the following parameter:

  1. USEREMAIL
  2. APIKEY
  3. lat, latitude coordinate in WGS84 of the field (e.g. 43.60990243471787)
  4. longi, longitude coordinate in WGS84 of the field (e.g. 13.393299188945429)
// 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 Latitidue="43.60990243471787";
const Longitude="13.393299188945429";

// Set API Url

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

// Set endpoint Url

const apiEndpoint = api_url.concat("lat=", Latitidue, "&longi=", Longitude);

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

Java

Work in progress

Back to top