def geocode_and_stack(infile: Path, outfile: Path, temp_dir: Path = Path("tmp/"), dem_type="AW3D30", **kwargs): """Process one sentinel 1 zip file. The main preprocessing is done inside geocode. In the future, this may be rewritten to allow more control over the processing graph""" bbox = identify(str(infile)).bbox() print("Downloading DEM files") vrt = dem_autoload(geometries=[bbox], demType=dem_type, vrt=f"{dem_type}.vrt", buffer=0.01) print("Converting DEM Files") dem_create(vrt, str(f"{dem_type}.tif")) geocode(str(infile), str(temp_dir), externalDEMFile=str(f"{dem_type}.tif"), **kwargs) os.remove(f"{dem_type}.tif") file_list = [ str(file) for file in temp_dir.iterdir() if file.suffix == ".tif" ] print("Stacking GeoTiffs") stack_geotiffs(file_list, str(outfile)) shutil.rmtree(temp_dir)
def test_dem_create(tmpdir): with bbox({ 'xmin': 11.5, 'xmax': 11.9, 'ymin': 51, 'ymax': 51.5 }, crs=4326) as box: with pytest.raises(RuntimeError): files = dem_autoload([box], 'foobar') vrt = dem_autoload([box], 'SRTM 3Sec', vrt='/vsimem/test.vrt') out = os.path.join(str(tmpdir), 'srtm.tif') dem_create(src=vrt, dst=out, t_srs=32632, tr=(90, 90)) assert os.path.isfile(out)
dem_snap = os.path.join(args.path, dem_base + '.tif') if not os.path.isfile(dem_snap): # compile vrt of tiles covering aoi with Vector(args.aoi) as site: vrt = dem_autoload(geometries=[site], demType=args.dem, vrt='/vsimem/{}.vrt'.format(dem_base), buffer=0.1, username='******', password='******') # create a DEM GTiff file from the VRT dem_create(src=vrt, dst=dem_snap, t_srs=args.srs, tr=(args.res, args.res), geoid_convert=True) print('Created SNAP dem file: ' + dem_snap) # create gamma dem files dem_base = 'dem_gamma_{}'.format(dem_id) dem_gamma = os.path.join(args.path, dem_base) if not os.path.isfile(dem_gamma): parfile = dem_gamma + '.par' if not os.path.isfile(parfile): # note - hardcoded utm wgs84 with 4326 datum - UTM zone (60), false northing (north = 0, south hemisphere = 1000000 ) inlist = [
import os from spatialist import gdalwarp, Vector #set directories maindir = '/exports/csce/datastore/geos/groups/MSCGIS/s1937352/DEM_Uganda' os.chdir(maindir) #specify shapefile of region of interest roi = '/exports/csce/datastore/geos/groups/MSCGIS/s1937352/DEM_Uganda/UgandaBigBox.shp' epsg = 21036 #specify epsg demType = 'SRTM 1Sec HGT' #specify dem type (Others: AW3D30, SRTM 3Sec) dem_name = 'UgandaBig10' dem_path = f'{maindir}/{dem_name}/.tif' if not os.path.isfile(dem_path): #create VRT with the specified DEM and Bounding Box with Vector(roi) as bbox: vrt = dem_autoload(geometries=[bbox], demType=demType, vrt=f'/{dem_name}.vrt', buffer=0.1) # create a tif from the VRT with a given resolution dem_create(src=vrt, dst=dem_path, t_srs=epsg, tr=(10, 10), geoid_convert=True) # convert tif to Gamma files (dem, dem_par) gammastring = f'dem_import UgandaBig10.tif UgandaB.dem UgandaB.dem_par - - /opt/gamma/20190613/DIFF/scripts/egm96.dem /opt/gamma/20190613/DIFF/scripts/egm96.dem_par' os.system(gammastring)