Example #1
0
def test_Raster_extract(testdata):
    with sp.Raster(testdata['tif']) as ras:
        assert ras.extract(px=624000, py=4830000,
                           radius=5) == -10.48837461270875
        with pytest.raises(RuntimeError):
            ras.extract(1, 4830000)
        with pytest.raises(RuntimeError):
            ras.extract(624000, 1)

        # ensure corner extraction capability
        assert ras.extract(px=ras.geo['xmin'],
                           py=ras.geo['ymax']) == -10.147890090942383
        assert ras.extract(px=ras.geo['xmin'],
                           py=ras.geo['ymin']) == -14.640368461608887
        assert ras.extract(px=ras.geo['xmax'],
                           py=ras.geo['ymax']) == -9.599242210388182
        assert ras.extract(px=ras.geo['xmax'],
                           py=ras.geo['ymin']) == -9.406558990478516

        # test nodata handling capability and correct indexing
        mat = ras.matrix()
        mat[0:10, 0:10] = ras.nodata
        mat[207:217, 258:268] = ras.nodata
        ras.assign(mat, band=0)
        assert ras.extract(px=ras.geo['xmin'], py=ras.geo['ymax'],
                           radius=5) == ras.nodata
        assert ras.extract(px=ras.geo['xmax'], py=ras.geo['ymin'],
                           radius=5) == ras.nodata
Example #2
0
def test_auxil(tmpdir, testdata):
    dir = str(tmpdir)
    with sp.Raster(testdata['tif']) as ras:
        bbox = os.path.join(dir, 'bbox.shp')
        ras.bbox(bbox)
        sp.ogr2ogr(bbox, os.path.join(dir, 'bbox.gml'), {'format': 'GML'})
        sp.gdal_translate(ras.raster, os.path.join(dir, 'test'),
                          {'format': 'ENVI'})
    sp.gdal_rasterize(bbox, os.path.join(dir, 'test2'), {
        'format': 'GTiff',
        'xRes': 20,
        'yRes': 20
    })
Example #3
0
def test_rasterize(tmpdir, testdata):
    outname = os.path.join(str(tmpdir), 'test.shp')
    with sp.Raster(testdata['tif']) as ras:
        vec = ras.bbox()

        # test length mismatch between burn_values and expressions
        with pytest.raises(RuntimeError):
            sp.rasterize(vec,
                         reference=ras,
                         outname=outname,
                         burn_values=[1],
                         expressions=['foo', 'bar'])

        # test a faulty expression
        with pytest.raises(RuntimeError):
            sp.rasterize(vec,
                         reference=ras,
                         outname=outname,
                         burn_values=[1],
                         expressions=['foo'])

        # test default parametrization
        sp.rasterize(vec, reference=ras, outname=outname)
        assert os.path.isfile(outname)

        # test appending to existing file with valid expression
        sp.rasterize(vec,
                     reference=ras,
                     outname=outname,
                     append=True,
                     burn_values=[1],
                     expressions=['area=7.573045244595988'])

        # test wrong input type for reference
        with pytest.raises(RuntimeError):
            sp.rasterize(vec, reference='foobar', outname=outname)
Example #4
0
def test_Raster(tmpdir, testdata):
    with pytest.raises(OSError):
        ras = sp.Raster('foobar')
    with sp.Raster(testdata['tif']) as ras:
        print(ras)
        assert ras.bands == 1
        assert ras.proj4 == '+proj=utm +zone=31 +datum=WGS84 +units=m +no_defs '
        assert ras.cols == 268
        assert ras.rows == 217
        assert ras.dim == (217, 268, 1)
        assert ras.dtype == 'Float32'
        assert ras.epsg == 32631
        assert ras.format == 'GTiff'
        assert ras.geo == {
            'ymax': 4830114.70107,
            'rotation_y': 0.0,
            'rotation_x': 0.0,
            'xmax': 625408.241204,
            'xres': 20.0,
            'xmin': 620048.241204,
            'ymin': 4825774.70107,
            'yres': -20.0
        }
        assert ras.geogcs == 'WGS 84'
        assert ras.is_valid() is True
        assert ras.proj4args == {
            'units': 'm',
            'no_defs': None,
            'datum': 'WGS84',
            'proj': 'utm',
            'zone': '31'
        }
        assert ras.allstats() == [{
            'min': -26.65471076965332,
            'max': 1.4325850009918213,
            'mean': -12.124929534450377,
            'sdev': 4.738273594738293
        }]
        assert ras.bbox().getArea() == 23262400.0
        assert len(ras.layers()) == 1
        assert ras.projcs == 'WGS 84 / UTM zone 31N'
        assert ras.res == (20.0, 20.0)

        # test writing a subset with no original data in memory
        outname = os.path.join(str(tmpdir), 'test_sub.tif')
        with ras[0:200, 0:100] as sub:
            sub.write(outname, format='GTiff')
        with sp.Raster(outname) as ras2:
            assert ras2.cols == 100
            assert ras2.rows == 200

        ras.load()
        mat = ras.matrix()
        assert isinstance(mat, np.ndarray)
        ras.assign(mat, band=0)
        # ras.reduce()
        ras.rescale(lambda x: 10 * x)

        # test writing data with original data in memory
        ras.write(os.path.join(str(tmpdir), 'test'),
                  format='GTiff',
                  compress_tif=True)
        with pytest.raises(RuntimeError):
            ras.write(os.path.join(str(tmpdir), 'test.tif'), format='GTiff')
Example #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(OSError):
        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)