#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Georeference HoliCity (London) LAS tiles into a real-world projected CRS. Role in the pipeline: HoliCity point clouds are sampled (via CloudCompare) from FBX meshes in a local coordinate frame with no spatial reference. This script geo-registers the four 500x500 m tiles (NW/NE/SW/SE) of a HoliCity block onto the London map so they line up with satellite imagery before tiling. Method: - The WGS84 latitude/longitude of the NW tile's top-left (north-west) corner is known and hard-coded below (NW_LAT/NW_LON; west longitude is negative). - That anchor is projected from WGS84 (EPSG:4326) into the target projected CRS (default EPSG:27700, OSGB36 / British National Grid). - The remaining tiles are placed by a fixed 500 m east/south offset. - For each tile, the local top-left corner (minX, maxY) is read from the LAS header bounding box, and a planar XY translation is computed to move that corner onto its target projected coordinate. Inputs: The four LAS files listed in INPUT_FILES, located in the current directory. Outputs: For each input, a translated copy named *_georef.las with the target EPSG written into its header. External tools: laspy (LAS I/O), pyproj (CRS transform), numpy. """ import os from pathlib import Path import laspy import numpy as np from pyproj import Transformer # ==== Parameters to confirm / adjust ==== # Known WGS84 lat/lon of the NW tile's top-left (north-west) corner NW_LAT = 51.512499 NW_LON = -0.099173 # WGS84 longitude; west of Greenwich is negative # Real-world size of a single tile (meters) TILE_SIZE_M = 500.0 # Target projected CRS (British National Grid recommended for London) TARGET_EPSG = 27700 # OSGB36 / British National Grid SOURCE_CRS = "EPSG:4326" # NW_LAT/NW_LON are given in WGS84 # The 4 files to process (filenames must distinguish the direction) INPUT_FILES = [ "TQ3280_NW.las", "TQ3280NE.las", "TQ3280SW.las", "TQ3280SE.las", ] # Output filename suffix OUT_SUFFIX = "_georef.las" # ================================= def read_bbox(path: Path): with laspy.open(str(path)) as f: hdr = f.header mins = np.array(getattr(hdr, "mins", getattr(hdr, "min", (0,0,0))), dtype=float) maxs = np.array(getattr(hdr, "maxs", getattr(hdr, "max", (0,0,0))), dtype=float) scales = np.array(hdr.scales) offsets = np.array(hdr.offsets) return mins, maxs, scales, offsets def apply_translation(in_path: Path, out_path: Path, tx: float, ty: float, target_epsg: int): las = laspy.read(str(in_path)) # Translate (X/Y only; if a Z datum correction is needed, add a Z offset here) las.x = las.x + tx las.y = las.y + ty # Write the EPSG (laspy 2.x: header.epsg) try: las.header.epsg = int(target_epsg) except Exception: # Some versions may require writing via a VLR; keep the simplest setting here pass # Optional: tag generation metadata try: las.header.system_identifier = "GeorefByScript" las.header.generating_software = "laspy_pyproj_georef" except Exception: pass las.write(str(out_path)) def main(): root = Path(".").resolve() # 1) Project the NW top-left corner (WGS84) into target CRS coords (easting, northing) transformer = Transformer.from_crs(SOURCE_CRS, f"EPSG:{TARGET_EPSG}", always_xy=True) # always_xy=True => input order is longitude, latitude (lon, lat) nw_e, nw_n = transformer.transform(NW_LON, NW_LAT) # 2) Build the target "top-left" coords for the four directions (top-left = north-west) # NE: +500m east of NW # SW: +500m south of NW # SE: +500m east and +500m south of NW targets = { "NW": (nw_e, nw_n), "NE": (nw_e + TILE_SIZE_M, nw_n), "SW": (nw_e, nw_n - TILE_SIZE_M), "SE": (nw_e + TILE_SIZE_M, nw_n - TILE_SIZE_M), } # 3) Per file: compute translation from local top-left (minX, maxY) to target top-left for fname in INPUT_FILES: in_path = root / fname if not in_path.exists(): print(f"[SKIP] File not found: {in_path}") continue # Determine the direction from the filename up = fname.upper() if "NW" in up and "TQ3280NW" in up: key = "NW" elif "NE" in up: key = "NE" elif "SW" in up: key = "SW" elif "SE" in up: key = "SE" elif "NW" in up: # Case where the name contains _NW key = "NW" else: print(f"[WARN] Cannot infer direction from filename; treating as NW: {fname}") key = "NW" tgt_e, tgt_n = targets[key] # Read the local bbox mins, maxs, scales, offsets = read_bbox(in_path) minX, minY = float(mins[0]), float(mins[1]) maxX, maxY = float(maxs[0]), float(maxs[1]) # Local top-left corner (north-west) = (minX, maxY) local_left_top = np.array([minX, maxY], dtype=float) target_left_top = np.array([tgt_e, tgt_n], dtype=float) # Translation t = target - local t = target_left_top - local_left_top tx, ty = float(t[0]), float(t[1]) # Print diagnostic info print(f"\n=== {fname} ===") print(f"Local bbox X:[{minX:.3f}, {maxX:.3f}] Y:[{minY:.3f}, {maxY:.3f}]") print(f"Local top-left (NW local) = ({local_left_top[0]:.3f}, {local_left_top[1]:.3f})") print(f"Target top-left (NW target EPSG:{TARGET_EPSG}) = ({target_left_top[0]:.3f}, {target_left_top[1]:.3f})") print(f"Translation (tx, ty) = ({tx:.3f}, {ty:.3f}) [units: meters, projected coords]") out_path = in_path.with_name(in_path.stem + OUT_SUFFIX) apply_translation(in_path, out_path, tx, ty, TARGET_EPSG) print(f"Written: {out_path.name} (EPSG:{TARGET_EPSG} set)") print("\nDone. Load *_georef.las into QGIS and set the project CRS to EPSG:%d (or enable on-the-fly reprojection)." % TARGET_EPSG) if __name__ == "__main__": main()