Code to automate building SU (Stratigraphic Unit) Sheets for the Tharros Archaeological Project. It turns shapefiles and 3D volumetrics into structured PDF reports and QGIS projects.
Into beautiful PDFs and QGS projects like this:
The pipeline does three main things:
-
Converts 3D SU geometry to 2D GIS shapefiles
Takes OBJ files from photogrammetry/volumetrics (e.g.SU_17001_top.obj) and produces projected polygon shapefiles (e.g.SU_17001_EPSG_32632.shp) for use in QGIS. -
Builds a QGIS map for each SU
For each stratigraphic unit it loads a base project, adds the relevant layers (drone basemap, orthophoto, DEM, contours, trench boundaries, architecture, SU polygon), styles them, and configures map extents and scale bars. -
Exports multi-page PDF “SU sheets”
Uses trench-specific layout templates (.qpt) to generate a PDF per SU with title, description, overview map, ortho map, DEM/contour map, and elevation legend—ready for archaeological documentation.
SU Top OBJ (optional) → 2D SU shapefile → Load QGIS project + layers
→ Clip DEM to SU → Generate contours → Fill layout template → Export PDF
- If the SU shapefile already exists (e.g. in
GIS_2025/3D_SU_Shapefiles/), it is used as-is. - If it does not exist, the script can create it from the corresponding
SU_*_top.objinVolumetrics_2025/SU Top OBJs/using Blender (headless) and QGIS processing (see Shapefile generation below).
For each SU, the code then:
- Loads the base QGIS project (e.g.
TARP_SU_Sheets_2025.qgs). - Clears existing layers and adds a drone-flight basemap.
- Adds the orthophoto for the given job ID (from the
Orthos/folder). - Adds trench overview boundary, trench boundaries, and architecture layers.
- Adds the SU polygon layer (from the shapefile).
- Clips the DEM (from
DEM/, matched by job ID) to the SU polygon. - Generates contours from the clipped DEM (default 2 cm interval) and adds them with a style.
- Adds the DEM (with high-contrast color ramp and optional hillshade) and elevation stats.
- Instantiates a SUSheet with a trench-specific layout template (e.g.
SU_Layout_Templates/SU_Template_17000.qpt). - Fills title, description, map extents, scale bars, and elevation legend; then exports the layout to PDF.
Errors for individual SUs can be caught and logged (e.g. to error_log.txt) so the batch continues.
| File | Purpose |
|---|---|
generate_su_sheets.py |
Entry point: starts QGIS, loops over (su, job_id) pairs, calls generate_SU_Sheet() for each, skips if the output PDF already exists, logs errors, then closes QGIS. Configure PATH, QGS_FILE_NAME, and the list of (su, job_id) here. |
qgs_su_sheets_utils.py |
Core logic: QGIS lifecycle (start_QGS / close_QGS), project setup, layer loading (ortho, DEM, contours, trench, architecture, SU shapefile), DEM styling and clipping, contour generation, SUSheet class (load .qpt, fill maps and labels, export PDF), and generate_SU_Sheet() which runs the full pipeline for one SU. |
generate_top_shp.py |
Converts one SU 3D OBJ to a 2D polygon shapefile: Blender (headless + BlenderGIS) exports a temp shapefile, then QGIS processing fixes geometry, dissolves, deletes holes, translates to project coordinates (e.g. EPSG:32632), adds Year/Trench/SU fields, and saves SU_<n>_EPSG_32632.shp. |
create_shp_files_in_bulk_from_volumetrics.py |
Batch job: starts QGIS, scans Volumetrics_2025/SU Top OBJs/ for SU_*.obj, and for each OBJ without an existing shapefile in GIS_2025/3D_SU_Shapefiles/ calls create_SU_shp_file(). Logs progress and errors (e.g. shp_error_log.txt). |
DrawMasks.py |
Minimal helper: loads a QGIS project (e.g. TARP_2025_SU_Template.qgs). Used for manual or legacy workflows. |
When an SU shapefile is missing, generate_top_shp.create_SU_shp_file() is used (from qgs_su_sheets_utils or from the bulk script). It:
-
Blender (headless)
Uses Blender with the BlenderGIS addon to import the OBJ and export a temporary polygon shapefile (POLYGONZ, obj-to-feature). Blender is expected under standard install paths (e.g.Program Files\Blender Foundationon Windows, orApplicationson macOS). A small script is written to~/SU_tool/Run BlenderGIS headless.pyand invoked with the OBJ path. -
QGIS processing
- Fix geometries → Dissolve → Delete holes → Fix geometries again.
- Save local-coordinate shapefile:
SU_<n>_LC.shp. - Translate by a fixed offset (e.g.
DELTA_X,DELTA_Y) and assign CRS EPSG:32632. - Add integer fields: Year, Trench (derived from SU number), SU.
- Save projected shapefile:
SU_<n>_EPSG_32632.shp.
So the “top” OBJ is effectively turned into a 2D footprint in project coordinates for use in the SU sheet maps and DEM clipping.
Paths are currently hard-coded or built from variables (e.g. PATH = "C:\\Users\\Photogrammetry"). The logic expects a structure along these lines:
- Base path (e.g.
PATH): root for photogrammetry/GIS data. PATH/AutomateSuSheetCreation/(or equivalent): script location; error logs (error_log.txt). Output PDFs are written toVolumetrics_<year>/<SU>.pdf.PATH/GIS_2025/3D_SU_Shapefiles/: SU polygon shapefiles (SU_<n>_EPSG_32632.shp).PATH/Volumetrics_2025/SU Top OBJs/: OBJ files (SU_<n>_top.obj) used when generating shapefiles.PATH/Orthos/: Orthophoto JPGs; filename pattern used to match job ID (e.g.*_<job_id>_*.jpg).DEM/: DEM GeoTIFFs; filename must containPgram_Job_<job_id>(e.g.Pgram_Job_707_...tif).GCP-Drone-Flight-2025.jpg: Basemap raster used in the project.SU_Layout_Templates/: One.qptper trench, e.g.SU_Template_17000.qpt,SU_Template_18000.qpt.Styles/: QML style files (e.g. contour, DEM hillshade, SU outlines, trench, architecture).- Trench/architecture shapefiles: e.g.
TARP 2025 Trench Boundaries 6-1-2025.shp,Architecture_2025.shp, and trench overview boundary (e.g.*_Overview_Zoom_Rough_Boundary.shp).
The QGIS project template (e.g. TARP_SU_Sheets_2025.qgs) and QGIS install path (e.g. C:\Program Files\QGIS 3.40.8) are set in the scripts; adjust these for your machine and project.
The SUSheet class in qgs_su_sheets_utils.py assumes a multi-page layout template with:
- Page 1: DEM map, Ortho map, Overview map; title (e.g. “Trench 17000 • SU 17001”), description, scale bars, elevation legend (min/max).
- Page 2: Overview map (and scale bar).
- Page 3: Ortho map (and scale bar).
- Page 4: DEM map and elevation legend.
It looks up layout items by display name (e.g. “DEM Page 1”, “Ortho Page 1”, “Overview Page 1”, “Scalebar DEM Page 1”, “Lower/ Higher Elevation Page 1/4”, “Description:”). It sets map extents from the SU layer (with optional buffer), links scale bars to the correct map and width, applies layer visibility and styles (overview vs ortho vs DEM), then exports the layout to PDF.
- Python 3 with dependencies in
requirements.txt(PyQt5; used by QGIS Python bindings). - QGIS 3.x (e.g. 3.40.8) with its Python environment; scripts are intended to be run with QGIS’s Python (e.g.
python-qgis-ltr.bator similar) so thatqgis.coreandprocessingare available. The code appends QGIS paths tosys.pathfor use outside the QGIS GUI. - Blender with BlenderGIS addon (only if you generate shapefiles from OBJ). Blender is used in headless mode; the addon name in the command is
BlenderGIS-master.
-
Single SU sheet (with existing shapefile)
Ingenerate_su_sheets.py, setPATH,QGS_FILE_NAME, and a single(su, job_id)in the loop, then run with QGIS Python:# Example (adjust to your QGIS Python) python generate_su_sheets.py -
Batch SU sheets
Edit the list of(su, job_id)ingenerate_su_sheets.py(and ensure orthos/DEMs exist for each job ID). Run the same script; it will skip SUs that already have an output PDF and log failures. -
Bulk shapefile creation from OBJs
SetPATHandYEARincreate_shp_files_in_bulk_from_volumetrics.py, then run with QGIS Python. It will create only missingSU_*_EPSG_32632.shpfiles fromVolumetrics_2025/SU Top OBJs/. -
DrawMasks
RunDrawMasks.pywith QGIS Python to load the template project; adjust the project path inside the script if needed.
Every season the stone-by-stone architecture layer (Architecture_<year>.shp) — every
individual rock traced as its own polygon — was digitized by hand from the drone ortho in
QGIS. Thousands of stones, hours of monotonous clicking. This repo now automates that first
pass: point it at the georeferenced drone ortho and it identifies and outlines every rock into
a reviewable GIS layer. Rare features (e.g. cisterns) are still segmented by hand — there
weren't enough training examples to learn them.
We fine-tuned Segment Anything (SAM) on three seasons of hand-labelled stones (2023–2025) and compared three ways of proposing where the stones are:
| Model | How stones are found | Result |
|---|---|---|
yolo_sam — shipped ✅ |
a trained YOLO detector boxes each stone, SAM traces it | ~2.8k rocks, ≈ the human count — clean |
rgb_sam |
SAM auto-grids points over the whole tile | ~13–17k polygons — over-segments (ground, rubble, veg) |
rgb_dem_sam |
same, with a DEM hillshade blended into the input | ~13–17k — height didn't beat plain RGB |
We chose yolo_sam: the detector front-end keeps SAM on actual stones, so the layer lands
near the human count instead of drowning in noise. The images tell the rest — same scene, three models:
yolo_sam — shipped ✅ · YOLO detector → SAM traces each box. Rocks only, ~2.8k polygons ≈ the human count. Clean.

