Integrate infrastructure preparation and qualified map delivery with local scenes. Add semantic editing, object provenance, survey caching and operator scene tools.
78 lines
3.4 KiB
Python
78 lines
3.4 KiB
Python
"""Reuse verified survey derivatives across vectorizer releases without copying them."""
|
|
import hashlib
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
|
|
from local_state import read_json, write_json
|
|
|
|
|
|
def file_hash(path):
|
|
result = hashlib.sha256()
|
|
with Path(path).open('rb') as stream:
|
|
for block in iter(lambda: stream.read(8 * 1024**2), b''):
|
|
result.update(block)
|
|
return result.hexdigest()
|
|
|
|
|
|
def survey_identity(module, terrain, profile):
|
|
# Vector code may change without invalidating the measured survey. All code
|
|
# that creates cameras, renders, segments or projects ground remains bound.
|
|
files = {name: file_hash(module / name) for name in
|
|
('whole_scene.py', 'mapping.py', 'atlas.py', 'job.example.json')}
|
|
files['terrain.py'] = file_hash(terrain)
|
|
content = dict(files=files, mapper_image=profile['mapper_image'],
|
|
semantic_model=profile['semantic_model'])
|
|
return hashlib.sha256(json.dumps(content, sort_keys=True).encode()).hexdigest()
|
|
|
|
|
|
def verify_tiles(folder, request, pulse):
|
|
index = read_json(folder / 'tile-index.json')
|
|
if not index or not 0 < len(index.get('tiles', [])) <= 256:
|
|
raise ValueError('Missing completed survey index')
|
|
for tile in index['tiles']:
|
|
pulse()
|
|
if not re.fullmatch(r'tile-\d{3}-\d{3}', tile['id']):
|
|
raise ValueError('Invalid survey tile path')
|
|
if tile['status'] == 'no-surface':
|
|
continue
|
|
if tile['status'] not in ('surveyed', 'unobserved-ground'):
|
|
raise ValueError('Survey is incomplete')
|
|
part = folder / tile['id']
|
|
receipt = read_json(part / 'survey-receipt.json')
|
|
job = read_json(part / 'job.json')
|
|
if (not receipt or not job or receipt['status'] != tile['status']
|
|
or receipt['job_sha256'] != file_hash(part / 'job.json')
|
|
or job['source'] != request['source'] or job['model'] != request['model']):
|
|
raise ValueError('Survey input identity changed')
|
|
if set(receipt['files']) != {'top.png', 'top.geometry.npz', 'surface-atlas.npz'}:
|
|
raise ValueError('Unexpected survey artifacts')
|
|
for name, expected in receipt['files'].items():
|
|
if file_hash(part / name) != expected:
|
|
raise ValueError('Survey artifact changed')
|
|
return index
|
|
|
|
|
|
def record_survey(folder, request, identity, pulse):
|
|
verify_tiles(folder, request, pulse)
|
|
write_json(folder / 'survey-cache.json', dict(
|
|
schema_version='missioncore.infrastructure-survey-cache/v1',
|
|
survey_sha256=identity, source=request['source'], model=request['model'],
|
|
index_sha256=file_hash(folder / 'tile-index.json')))
|
|
|
|
|
|
def find_survey(jobs, request, identity, pulse):
|
|
for path in sorted(jobs.glob('*/survey-cache.json')):
|
|
try:
|
|
record = read_json(path)
|
|
if (record['schema_version'] != 'missioncore.infrastructure-survey-cache/v1'
|
|
or record['survey_sha256'] != identity
|
|
or record['source'] != request['source'] or record['model'] != request['model']
|
|
or record['index_sha256'] != file_hash(path.parent / 'tile-index.json')):
|
|
continue
|
|
verify_tiles(path.parent, request, pulse)
|
|
return path.parent
|
|
except (OSError, ValueError, KeyError, TypeError):
|
|
continue
|
|
return None
|