FIA Database¶
The FIA class is the main entry point for working with FIA data.
Overview¶
import pyfia
# Basic usage
db = pyfia.FIA("georgia.duckdb")
db.clip_by_state("GA")
# Use estimation functions
result = pyfia.volume(db, grp_by="SPCD")
Spatial Filtering¶
PyFIA supports spatial filtering using polygon boundaries:
with pyfia.FIA("southeast.duckdb") as db:
db.clip_by_state(37) # North Carolina
db.clip_most_recent(eval_type="VOL")
# Filter to custom region
db.clip_by_polygon("my_region.geojson")
# Join polygon attributes for grouping
db.intersect_polygons("counties.shp", attributes=["NAME"])
# Group estimates by polygon attribute
result = pyfia.tpa(db, grp_by=["NAME"])
See the Spatial Filtering Guide for detailed usage.
Class Reference¶
FIA
¶
Main FIA database class for working with Forest Inventory and Analysis data.
This class provides methods for loading FIA data from DuckDB databases, filtering by EVALID, and preparing data for estimation functions.
| ATTRIBUTE | DESCRIPTION |
|---|---|
db_path |
Path to the DuckDB database.
TYPE:
|
tables |
Loaded FIA tables as lazy frames.
TYPE:
|
evalid |
Active EVALID filter.
TYPE:
|
most_recent |
Whether to use most recent evaluations.
TYPE:
|
Initialize FIA database connection.
| PARAMETER | DESCRIPTION |
|---|---|
db_path
|
Path to FIA database (DuckDB or SQLite).
TYPE:
|
engine
|
Database engine ('duckdb', 'sqlite', or None for auto-detect).
TYPE:
|
Source code in src/pyfia/core/fia.py
load_table
¶
load_table(table_name: str, columns: Optional[List[str]] = None, where: Optional[str] = None) -> LazyFrame
Load a table from the FIA database as a lazy frame.
| PARAMETER | DESCRIPTION |
|---|---|
table_name
|
Name of the FIA table to load (e.g., 'PLOT', 'TREE', 'COND').
TYPE:
|
columns
|
Columns to load. Loads all columns if None.
TYPE:
|
where
|
Additional SQL WHERE clause to apply (without 'WHERE' keyword). Used for database-side filtering to reduce memory usage.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LazyFrame
|
Polars LazyFrame of the requested table. |
Source code in src/pyfia/core/fia.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
find_evalid
¶
find_evalid(most_recent: bool = True, state: Optional[Union[int, List[int]]] = None, year: Optional[Union[int, List[int]]] = None, eval_type: Optional[str] = None) -> List[int]
Find EVALID values matching criteria.
Identify evaluation IDs for filtering FIA data based on specific criteria.
| PARAMETER | DESCRIPTION |
|---|---|
most_recent
|
If True, return only most recent evaluations per state.
TYPE:
|
state
|
State FIPS code(s) to filter by.
TYPE:
|
year
|
End inventory year(s) to filter by.
TYPE:
|
eval_type
|
Evaluation type ('VOL', 'GRM', 'CHNG', 'ALL', etc.).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list of int
|
EVALID values matching the specified criteria. |
Source code in src/pyfia/core/fia.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
clip_by_evalid
¶
clip_by_evalid(evalid: Union[int, List[int]]) -> FIA
Filter FIA data by EVALID (evaluation ID).
This is the core filtering method that ensures statistically valid plot groupings by evaluation.
| PARAMETER | DESCRIPTION |
|---|---|
evalid
|
Single EVALID or list of EVALIDs to filter by.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FIA
|
Self for method chaining. |
Source code in src/pyfia/core/fia.py
clip_by_state
¶
clip_by_state(state: Union[int, List[int]], most_recent: bool = True, eval_type: Optional[str] = 'ALL') -> FIA
Filter FIA data by state code(s).
This method efficiently filters data at the database level by: 1. Setting a state filter for direct table queries 2. Finding appropriate EVALIDs for the state(s) 3. Combining both filters for optimal performance
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Single state FIPS code or list of codes.
TYPE:
|
most_recent
|
If True, use only most recent evaluations.
TYPE:
|
eval_type
|
Evaluation type to use. Default 'ALL' for EXPALL which is appropriate for area estimation. Use None to get all types.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FIA
|
Self for method chaining. |
Source code in src/pyfia/core/fia.py
clip_most_recent
¶
clip_most_recent(eval_type: str = 'VOL') -> FIA
Filter to most recent evaluation of specified type.
| PARAMETER | DESCRIPTION |
|---|---|
eval_type
|
Evaluation type ('VOL' for volume, 'GRM' for growth, etc.).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FIA
|
Self for method chaining. |
Source code in src/pyfia/core/fia.py
clip_by_polygon
¶
clip_by_polygon(polygon: Union[str, Path], predicate: str = 'intersects') -> FIA
Filter FIA plots to those within or intersecting a polygon boundary.
Uses DuckDB spatial extension for efficient point-in-polygon filtering. Supports Shapefiles, GeoJSON, GeoPackage, and GeoParquet formats.
| PARAMETER | DESCRIPTION |
|---|---|
polygon
|
Path to spatial file containing polygon(s). Supported formats: - Shapefile (.shp) - GeoJSON (.geojson, .json) - GeoPackage (.gpkg) - GeoParquet (.parquet with geometry) - Any format supported by GDAL/OGR
TYPE:
|
predicate
|
Spatial predicate for filtering: - 'intersects': Plots that intersect the polygon (recommended) - 'within': Plots completely within the polygon
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FIA
|
Self for method chaining. |
| RAISES | DESCRIPTION |
|---|---|
SpatialFileError
|
If the polygon file cannot be read or does not exist. |
SpatialExtensionError
|
If the DuckDB spatial extension cannot be loaded. |
NoSpatialFilterError
|
If no plots fall within the polygon boundary. |
Notes
- FIA plot coordinates (LAT/LON) are in EPSG:4326 (WGS84)
- The polygon file should use EPSG:4326 or will be transformed automatically
- Public FIA coordinates are fuzzed up to 1 mile for privacy, so precision below ~1 mile is not meaningful
- This filter combines with state and EVALID filters
Examples:
>>> with FIA("southeast.duckdb") as db:
... db.clip_by_state(37) # North Carolina
... db.clip_by_polygon("my_region.geojson")
... result = db.tpa()
>>> # Using a shapefile
>>> with FIA("data.duckdb") as db:
... db.clip_by_polygon("counties.shp")
... result = db.area()
Source code in src/pyfia/core/fia.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | |
intersect_polygons
¶
intersect_polygons(polygon: Union[str, Path], attributes: List[str]) -> FIA
Perform spatial join between plots and polygons, adding polygon attributes to plots for use in grp_by.
This method joins polygon attributes to FIA plots based on spatial intersection. The resulting attributes can be used as grouping variables in estimator functions.
| PARAMETER | DESCRIPTION |
|---|---|
polygon
|
Path to spatial file containing polygon(s). Supported formats: - Shapefile (.shp) - GeoJSON (.geojson, .json) - GeoPackage (.gpkg) - GeoParquet (.parquet with geometry) - Any format supported by GDAL/OGR
TYPE:
|
attributes
|
Polygon attribute columns to add to plots. These columns must exist in the polygon file and will be available for grp_by.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FIA
|
Self for method chaining. |
| RAISES | DESCRIPTION |
|---|---|
SpatialFileError
|
If the polygon file cannot be read or does not exist. |
SpatialExtensionError
|
If the DuckDB spatial extension cannot be loaded. |
ValueError
|
If requested attributes don't exist in the polygon file. |
Notes
- Plots that don't intersect any polygon will have NULL values
- If a plot intersects multiple polygons, the first match is used
- Attributes are available immediately for grp_by in estimators
- This method is independent of clip_by_polygon (can use both)
Examples:
>>> with FIA("southeast.duckdb") as db:
... db.clip_by_state(37) # North Carolina
... db.intersect_polygons("counties.shp", attributes=["NAME", "FIPS"])
... # Group TPA estimates by county
... result = tpa(db, grp_by=["NAME"])
>>> # Use multiple attributes for grouping
>>> with FIA("data.duckdb") as db:
... db.intersect_polygons("regions.geojson", ["REGION", "DISTRICT"])
... result = area(db, grp_by=["REGION", "DISTRICT"])
Source code in src/pyfia/core/fia.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 | |
get_plots
¶
Get PLOT table filtered by current EVALID, state, and spatial settings.
| PARAMETER | DESCRIPTION |
|---|---|
columns
|
Columns to return. Returns all columns if None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Filtered PLOT dataframe. |
Source code in src/pyfia/core/fia.py
get_trees
¶
Get TREE table filtered by current EVALID and state settings.
| PARAMETER | DESCRIPTION |
|---|---|
columns
|
Columns to return. Returns all columns if None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Filtered TREE dataframe. |
Source code in src/pyfia/core/fia.py
get_conditions
¶
Get COND table filtered by current EVALID and state settings.
| PARAMETER | DESCRIPTION |
|---|---|
columns
|
Columns to return. Returns all columns if None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Filtered COND dataframe. |
Source code in src/pyfia/core/fia.py
prepare_estimation_data
¶
Prepare standard set of tables for estimation functions.
This method loads and filters the core tables needed for most FIA estimation procedures, properly filtered by EVALID.
| PARAMETER | DESCRIPTION |
|---|---|
include_trees
|
Whether to include the TREE table. Set to False for area estimation which doesn't need tree data (saves significant memory on constrained environments).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, DataFrame]
|
Dictionary with filtered dataframes for estimation containing: 'plot', 'tree', 'cond', 'pop_plot_stratum_assgn', 'pop_stratum', 'pop_estn_unit'. If include_trees=False, 'tree' will be an empty DataFrame. |
Source code in src/pyfia/core/fia.py
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 | |