def test_copy(self): name = os.path.join(self.tempdir, 'test_copy.tif') rasterio.copy( 'tests/data/RGB.byte.tif', name) info = subprocess.check_output(["gdalinfo", name]) self.assert_("GTiff" in info.decode('utf-8'))
def saveTimeGrid(timefile, timegrid, geodict, metadict): fbase, fname = os.path.split(timefile) timebase, timext = os.path.splitext(fname) timetiff = os.path.join(fbase, timebase + '.tiff') aff = Affine(geodict['xdim'], 0.0, geodict['xmin'], 0.0, geodict['xdim'], geodict['ymax']) crs = { 'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat' } m, n = timegrid.shape timeio = rasterio.open(timetiff, mode='w', driver='GTiff', dtype=rasterio.float32, transform=aff, crs=crs, count=1, height=m, width=n) timeio.write_band(1, timegrid.astype(np.float32)) timeio.close() with rasterio.drivers(): rasterio.copy(timetiff, timefile, driver='EHdr') os.remove(timetiff) timehdr = os.path.join(fbase, timebase + '.hdr') timedict = readTimeHeader(timehdr) for key, value in metadict.iteritems(): timedict[key] = value writeTimeHeader(timedict, timehdr) return True
def test_copy(tmpdir, path_rgb_byte_tif, pass_handle): """Ensure ``rasterio.copy()`` can read from a path to a file on disk and an open dataset handle. """ outfile = str(tmpdir.join('test_copy.tif')) if pass_handle: src = rasterio.open(path_rgb_byte_tif) else: src = path_rgb_byte_tif rasterio.copy( src, outfile, # Test a mix of boolean, ints, and strings to make sure creation # options passed as Python types are properly cast. tiled=True, blockxsize=512, BLOCKYSIZE='256') if isinstance(src, str): src = rasterio.open(path_rgb_byte_tif) with rasterio.open(outfile) as dst: assert dst.driver == 'GTiff' assert set(dst.block_shapes) == {(256, 512)} src_data = src.read() dst_data = dst.read() assert numpy.array_equal(src_data, dst_data) src.close()
def test_copy(self): name = os.path.join(self.tempdir, 'test_copy.tif') rasterio.copy( 'rasterio/tests/data/RGB.byte.tif', name) info = subprocess.check_output(["gdalinfo", name]) self.assert_("GTiff" in info.decode('utf-8'))
def test_copy_strict_failure(tmpdir, path_float_tif): """Ensure that strict=True raises an exception for a bad write instead of failing silently.""" outfile = str(tmpdir.join('test_copy.jpg')) with pytest.raises(CPLE_NotSupportedError): rasterio.copy(path_float_tif, outfile, strict=True, driver='JPEG')
def test_copy_strict_failure(tmpdir, path_float_tif): """Ensure that strict=True raises an exception for a bad write instead of failing silently.""" outfile = str(tmpdir.join('test_copy.jpg')) with pytest.raises(CPLE_NotSupportedError): rasterio.copy( path_float_tif, outfile, strict=True, driver='JPEG')
def test_copy_strict_silent_failure(tmpdir, path_float_tif): """Ensure that strict=False allows a bad write to fail silently. The raster will exist but will not be valid""" outfile = str(tmpdir.join('test_copy.jpg')) rasterio.copy(path_float_tif, outfile, strict=False, driver='JPEG') with rasterio.open(outfile) as dst: assert dst.driver == 'JPEG' assert dst.read().max( ) == 0 # it should be 1.4099; 0 indicates bad data
def test_copy_strict_silent_failure(tmpdir, path_float_tif): """Ensure that strict=False allows a bad write to fail silently. The raster will exist but will not be valid""" outfile = str(tmpdir.join('test_copy.jpg')) rasterio.copy( path_float_tif, outfile, strict=False, driver='JPEG') with rasterio.open(outfile) as dst: assert dst.driver == 'JPEG' assert dst.read().max() == 0 # it should be 1.4099; 0 indicates bad data
def saveTimeGrid(timefile,timegrid,geodict,metadict): fbase,fname = os.path.split(timefile) timebase,timext = os.path.splitext(fname) timetiff = os.path.join(fbase,timebase+'.tiff') aff = Affine(geodict['xdim'],0.0,geodict['xmin'],0.0,geodict['xdim'],geodict['ymax']) crs = {'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat'} m,n = timegrid.shape timeio = rasterio.open(timetiff,mode='w',driver='GTiff', dtype=rasterio.float32,transform=aff, crs=crs,count=1,height=m,width=n) timeio.write_band(1,timegrid.astype(np.float32)) timeio.close() with rasterio.drivers(): rasterio.copy(timetiff,timefile,driver='EHdr') os.remove(timetiff) timehdr = os.path.join(fbase,timebase+'.hdr') timedict = readTimeHeader(timehdr) for key,value in metadict.iteritems(): timedict[key] = value writeTimeHeader(timedict,timehdr) return True
import os.path import subprocess import tempfile import rasterio with rasterio.Env(): with rasterio.open('tests/data/RGB.byte.tif') as src: b, g, r = (src.read(k) for k in (1, 2, 3)) meta = src.meta tmpfilename = os.path.join(tempfile.mkdtemp(), 'decimate.tif') meta.update( width=src.width/2, height=src.height/2) with rasterio.open( tmpfilename, 'w', **meta ) as dst: for k, a in [(1, b), (2, g), (3, r)]: dst.write(a, indexes=k) outfilename = os.path.join(tempfile.mkdtemp(), 'decimate.jpg') rasterio.copy(tmpfilename, outfilename, driver='JPEG', quality='30') info = subprocess.call(['open', outfilename])
import os.path import subprocess import tempfile import rasterio with rasterio.drivers(): with rasterio.open("tests/data/RGB.byte.tif") as src: b, g, r = (src.read_band(k) for k in (1, 2, 3)) meta = src.meta tmpfilename = os.path.join(tempfile.mkdtemp(), "decimate.tif") meta.update(width=src.width / 2, height=src.height / 2) with rasterio.open(tmpfilename, "w", **meta) as dst: for k, a in [(1, b), (2, g), (3, r)]: dst.write_band(k, a) outfilename = os.path.join(tempfile.mkdtemp(), "decimate.jpg") rasterio.copy(tmpfilename, outfilename, driver="JPEG", quality="30") info = subprocess.call(["open", outfilename])
import os.path import subprocess import tempfile import rasterio with rasterio.drivers(): with rasterio.open('tests/data/RGB.byte.tif') as src: b, g, r = (src.read(k) for k in (1, 2, 3)) meta = src.meta tmpfilename = os.path.join(tempfile.mkdtemp(), 'decimate.tif') meta.update(width=src.width / 2, height=src.height / 2) with rasterio.open(tmpfilename, 'w', **meta) as dst: for k, a in [(1, b), (2, g), (3, r)]: dst.write_band(k, a) outfilename = os.path.join(tempfile.mkdtemp(), 'decimate.jpg') rasterio.copy(tmpfilename, outfilename, driver='JPEG', quality='30') info = subprocess.call(['open', outfilename])
def test_bad_driver(): with pytest.raises(DriverRegistrationError): rasterio.copy('tests/data/RGB.byte.tif', None, driver='trash')
# it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # import rasterio input_file = "/home/cayetano/Dropbox/documentos/python/GIS/workshop_RasterGIS_data/data/raster_out/example_data" out_tif_file = "/tmp/out.tif" with rasterio.drivers(): with rasterio.open(input_file) as src: print(src.width, src.height) print(src.crs) print(src.affine) print(src.count) print(src.indexes) with rasterio.drivers(): rasterio.copy(input_file, out_tif_file, driver='GTiff')