def test_run(tmpdir, testdata): log = os.path.join(str(tmpdir), 'test_run.log') out, err = anc.run(cmd=['gdalinfo', testdata['tif']], logfile=log, void=False) with pytest.raises(OSError): anc.run(['foobar']) with pytest.raises(sp.CalledProcessError): anc.run(['gdalinfo', 'foobar'])
def main(): # define a shapefile for the study area shp = '/.../testsite.shp' # define the output name of the DEM (no file extension like .tif etc.!) outname = '/path/to/demfile' # define a buffer around the study area boundaries (in degrees) buffer = 0.01 # load the defined shapefile site = vector.Vector(shp) # reproject the shapefile to latlon (in-memory, no file written or modified) site.reproject('+proj=longlat +datum=WGS84 +no_defs ') # define a GDAL VRT file containing all SRTM tiles # this file has all hgt tiles in the same directory registered and is used for subsetting/mosaicing srtm_vrt = '/path/to/SRTM_1_HGT.vrt' # extract the extent (bounding box) of the shapefile ext = site.extent # add the buffer to the bounding box ext['xmin'] -= buffer ext['ymin'] -= buffer ext['xmax'] += buffer ext['ymax'] += buffer # create a temporary directory for writing intermediate files (will be deleted at the end) tmpdir = outname + '__tmp' if not os.path.isdir(tmpdir): os.makedirs(tmpdir) # define a name for a temporary DEM file dem_tmp = os.path.join(tmpdir, 'srtm_tmp.tif') # create a DEM mosaic for the study site run([ 'gdalwarp', '-q', '-of', 'GTiff', '-te', ext['xmin'], ext['ymin'], ext['xmax'], ext['ymax'], srtm_vrt, dem_tmp ]) # transform the DEM to GAMMA format (including EGM96 geoid to WGS84 ellipsoid height reference correction) gamma.process(['srtm2dem', dem_tmp, outname, outname + '.par', 2, '-'], outdir=tmpdir) # create an ENVI header file hdr(outname + '.par') # remove the temporary directory with all intermediate files shutil.rmtree(tmpdir) # optional: transform DEM to UTM projection # the UTM zone is automatically computed for the center of the DEM file srtm.transform(outname, outname + '_utm', posting=20)
def test_run(self): log = 'pyroSAR/tests/data/test_run.log' out, err = anc.run(cmd=['gdalinfo', 'pyroSAR/tests/data/S1A__IW___A_20150309T173017_VV_grd_mli_geo_norm_db.tif'], logfile=log, void=False) os.remove(log) with self.assertRaises(OSError): anc.run(['foobar']) with self.assertRaises(sp.CalledProcessError): anc.run(['gdalinfo', 'foobar'])
def test_run(tmpdir): log = os.path.join(str(tmpdir), 'test_run.log') out, err = anc.run(cmd=[ 'gdalinfo', 'ci_testing/tests/data/S1A__IW___A_20150309T173017_VV_grd_mli_geo_norm_db.tif' ], logfile=log, void=False) with pytest.raises(OSError): anc.run(['foobar']) with pytest.raises(sp.CalledProcessError): anc.run(['gdalinfo', 'foobar'])
def mosaic(demlist, outname, byteorder=1, gammapar=True): """ mosaicing of multiple DEMs """ if len(demlist) < 2: raise IOError('length of demlist < 2') nodata = str(raster.Raster(demlist[0]).nodata) run([ 'gdalwarp', '-q', '-of', 'ENVI', '-srcnodata', nodata, '-dstnodata', nodata, demlist, outname ]) if byteorder == 1: swap(outname, outname + '_swap') for item in [outname, outname + '.hdr', outname + '.aux.xml']: os.remove(item) os.rename(outname + '_swap', outname) os.rename(outname + '_swap.hdr', outname + '.hdr') if gammapar: dempar(outname)
def makeSRTM(scenes, srtmdir, outname): """ Create a DEM from SRTM tiles Input is a list of pyroSAR.ID objects from which coordinates are read to determine the required DEM extent Mosaics SRTM DEM tiles, converts them to Gamma format and subtracts offset to WGS84 ellipsoid for DEMs downloaded from USGS http://gdex.cr.usgs.gov or CGIAR http://srtm.csi.cgiar.org """ tempdir = outname + '___temp' os.makedirs(tempdir) hgt_options = hgt(scenes) hgt_files = finder(srtmdir, hgt_options) # todo: check if really needed nodatas = [str(int(raster.Raster(x).nodata)) for x in hgt_files] srtm_vrt = os.path.join(tempdir, 'srtm.vrt') srtm_temp = srtm_vrt.replace('.vrt', '_tmp') srtm_final = srtm_vrt.replace('.vrt', '') run([ 'gdalbuildvrt', '-overwrite', '-srcnodata', ' '.join(nodatas), srtm_vrt, hgt_files ]) run([ 'gdal_translate', '-of', 'ENVI', '-a_nodata', -32768, srtm_vrt, srtm_temp ]) process(['srtm2dem', srtm_temp, srtm_final, srtm_final + '.par', 2, '-'], outdir=tempdir) shutil.move(srtm_final, outname) shutil.move(srtm_final + '.par', outname + '.par') hdr(outname + '.par') shutil.rmtree(tempdir)