rgb_sam · SAM auto-grids points over everything. ~13–17k polygons — over-segments ground, rubble, and vegetation. Noisy.

rgb_dem_sam · same as rgb_sam, with a DEM hillshade blended into SAM's input. ~13–17k polygons — height didn't beat plain RGB.

Run it on a new flight:
"C:/Users/Photogrammetry/sv/Scripts/python.exe" \
SAM_prototype/run_rock_mask.py --model yolo_sam <drone_ortho.tif> SAM_prototype/out.gpkgModel weights for the shipped yolo_sam are large binaries kept out of git. After
downloading, drop each at the exact path the code reads:
- YOLO detector →
SAM_prototype/yolo_runs/feature_detector/weights/best.pt - Fine-tuned SAM decoder →
SAM_prototype/sam_decoder_multiyear.pth - Base SAM ViT-H (auto-downloaded on first run) →
~/.cache/torch/hub/checkpoints/sam_vit_h_4b8939.pth
Download the trained weights (for all three models) from Google Drive: https://drive.google.com/drive/folders/1ynm_QLasgrduimek-i76Cc30tHWEjyY7?usp=drive_link
Full model card — architecture, training, metrics, and the DEM-fusion experiment — is in
SAM_prototype/ROCK_MODEL.md; the per-season operational steps live
in CLAUDE.md.
- More detailed user documentation.
- Portrait view option for SU sheets.

