def transform(infile, outfile, posting=90): """ transform SRTM DEM from EQA to UTM projection """ # read DEM parameter file par = ISPPar(infile + '.par') # transform corner coordinate to UTM utm = UTM(infile + '.par') for item in [outfile, outfile + '.par']: if os.path.isfile(item): os.remove(item) # determine false northing from parameter file coordinates falsenorthing = 10000000. if par.corner_lat < 0 else 0 # create new DEM parameter file with UTM projection details inlist = [ 'UTM', 'WGS84', 1, utm.zone, falsenorthing, os.path.basename(outfile), '', '', '', '', '', '-{0} {0}'.format(posting), '' ] process(['create_dem_par', outfile + '.par'], inlist=inlist) # transform dem process([ 'dem_trans', infile + '.par', infile, outfile + '.par', outfile, '-', '-', '-', 1 ]) hdr(outfile + '.par')
def fill(dem, dem_out, logpath=None, replace=False): width = ISPPar(dem + '.par').width path_dem = os.path.dirname(dem_out) rpl_flg = 0 dtype = 4 # replace values value = 0 new_value = 1 process([ 'replace_values', dem, value, new_value, dem + '_temp', width, rpl_flg, dtype ], path_dem, logpath) value = -32768 new_value = 0 process([ 'replace_values', dem + '_temp', value, new_value, dem + '_temp2', width, rpl_flg, dtype ], path_dem, logpath) # interpolate missing values r_max = 9 np_min = 40 np_max = 81 w_mode = 2 process([ 'interp_ad', dem + '_temp2', dem_out, width, r_max, np_min, np_max, w_mode, dtype ], path_dem, logpath) # remove temporary files os.remove(dem + '_temp') os.remove(dem + '_temp2') # duplicate parameter file for newly created dem shutil.copy(dem + '.par', dem_out + '.par') # create ENVI header file hdr(dem_out + '.par') if replace: for item in [ dem + x for x in ['', '.par', '.hdr', '.aux.xml'] if os.path.isfile(dem + x) ]: os.remove(item)
def test_envi(tmpdir): with pytest.raises(RuntimeError): obj = HDRobject(1) with pytest.raises(RuntimeError): obj = HDRobject('foobar') outname = os.path.join(str(tmpdir), 'test') with HDRobject() as header: header.band_names = ['band1'] header.write(outname) outname += '.hdr' with HDRobject(outname) as header: assert header.band_names == ['band1'] vals = vars(header) with HDRobject(vals) as header: assert header.byte_order == 0 hdr(vals, outname + '2')
def par2hdr(parfile, hdrfile): """ Create an ENVI HDR file from a Gamma PAR file Parameters ---------- parfile: str the Gamma parfile hdrfile: str the ENVI HDR file Returns ------- """ with ISPPar(parfile) as par: hdr(par.envidict(), hdrfile)
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)
def swap(data, outname): """ byte swapping from small to big endian (as required by GAMMA) """ rast = raster.Raster(data) dtype = rast.dtype if rast.format != 'ENVI': raise IOError('only ENVI format supported') dtype_lookup = { 'Int16': 2, 'CInt16': 2, 'Int32': 4, 'Float32': 4, 'CFloat32': 4, 'Float64': 8 } if dtype not in dtype_lookup: raise IOError('data type {} not supported'.format(dtype)) process(['swap_bytes', data, outname, str(dtype_lookup[dtype])]) header = HDRobject(data + '.hdr') header.byte_order = 1 hdr(header, outname + '.hdr')
def par2hdr(parfile, hdrfile, modifications=None, nodata=None): """ Create an ENVI HDR file from a Gamma PAR file Parameters ---------- parfile: str the Gamma parfile hdrfile: str the ENVI HDR file modifications: dict or None a dictionary containing value deviations to write to the HDR file nodata: int, float or None a no data value to write to the HDR file via attribute 'data ignore value' Returns ------- Examples -------- >>> from pyroSAR.gamma.auxil import par2hdr >>> par2hdr('dem_seg.par', 'inc.hdr') # write a HDR file for byte data based on a parfile of float data >>> par2hdr('dem_seg.par', 'ls_map.hdr', modifications={'data_type': 1}) See Also -------- :class:`spatialist.envi.HDRobject` :func:`spatialist.envi.hdr` """ with ISPPar(parfile) as par: items = par.envidict(nodata) if modifications is not None: items.update(modifications) hdr(items, hdrfile)