matchgeodem / scripts /write_qgis_metadata.py
paeslemesa's picture
Updated statcs/ splis/ and scrips/
0a7933d verified
Raw History Blame Contribute Delete
11.7 kB
import xml.etree.ElementTree as ET
from pathlib import Path
import json
areas = {'ATA_MV', 'BRA_SP', 'CHN_WS', 'ESP_EH', 'FIN_LM', 'GER_BN', 'IDN_SV',
'KAZ_AC', 'KSA_WA', 'NAM_HF', 'NZL_KP', 'PHL_TA', 'USA_GC'}
data_path = Path("/home/sabrina/Documents/Tese/05_Dataset/MatchGeo-DEM-v1/")
metadata_path = Path("/home/sabrina/Documents/Tese/05_Dataset/MatchGeo-DEM-v1/city_metadata/")
# Load all metadata
all_metadata = {}
for location in areas:
meta_file = metadata_path / f"{location}_metadata.json"
if meta_file.exists():
with open(meta_file, 'r') as f:
all_metadata[location] = json.load(f)
print("=" * 70)
print("MatchGeo-DEM Complete QGIS .qmd Metadata Writer")
print("=" * 70)
for location in sorted(areas):
if location not in all_metadata:
print(f"\n⚠️ {location}: Metadata JSON not found — skipping")
continue
meta = all_metadata[location]
# Create QGIS metadata XML
root = ET.Element("qgis")
root.set("version", "3.40")
# IDENTIFICATION
ident = ET.SubElement(root, "identifier")
ident.text = meta["identification"]["tile_id"]
parent_ident = ET.SubElement(root, "parentidentifier")
parent_ident.text = "10.5281/zenodo.21229785"
lang = ET.SubElement(root, "language")
lang.text = "en-US"
type_el = ET.SubElement(root, "type")
type_el.text = "dataset"
title = ET.SubElement(root, "title")
title.text = f"MatchGeo-DEM: {meta['identification']['full_name']}"
abstract = ET.SubElement(root, "abstract")
abstract_text = (
f"Digital Surface Model (DSM) from {meta['acquisition']['source']}. "
f"Resolution: {meta['raster']['resolution_meters']}m. "
f"Method: {meta['acquisition']['method']}. "
f"Year: {meta['acquisition']['date_start'][:4]}-{meta['acquisition']['date_end'][:4]}. "
f"CRS: {meta['spatial']['crs']['name']}. "
f"NoData: {meta['raster']['nodata_value']}. "
f"Format: {meta['raster']['tiff_variant']}, {meta['raster']['compression']} compressed. "
f"Tiles: {meta['spatial']['tile_index']['n_tiles']} patches of 333x333 pixels. "
f"{meta.get('notes', '')}"
)
abstract.text = abstract_text
# CONTACTS
contacts = ET.SubElement(root, "contacts")
contact = ET.SubElement(contacts, "contact")
c_name = ET.SubElement(contact, "name")
c_name.text = "Sabrina Correa"
c_org = ET.SubElement(contact, "organization")
c_org.text = "Universidade Federal de Vicosa"
c_pos = ET.SubElement(contact, "position")
c_pos.text = "Dataset Maintainer"
c_voice = ET.SubElement(contact, "voice")
c_voice.text = ""
c_fax = ET.SubElement(contact, "fax")
c_fax.text = ""
c_email = ET.SubElement(contact, "email")
c_email.text = "sabrina.correa@ufv.br"
c_role = ET.SubElement(contact, "role")
c_role.text = "distributor"
# Also add original data provider as second contact
contact2 = ET.SubElement(contacts, "contact")
c2_name = ET.SubElement(contact2, "name")
c2_name.text = meta["acquisition"]["provider"]
c2_org = ET.SubElement(contact2, "organization")
c2_org.text = meta["acquisition"]["source"]
c2_pos = ET.SubElement(contact2, "position")
c2_pos.text = "Original Data Provider"
c2_voice = ET.SubElement(contact2, "voice")
c2_voice.text = ""
c2_fax = ET.SubElement(contact2, "fax")
c2_fax.text = ""
c2_email = ET.SubElement(contact2, "email")
c2_email.text = ""
c2_role = ET.SubElement(contact2, "role")
c2_role.text = "owner"
# LINKS
links = ET.SubElement(root, "links")
# Source link
link1 = ET.SubElement(links, "link")
l1_name = ET.SubElement(link1, "name")
l1_name.text = "Original Data Source"
l1_type = ET.SubElement(link1, "type")
l1_type.text = "WWW:LINK-1.0-http--link"
l1_url = ET.SubElement(link1, "url")
l1_url.text = meta["acquisition"]["url"]
l1_desc = ET.SubElement(link1, "description")
l1_desc.text = f"Official portal for {meta['acquisition']['source']}"
l1_format = ET.SubElement(link1, "format")
l1_format.text = "HTML"
l1_mime = ET.SubElement(link1, "mimeType")
l1_mime.text = "text/html"
l1_size = ET.SubElement(link1, "size")
l1_size.text = ""
# DOI link
doi = meta["acquisition"].get("doi", "10.5281/zenodo.19339008")
link2 = ET.SubElement(links, "link")
l2_name = ET.SubElement(link2, "name")
l2_name.text = "Dataset DOI"
l2_type = ET.SubElement(link2, "type")
l2_type.text = "DOI"
l2_url = ET.SubElement(link2, "url")
l2_url.text = f"https://doi.org/{doi}"
l2_desc = ET.SubElement(link2, "description")
l2_desc.text = "MatchGeo-DEM dataset DOI"
l2_format = ET.SubElement(link2, "format")
l2_format.text = "HTML"
l2_mime = ET.SubElement(link2, "mimeType")
l2_mime.text = "text/html"
l2_size = ET.SubElement(link2, "size")
l2_size.text = ""
# License link
link3 = ET.SubElement(links, "link")
l3_name = ET.SubElement(link3, "name")
l3_name.text = "License (CC BY 4.0)"
l3_type = ET.SubElement(link3, "type")
l3_type.text = "WWW:LINK-1.0-http--link"
l3_url = ET.SubElement(link3, "url")
l3_url.text = "https://creativecommons.org/licenses/by/4.0/"
l3_desc = ET.SubElement(link3, "description")
l3_desc.text = "Creative Commons Attribution 4.0 International"
l3_format = ET.SubElement(link3, "format")
l3_format.text = "HTML"
l3_mime = ET.SubElement(link3, "mimeType")
l3_mime.text = "text/html"
l3_size = ET.SubElement(link3, "size")
l3_size.text = ""
# REMA documentation link (for ATA_MV)
if location == "ATA_MV":
link4 = ET.SubElement(links, "link")
l4_name = ET.SubElement(link4, "name")
l4_name.text = "REMA Documentation"
l4_type = ET.SubElement(link4, "type")
l4_type.text = "WWW:LINK-1.0-http--link"
l4_url = ET.SubElement(link4, "url")
l4_url.text = "https://www.pgc.umn.edu/data/rema/"
l4_desc = ET.SubElement(link4, "description")
l4_desc.text = "Reference Elevation Model of Antarctica documentation"
l4_format = ET.SubElement(link4, "format")
l4_format.text = "HTML"
l4_mime = ET.SubElement(link4, "mimeType")
l4_mime.text = "text/html"
l4_size = ET.SubElement(link4, "size")
l4_size.text = ""
# DATES
dates = ET.SubElement(root, "dates")
date_created = ET.SubElement(dates, "date")
date_created.set("type", "Created")
date_created.text = meta["acquisition"]["date_start"]
date_published = ET.SubElement(dates, "date")
date_published.set("type", "Published")
date_published.text = "2026-05-11"
date_revised = ET.SubElement(dates, "date")
date_revised.set("type", "Revised")
date_revised.text = "2026-05-11"
# FEES
fees = ET.SubElement(root, "fees")
fees.text = "None. This dataset is open access under CC BY 4.0."
# ENCODING
encoding = ET.SubElement(root, "encoding")
encoding.text = "UTF-8"
# CRS
crs = ET.SubElement(root, "crs")
spatialrefsys = ET.SubElement(crs, "spatialrefsys")
crs_native = ET.SubElement(spatialrefsys, "nativeFormat")
crs_native.text = "Wkt"
crs_wkt = ET.SubElement(spatialrefsys, "wkt")
crs_wkt.text = meta["spatial"]["crs"].get("wkt", "")
crs_desc = ET.SubElement(spatialrefsys, "description")
crs_desc.text = meta["spatial"]["crs"]["name"]
crs_type = ET.SubElement(spatialrefsys, "type")
crs_type.text = "crs"
# EXTENT
extent = ET.SubElement(root, "extent")
# Spatial extent
spatial = ET.SubElement(extent, "spatial")
spatial.set("crs", meta["spatial"]["crs"]["name"])
bbox = meta["spatial"]["extent"]["bbox"]
if bbox and None not in bbox:
spatial.set("dimensions", "2")
spatial.set("minx", str(bbox[0]))
spatial.set("miny", str(bbox[1]))
spatial.set("maxx", str(bbox[2]))
spatial.set("maxy", str(bbox[3]))
# Temporal extent
temporal = ET.SubElement(extent, "temporal")
temp_start = ET.SubElement(temporal, "start")
temp_start.text = meta["acquisition"]["date_start"]
temp_end = ET.SubElement(temporal, "end")
temp_end.text = meta["acquisition"]["date_end"]
# Vertical extent (Z min/max)
vertical = ET.SubElement(extent, "vertical")
# We don't have actual Z min/max per city, but we can leave it empty or estimate
vert_min = ET.SubElement(vertical, "minimum")
vert_min.text = "0"
vert_max = ET.SubElement(vertical, "maximum")
vert_max.text = "0"
vert_unit = ET.SubElement(vertical, "unit")
vert_unit.text = "meters"
# CATEGORIES
categories = ET.SubElement(root, "categories")
cat = ET.SubElement(categories, "category")
cat.text = "Elevation"
cat2 = ET.SubElement(categories, "category")
cat2.text = "Geoscientific Information"
# KEYWORDS
keywords = ET.SubElement(root, "keywords")
# Add vocabulary attribute for GCMD
vocab = ET.SubElement(keywords, "vocabulary")
vocab.set("name", "GCMD")
vocab.text = "EARTH SCIENCE > LAND SURFACE > TOPOGRAPHY > TERRAIN ELEVATION > DIGITAL ELEVATION/TERRAIN MODEL (DEM)"
for kw in meta["fair"]["findable"]["keywords"]:
kw_el = ET.SubElement(keywords, "keyword")
kw_el.text = kw
# Add extra keywords
extra_kws = ["DSM", "local feature matching", "computer vision", "urban terrain"]
for kw in extra_kws:
kw_el = ET.SubElement(keywords, "keyword")
kw_el.text = kw
# RIGHTS
rights = ET.SubElement(root, "rights")
rights.text = f"{meta['fair']['reusable']['license']}. You are free to share and adapt under attribution terms."
# LICENSE
license_el = ET.SubElement(root, "license")
license_el.text = "Creative Commons Attribution 4.0 International (CC BY 4.0)"
# HISTORY
history = ET.SubElement(root, "history")
hist_entry = ET.SubElement(history, "history")
hist_entry.text = (
f"Dataset created from {meta['acquisition']['source']} data. "
f"Processed with PDAL {meta['processing']['software_version']}. "
f"Patch extraction: 333x333 pixels. "
f"Released as MatchGeo-DEM v1.1 on 2026-05-11."
)
# CONSTRAINTS
constraints = ET.SubElement(root, "constraints")
# Use constraints
use_constraints = ET.SubElement(constraints, "constraint")
use_constraints.set("type", "Use")
use_constraints.text = "Attribution required. See LICENSE file or https://creativecommons.org/licenses/by/4.0/"
# Access constraints
access_constraints = ET.SubElement(constraints, "constraint")
access_constraints.set("type", "Access")
access_constraints.text = "Open access. No registration required."
# Write QMD file
qmd_path = data_path / f"{location}/{location}.qmd"
# Pretty print XML
ET.indent(root, space=" ")
tree = ET.ElementTree(root)
tree.write(qmd_path, encoding="utf-8", xml_declaration=True)
print(f"\n✅ {location}: Written {qmd_path}")
print(f" Title: {title.text[:50]}...")
print(f" Contacts: 2 (Maintainer + Data Provider)")
print(f" Links: 3+ (Source + DOI + License)")
print(f" Dates: Created, Published, Revised")
print(f" Categories: Elevation, Geoscientific Information")
print(f" Keywords: {len(meta['fair']['findable']['keywords']) + 4} total")
print("\n" + "=" * 70)
print("Done! Each .tif now has a matching .qmd with COMPLETE metadata.")
print("QGIS will auto-load the .qmd when you add the layer.")
print("=" * 70)