Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Changelog

## [Version 3.0.2]
## [Version 3.0.3]

### What changed and why

Version 3 reworks the export and validation stages of the workflow to support new output formats (3D shapefiles, GA Portal / Earth Sciences format) and stricter interpretation validation against confidence, contact-type, interpretation-basis, operator, date, and ASUD geological Era lookups. Export outputs are now consolidated into a single `export/` directory instead of being written in `SORT/`.

Version 3 also introduces a new Linear Geology workflow for generating linear geology polygon shapefiles from AEM flight-line path files and geological map-data polygon layers.

All new CLI commands and usage examples are documented in [README.rst](README.rst).

### New export outputs
Expand Down Expand Up @@ -42,6 +44,7 @@ All new CLI commands and usage examples are documented in [README.rst](README.rs
- **Output file locations**: export outputs (CSV, MDC/MDCH, Earth Sciences, 3D shapefiles) are now written to a new `export/` directory instead of `SORT/`. Scripts that read output from `SORT/` will need to look in `export/` instead.
- **MDC/MDCH metadata field changes**: `*metadata*Line` renamed to `*metadata*SURVEY_LINE`; `*metadata*BoundaryNm` removed; `*metadata*Organization` corrected to `*metadata*Organisation`; `OvrStrtCod`/`UndStrtCod` renamed to `OvrStratNo`/`UndStratNo`; `WithinType` removed; new fields added: `OverAge`, `UnderAge`, `ContactTyp`, `HydStrtType`, `HydStrConf`, `BOMNAFUnt`, `BOMNAFNo`, `Date`. These label names are not cosmetic — in v2 several metadata header labels did not line up with the data value actually written under them (e.g. the `BoundaryNm` label was printing what was really the `BoundConf` value, one field out of place); the field list above reflects the corrected label/data alignment, so v2 output produced under the old labels should not be assumed correct and downstream consumers should re-derive any values they depend on from the field position, not the old label name.
- **Validation summary file renamed**: `AEM_validation_summary_{date}.txt` is now `ASUD_validation_summary_{date}.txt`.
- Added a new `linear-geology` CLI command for generating linear geology outputs from geological map datasets and AEM flight-line path files.

### New required input files

Expand Down
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,28 @@ split file Yes None
============================= ============== =============== ================================================ =============================================


Linear Geology
-----------------------

.. code-block:: bash

aemworkflow linear_geology --i "{input_directory}" --o "{output_directory}" --map-data {Shapefile1 stem} {Column index} --map-data {Shapefile2 stem} {Column index} ...

The ``--map-data`` option requires two values:
1. **Shapefile stem** (without the ``.shp`` extension)
2. **Column number** used to group the joined geology attributes

**Parameter examples:**

============================= ============== =============== ================================================ =============================================
Argument Required Default Options Notes
============================= ============== =============== ================================================ =============================================
input directory Yes None A non zipped folder containing required files
output directory Yes None
map data entries Yes None multiple --map-data <stem> <column> Shapefile name without .shp and column number
============================= ============== =============== ================================================ =============================================



Useful Links
------------
Expand Down
15 changes: 15 additions & 0 deletions aemworkflow/aemworkflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .conversion import main as conversion
from .exports import main as exports
from .interpretation import main as interpretation
from .linear_geology import run_linear_geology
from .pre_interpretation import main as pre_interpretation
from .validation import main as validation

Expand Down Expand Up @@ -116,3 +117,17 @@ def export(input_directory, output_directory, boundary_filename, split_filename,
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)


@cli.command()
@click.option("--i", "input_directory", type=click.Path(exists=True), required=True)
@click.option("--o", "output_directory", type=click.Path(), required=True)
@click.option("--map-data", "entries", type=(str, int), multiple=True, required=True, metavar="STEM COLUMN",
help=("Map shapefile stem and one-based joined-attribute column number."))
def linear_geology(input_directory, output_directory, entries):
try:
run_linear_geology(input_directory, output_directory, entries)
click.echo("Completed linear geology processing")
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
221 changes: 221 additions & 0 deletions aemworkflow/linear_geology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import os
from pathlib import Path
from typing import List, Tuple

import geopandas as gpd
from loguru import logger

from aemworkflow.utilities import get_ogr_path, run_command


def path_to_point_gmt(path_file: str, output_gmt: str, logger_session=logger) -> None:
"""
Translate all_pts_i_gmt.awk: convert a .path.txt file into a GMT point file.
Uses cols 7,8 (geographic lon/lat in EPSG:4283) — these are already in the
target CRS and match the polygon shapefiles, avoiding projection zone errors.
Columns: col1=linenum, col2=flib, col7=longitude, col8=latitude.
"""
logger_session.info(f"Converting path file to GMT: {Path(path_file).name}")

with open(output_gmt, 'w') as out:
out.write("# @VGMT1.0 @GPOINT\n")
out.write("# @Je4283\n")
out.write("# @Nlinenum|flib|date|Survey|Company|Status\n")
out.write("# @Tinteger|integer|integer|string|string|string\n")
out.write("# FEATURE_DATA\n")

with open(path_file, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) < 8:
continue
linenum = parts[0]
flib = parts[1]
lon = parts[6]
lat = parts[7]
out.write(">\n")
out.write(f"# @D{linenum}|{flib}\n")
out.write(f"{lon} {lat}\n")

logger_session.info(f"GMT point file written: {Path(output_gmt).name}")


def path_gmt_to_shp(gmt_file: str, shp_file: str, logger_session=logger) -> None:
"""Convert a GMT point file to a shapefile (no reprojection needed)."""
run_command([get_ogr_path(), "-f", "ESRI Shapefile", shp_file, gmt_file])
logger_session.info(f"Point shapefile written: {Path(shp_file).name}")


def spatial_join(point_shp_dir: str, polygon_shp: str, output_dir: str,
logger_session=logger) -> None:
"""
Translate test_point_poly_join_mgn2.py: spatial join all point shapefiles in
point_shp_dir against polygon_shp, writing *_new.shp to output_dir.
"""
os.makedirs(output_dir, exist_ok=True)
polygons = gpd.read_file(polygon_shp)
logger_session.info(f"Loaded polygon shapefile: {Path(polygon_shp).name} ({len(polygons)} features)")

for filename in sorted(os.listdir(point_shp_dir)):
if not filename.endswith(".shp"):
continue
points = gpd.read_file(os.path.join(point_shp_dir, filename))
points = points.to_crs(polygons.crs)
joined = gpd.sjoin(points, polygons, how="left", predicate="within")
new_filename = f"{filename.split('.')[0]}_new.shp"
joined.to_file(os.path.join(output_dir, new_filename))
logger_session.info(f"Spatial join written: {new_filename}")


def build_polygon_gmt(joined_shp: str, output_gmt: str, column_index: int,
logger_session=logger) -> None:
"""
Translate lin_geology_5p_clm.awk: group consecutive points by the value at
column_index and write 5-point rectangular polygons (height=20 image units).
"""
logger_session.info(f"Building polygon GMT: {Path(joined_shp).name}, col={column_index}")

stem = Path(joined_shp).stem
path_surfgeo_gmt = os.path.join(str(Path(joined_shp).parent), f"{stem}_path_SurfGeo.gmt")
run_command([get_ogr_path(), "-f", "GMT", path_surfgeo_gmt, joined_shp])

h1 = h2 = ""
first = True
current_key = None
x_pts: list = []
header_written = False

with open(path_surfgeo_gmt, 'r') as fin, open(output_gmt, 'w') as fout:
def _write_polygon(x_list: list) -> None:
if not x_list:
return
fout.write(f"{x_list[-1]} 0\n")
fout.write(f"{x_list[-1]} 20\n")
fout.write(f"{x_list[0]} 20\n")
fout.write(f"{x_list[0]} 0\n")

for raw in fin:
line = raw.strip()
if not line:
continue
parts = line.split()
if len(parts) >= 2 and parts[1].startswith("@Nlinenum"):
h1 = line
continue
if len(parts) >= 2 and parts[1].startswith("@Tinteger"):
h2 = line
continue
if len(parts) >= 2 and "@D" in parts[1]:
fields = line.split("|")
key = fields[column_index - 1] if len(fields) >= column_index else None

if not header_written:
fout.write("# @VGMT1.0 @GPOLYGON\n")
fout.write("# @NId\n")
fout.write(f"{h1}\n")
fout.write(f"{h2}\n")
fout.write("# FEATURE_DATA\n")
header_written = True

if first:
fout.write(">\n")
fout.write(f"{line}\n")
fout.write("# @P\n")
x_pts = [fields[1]]
current_key = key
first = False
else:
if key == current_key:
x_pts.append(fields[1])
else:
_write_polygon(x_pts)
fout.write(">\n")
fout.write(f"{line}\n")
fout.write("# @P\n")
x_pts = [fields[1]]
current_key = key

if x_pts:
_write_polygon(x_pts)

logger_session.info(f"Polygon GMT written: {Path(output_gmt).name}")


def find_map_data_dir(input_directory: str) -> str:
"""Return the map_data subfolder path, case-insensitive. Empty string if not found."""
for child in Path(input_directory).iterdir():
if child.is_dir() and child.name.lower() == "map_data":
return str(child)
return ""


def run_linear_geology(input_directory: str, output_directory: str,
entries: List[Tuple[str, int]],
logger_session=logger) -> bool:
"""
Run the full linear geology pipeline.

Args:
input_directory: contains *.path.txt and map_data/ folder
output_directory: root output dir; results go under lin_geo/
entries: list of (shapefile_stem, column_index) from the UI selection
"""
try:
map_data_dir = find_map_data_dir(input_directory)
if not map_data_dir:
logger_session.error("No map_data folder found in input directory.")
return False

path_files = sorted(Path(input_directory).glob("*.path.txt"))
if not path_files:
logger_session.error("No .path.txt files found in input directory.")
return False

lin_geo_root = os.path.join(output_directory, "lin_geo")

# Stage 1: convert all path files to point GMT + SHP
point_shp_dir = os.path.join(lin_geo_root, "_point_shp")
os.makedirs(point_shp_dir, exist_ok=True)

for path_file in path_files:
stem = path_file.stem.split(".")[0]
gmt_out = os.path.join(point_shp_dir, f"{stem}.gmt")
shp_out = os.path.join(point_shp_dir, f"{stem}.shp")
path_to_point_gmt(str(path_file), gmt_out, logger_session=logger_session)
path_gmt_to_shp(gmt_out, shp_out, logger_session=logger_session)

# Stage 2 + 3: per selected map dataset
for map_stem, col_idx in entries:
polygon_shp = os.path.join(map_data_dir, f"{map_stem}.shp")
if not Path(polygon_shp).is_file():
logger_session.warning(f"Map shapefile not found, skipping: {map_stem}.shp")
continue

logger_session.info(f"Processing map dataset: {map_stem} (column {col_idx})")

joined_dir = os.path.join(lin_geo_root, map_stem, "joined")
poly_dir = os.path.join(lin_geo_root, map_stem, "polygons")
os.makedirs(joined_dir, exist_ok=True)
os.makedirs(poly_dir, exist_ok=True)

spatial_join(point_shp_dir, polygon_shp, joined_dir, logger_session=logger_session)

for joined_shp in sorted(Path(joined_dir).glob("*_new.shp")):
nm = joined_shp.stem.replace("_new", "")
poly_gmt = os.path.join(poly_dir, f"{nm}_{map_stem}_pl.gmt")
poly_shp = os.path.join(poly_dir, f"{nm}_{map_stem}.shp")
build_polygon_gmt(str(joined_shp), poly_gmt, col_idx,
logger_session=logger_session)
run_command([get_ogr_path(), "-f", "ESRI Shapefile", poly_shp, poly_gmt])
logger_session.info(f"Polygon shapefile written: {Path(poly_shp).name}")

logger_session.info("Linear geology pipeline completed.")
return True

except Exception as e:
logger_session.error(f"Linear geology pipeline failed: {e}")
return False


if __name__ == "__main__":
run_linear_geology()
Comment thread
TehreemGA marked this conversation as resolved.
Dismissed
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "aemworkflow"
version = "3.0.0"
version = "3.0.3"
authors = [
{ name="Geoscience Australia", email="toolkits@ga.gov.au" },
]
Expand Down
Loading
Loading