FruitKount - Interpolated & Prescription Map

In this section we will explore the API Endpoint that allow to download elaborated Map in shapefile format (WGS 84):

- Download Interpolated Map - /sensors/html-widget-map/{id_sensor}/{date}/{variable}/

- Download Prescription Map - /sensors/html-widget-prescription-map/{id_sensor}/{date}/{automatic}/{zone}/{fertilizer}/{strategy}/{percentage}/

Interpolated Map - Shapefile

This endpoint allow to download a shapefile format (WGS 84) of the interpolated map of one data acquisition.

In order to use this endpoint you have to provide three different API Parameters:

  1. id_sensor, which is the sensor id of your device
  2. date, which is the date of the acquisition that the device performed
  3. variable, could be set as count, area, widht, height

Python

# Load the libraries
import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth

# Set the token from the environment variable
token = "yourtoken"

# Set the domain
domain = "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint = "sensors/download-interpolated-map/"

# Set API parameters
id_sensor="1"
date="2024-08-12T07:23:22Z"
variable="count"

# Create the API URL
api_url = f"{domain}{endpoint}{id_sensor}/{date}/{variable}/"

# Make the GET request with Bearer Authentication
headers = {
    "Authorization": f"Bearer {token}"
}

response = requests.get(api_url, headers=headers)

b = bytes(response.content)

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

# Save localy
gdf.to_file('interpolated_map.shp')  

#  Visualize the data
gdf.explore("count")

R

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

# Set the token from the environment variable
token <- Sys.getenv("token")

# Set the domain
domain <- "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint <- "sensors/download-interpolated-map/"

# Set the API Parameters
id_sensor="1"
date="2024-08-12T07:23:22Z"
variable="count"

# Create the API URL
api_url <- paste0(domain, endpoint, id_sensor, "/", date, "/", variable)

# Make the POST request

response <- GET(
  api_url,
  add_headers(Authorization = paste("Bearer", token))
)

# Check the status code of the response
print(response$status_code)
[1] 200
# Visualize the prescription map

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

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

unzip("Rx.zip")

mappa<-sf::st_read(dsn = ".",
                   layer = "FruitKount")
Reading layer `FruitKount' from data source 
  `C:\Users\Utente\Desktop\APIdocumentation' using driver `ESRI Shapefile'
Simple feature collection with 22 features and 1 field
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 371414.5 ymin: 4828175 xmax: 371524.5 ymax: 4828405
Projected CRS: +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs
mapview::mapview(mappa, zcol="count", layer.name="Grape Number")

Node.js

// Load Libraries
const fs = require('fs');
const axios = require('axios');

// Set domain
const apidomain = 'https://fruitkountbackend.automaticfarmsolutionwebapp.com/';

// Set API Endpoint
const endpoint = 'sensors/download-interpolated-map/';

// Set the user token
const token = 'XXXXXXXXXXXXX';

// Set the API Parameter
const id_sensor="1";
const date="2024-08-12T07:23:22Z";
const variable="count";

// API Url
const apiEndpoint = apidomain.concat(endpoint, id_sensor, "/", date, "/", variable, "/");

// Salva il file shapefile localmente
const shapefilePath = './result.zip';

async function main() {
  try {
    
    // Use responseType 'stream' to handle the download as a stream
    const response = await axios.get(apiEndpoint, {
      headers: {
        'Authorization': `Bearer ${token}`,
      },
      responseType: 'stream',  // This is crucial for downloading files as streams
    });

    console.log(response.status);  // Log status code

    // Pipe the response stream to the local file
    response.data.pipe(fs.createWriteStream(shapefilePath));

    // Wait for the download to finish
    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();

Prescription Map - Shapefile

This endpoint allow to download a shapefile format (WGS 84) of the prescription map of one date acquisition.

In order to use this endpoint you have to provide seven different API Parameters:

  1. id_sensor, which is the sensor id of your device
  2. date, which is the date of the acquisition that the device performed
  3. automatic, 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
  4. zone, if AutoPrescription is setted False, here you can specify the number of zone
  5. fertilizer, set the amount of agronomic input to provide on avarage to the field
  6. 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
  7. percentage, 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.

Python

# Load the libraries
import requests
import fiona
import geopandas as gpd
from requests.auth import HTTPBasicAuth

# Set the token from the environment variable
token = "yourtoken"

# Set the domain
domain = "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint = "sensors/download-prescription-map/"

# Set API parameters
id_sensor="1"
date="2024-08-12T07:23:22Z"
automatic="False"
zone="3"
fertilizer="180"
strategy="highwherehigh"
percentage="10"

# Create the API URL
api_url = f"{domain}{endpoint}{id_sensor}/{date}/{automatic}/{zone}/{fertilizer}/{strategy}/{percentage}/"

# Make the GET request with Bearer Authentication
headers = {
    "Authorization": f"Bearer {token}"
}

response = requests.get(api_url, headers=headers)

b = bytes(response.content)

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

# Save localy
gdf.to_file('prescription_map.shp') 

#  Visualize the data
gdf.explore("Zone")

R

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

# Set the token from the environment variable
token <- Sys.getenv("token")

# Set the domain
domain <- "https://fruitkountbackend.automaticfarmsolutionwebapp.com/"

# Set the endpoint
endpoint <- "sensors/download-prescription-map/"

# Set the API Parameters
id_sensor="1"
date="2024-08-12T07:23:22Z"
automatic="False"
zone="3"
fertilizer="180"
strategy="highwherehigh"
percentage="10"

# Create the API URL
api_url <- paste0(domain, endpoint, id_sensor, "/", date, "/", automatic, "/", zone, "/", fertilizer, "/", strategy, "/", percentage)

# Make the POST request

response <- GET(
  api_url,
  add_headers(Authorization = paste("Bearer", token))
)

# Check the status code of the response
print(response$status_code)
[1] 200
# Visualize the prescription map

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

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

unzip("Rx.zip")

mappa<-sf::st_read(dsn = ".",
                   layer = "Rx")
Reading layer `Rx' from data source `C:\Users\Utente\Desktop\APIdocumentation' using driver `ESRI Shapefile'
Simple feature collection with 3 features and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 371415 ymin: 4828170 xmax: 371525 ymax: 4828405
Projected CRS: +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs
mapview::mapview(mappa, zcol="Zone", layer.name="Dose Fertilizer kg/ha")

Node.js

// Load Libraries
const fs = require('fs');
const axios = require('axios');

// Set domain
const apidomain = 'https://fruitkountbackend.automaticfarmsolutionwebapp.com/';

// Set API Endpoint
const endpoint = 'sensors/download-prescription-map/';

// Set the user token
const token = 'XXXXXXXXXXXXX';

// Set the API Parameter
const id_sensor="1";
const date="2024-08-12T07:23:22Z";
const automatic="False"
const zone="3"
const fertilizer="180"
const strategy="highwherehigh"
const percentage="10"

// API Url
const apiEndpoint = apidomain.concat(endpoint, id_sensor, "/", date, "/", automatic, "/", zone, "/", fertilizer, "/", strategy, "/", percentage, "/");

// Salva il file shapefile localmente
const shapefilePath = './result.zip';

async function main() {
  try {
    
    // Use responseType 'stream' to handle the download as a stream
    const response = await axios.get(apiEndpoint, {
      headers: {
        'Authorization': `Bearer ${token}`,
      },
      responseType: 'stream',  // This is crucial for downloading files as streams
    });

    console.log(response.status);  // Log status code

    // Pipe the response stream to the local file
    response.data.pipe(fs.createWriteStream(shapefilePath));

    // Wait for the download to finish
    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();
Back to top