Skip to content

Instantly share code, notes, and snippets.

@jmigueldelgado
Last active May 30, 2022 13:03
Show Gist options
  • Save jmigueldelgado/6ca8c17d5b27d595aeb91c30549b5bed to your computer and use it in GitHub Desktop.
Save jmigueldelgado/6ca8c17d5b27d595aeb91c30549b5bed to your computer and use it in GitHub Desktop.
Helper script to create an archive with mocked raster and vector files and real XML files out of a DIMAP or similar satellite imagery product
from pathlib import Path
import zipfile
import os
import datetime
import rasterio
from rasterio.io import MemoryFile
import numpy
def create_mock_product(product_path: Path, collection_name: str, product_configuration: str, keep_tifs: bool=True):
local_path = (Path.cwd() / Path(__file__).parent).resolve() / collection_name / product_configuration
local_path.mkdir(parents=True, exist_ok=True)
level_to_keep = len(product_path.parts)
right_now = int(datetime.datetime.now().timestamp())
with zipfile.ZipFile(local_path / f"{product_path.name}.zip", "w", zipfile.ZIP_DEFLATED) as zip_archive:
for p in product_path.rglob("*"):
os.utime(p, (right_now, right_now))
relative_path="/".join(p.parts[level_to_keep:])
if p.stem == '.DS_Store' or p.suffix == "":
continue
if p.suffix in [".XML", ".TFW"]:
zip_archive.write(p, p.relative_to(product_path))
continue
if p.suffix.upper() == ".TIF" and keep_tifs:
with MemoryFile() as memfile, zip_archive.open(str(relative_path), 'w') as tif_file:
with rasterio.open(p, 'r') as src, memfile.open(**src.profile) as dst:
for band in range(1,src.count+1):
zeros = numpy.zeros(src.shape)
dst.write(zeros, band)
tif_file.write(memfile.read())
continue
zip_archive.writestr(str(relative_path),"")
# PHR Analytic
# path = Path('/Users/jose.delgado/playground/phr/analytic/oneatlas_0bc33351-13ff-49e4-9598-2e4f069e610f')
# create_mock_product(path, 'phr', 'analytic')
# SPOT
# PNEO
path = Path('/Users/jose.delgado/playground/pleiades-neo/test-data/gtiff-16bit/WO_000001686_1_1_STD_A', keep_tifs=True)
create_mock_product(path, 'pneo', 'analytic')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment