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 with vector.Vector(shp) as site: # reproject the shapefile to latlon (in-memory, no file written or modified) site.reproject('+proj=longlat +datum=WGS84 +no_defs ') # 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 # 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' # 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 par2hdr(outname + '.par', outname + '.hdr') # 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 dem.transform(outname, outname + '_utm', posting=20)
def get_tile(START, END, gjson, out): # search database for matching archives print('Querying database...') footprint = geojson_to_wkt(read_geojson(gjson)) products = api.query(footprint, ingestiondate=(START, END), platformname='Sentinel-1', producttype='GRD', sensoroperationalmode='IW', orbitdirection='ASCENDING', polarisationmode='VH') # download archive print('Downloading archive...') pmd = api.download_all(products, directory_path='./temp/') fname = './temp/' + list(pmd[0].values())[0]['title'] + '.zip' # unpack and ingest print('Unpacking archive...') scene = pyroSAR.identify(fname) scene.unpack('./temp/', overwrite=True) # geocode print('Geocoding data...') shp = vector.Vector(filename=gjson) geocode(infile=scene, outdir='./temp/', tr=int(sys.argv[3]), scaling='db', removeS1ThermalNoise=True, demResamplingMethod='BISINC_21_POINT_INTERPOLATION', terrainFlattening=True, allow_RES_OSV=True, speckleFilter='Refined Lee', shapefile=shp, cleanup=True) # save image print('Copying image...') smd = scene.scanMetadata() iname = './temp/' + [ file for file in os.listdir('./temp/') if '{}__{}___{}_{}_VH'.format(smd['sensor'], smd['acquisition_mode'], smd['orbit'], smd['start']) in file ][0] shutil.copy2(iname, out) # done print('Done.')
def worker(sitename): ####################################################################################### # setup general processing parameters resolution = 20 ####################################################################################### # define the directories for writing temporary and final results sitedir = os.path.join(maindir, sitename) outdir = os.path.join(sitedir, 'proc_out') ####################################################################################### # load the test site geometry into a vector object sites = vector.Vector('/.../testsites.shp') # query the test site by name; a column name 'Site_Name' must be saved in your shapefile site = sites["Site_Name='{}'".format(sitename)] ####################################################################################### # query the database for scenes to be processed with Archive(dbfile) as archive: selection_proc = archive.select(vectorobject=site, processdir=outdir, sensor=('S1A', 'S1B'), product='GRD', acquisition_mode='IW', vv=1) print('{0}: {1} scenes found for site {2}'.format(socket.gethostname(), len(selection_proc), sitename)) ####################################################################################### # call to processing utility if len(selection_proc) > 1: print('start processing') for scene in selection_proc: geocode(infile=scene, outdir=outdir, tr=resolution, scaling='db') return len(selection_proc)
def worker(sitename): ####################################################################################### # setup general processing parameters resolution = 20 # number of processes for Python pathos framework (multiple scenes in parallel) parallel1 = 6 # number of parallel OpenMP threads; this is used by GAMMA internally parallel2 = 6 os.environ['OMP_NUM_THREADS'] = str(parallel2) ####################################################################################### # get the maximum date of the precise orbit files # as type also 'RES' can be selected. These files are not as precise as POE and thus geocoding might not be # quite as accurate with OSV(osvdir) as osv: maxdate = osv.maxdate(osvtype='POE', datetype='stop') ####################################################################################### # define the directories for writing temporary and final results sitedir = os.path.join(maindir, sitename) tempdir = os.path.join(sitedir, 'proc_in') outdir = os.path.join(sitedir, 'proc_out') ####################################################################################### # load the test site geometry into a vector object sites = vector.Vector('/.../testsites.shp') # query the test site by name; a column name 'Site_Name' must be saved in your shapefile site = sites["Site_Name='{}'".format(sitename)] ####################################################################################### # query the database for scenes to be processed with Archive(dbfile) as archive: selection_proc = archive.select(vectorobject=site, processdir=outdir, maxdate=maxdate, sensor=('S1A', 'S1B'), product='GRD', acquisition_mode='IW', vv=1) print('{0}: {1} scenes found for site {2}'.format(socket.gethostname(), len(selection_proc), sitename)) ####################################################################################### # define the DEM file demfile = '{0}/{1}/DEM/{1}_srtm_utm'.format(maindir, sitename) if not os.path.isfile(demfile): print('DEM missing for site {}'.format(sitename)) return ####################################################################################### # call to processing utility if len(selection_proc) > 1: print('start processing') if len(selection_proc) > 1: if len(selection_proc) < parallel1: parallel1 = len(selection_proc) # run the function on multiple cores in parallel multicore(geocode, cores=parallel1, multiargs={'scene': selection_proc}, dem=demfile, tempdir=tempdir, outdir=outdir, targetres=resolution, scaling='db', func_geoback=2, func_interp=0, sarSimCC=False, osvdir=osvdir, cleanup=True, allow_RES_OSV=False) elif len(selection_proc) == 1: scene = selection_proc[0] # run the function on a single core geocode(scene, dem=demfile, tempdir=tempdir, outdir=outdir, targetres=resolution, scaling='db', func_geoback=2, func_interp=0, sarSimCC=False, osvdir=osvdir, cleanup=True, allow_RES_OSV=False) return len(selection_proc)