示例#1
0
def main():
    # define input directory containing file sto be stacked
    dir_in = '/...'
    
    # define output file name
    dstfile = '/.../x'
    
    # shapefile (for stack boundaries)
    shp = '/../x.shp'
    
    # store results in separate files or one single stack file? If separate then dstfile is used as a directory.
    sep = True
    
    # list files to be resampled; those not overlapping with the shapefile geometry will excluded by function stack
    srcfiles = finder(dir_in, ['S1*_VV_*norm_db.tif'])
    
    # check whether dstfile is already a file
    if os.path.isfile(dstfile):
        raise IOError('dstfile already exists')
    
    # create groups of similar time stamps for mosaicking.
    # All images with a time stamp of less than 30s difference will be grouped
    groups = groupbyTime(srcfiles, seconds, 30)
    
    # final function call
    # groups will be mosaicked first
    # the resulting images will all have the same extent
    stack(srcfiles=groups, dstfile=dstfile, resampling='bilinear',
          targetres=[20, 20], srcnodata=-99, dstnodata=-99,
          shapefile=shp, sortfun=seconds, separate=sep, overwrite=False)
示例#2
0
def main():

    dirs = [
        "stack_savi"
    ]  #, "stack_ndvi", "stack_msavi", "stack_reip", "stack_rvi", "stack_dvi"]

    resolution = [30, 30]

    # shapefile (for stack boundaries)
    shp = 'F:/geodata/geo402/02_features/LADYBRAND_final_enlarged_study_area.shp'

    # store results in separate files or one single stack file? If separate then dstfile is used as a directory.
    sep = True

    for dir in dirs:

        # define input directory containing files to be stacked
        dir_in = 'F:/geodata/geo402/S2/xx_S2_indices/' + dir
        print(dir_in)
        os.makedirs(dir_in, exist_ok=True)

        # define output file name
        dstfile = 'F:/geodata/geo402/S2/xx_S2_indices/mosaics/' + dir
        print(dstfile)

        # list files to be resampled; those not overlapping with the shapefile geometry will excluded by function stack
        srcfiles = finder(dir_in, ['*'])

        # check whether dstfile is already a file
        if os.path.isfile(dstfile):
            raise IOError('dstfile already exists')

        # create groups of similar time stamps for mosaicking.
        # All images with a time stamp of less than 30s difference will be grouped
        groups = groupbyTime(srcfiles, seconds, 30)

        # final function call
        # groups will be mosaicked first
        # the resulting images will all have the same extent
        stack(srcfiles=groups,
              dstfile=dstfile,
              resampling='bilinear',
              targetres=resolution,
              srcnodata=-9999,
              dstnodata=-9999,
              shapefile=shp,
              sortfun=seconds,
              separate=sep,
              overwrite=True)
def main():
    '''
    both polarisations are included in the script but are stored in different folders
    '''

    resolution = [30, 30]

    # shapefile (for stack boundaries)
    shp = 'F:/geodata/geo402/##study_area/LADYBRAND_final_enlarged_study_area.shp'

    # store results in separate files or one single stack file? If separate then dstfile is used as a directory.
    sep = False

    # define input directory containing files to be stacked
    dir_in = 'F:/geodata/geo402/S1_GRD/xx_new/GRD_VH_vrts/'
    print(dir_in)
    # os.makedirs(dir_in, exist_ok=True)

    # define output file name
    dstfile = 'F:/geodata/geo402/S1_GRD/xx_new/S1A_IW_GRD_VH_stack'
    print(dstfile)

    # list files to be resampled; those not overlapping with the shapefile geometry will excluded by function stack
    srcfiles = finder(dir_in, ['*'])

    # check whether dstfile is already a file
    if os.path.isfile(dstfile):
        raise IOError('dstfile already exists')

    # create groups of similar time stamps for mosaicking.
    # All images with a time stamp of less than 30s difference will be grouped
    groups = groupbyTime(srcfiles, seconds, 30)

    # final function call
    # groups will be mosaicked first
    # the resulting images will all have the same extent
    stack(srcfiles=groups,
          dstfile=dstfile,
          resampling='bilinear',
          targetres=resolution,
          srcnodata=-99,
          dstnodata=-99,
          shapefile=shp,
          sortfun=seconds,
          separate=sep,
          overwrite=True,
          cores=7)
