Macros expand shorthand syntax into full SQL. Persistent Areas of Interest unlock spatial clipping, interpretation runs, and DocBook reports — all through the SDA REST endpoint. This guide covers every macro, function, and service call, with live examples.
The advanced SDA API extends plain SQL with macros, spatial functions, and service endpoints for areas of interest, interpretations, and reports.
Tilde-delimited statements that expand into DECLARE, table definitions, and multi-step spatial operations — making complex queries concise.
Define an area once — from an SSA, a mukey list, a WKT polygon, a shapefile, or a GeoJSON — and reuse it across spatial clips, interpretation runs, and map renders.
Server-side functions return areasymbol, mukey, or geometry for any polygon sent as WKT — both WGS84 and Web Mercator projections supported.
A macro is a shorthand statement the SDA server expands into full T-SQL before execution. Every macro is surrounded by tilde characters with no embedded whitespace, and variable names are prefixed with @.
The three rules that govern every macro statement
Macros follow three strict rules:
~MacroName(args)~@ — e.g., @aoi, @aoiid~DeclareGeometry(@aoi)~ expands to DECLARE @aoi geometry; — the server does the substitution before running your SQL.These macros declare a single SQL variable of the specified type. They replace verbose DECLARE @var type; syntax with a one-liner.
Integer, float, decimal, date, and time variable declarations
| Macro | Expands To | Notes |
|---|---|---|
| ~DeclareBigint(@v)~ | DECLARE @v bigint; | 64-bit integer |
| ~DeclareInt(@v)~ | DECLARE @v int; | Most common integer |
| ~DeclareSmallint(@v)~ | DECLARE @v smallint; | -32768 to 32767 |
| ~DeclareTinyint(@v)~ | DECLARE @v tinyint; | 0 to 255 |
| ~DeclareFloat(@v)~ | DECLARE @v float; | Double precision |
| ~DeclareReal(@v)~ | DECLARE @v real; | Single precision |
| ~DeclareBit(@v)~ | DECLARE @v bit; | Boolean 0/1 |
| ~DeclareDecimal(@v,p,s)~ | DECLARE @v decimal(p,s); | Fixed precision & scale |
| ~DeclareNumeric(@v,p,s)~ | DECLARE @v numeric(p,s); | Alias for decimal |
| ~DeclareDate(@v)~ | DECLARE @v date; | Date only |
| ~DeclareDatetime(@v)~ | DECLARE @v datetime; | Date + time |
| ~DeclareTime(@v)~ | DECLARE @v time; | Time only |
Char, varchar, geometry, and geography variable declarations
| Macro | Expands To | Notes |
|---|---|---|
| ~DeclareChar(@v,n)~ | DECLARE @v char(n); | Fixed-length string |
| ~DeclareVarchar(@v,n)~ | DECLARE @v varchar(n); | Variable-length string |
| ~DeclareVarchar(@v,max)~ | DECLARE @v varchar(max); | Unlimited text |
| ~DeclareGeometry(@v)~ | DECLARE @v geometry; | Planar spatial — Web Mercator |
| ~DeclareGeography(@v)~ | DECLARE @v geography; | Geodetic — WGS84 |
These macros declare an in-memory table variable with a predefined schema — essential for passing mukey lists into spatial functions and accumulating geometry results.
Integer and varchar table variables for passing lists to spatial functions
| Macro | Schema Created |
|---|---|
| ~DeclareIntTable(@t)~ | Single column: i int |
| ~DeclareVarchar255Table(@t)~ | Single column: s varchar(255) |
Two-column tables pairing an ID or string key with a spatial geometry/geography column
| Macro | Schema: (col1, col2) |
|---|---|
| ~DeclareIdGeomTable(@t)~ | id int, geom geometry |
| ~DeclareIdGeogTable(@t)~ | id int, geog geography |
| ~DeclareVarchar255GeomTable(@t)~ | s varchar(255), geom geometry |
| ~DeclareVarchar255GeogTable(@t)~ | s varchar(255), geog geography |
~DeclareIdGeomTable(@intersected)~ is the input container for ~GetClippedMapunits(...)~. Then ~DeclareIdGeogTable(@areas)~ holds the converted geodetic result for STArea() calculations.These macros perform the heavy spatial work — clipping soil polygons to your AOI, converting between geometry and geography, and splitting multi-part geometries.
Clip soil polygons to a WKT boundary and compute area in square meters
| Macro | Purpose |
|---|---|
| ~GetClippedMapunits(@aoi,polygon,geo,@out)~ | Clip mupolygon to @aoi; put results in @out (id=mukey, geom=clipped geometry, WGS84) |
| ~GetClippedMapunits(@aoi,polygon,proj,@out)~ | Same, but in Web Mercator projection |
| ~GetGeogFromGeomWgs84(@in,@out)~ | Convert geometry table (WGS84) → geography table for STArea() in m² |
| ~SplitIdGeomTable(@in,@out)~ | Split multi-part geometries into individual rows |
STArea() returns square meters. Multiply by 0.000247105 to get acres.Three macros create a persistent area of interest server-side and return an @aoiid integer you reuse in all subsequent queries, interpretation runs, and report requests.
From a survey area symbol, a mukey list, or an existing WSS AOI
| Macro | Creates pAOI From |
|---|---|
| ~CreateAoiFromSsa(@ssa,@aoiid,@message)~ | A soil survey area areasymbol (e.g. 'CA795') |
| ~CreateAoiFromMukeyList(@mukeyList,@aoiid,@message)~ | A ~DeclareIntTable~ populated with mukeys |
| ~CreateAoiFromWssAoi(@wAoiId,@pAoiId,@message)~ | An existing Web Soil Survey transient AOI ID |
A persistent Area of Interest lives on the SDA server and is referenced by its numeric aoiid. Four creation methods cover every use case — from a simple survey area to a GeoJSON FeatureCollection with filter expressions.
REST endpoint patterns for all four pAOI types
GeoJSON supports a filter parameter (SQL WHERE clause) and a partname template like [PLU Number],[Id];{0} ({1}) for composing part labels from feature properties.
All server-side functions available once you have an aoiid
| Function | Returns |
|---|---|
| SDA_Get_Aoi_By_AoiId(@id) | AOI metadata: acres, creation method, multipart flag, geometry |
| SDA_Get_AoiMapunit_By_AoiId(@id) | AoiId, AoiMapunitId, MapUnitKey |
| SDA_Get_AoiPart_By_AoiId(@id) | Part-level metadata: name, acreage, geometry (aoicoords pAOIs only) |
| SDA_Get_AoiSoilMapunitPolygon_By_AoiId(@id) | Clipped polygon geometries per map unit |
| SDA_Get_AoiSoilMapunitLine_By_AoiId(@id) | Clipped line geometries |
| SDA_Get_AoiSoilMapunitPoint_By_AoiId(@id) | Point features |
| SDA_Get_AoiSoilThematicMap_By_AoiId(@id) | Interpretation thematic maps stored with this AOI |
| SDA_Get_AoiSoilThematicMapRating_By_AoiId(@id) | Per-mukey ratings with color strings |
| SDA_Get_AoiSld_By_AoiId(@id) | Styled Layer Descriptor XML for WMS rendering |
_And_AoiPartId(@id, @partId) to any function name to filter results to a single part of a multipart AOI.These server-side table-valued functions accept a WKT polygon and return areasymbol, mukey, or geometry — no macro required. Available in both WGS84 (WktWgs84) and Web Mercator (WktWm) flavors.
Identify which survey areas and map units overlap a polygon
| Function | Returns |
|---|---|
| SDA_Get_Areasymbol_from_intersection_with_WktWgs84(wkt) | areasymbol of overlapping survey areas |
| SDA_Get_Areasymbol_from_intersection_with_WktWm(wkt) | Same, Web Mercator coordinates |
| SDA_Get_Mukey_from_intersection_with_WktWgs84(wkt) | mukey of overlapping map units |
| SDA_Get_Mukey_from_intersection_with_WktWm(wkt) | Same, Web Mercator |
| SDA_Get_AreasymbolWKTWgs84_from_Areasymbol(ssa) | Boundary WKT of a survey area in WGS84 |
| SDA_Get_MupolygonWktWgs84_from_Mukey(mukey) | Polygon WKT for a specific mukey |
| SDA_Get_MupolygonWktWgs84_from_MukeyTable(@table) | Polygons for a list of mukeys |
Return actual geometry WKT for map unit polygons, lines, and points
| Function | Geometry Type |
|---|---|
| SDA_Get_MupolygonWktWgs84_from_Mukey(mukey) | Polygon — single mukey |
| SDA_Get_MupolygonWktWm_from_Mukey(mukey) | Polygon — single mukey, Web Mercator |
| SDA_Get_MupolygonWktWgs84_from_MukeyTable(@t) | Polygon — list of mukeys |
| SDA_Get_MupointWktWgs84_from_Mukey(mukey) | Miscellaneous area points |
| SDA_Get_MupointWktWgs84_from_MukeyTable(@t) | Points — list of mukeys |
| SDA_Get_MulineWktWgs84_from_Mukey(mukey) | Linear features |
| SDA_Get_MulineWktWgs84_from_MukeyTable(@t) | Lines — list of mukeys |
| SDA_Get_Sapolygonkey_from_intersection_with_WktWgs84(wkt) | Survey area polygon key |
| SDA_Get_Mupolygonkey_from_intersection_with_WktWgs84(wkt) | Map unit polygon key |
| SDA_Get_Featlinekey_from_intersection_with_WktWgs84(wkt) | Feature line key |
| SDA_Get_Featpointkey_from_intersection_with_WktWgs84(wkt) | Feature point key |
SDA_Get_[Object]Wkt[Proj]_from_[Input](arg). Swap Wgs84↔Wm to switch coordinate systems. Append Table to the source name to accept a table variable instead of a scalar.Interpretations are derived ratings — computed from soil properties by predefined rules. You request a catalog of available interpretations, configure rule parameters, and receive an interpresultid linking results back to your pAOI.
Discover which interpretations are available for your AOI
| usecategoryid | Category |
|---|---|
| 0 | All Uses |
| 4 | Cropland |
| 6 | Forestland |
| 7 | Horticulture |
| 9 | Hayland / Pastureland |
| 10 | Rangeland |
| 11 | Recreation |
| 16 | Urban Uses |
The response JSON lists folders and attributekey values for every interpretation in the selected category. Pass an attributekey to getruledata to see configurable parameters.
Configure rule parameters and receive a result ID for thematic mapping
Response: {"interpresultid": "504"} — use this ID to query SDA_Get_AoiSoilThematicMapRating_By_AoiId for per-mukey ratings and colors.
Upload your own ratings table to attach a custom thematic map to a pAOI
When predefined rules don't fit your use case, populate a custom rating table and load it as an ad-hoc interpretation:
| Macro | Purpose |
|---|---|
| ~DeclareAdHocRatingTable(@t)~ | Declares table: (MapUnitKey int, MapUnitRatingString varchar(255), MapUnitRatingNumeric numeric(17,6), RgbString varchar(11)) |
| ~DeclareAdHocLegendTable(@t)~ | Declares table: (sequence int, LegendText varchar(255), RgbString varchar(11)) |
| ~LoadAdHocInterpretation(@pAoiId,@ratings,@interpresultid,@msg)~ | Loads ratings (no legend) |
| ~LoadAdHocInterpretation(@pAoiId,@ratings,@legend,@interpresultid,@msg)~ | Loads ratings + custom legend |
Reports produce DocBook XML documents matching Web Soil Survey's catalog. Results are returned immediately — not persisted — so the calling application is responsible for transforming the XML to a presentation format.
Catalog, describe, configure, and run soil reports for a pAOI
| Request Pattern | Returns |
|---|---|
service=report&request=getusecategories | JSON list of use category IDs and names |
service=report&request=getcatalog&aoiid=N | JSON folder tree of report names and IDs |
service=report&request=getdescription&reportid=N | DocBook XML description of the report |
service=report&request=getreportdata&aoiid=N&reportid=N | JSON shortformdata template with defaults |
service=report&request=getreport&shortformdata={...} | DocBook XML report for the configured parameters |
service=report&request=getreport&aoiid=N&reportid=N | Report with all default parameters |
| reportid | Report Name |
|---|---|
| 461 | Component Description (Nontechnical) |
| 17 | Dwellings and Small Commercial Buildings |
| 33 | Taxonomic Classification of the Soils |
| 119 | Map Unit Description (Brief, Generated) |
| 6 | Irrigated and Nonirrigated Yields by Map Unit Component |
Primary sources from the USDA Soil Data Access team.
Official SDA macros, pAOI, interpretations, and reports documentation. Source for all content on this page.
sdmdataaccess.nrcs.usda.gov ↗Detailed walkthrough of spatial functions, WKT patterns, AOI creation, and projection handling.
Download PDF ↗Run macro queries, multi-step pAOI workflows, and queries using temp tables — the full SDA query interface.
Open SDA ↗Run simple (single-SELECT, no macro) SDA queries directly in the browser with live results and CSV export.
Open Query Lab →