mirror of
https://github.com/ARUP-CAS/aiscr-qgis-amcr-viewer.git
synced 2026-08-11 01:36:06 +02:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ab3ace310 | |||
| 27a0f4e9ea | |||
| f4ad402f52 | |||
| 2fdbb15001 | |||
| 8248d1bcae | |||
| abb06b9858 | |||
| e84ee8e543 | |||
| 66532c108b | |||
| 86da8b9cf4 | |||
| a95831f180 |
+136
-84
@@ -9,24 +9,30 @@ from qgis.core import QgsMessageLog, Qgis
|
||||
# Define paths for the plugin and its codelists directory
|
||||
PLUGIN_DIR = os.path.dirname(__file__)
|
||||
CODELISTS_DIR = os.path.join(PLUGIN_DIR, 'codelists')
|
||||
BASE_URL = "https://api.aiscr.cz/2.2/oai"
|
||||
BASE_URL_AMCR = "https://api.aiscr.cz/2.2/oai"
|
||||
BASE_URL_DA = "https://digiarchiv.aiscr.cz/api/search/query"
|
||||
OUTPUT_FILE = os.path.join(CODELISTS_DIR, 'heslar.csv')
|
||||
|
||||
slovnicek = {
|
||||
'obdobi': 'heslo:obdobi',
|
||||
'typ_akce': 'heslo:akce_typ',
|
||||
'areal': 'heslo:areal',
|
||||
'kraj': 'ruian_kraj',
|
||||
'organizace': 'organizace',
|
||||
'okres': 'ruian_okres',
|
||||
'katastr': 'ruian_katastr',
|
||||
'vedouci': 'osoba',
|
||||
'pian_presnost': 'heslo:pian_presnost',
|
||||
'typ_lokality': 'heslo:lokalita_typ',
|
||||
'druh_lokality': 'heslo:lokalita_druh',
|
||||
'jistota': 'heslo:jistota_urceni',
|
||||
'lokalita_zachovalost': 'heslo:stav_dochovani',
|
||||
'pristupnost': 'heslo:pristupnost'
|
||||
'obdobi': (BASE_URL_AMCR, 'heslo:obdobi'),
|
||||
'typ_akce': (BASE_URL_AMCR, 'heslo:akce_typ'),
|
||||
'areal': (BASE_URL_AMCR, 'heslo:areal'),
|
||||
'kraj': (BASE_URL_AMCR, 'ruian_kraj'),
|
||||
'organizace': (BASE_URL_AMCR, 'organizace'),
|
||||
'okres': (BASE_URL_AMCR, 'ruian_okres'),
|
||||
'katastr': (BASE_URL_AMCR, 'ruian_katastr'),
|
||||
'pian_presnost': (BASE_URL_AMCR, 'heslo:pian_presnost'),
|
||||
'typ_lokality': (BASE_URL_AMCR, 'heslo:lokalita_typ'),
|
||||
'druh_lokality': (BASE_URL_AMCR, 'heslo:lokalita_druh'),
|
||||
'jistota': (BASE_URL_AMCR, 'heslo:jistota_urceni'),
|
||||
'lokalita_zachovalost': (BASE_URL_AMCR, 'heslo:stav_dochovani'),
|
||||
'pristupnost': (BASE_URL_AMCR, 'heslo:pristupnost'),
|
||||
'nalez_kategorie': (BASE_URL_AMCR, 'heslo:predmet_druh_kat'),
|
||||
'druh_nalezu': (BASE_URL_AMCR, 'heslo:predmet_druh'),
|
||||
'specifikace': (BASE_URL_AMCR, 'heslo:predmet_specifikace'),
|
||||
'nalezove_okolnosti': (BASE_URL_AMCR, 'heslo:nalezove_okolnosti'),
|
||||
'vedouci': (BASE_URL_DA, 'f_vedouci'),
|
||||
'nalezce': (BASE_URL_DA, 'f_nalezce'),
|
||||
}
|
||||
|
||||
NS = {
|
||||
@@ -97,13 +103,19 @@ def load_all_data():
|
||||
return categorized_data
|
||||
|
||||
|
||||
def fetch_set(internal_name, api_set, task=None):
|
||||
def fetch_set(base_url, internal_name, api_set, task=None):
|
||||
dataset = []
|
||||
params = {
|
||||
params_amcr = {
|
||||
"verb": "ListRecords",
|
||||
"metadataPrefix": "oai_dc",
|
||||
"set": api_set
|
||||
}
|
||||
params_da = {
|
||||
"entity": "samostatny_nalez" if internal_name == "nalezce" else "akce",
|
||||
"rows": 0,
|
||||
"noFacets": "false",
|
||||
"onlyFacets": "true"
|
||||
}
|
||||
|
||||
while True:
|
||||
# Check for cancellation at each iteration
|
||||
@@ -111,75 +123,95 @@ def fetch_set(internal_name, api_set, task=None):
|
||||
return None
|
||||
|
||||
try:
|
||||
response = requests.get(BASE_URL, params=params, timeout=30)
|
||||
response.raise_for_status()
|
||||
root = ET.fromstring(response.content) # nosec
|
||||
if "digiarchiv" not in base_url:
|
||||
response = requests.get(base_url, params=params_amcr, timeout=30)
|
||||
response.raise_for_status()
|
||||
root = ET.fromstring(response.content) # nosec
|
||||
|
||||
records = root.findall('.//oai:record', NS)
|
||||
for rec in records:
|
||||
metadata = rec.find('.//oai_dc:dc', NS)
|
||||
if metadata is not None:
|
||||
# Code (identifier)
|
||||
identifier_el = metadata.find('dc:identifier', NS)
|
||||
kod = (
|
||||
identifier_el.text
|
||||
if identifier_el is not None
|
||||
else ""
|
||||
)
|
||||
|
||||
# Title – filter out system labels "AMČR - ..."
|
||||
titles = metadata.findall('dc:title', NS)
|
||||
nazev = ""
|
||||
for t in titles:
|
||||
if (
|
||||
t.text
|
||||
and not t.text.startswith("AMČR -")
|
||||
and not t.text.startswith(" AMČR -")
|
||||
):
|
||||
nazev = t.text
|
||||
break
|
||||
# If no title passed the filter, fall back
|
||||
# to the first available one
|
||||
if not nazev and titles:
|
||||
nazev = titles[0].text
|
||||
|
||||
specialni_pripady = ['okres', 'katastr']
|
||||
|
||||
if internal_name in specialni_pripady:
|
||||
kod = nazev
|
||||
|
||||
if internal_name == 'pristupnost':
|
||||
kod = next(
|
||||
(
|
||||
t.text for t in titles
|
||||
if t.text
|
||||
and len(t.text) == 1
|
||||
and t.text.isalpha()
|
||||
),
|
||||
None
|
||||
records = root.findall('.//oai:record', NS)
|
||||
for rec in records:
|
||||
metadata = rec.find('.//oai_dc:dc', NS)
|
||||
if metadata is not None:
|
||||
# Code (identifier)
|
||||
identifier_el = metadata.find('dc:identifier', NS)
|
||||
kod = (
|
||||
identifier_el.text
|
||||
if identifier_el is not None
|
||||
else ""
|
||||
)
|
||||
# Skip records without a valid one-letter code –
|
||||
# a None code would end up in the CSV and later
|
||||
# in the API filter as the string "None"
|
||||
if not kod:
|
||||
continue
|
||||
|
||||
# Title – filter out system labels "AMČR - ..."
|
||||
titles = metadata.findall('dc:title', NS)
|
||||
nazev = ""
|
||||
for t in titles:
|
||||
if (
|
||||
t.text
|
||||
and not t.text.startswith("AMČR -")
|
||||
and not t.text.startswith(" AMČR -")
|
||||
):
|
||||
nazev = t.text
|
||||
break
|
||||
# If no title passed the filter, fall back
|
||||
# to the first available one
|
||||
if not nazev and titles:
|
||||
nazev = titles[0].text
|
||||
|
||||
specialni_pripady = ['okres', 'katastr']
|
||||
|
||||
if internal_name in specialni_pripady:
|
||||
kod = nazev
|
||||
|
||||
if internal_name == 'pristupnost':
|
||||
kod = next(
|
||||
(
|
||||
t.text for t in titles
|
||||
if t.text
|
||||
and len(t.text) == 1
|
||||
and t.text.isalpha()
|
||||
),
|
||||
None
|
||||
)
|
||||
# Skip records without a valid one-letter code –
|
||||
# a None code would end up in the CSV and later
|
||||
# in the API filter as the string "None"
|
||||
if not kod:
|
||||
continue
|
||||
|
||||
dataset.append({
|
||||
'Název': nazev,
|
||||
'Kód': kod,
|
||||
'Kategorie': internal_name
|
||||
})
|
||||
|
||||
# Pagination
|
||||
token = root.find('.//oai:resumptionToken', NS)
|
||||
if token is not None and token.text:
|
||||
params_amcr = {
|
||||
"verb": "ListRecords",
|
||||
"resumptionToken": token.text
|
||||
}
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
break
|
||||
|
||||
else:
|
||||
response = requests.get(base_url, params=params_da, timeout=30)
|
||||
response.raise_for_status()
|
||||
data_json = response.json()
|
||||
|
||||
records = data_json['facet_counts']['facet_fields'][api_set]
|
||||
|
||||
for r in records:
|
||||
|
||||
nazev = r["name"]
|
||||
|
||||
dataset.append({
|
||||
'Název': nazev,
|
||||
'Kód': kod,
|
||||
'Kategorie': internal_name
|
||||
})
|
||||
|
||||
# Pagination
|
||||
token = root.find('.//oai:resumptionToken', NS)
|
||||
if token is not None and token.text:
|
||||
params = {
|
||||
"verb": "ListRecords",
|
||||
"resumptionToken": token.text
|
||||
}
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
break
|
||||
'Název': nazev,
|
||||
'Kód': nazev,
|
||||
'Kategorie': internal_name
|
||||
})
|
||||
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
QgsMessageLog.logMessage(
|
||||
@@ -195,8 +227,13 @@ def download_heslare(task=None):
|
||||
ensure_codelists_dir()
|
||||
all_data = []
|
||||
total_sets = len(slovnicek)
|
||||
# index, (interni, api_nazev)
|
||||
for index, (key, value) in enumerate(slovnicek.items()):
|
||||
|
||||
base_url = value[0]
|
||||
interni = key
|
||||
api_nazev = value[1]
|
||||
|
||||
for index, (interni, api_nazev) in enumerate(slovnicek.items()):
|
||||
# Check if the user cancelled the task via the QGIS taskbar
|
||||
if task and task.isCanceled():
|
||||
return False
|
||||
@@ -206,7 +243,7 @@ def download_heslare(task=None):
|
||||
"AMČR", Qgis.Info)
|
||||
|
||||
# Pass the task correctly to the updated fetch function
|
||||
data = fetch_set(interni, api_nazev, task=task)
|
||||
data = fetch_set(base_url, interni, api_nazev, task=task)
|
||||
|
||||
if data is None:
|
||||
return False # Cancelled mid-download
|
||||
@@ -260,6 +297,16 @@ def refresh_globals():
|
||||
LOKALITA_ZACHOVALOST.update(data.get('lokalita_zachovalost', {}))
|
||||
PRISTUPNOST.clear()
|
||||
PRISTUPNOST.update(data.get('pristupnost', {}))
|
||||
NALEZ_KATEGORIE.clear()
|
||||
NALEZ_KATEGORIE.update(data.get('nalez_kategorie', {}))
|
||||
DRUH_NALEZU.clear()
|
||||
DRUH_NALEZU.update(data.get('druh_nalezu', {}))
|
||||
SPECIFIKACE.clear()
|
||||
SPECIFIKACE.update(data.get('specifikace', {}))
|
||||
NALEZOVE_OKOLNOSTI.clear()
|
||||
NALEZOVE_OKOLNOSTI.update(data.get('nalezove_okolnosti', {}))
|
||||
NALEZCE.clear()
|
||||
NALEZCE.update(data.get('nalezce', {}))
|
||||
|
||||
|
||||
# Initialize empty dicts that will be populated immediately below
|
||||
@@ -277,5 +324,10 @@ DRUH_LOKALITY = {}
|
||||
JISTOTA = {}
|
||||
LOKALITA_ZACHOVALOST = {}
|
||||
PRISTUPNOST = {}
|
||||
NALEZ_KATEGORIE = {}
|
||||
DRUH_NALEZU = {}
|
||||
SPECIFIKACE = {}
|
||||
NALEZOVE_OKOLNOSTI = {}
|
||||
NALEZCE = {}
|
||||
|
||||
refresh_globals()
|
||||
|
||||
+72
-25
@@ -11,7 +11,9 @@ from qgis.utils import iface
|
||||
from .amcr_codelists import (OBDOBI, TYP_AKCE, KRAJE, AREAL, ORGANIZACE,
|
||||
OKRESY, KATASTRY, VEDOUCI, PIAN_PRESNOST,
|
||||
TYP_LOKALITY, DRUH_LOKALITY, JISTOTA,
|
||||
LOKALITA_ZACHOVALOST, PRISTUPNOST,
|
||||
LOKALITA_ZACHOVALOST, PRISTUPNOST,
|
||||
NALEZ_KATEGORIE, DRUH_NALEZU, SPECIFIKACE,
|
||||
NALEZOVE_OKOLNOSTI, NALEZCE,
|
||||
download_heslare, refresh_globals)
|
||||
|
||||
|
||||
@@ -168,6 +170,11 @@ class AmcrFilterDialog(QDialog):
|
||||
'druh_lokality': [],
|
||||
'jistota': [],
|
||||
'lokalita_zachovalost': [],
|
||||
'nalez_kategorie': [],
|
||||
'druh_nalezu': [],
|
||||
'specifikace': [],
|
||||
'nalezove_okolnosti': [],
|
||||
'nalezce': [],
|
||||
}
|
||||
|
||||
layout = QVBoxLayout()
|
||||
@@ -218,7 +225,7 @@ class AmcrFilterDialog(QDialog):
|
||||
|
||||
# Filters valid for Akce
|
||||
|
||||
if self.typ_dat == "akce":
|
||||
if self.typ_dat in ["samostatny_nalez", "akce"]:
|
||||
self.picker_org = self.setup_picker(
|
||||
"Organizace",
|
||||
'organizace',
|
||||
@@ -226,6 +233,7 @@ class AmcrFilterDialog(QDialog):
|
||||
)
|
||||
layout.addWidget(self.picker_org)
|
||||
|
||||
if self.typ_dat == "akce":
|
||||
self.picker_vedouci = self.setup_picker(
|
||||
"Vedoucí výzkumu",
|
||||
'vedouci',
|
||||
@@ -278,12 +286,49 @@ class AmcrFilterDialog(QDialog):
|
||||
self.picker_obdobi = self.setup_picker("Období", 'obdobi', OBDOBI)
|
||||
layout.addWidget(self.picker_obdobi)
|
||||
|
||||
self.picker_areal = self.setup_picker("Areál", 'areal', AREAL)
|
||||
layout.addWidget(self.picker_areal)
|
||||
if self.typ_dat == "samostatny_nalez":
|
||||
self.picker_nalez_kategorie = self.setup_picker(
|
||||
"Kategorie nálezu",
|
||||
'nalez_kategorie',
|
||||
NALEZ_KATEGORIE
|
||||
)
|
||||
layout.addWidget(self.picker_nalez_kategorie)
|
||||
|
||||
# Option to download related components table
|
||||
self.chk_komponenty = QCheckBox("Načíst komponenty")
|
||||
layout.addWidget(self.chk_komponenty)
|
||||
self.picker_druh_nalezu = self.setup_picker(
|
||||
"Druh nálezu",
|
||||
'druh_nalezu',
|
||||
DRUH_NALEZU
|
||||
)
|
||||
layout.addWidget(self.picker_druh_nalezu)
|
||||
|
||||
self.picker_specifikace = self.setup_picker(
|
||||
"Specifikace nálezu",
|
||||
'specifikace',
|
||||
SPECIFIKACE
|
||||
)
|
||||
layout.addWidget(self.picker_specifikace)
|
||||
|
||||
self.picker_nalezove_okolnosti = self.setup_picker(
|
||||
"Okolnosti nálezu",
|
||||
'nalezove_okolnosti',
|
||||
NALEZOVE_OKOLNOSTI
|
||||
)
|
||||
layout.addWidget(self.picker_nalezove_okolnosti)
|
||||
|
||||
self.picker_nalezce = self.setup_picker(
|
||||
"Nálezce",
|
||||
'nalezce',
|
||||
NALEZCE
|
||||
)
|
||||
layout.addWidget(self.picker_nalezce)
|
||||
|
||||
if self.typ_dat != "samostatny_nalez":
|
||||
self.picker_areal = self.setup_picker("Areál", 'areal', AREAL)
|
||||
layout.addWidget(self.picker_areal)
|
||||
|
||||
# Option to download related components table
|
||||
self.chk_komponenty = QCheckBox("Načíst komponenty")
|
||||
layout.addWidget(self.chk_komponenty)
|
||||
|
||||
# Warning label
|
||||
self.lbl_komponenty_warning = QLabel(
|
||||
@@ -299,9 +344,10 @@ class AmcrFilterDialog(QDialog):
|
||||
self.lbl_komponenty_warning.setVisible(False)
|
||||
layout.addWidget(self.lbl_komponenty_warning)
|
||||
|
||||
self.chk_komponenty.toggled.connect(
|
||||
self.lbl_komponenty_warning.setVisible
|
||||
)
|
||||
if self.typ_dat != "samostatny_nalez":
|
||||
self.chk_komponenty.toggled.connect(
|
||||
self.lbl_komponenty_warning.setVisible
|
||||
)
|
||||
|
||||
# Pushes everything above to the top
|
||||
layout.addStretch(1)
|
||||
@@ -470,22 +516,23 @@ class AmcrFilterDialog(QDialog):
|
||||
filters['posevidence'] = 'true'
|
||||
if self.chk_proj_akce.isChecked():
|
||||
filters['proj_akce'] = 'true'
|
||||
if self.selection_cache['organizace']:
|
||||
filters['f_organizace'] = self.selection_cache['organizace']
|
||||
if self.selection_cache['typ_akce']:
|
||||
filters['f_typ_vyzkumu'] = self.selection_cache['typ_akce']
|
||||
if self.selection_cache['vedouci']:
|
||||
filters['f_vedouci'] = self.selection_cache['vedouci']
|
||||
|
||||
if self.typ_dat == "lokalita":
|
||||
if self.selection_cache['typ_lokality']:
|
||||
filters['f_typ_lokality'] = self.selection_cache['typ_lokality']
|
||||
if self.selection_cache['druh_lokality']:
|
||||
filters['f_druh_lokality'] = self.selection_cache['druh_lokality']
|
||||
if self.selection_cache['jistota']:
|
||||
filters['f_jistota'] = self.selection_cache['jistota']
|
||||
if self.selection_cache['lokalita_zachovalost']:
|
||||
filters['f_lokalita_zachovalost'] = self.selection_cache['lokalita_zachovalost']
|
||||
if self.selection_cache['typ_akce']:
|
||||
filters['f_typ_vyzkumu'] = self.selection_cache['typ_akce']
|
||||
if self.selection_cache['vedouci']:
|
||||
filters['f_vedouci'] = self.selection_cache['vedouci']
|
||||
|
||||
if self.selection_cache['organizace']:
|
||||
filters['f_organizace'] = self.selection_cache['organizace']
|
||||
|
||||
if self.selection_cache['typ_lokality']:
|
||||
filters['f_typ_lokality'] = self.selection_cache['typ_lokality']
|
||||
if self.selection_cache['druh_lokality']:
|
||||
filters['f_druh_lokality'] = self.selection_cache['druh_lokality']
|
||||
if self.selection_cache['jistota']:
|
||||
filters['f_jistota'] = self.selection_cache['jistota']
|
||||
if self.selection_cache['lokalita_zachovalost']:
|
||||
filters['f_lokalita_zachovalost'] = self.selection_cache['lokalita_zachovalost']
|
||||
|
||||
return filters
|
||||
|
||||
|
||||
+265
-241
@@ -26,6 +26,17 @@ LAST_LOGIN_ERROR: str | None = None
|
||||
# a second download while the first one is still running
|
||||
_LOADING = False
|
||||
|
||||
archeologicky_zaznam_l = [
|
||||
"akce",
|
||||
"lokalita",
|
||||
]
|
||||
|
||||
typ_dat_vocab = {
|
||||
"akce": "Akce",
|
||||
"lokalita": "Lokalita",
|
||||
"samostatny_nalez": "PAS",
|
||||
}
|
||||
|
||||
|
||||
def _log(msg: str, level=Qgis.MessageLevel.Info):
|
||||
"""
|
||||
@@ -440,276 +451,289 @@ def load_amcr_data(canvas, bb, filters=None,
|
||||
return ", ".join([str(x) for x in val if x])
|
||||
|
||||
# Process each downloaded metadata record
|
||||
for doc in docs:
|
||||
piani = doc.get('az_dj_pian', [])
|
||||
if not piani:
|
||||
continue
|
||||
|
||||
if only_projektove_akce and not doc.get("akce_projekt", False):
|
||||
continue
|
||||
|
||||
actions_with_geom += 1
|
||||
|
||||
# Extract protected fields ('or {}' – key may hold None)
|
||||
az_chranene = doc.get('az_chranene_udaje') or {}
|
||||
chranene = (
|
||||
doc.get('akce_chranene_udaje')
|
||||
or doc.get('lokalita_chranene_udaje')
|
||||
or {}
|
||||
)
|
||||
|
||||
# Format additional cadastral areas from nested dicts
|
||||
dalsi_kat = az_chranene.get('dalsi_katastr', [])
|
||||
dalsi_kat_str = ""
|
||||
if isinstance(dalsi_kat, list):
|
||||
items = [
|
||||
x.get('value', '') if isinstance(x, dict) else str(x)
|
||||
for x in dalsi_kat
|
||||
]
|
||||
dalsi_kat_str = ", ".join([i for i in items if i])
|
||||
|
||||
lokalizace = chranene.get('lokalizace_okolnosti', "")
|
||||
lokalita_nazev = chranene.get('nazev', "")
|
||||
lokalita_popis = chranene.get('popis', "")
|
||||
|
||||
# Core metadata structure
|
||||
meta = {
|
||||
"ident_cely": doc.get('ident_cely', ''),
|
||||
"az_okres": g(doc, 'az_okres'),
|
||||
"katastr": g_list(doc, 'katastr'),
|
||||
"dalsi_katastr": dalsi_kat_str,
|
||||
"pristupnost": g(doc, 'pristupnost'),
|
||||
"loc": g_list(doc, 'loc'),
|
||||
}
|
||||
|
||||
# Add entity-specific metadata
|
||||
if typ_dat == "akce":
|
||||
meta.update({
|
||||
"akce_hlavni_vedouci": g(
|
||||
doc,
|
||||
'akce_hlavni_vedouci'
|
||||
),
|
||||
"akce_organizace": tr_code(g(
|
||||
doc,
|
||||
'akce_organizace'
|
||||
)),
|
||||
"akce_specifikace_data": tr_code(g(
|
||||
doc,
|
||||
'akce_specifikace_data'
|
||||
)),
|
||||
"akce_datum_zahajeni": g(
|
||||
doc,
|
||||
'akce_datum_zahajeni'
|
||||
),
|
||||
"akce_datum_ukonceni": g(
|
||||
doc,
|
||||
'akce_datum_ukonceni'
|
||||
),
|
||||
"akce_hlavni_typ": tr_code(g(
|
||||
doc,
|
||||
'akce_hlavni_typ'
|
||||
)),
|
||||
"akce_vedlejsi_typ": g_list(
|
||||
doc,
|
||||
'akce_vedlejsi_typ',
|
||||
translate=True
|
||||
),
|
||||
"lokalizace_okolnosti": (
|
||||
str(lokalizace)
|
||||
if lokalizace
|
||||
else ""
|
||||
),
|
||||
"akce_je_nz": (
|
||||
"Ano"
|
||||
if doc.get('akce_je_nz') is True
|
||||
else "Ne"
|
||||
),
|
||||
"projekt": g(
|
||||
doc,
|
||||
'akce_projekt',
|
||||
""
|
||||
),
|
||||
})
|
||||
|
||||
elif typ_dat == "lokalita":
|
||||
meta.update({
|
||||
"lokalita_nazev": lokalita_nazev,
|
||||
"lokalita_popis": lokalita_popis,
|
||||
"lokalita_zachovalost": tr_code(g(
|
||||
doc,
|
||||
'lokalita_zachovalost'
|
||||
)),
|
||||
"lokalita_druh": tr_code(g(
|
||||
doc,
|
||||
'lokalita_druh'
|
||||
)),
|
||||
"lokalita_typ": tr_code(g(
|
||||
doc,
|
||||
'lokalita_typ_lokality'
|
||||
)),
|
||||
})
|
||||
|
||||
# Documentation units (DJ) within the record
|
||||
djs = doc.get('az_dokumentacni_jednotka', [])
|
||||
|
||||
for dj in djs:
|
||||
# Skip negative evidence units if requested
|
||||
if skip_negativni and dj.get('dj_negativni_jednotka') is True:
|
||||
if typ_dat in archeologicky_zaznam_l:
|
||||
for doc in docs:
|
||||
piani = doc.get('az_dj_pian', [])
|
||||
if not piani:
|
||||
continue
|
||||
|
||||
komps = dj.get('dj_komponenta', [])
|
||||
if only_projektove_akce and not doc.get("akce_projekt", False):
|
||||
continue
|
||||
|
||||
if filter_areal or filter_datace:
|
||||
if not komps:
|
||||
continue
|
||||
if not any(
|
||||
komp_projde_filtrem(
|
||||
komp, filter_areal,
|
||||
filter_datace, filters
|
||||
)
|
||||
for komp in komps
|
||||
):
|
||||
continue
|
||||
actions_with_geom += 1
|
||||
|
||||
dj_id = dj.get('ident_cely')
|
||||
dj_typ = dj.get('dj_typ')
|
||||
# Extract protected fields ('or {}' – key may hold None)
|
||||
az_chranene = doc.get('az_chranene_udaje') or {}
|
||||
chranene = (
|
||||
doc.get('akce_chranene_udaje')
|
||||
or doc.get('lokalita_chranene_udaje')
|
||||
or {}
|
||||
)
|
||||
|
||||
# Merge shared metadata with documentation unit-specific fields
|
||||
dj_meta = {
|
||||
**meta,
|
||||
'dj_id': dj_id,
|
||||
'dj_typ_value': dj_typ.get('value') if dj_typ else "",
|
||||
'dj_negativni': (
|
||||
"Negativní"
|
||||
if dj.get('dj_negativni_jednotka') is True
|
||||
else "Pozitivní"
|
||||
)
|
||||
# Format additional cadastral areas from nested dicts
|
||||
dalsi_kat = az_chranene.get('dalsi_katastr', [])
|
||||
dalsi_kat_str = ""
|
||||
if isinstance(dalsi_kat, list):
|
||||
items = [
|
||||
x.get('value', '') if isinstance(x, dict) else str(x)
|
||||
for x in dalsi_kat
|
||||
]
|
||||
dalsi_kat_str = ", ".join([i for i in items if i])
|
||||
|
||||
lokalizace = chranene.get('lokalizace_okolnosti', "")
|
||||
lokalita_nazev = chranene.get('nazev', "")
|
||||
lokalita_popis = chranene.get('popis', "")
|
||||
|
||||
# Core metadata structure
|
||||
meta = {
|
||||
"ident_cely": doc.get('ident_cely', ''),
|
||||
"az_okres": g(doc, 'az_okres'),
|
||||
"katastr": g_list(doc, 'katastr'),
|
||||
"dalsi_katastr": dalsi_kat_str,
|
||||
"pristupnost": g(doc, 'pristupnost'),
|
||||
"loc": g_list(doc, 'loc'),
|
||||
}
|
||||
|
||||
# Link Documentation Unit to Geometry (PIAN)
|
||||
dj_pian = dj.get('dj_pian')
|
||||
if dj_pian:
|
||||
dj_pian_value = dj_pian.get('id')
|
||||
if dj_pian_value:
|
||||
target_pian_ids.add(dj_pian_value)
|
||||
if dj_pian_value not in pian_lookup:
|
||||
pian_lookup[dj_pian_value] = []
|
||||
# Add entity-specific metadata
|
||||
if typ_dat == "akce":
|
||||
meta.update({
|
||||
"akce_hlavni_vedouci": g(
|
||||
doc,
|
||||
'akce_hlavni_vedouci'
|
||||
),
|
||||
"akce_organizace": tr_code(g(
|
||||
doc,
|
||||
'akce_organizace'
|
||||
)),
|
||||
"akce_specifikace_data": tr_code(g(
|
||||
doc,
|
||||
'akce_specifikace_data'
|
||||
)),
|
||||
"akce_datum_zahajeni": g(
|
||||
doc,
|
||||
'akce_datum_zahajeni'
|
||||
),
|
||||
"akce_datum_ukonceni": g(
|
||||
doc,
|
||||
'akce_datum_ukonceni'
|
||||
),
|
||||
"akce_hlavni_typ": tr_code(g(
|
||||
doc,
|
||||
'akce_hlavni_typ'
|
||||
)),
|
||||
"akce_vedlejsi_typ": g_list(
|
||||
doc,
|
||||
'akce_vedlejsi_typ',
|
||||
translate=True
|
||||
),
|
||||
"lokalizace_okolnosti": (
|
||||
str(lokalizace)
|
||||
if lokalizace
|
||||
else ""
|
||||
),
|
||||
"akce_je_nz": (
|
||||
"Ano"
|
||||
if doc.get('akce_je_nz') is True
|
||||
else "Ne"
|
||||
),
|
||||
"projekt": g(
|
||||
doc,
|
||||
'akce_projekt',
|
||||
""
|
||||
),
|
||||
})
|
||||
|
||||
if komponenty == "true":
|
||||
# One feature per component –
|
||||
# all data on a single row, no relations needed
|
||||
if komps:
|
||||
for komp in komps:
|
||||
if not komp_projde_filtrem(
|
||||
komp, filter_areal,
|
||||
filter_datace, filters
|
||||
):
|
||||
elif typ_dat == "lokalita":
|
||||
meta.update({
|
||||
"lokalita_nazev": lokalita_nazev,
|
||||
"lokalita_popis": lokalita_popis,
|
||||
"lokalita_zachovalost": tr_code(g(
|
||||
doc,
|
||||
'lokalita_zachovalost'
|
||||
)),
|
||||
"lokalita_druh": tr_code(g(
|
||||
doc,
|
||||
'lokalita_druh'
|
||||
)),
|
||||
"lokalita_typ": tr_code(g(
|
||||
doc,
|
||||
'lokalita_typ_lokality'
|
||||
)),
|
||||
})
|
||||
|
||||
# Documentation units (DJ) within the record
|
||||
djs = doc.get('az_dokumentacni_jednotka', [])
|
||||
|
||||
for dj in djs:
|
||||
# Skip negative evidence units if requested
|
||||
if skip_negativni and dj.get('dj_negativni_jednotka') is True:
|
||||
continue
|
||||
|
||||
komps = dj.get('dj_komponenta', [])
|
||||
|
||||
if filter_areal or filter_datace:
|
||||
if not komps:
|
||||
continue
|
||||
if not any(
|
||||
komp_projde_filtrem(
|
||||
komp, filter_areal,
|
||||
filter_datace, filters
|
||||
)
|
||||
for komp in komps
|
||||
):
|
||||
continue
|
||||
|
||||
dj_id = dj.get('ident_cely')
|
||||
dj_typ = dj.get('dj_typ')
|
||||
|
||||
# Merge shared metadata with documentation unit-specific fields
|
||||
dj_meta = {
|
||||
**meta,
|
||||
'dj_id': dj_id,
|
||||
'dj_typ_value': dj_typ.get('value') if dj_typ else "",
|
||||
'dj_negativni': (
|
||||
"Negativní"
|
||||
if dj.get('dj_negativni_jednotka') is True
|
||||
else "Pozitivní"
|
||||
)
|
||||
}
|
||||
|
||||
# Link Documentation Unit to Geometry (PIAN)
|
||||
dj_pian = dj.get('dj_pian')
|
||||
if dj_pian:
|
||||
dj_pian_value = dj_pian.get('id')
|
||||
if dj_pian_value:
|
||||
target_pian_ids.add(dj_pian_value)
|
||||
if dj_pian_value not in pian_lookup:
|
||||
pian_lookup[dj_pian_value] = []
|
||||
|
||||
if komponenty == "true":
|
||||
# One feature per component –
|
||||
# all data on a single row, no relations needed
|
||||
if komps:
|
||||
for komp in komps:
|
||||
if not komp_projde_filtrem(
|
||||
komp, filter_areal,
|
||||
filter_datace, filters
|
||||
):
|
||||
continue
|
||||
|
||||
komp_meta = {
|
||||
**dj_meta,
|
||||
'komponenta_id': komp.get(
|
||||
'ident_cely',
|
||||
""
|
||||
),
|
||||
'komponenta_areal': (
|
||||
komp.get('komponenta_areal')
|
||||
or {}
|
||||
).get('value', ""),
|
||||
'komponenta_obdobi': (
|
||||
komp.get('komponenta_obdobi')
|
||||
or {}
|
||||
).get('value', ""),
|
||||
}
|
||||
pian_lookup[dj_pian_value].append(komp_meta)
|
||||
target_pian_ids_count += 1
|
||||
else:
|
||||
# DJ without components — still include
|
||||
# with empty component fields
|
||||
if filter_areal or filter_datace:
|
||||
continue
|
||||
|
||||
komp_meta = {
|
||||
empty_meta = {
|
||||
**dj_meta,
|
||||
'komponenta_id': komp.get(
|
||||
'ident_cely',
|
||||
""
|
||||
),
|
||||
'komponenta_areal': (
|
||||
komp.get('komponenta_areal')
|
||||
or {}
|
||||
).get('value', ""),
|
||||
'komponenta_obdobi': (
|
||||
komp.get('komponenta_obdobi')
|
||||
or {}
|
||||
).get('value', ""),
|
||||
'komponenta_id': "",
|
||||
'komponenta_areal': "",
|
||||
'komponenta_obdobi': "",
|
||||
}
|
||||
pian_lookup[dj_pian_value].append(komp_meta)
|
||||
pian_lookup[dj_pian_value].append(empty_meta)
|
||||
target_pian_ids_count += 1
|
||||
else:
|
||||
# DJ without components — still include
|
||||
# with empty component fields
|
||||
if filter_areal or filter_datace:
|
||||
continue
|
||||
|
||||
empty_meta = {
|
||||
**dj_meta,
|
||||
'komponenta_id': "",
|
||||
'komponenta_areal': "",
|
||||
'komponenta_obdobi': "",
|
||||
}
|
||||
pian_lookup[dj_pian_value].append(empty_meta)
|
||||
target_pian_ids_count += 1
|
||||
else:
|
||||
target_pian_ids_count += 1
|
||||
pian_lookup[dj_pian_value].append(dj_meta)
|
||||
pian_lookup[dj_pian_value].append(dj_meta)
|
||||
|
||||
if not target_pian_ids:
|
||||
iface.messageBar().pushMessage(
|
||||
"AMCR",
|
||||
f"Nalezeno {len(docs)} záznamů, ale žádný nemá geometrii.",
|
||||
level=Qgis.MessageLevel.Warning
|
||||
)
|
||||
return
|
||||
|
||||
# ==========================================
|
||||
# C) GEOMETRY FETCHING (PIAN)
|
||||
# ==========================================
|
||||
ids_list = list(target_pian_ids)
|
||||
total_pians = len(ids_list)
|
||||
docs_pian = []
|
||||
# Geometry requests are batch-processed
|
||||
# to stay under URL length limits:
|
||||
BATCH_PIAN = 200
|
||||
|
||||
if not target_pian_ids:
|
||||
iface.messageBar().pushMessage(
|
||||
"AMCR",
|
||||
f"Nalezeno {len(docs)} záznamů, ale žádný nemá geometrii.",
|
||||
level=Qgis.MessageLevel.Warning
|
||||
f"Záznamů: {len(docs)} (z toho {actions_with_geom} s mapou). "
|
||||
f"Stahuji {total_pians} unikátních geometrií, "
|
||||
f"vykresluji {target_pian_ids_count} geometrií...",
|
||||
level=Qgis.MessageLevel.Info
|
||||
)
|
||||
return
|
||||
|
||||
# ==========================================
|
||||
# C) GEOMETRY FETCHING (PIAN)
|
||||
# ==========================================
|
||||
ids_list = list(target_pian_ids)
|
||||
total_pians = len(ids_list)
|
||||
docs_pian = []
|
||||
# Geometry requests are batch-processed
|
||||
# to stay under URL length limits:
|
||||
BATCH_PIAN = 200
|
||||
fl_pian = [
|
||||
"ident_cely",
|
||||
"pian_typ",
|
||||
"pian_chranene_udaje",
|
||||
"pian_presnost",
|
||||
]
|
||||
|
||||
iface.messageBar().pushMessage(
|
||||
"AMCR",
|
||||
f"Záznamů: {len(docs)} (z toho {actions_with_geom} s mapou). "
|
||||
f"Stahuji {total_pians} unikátních geometrií, "
|
||||
f"vykresluji {target_pian_ids_count} geometrií...",
|
||||
level=Qgis.MessageLevel.Info
|
||||
)
|
||||
for i in range(0, total_pians, BATCH_PIAN):
|
||||
batch = ids_list[i: i + BATCH_PIAN]
|
||||
or_query = " OR ".join(batch)
|
||||
fq_pian = f"ident_cely:({or_query})"
|
||||
|
||||
fl_pian = [
|
||||
"ident_cely",
|
||||
"pian_typ",
|
||||
"pian_chranene_udaje",
|
||||
"pian_presnost",
|
||||
]
|
||||
params_pian = {
|
||||
"mapa": "true",
|
||||
"entity": "pian",
|
||||
"q": fq_pian,
|
||||
"rows": len(batch),
|
||||
"fl": ",".join(fl_pian),
|
||||
}
|
||||
try:
|
||||
QApplication.processEvents()
|
||||
r_json = _api_get_json(url, params=params_pian, timeout=15)
|
||||
docs_pian.extend(r_json.get('response', {}).get('docs', []))
|
||||
except requests.exceptions.RequestException as e:
|
||||
# Network is down – stop immediately instead of
|
||||
# uselessly retrying every remaining batch
|
||||
network_error = True
|
||||
QgsMessageLog.logMessage(
|
||||
f"Chyba sítě při stahování geometrií PIAN: {e}",
|
||||
"AMČR", Qgis.MessageLevel.Critical
|
||||
)
|
||||
break
|
||||
except Exception as e:
|
||||
QgsMessageLog.logMessage(
|
||||
f"Chyba PIAN: {e}",
|
||||
"AMČR", Qgis.MessageLevel.Warning
|
||||
)
|
||||
|
||||
for i in range(0, total_pians, BATCH_PIAN):
|
||||
batch = ids_list[i: i + BATCH_PIAN]
|
||||
or_query = " OR ".join(batch)
|
||||
fq_pian = f"ident_cely:({or_query})"
|
||||
elif typ_dat == "samostatny_nalez":
|
||||
for doc in docs:
|
||||
loc = g(doc, "loc", [])
|
||||
if not loc:
|
||||
continue
|
||||
|
||||
params_pian = {
|
||||
"mapa": "true",
|
||||
"entity": "pian",
|
||||
"q": fq_pian,
|
||||
"rows": len(batch),
|
||||
"fl": ",".join(fl_pian),
|
||||
}
|
||||
try:
|
||||
QApplication.processEvents()
|
||||
r_json = _api_get_json(url, params=params_pian, timeout=15)
|
||||
docs_pian.extend(r_json.get('response', {}).get('docs', []))
|
||||
except requests.exceptions.RequestException as e:
|
||||
# Network is down – stop immediately instead of
|
||||
# uselessly retrying every remaining batch
|
||||
network_error = True
|
||||
QgsMessageLog.logMessage(
|
||||
f"Chyba sítě při stahování geometrií PIAN: {e}",
|
||||
"AMČR", Qgis.MessageLevel.Critical
|
||||
)
|
||||
break
|
||||
except Exception as e:
|
||||
QgsMessageLog.logMessage(
|
||||
f"Chyba PIAN: {e}",
|
||||
"AMČR", Qgis.MessageLevel.Warning
|
||||
)
|
||||
actions_with_geom += 1
|
||||
|
||||
sn_chranene = doc.get("samostatny_nalez_chranene_udaje") or {}
|
||||
|
||||
|
||||
|
||||
# ==========================================
|
||||
# D) LAYER CREATION (QGIS Memory Layers)
|
||||
# ==========================================
|
||||
|
||||
archeologicky_zaznam = "Akce" if typ_dat == "akce" else "Lokalita"
|
||||
archeologicky_zaznam = typ_dat_vocab[typ_dat]
|
||||
|
||||
# Initialize three layers for different geometry types (S-JTSK CRS)
|
||||
vl_poly = QgsVectorLayer(
|
||||
|
||||
@@ -90,6 +90,7 @@ class AmcrViewer:
|
||||
"""
|
||||
# Define paths for action-specific icons
|
||||
icon_akce_path = os.path.join(self.plugin_dir, 'akce.png')
|
||||
icon_pas_path = os.path.join(self.plugin_dir, 'akce.png')
|
||||
icon_lokality_path = os.path.join(self.plugin_dir, 'lokality.png')
|
||||
icon_amcr_help_path = os.path.join(self.plugin_dir, 'amcr-help.png')
|
||||
|
||||
@@ -109,6 +110,16 @@ class AmcrViewer:
|
||||
)
|
||||
self.plugin_menu.addAction(self.action_download_akce)
|
||||
|
||||
self.action_download_pas = self.add_action(
|
||||
icon_path=icon_pas_path,
|
||||
text=self.tr(u'Stáhnout data samostatných nálezů | AMČR Viewer'),
|
||||
callback=lambda checked=False: self.run_download('samostatny_nalez'),
|
||||
parent=self.iface.mainWindow(),
|
||||
add_to_menu=False,
|
||||
add_to_toolbar=False
|
||||
)
|
||||
self.plugin_menu.addAction(self.action_download_pas)
|
||||
|
||||
self.action_download_lokality = self.add_action(
|
||||
icon_path=icon_lokality_path,
|
||||
text=self.tr(u'Stáhnout data lokalit | AMČR Viewer'),
|
||||
|
||||
+16835
-21873
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user