示例#4
0
def test_stack(tmpdir, testdata):
    name = testdata['tif']
    outname = os.path.join(str(tmpdir), 'test')
    tr = (30, 30)
    # no input files provided
    with pytest.raises(RuntimeError):
        stack(srcfiles=[],
              resampling='near',
              targetres=tr,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # two files, but only one layer name
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name],
              resampling='near',
              targetres=tr,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname,
              layernames=['a'])

    # targetres must be a two-entry tuple/list
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name],
              resampling='near',
              targetres=30,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # only one file specified
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name],
              resampling='near',
              targetres=tr,
              overwrite=True,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # targetres must contain two values
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name],
              resampling='near',
              targetres=(30, 30, 30),
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # unknown resampling method
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name],
              resampling='foobar',
              targetres=tr,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # non-existing files
    with pytest.raises(RuntimeError):
        stack(srcfiles=['foo', 'bar'],
              resampling='near',
              targetres=tr,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # create a multi-band stack
    stack(srcfiles=[name, name],
          resampling='near',
          targetres=tr,
          overwrite=True,
          srcnodata=-99,
          dstnodata=-99,
          dstfile=outname,
          layernames=['test1', 'test2'])
    with Raster(outname) as ras:
        assert ras.bands == 2
        # Raster.rescale currently only supports one band
        with pytest.raises(ValueError):
            ras.rescale(lambda x: x * 10)

    # outname exists and overwrite is False
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name],
              resampling='near',
              targetres=tr,
              overwrite=False,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname,
              layernames=['test1', 'test2'])

    # pass shapefile
    outname = os.path.join(str(tmpdir), 'test2')
    with Raster(name).bbox() as box:
        stack(srcfiles=[name, name],
              resampling='near',
              targetres=tr,
              overwrite=True,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname,
              shapefile=box,
              layernames=['test1', 'test2'])
    with Raster(outname) as ras:
        assert ras.bands == 2

    # pass shapefile and do mosaicing
    outname = os.path.join(str(tmpdir), 'test3')
    with Raster(name).bbox() as box:
        stack(srcfiles=[[name, name]],
              resampling='near',
              targetres=tr,
              overwrite=True,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname,
              shapefile=box)
    with Raster(outname + '.tif') as ras:
        assert ras.bands == 1
        assert ras.format == 'GTiff'

    # projection mismatch
    name2 = os.path.join(str(tmpdir), os.path.basename(name))
    outname = os.path.join(str(tmpdir), 'test4')
    gdalwarp(name, name2, options={'dstSRS': crsConvert(4326, 'wkt')})
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name2],
              resampling='near',
              targetres=tr,
              overwrite=True,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # no projection found
    outname = os.path.join(str(tmpdir), 'test5')
    gdal_translate(name, name2, {'options': ['-co', 'PROFILE=BASELINE']})
    with Raster(name2) as ras:
        print(ras.projection)
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name2, name2],
              resampling='near',
              targetres=tr,
              overwrite=True,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outname)

    # create separate GeoTiffs
    outdir = os.path.join(str(tmpdir), 'subdir')
    stack(srcfiles=[name, name],
          resampling='near',
          targetres=tr,
          overwrite=True,
          layernames=['test1', 'test2'],
          srcnodata=-99,
          dstnodata=-99,
          dstfile=outdir,
          separate=True,
          compress=True)

    # repeat with overwrite disabled (no error raised, just a print message)
    stack(srcfiles=[name, name],
          resampling='near',
          targetres=tr,
          overwrite=False,
          layernames=['test1', 'test2'],
          srcnodata=-99,
          dstnodata=-99,
          dstfile=outdir,
          separate=True,
          compress=True)

    # repeat without layernames but sortfun
    # bandnames not unique
    outdir = os.path.join(str(tmpdir), 'subdir2')
    with pytest.raises(RuntimeError):
        stack(srcfiles=[name, name],
              resampling='near',
              targetres=tr,
              overwrite=True,
              sortfun=os.path.basename,
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outdir,
              separate=True,
              compress=True)

    # repeat without layernames but sortfun
    name2 = os.path.join(str(tmpdir),
                         os.path.basename(name).replace('VV', 'XX'))
    shutil.copyfile(name, name2)
    outdir = os.path.join(str(tmpdir), 'subdir2')
    stack(srcfiles=[name, name2],
          resampling='near',
          targetres=tr,
          overwrite=True,
          sortfun=os.path.basename,
          srcnodata=-99,
          dstnodata=-99,
          dstfile=outdir,
          separate=True,
          compress=True)

    # shapefile filtering
    outdir = os.path.join(str(tmpdir), 'subdir3')
    files = [testdata['tif'], testdata['tif2'], testdata['tif3']]
    with Raster(files[0]).bbox() as box:
        stack(srcfiles=files,
              resampling='near',
              targetres=(30, 30),
              overwrite=False,
              layernames=['test1', 'test2', 'test3'],
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outdir,
              separate=True,
              compress=True,
              shapefile=box)
        # repeated run with different scene selection and only one scene after spatial filtering
        stack(srcfiles=files[1:],
              resampling='near',
              targetres=(30, 30),
              overwrite=True,
              layernames=['test2', 'test3'],
              srcnodata=-99,
              dstnodata=-99,
              dstfile=outdir,
              separate=True,
              compress=True,
              shapefile=box)
