Skip to content

Variables API

Metadata and statistics for the data variables (indicators) available in the block-level shapefile. Variables represent measurable characteristics of each block - agriculture percentages, water indices, infrastructure counts, livestock densities, and more.

Each variable has a short code (e.g., AD, WA, CF) used as column names in the shapefile and GeoJSON properties. These endpoints provide human-readable labels, descriptions, group categories, and statistical ranges.

The label, description, and group metadata are sourced from the intervention/variable configuration (the dss_input Google Sheet, with a local DSS_input2.csv fallback). The numeric min/max/mean statistics are computed live from the block-level shapefile.


Get All Block-Level Variables

Returns metadata for every numeric variable in the block-level shapefile, including min/max/mean statistics computed across all blocks in the shapefile.

GET /api/variables

The handler iterates the columns of the block shapefile, skips the identifier/geometry columns (geometry, BLOCK_ID, STATE_ID, DISTRICT_I, Block_name, id), and emits one entry per column that contains numeric data. Labels, descriptions, and groups are looked up from the variable metadata; a column with no metadata group falls back to group "Other".

Response

200 OK - a JSON array of variable objects:

[
  {
    "field": "AD",
    "label": "Agricultural Diversity",
    "description": "Percentage of agricultural land diversity",
    "group": "Land & Agriculture",
    "data_min": 0.0,
    "data_max": 95.5,
    "data_mean": 42.3
  },
  {
    "field": "WA",
    "label": "Water Availability",
    "description": "Water availability index",
    "group": "Water",
    "data_min": 5.0,
    "data_max": 88.0,
    "data_mean": 35.7
  }
]
Field Type Description
field string Column code in the shapefile (used in filter criteria)
label string Human-readable name (falls back to the field code if no metadata)
description string What this variable measures (empty string if no metadata)
group string Category group for UI organization (falls back to "Other")
data_min number Minimum value across all blocks (0 if no numeric data)
data_max number Maximum value across all blocks (100 if no numeric data)
data_mean number Mean value across all blocks (50 if no numeric data)

Variable Groups

Variables are organized into thematic groups. The group names come from the group column of the variable configuration; the configured group set is:

Group Description Example Variables
Land & Agriculture Crop patterns, land use, agricultural diversity AD, CF, CI
Water Water availability, irrigation, groundwater WA, GW
Soil Soil quality and characteristics -
Climate Rainfall and climate indicators -
Infrastructure Roads, markets, connectivity, facilities IN, RD
Livestock Animal populations, livestock density LV, CT
People & Collectives Demographics, SHGs, cooperatives PO, SH

Group fallback

Any numeric shapefile column that has no matching group in the configuration is reported with group: "Other". The exact set of group strings returned depends on the current configuration sheet.

Errors

Status Body Cause
500 {"error": "<message>"} Failure loading the shapefile or metadata

Example

curl https://leaf-asrlm.in/api/variables
import requests
import pandas as pd

r = requests.get("https://leaf-asrlm.in/api/variables")
variables = r.json()

# Create a summary DataFrame
df = pd.DataFrame(variables)
print(f"Total variables: {len(df)}")
print(f"\nBy group:")
print(df.groupby('group').size().sort_values(ascending=False))
print(f"\nSample:")
print(df[['field', 'label', 'group', 'data_min', 'data_max']].head(10).to_string(index=False))
const r = await fetch('/api/variables');
const variables = await r.json();

// Group by category
const byGroup = {};
variables.forEach(v => {
  (byGroup[v.group] = byGroup[v.group] || []).push(v);
});

Object.entries(byGroup).forEach(([group, vars]) => {
  console.log(`${group}: ${vars.length} variables`);
});

Get Variable Groups

Returns the list of variable group categories used to organize variables in the UI. Groups are the unique, non-empty values of the group column in the variable configuration.

GET /api/variable-groups

Response

200 OK - a JSON object with a groups array of group-name strings:

{
  "groups": [
    "Land & Agriculture",
    "Water",
    "Infrastructure",
    "Livestock",
    "People & Collectives",
    "Soil",
    "Climate"
  ]
}
Field Type Description
groups array of string Distinct group names from the variable configuration (order and exact set depend on the config sheet)

Errors

Status Body Cause
500 {"error": "<message>"} Failure loading the variable metadata

Example

curl https://leaf-asrlm.in/api/variable-groups
import requests

r = requests.get("https://leaf-asrlm.in/api/variable-groups")
groups = r.json()['groups']
print(f"Available groups: {', '.join(groups)}")

Get Variable Statistics

Returns min, max, and mean values for a specific variable column, computed from the block-level shapefile.

GET /api/variable-stats/{variable}

Parameters

Parameter In Type Required Description
variable path string Yes Variable column code (e.g. AD, WA, CF)

Block-level only

This route computes statistics from the block-level shapefile.

Response

200 OK:

{
  "min": 0.0,
  "max": 95.5,
  "mean": 42.3
}
Field Type Description
min number Minimum value across all blocks
max number Maximum value across all blocks
mean number Mean value across all blocks

Unknown or empty variables

If the variable name doesn't exist as a column in the shapefile, the endpoint returns the default values {"min": 0, "max": 100, "mean": 50} instead of an error. The same defaults are used for any of the three values that cannot be computed (e.g. an all-empty column).

Errors

Status Body Cause
500 {"error": "<message>"} Failure loading the shapefile

A non-existent variable code does not produce an error - it returns the default range above.

Example

curl https://leaf-asrlm.in/api/variable-stats/AD
import requests

# Check stats for multiple variables
variables = ['AD', 'WA', 'CF', 'CI']
for var in variables:
    r = requests.get(f"https://leaf-asrlm.in/api/variable-stats/{var}")
    stats = r.json()
    print(f"{var}: {stats['min']:.1f} - {stats['max']:.1f} (mean {stats['mean']:.1f})")
const variable = 'AD';
const r = await fetch(`/api/variable-stats/${variable}`);
const stats = await r.json();
console.log(`${variable}: ${stats.min} - ${stats.max} (avg ${stats.mean.toFixed(1)})`);