shptreedump
Overview
shptreedump (code/shapelib-1.3.0a2/shptreedump.c) is a command-line spatial indexing, inspection, and query utility authored by Frank Warmerdam that exercises the shptree quadtree API (SHPCreateTree, SHPTreeTrimExtraNodes, SHPWriteTree, SHPTreeFindLikelyShapes, and SHPSearchDiskTree).
It serves three primary functions:
- ASCII Tree Visualization: Builds an in-memory quadtree for a
.shpfile and prints a hierarchical, indented S-expression dump of everySHPTreeNode, its bounding box, and the shape IDs (or full vertex geometries with-v) stored at that node. - Binary
.qixIndex Generation (-o): Builds and trims a quadtree for a.shpfile and writes it out to a binary.qixspatial index file viaSHPWriteTree(). - Bounding-Box Spatial Search (
-search): Queries either an in-memory quadtree (built on-the-fly from<shp_file>) or an existing on-disk.qixindex file (-i <index.qix>) for all shapes intersecting<xmin> <ymin> <xmax> <ymax>.
Usage
shptreedump [-maxdepth <n>] [-search <xmin> <ymin> <xmax> <ymax>] \
[-v] [-o <indexfilename.qix>] [-i <indexfilename.qix>] \
<shp_file>
Command-Line Options
| Flag | Arguments | Description |
|---|---|---|
-maxdepth | <n> | Maximum depth of the constructed quadtree (default 0, which instructs SHPCreateTree() to auto-select a depth up to MAX_DEFAULT_TREE_DEPTH = 12 targeting ~8 shapes per leaf node). |
-v | (none) | Verbose / Expand Shapes: When dumping the tree hierarchy, reads and prints the full ShapeId, Min/Max bounds, and Vertex[i] coordinates of every shape (EmitShape()) instead of only printing integer shape IDs. |
-o | <indexfilename.qix> | Writes the constructed and trimmed quadtree to a binary .qix spatial index file via SHPWriteTree() instead of dumping ASCII text to stdout. |
-search | <xmin> <ymin> <xmax> <ymax> | Executes a 2D spatial bounding-box search. Validates xmin <= xmax and ymin <= ymax. |
-i | <indexfilename.qix> | Used together with -search: searches the pre-built binary .qix file directly on disk via SHPSearchDiskTree() and prints Result: <id1> <id2> ... without needing to open <shp_file>. |
<shp_file> | (positional) | Path/basename of the input ESRI Shapefile (required unless -search is combined with -i <indexfilename.qix>). |
Internal Two-Phase Spatial Search (SHPTreeNodeSearchAndDump)
When -search <xmin> <ymin> <xmax> <ymax> is run against <shp_file> (without -i):
- Phase 1 β Quadtree Node Filtering (
SHPTreeFindLikelyShapes):- Collects a sorted list (
panHits) ofnShapeCountcandidate shape IDs stored in anySHPTreeNodewhose node bounding box overlaps the search rectangle.
- Collects a sorted list (
- Phase 2 β Exact Shape Bounding-Box Verification (
SHPCheckBoundsOverlap):- Because a quadtree nodeβs bounding box is larger than the individual shapes stored within it,
SHPTreeNodeSearchAndDump()reads each candidatepsObject = SHPReadObject(hTree->hSHP, panHits[i])and testsSHPCheckBoundsOverlap(padfBoundsMin, padfBoundsMax, &(psObject->dfXMin), &(psObject->dfXMax), hTree->nDimension):- If disjoint: prints
Shape <id>: not in area of interest, but fetched.(illustrating a Phase 1 false positive). - If overlapping: prints
Shape <id>: appears to be in area of interest..
- If disjoint: prints
- Because a quadtree nodeβs bounding box is larger than the individual shapes stored within it,
Example Workflows
# 1. Dump an ASCII representation of a shapefile's quadtree (limited to 4 levels)
shptreedump -maxdepth 4 nav_tracks.shp
# 2. Generate a binary .qix spatial index file and query it on disk
shptreedump -o nav_tracks.qix nav_tracks.shp
shptreedump -i nav_tracks.qix -search -63.60 44.60 -63.50 44.70