示例#5
0
def test_stack(tmpdir, testdata):
    name = testdata['tif']
    outname = os.path.join(str(tmpdir), 'test')
    tr = (30, 30)
    with pytest.raises(IOError):
        sp.stack(srcfiles=[],
                 resampling='near',
                 targetres=tr,
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname)

    with pytest.raises(IOError):
        sp.stack(srcfiles=[name, name],
                 resampling='near',
                 targetres=tr,
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname,
                 layernames=['a'])

    with pytest.raises(RuntimeError):
        sp.stack(srcfiles=[name, name],
                 resampling='near',
                 targetres=30,
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname)

    with pytest.raises(IOError):
        sp.stack(srcfiles=[name],
                 resampling='near',
                 targetres=tr,
                 overwrite=True,
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname)

    with pytest.raises(RuntimeError):
        sp.stack(srcfiles=[name, name],
                 resampling='near',
                 targetres=(30, 30, 30),
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname)

    with pytest.raises(IOError):
        sp.stack(srcfiles=[name, name],
                 resampling='foobar',
                 targetres=tr,
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname)

    with pytest.raises(RuntimeError):
        sp.stack(srcfiles=[name, 'foobar'],
                 resampling='near',
                 targetres=tr,
                 srcnodata=-99,
                 dstnodata=-99,
                 dstfile=outname)

    sp.stack(srcfiles=[name, name],
             resampling='near',
             targetres=tr,
             overwrite=True,
             srcnodata=-99,
             dstnodata=-99,
             dstfile=outname)

    outdir = os.path.join(str(tmpdir), 'subdir')
    sp.stack(srcfiles=[name, name],
             resampling='near',
             targetres=tr,
             overwrite=True,
             layernames=['test1', 'test2'],
             srcnodata=-99,
             dstnodata=-99,
             dstfile=outdir,
             separate=True,
             compress=True)

    with sp.Raster(outname) as ras:
        assert ras.bands == 2
        # Raster.rescale currently only supports one band
        with pytest.raises(ValueError):
            ras.rescale(lambda x: x * 10)