Skip to content

Commit

Permalink
small modifications for custom SAFE support
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestfwilliams committed Mar 20, 2024
1 parent a7d5f46 commit 2d050dd
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/hyp3_isce2/slc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from subprocess import PIPE, run
from zipfile import ZipFile

import lxml.etree as ET
from hyp3lib.fetch import download_file
from hyp3lib.scene import get_download_url
from shapely import geometry
Expand All @@ -19,6 +20,10 @@ def get_granule(granule: str) -> Path:
Returns:
The path to the unzipped granule
"""
if Path(f'{granule}.SAFE').exists():
print('SAFE file already exists, skipping download.')
return Path.cwd() / f'{granule}.SAFE'

download_url = get_download_url(granule)
zip_file = download_file(download_url, chunk_size=10485760)
safe_dir = unzip_granule(zip_file, remove=True)
Expand All @@ -41,6 +46,16 @@ def get_geometry_from_kml(kml_file: str) -> Polygon:
return geometry.shape(geojson)


def get_geometry_from_manifest(manifest_path: Path):
manifest = ET.parse(manifest_path).getroot()
frame_element = [x for x in manifest.findall('.//metadataObject') if x.get('ID') == 'measurementFrameSet'][0]
frame_string = frame_element.find('.//{http://www.opengis.net/gml}coordinates').text
coord_strings = [pair.split(',') for pair in frame_string.split(' ')]
coords = [(float(lon), float(lat)) for lat, lon in coord_strings]
footprint = Polygon(coords)
return footprint


def get_dem_bounds(reference_granule: Path, secondary_granule: Path) -> tuple:
"""Get the bounds of the DEM to use in processing from SAFE KML files
Expand All @@ -53,7 +68,7 @@ def get_dem_bounds(reference_granule: Path, secondary_granule: Path) -> tuple:
"""
bboxs = []
for granule in (reference_granule, secondary_granule):
footprint = get_geometry_from_kml(str(granule / 'preview' / 'map-overlay.kml'))
footprint = get_geometry_from_manifest(granule / 'manifest.safe')
bbox = geometry.box(*footprint.bounds)
bboxs.append(bbox)

Expand Down

0 comments on commit 2d050dd

Please sign in to comment.