예제 #1
0
파일: ds.py 프로젝트: ua-snap/downscale
	def interp_ds( anom, base, src_crs, src_nodata, dst_nodata, src_transform, resample_type='bilinear',*args, **kwargs ):
		'''	
		anom = [numpy.ndarray] 2-d array representing a single monthly timestep of the data to be downscaled. 
								Must also be representative of anomalies.
		base = [str] filename of the corresponding baseline monthly file to use as template and downscale 
								baseline for combining with anomalies.
		src_transform = [affine.affine] 6 element affine transform of the input anomalies. [should be greenwich-centered]
		resample_type = [str] one of ['bilinear', 'count', 'nearest', 'mode', 'cubic', 'index', 'average', 'lanczos', 'cubic_spline']
		'''	
		import rasterio
		from rasterio.warp import reproject, RESAMPLING

		resampling = {'average':RESAMPLING.average,
					'cubic':RESAMPLING.cubic,
					'lanczos':RESAMPLING.lanczos,
					'bilinear':RESAMPLING.bilinear,
					'cubic_spline':RESAMPLING.cubic_spline,
					'mode':RESAMPLING.mode,
					'count':RESAMPLING.count,
					'index':RESAMPLING.index,
					'nearest':RESAMPLING.nearest }
		
		base = rasterio.open( base )
		baseline_arr = base.read( 1 )
		baseline_meta = base.meta
		baseline_meta.update( compress='lzw' )
		output_arr = np.empty_like( baseline_arr )
		
		reproject( anom, output_arr, src_transform=src_transform, src_crs=src_crs, src_nodata=src_nodata, \
				dst_transform=baseline_meta['affine'], dst_crs=baseline_meta['crs'],\
				dst_nodata=dst_nodata, resampling=resampling[ resample_type ], SOURCE_EXTRA=1000 )
		return output_arr
def run( df, meshgrid_tuple, lons_pcll, template_raster_fn, src_transform, src_crs, src_nodata, output_filename ):
	'''
	run the interpolation to a grid, and reprojection / resampling to the Alaska / Canada rasters
	extent, resolution, origin (template_raster).

	This function is intended to be used to run a pathos.multiprocessing Pool's map function
	across a list of pre-computed arguments.
			
	RETURNS:

	[str] path to the output filename generated

	'''
	template_raster = rasterio.open( template_raster_fn )
	interp_arr = xyz_to_grid( np.array(df['lon'].tolist()), \
					np.array(df['lat'].tolist()), \
					np.array(df['anom'].tolist()), grid=meshgrid_tuple, method='cubic' ) 

	src_nodata = -9999.0 # nodata
	interp_arr[ np.isnan( interp_arr ) ] = src_nodata
	dat, lons = shiftgrid( 180., interp_arr, lons_pcll, start=False )
	output_arr = np.empty_like( template_raster.read( 1 ) )
	# mask it with the internal mask in the template raster, where 0 is oob.
	output_arr = np.ma.masked_where( template_raster.read_masks( 1 ) == 0, output_arr )
	template_meta = template_raster.meta

	if 'transform' in template_meta.keys():
		template_meta.pop( 'transform' )

	reproject( dat, output_arr, src_transform=src_transform, src_crs=src_crs, src_nodata=src_nodata, \
				dst_transform=template_meta['affine'], dst_crs=template_meta['crs'],\
				dst_nodata=None, resampling=RESAMPLING.nearest, num_threads=1, SOURCE_EXTRA=1000 )	
	return write_gtiff( output_arr, template_meta, output_filename, compress=True )
예제 #3
0
def test_reproject_ndarray():
    with rasterio.drivers():
        with rasterio.open('tests/data/RGB.byte.tif') as src:
            source = src.read_band(1)

        dst_crs = dict(
            proj='merc',
            a=6378137,
            b=6378137,
            lat_ts=0.0,
            lon_0=0.0,
            x_0=0.0,
            y_0=0,
            k=1.0,
            units='m',
            nadgrids='@null',
            wktext=True,
            no_defs=True)
        out = numpy.empty(src.shape, dtype=numpy.uint8)
        reproject(
            source,
            out,
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=DST_TRANSFORM,
            dst_crs=dst_crs,
            resampling=RESAMPLING.nearest)
        assert (out > 0).sum() == 438146
예제 #4
0
 def _warp(self, proj_data, bands, new_bands):
     self.output("Projecting", normal=True, arrow=True)
     for i, band in enumerate(bands):
         self.output("band %s" % self.bands[i], normal=True, color='green', indent=1)
         reproject(band, new_bands[i], src_transform=proj_data['transform'], src_crs=proj_data['crs'],
                   dst_transform=proj_data['dst_transform'], dst_crs=self.dst_crs, resampling=RESAMPLING.nearest,
                   num_threads=2)
예제 #5
0
def warp_tif(combined_tif_path, warped_tif_path, dst_crs={
        'init': 'EPSG:3857'
}):
    logger.info('Warping tif to web mercator: %s', combined_tif_path)
    with rasterio.open(combined_tif_path) as src:
        meta = src.meta
        new_meta = meta.copy()
        transform, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)
        new_meta.update({
            'crs': dst_crs,
            'transform': transform,
            'width': width,
            'height': height,
            'nodata': -28762
        })
        with rasterio.open(
                warped_tif_path, 'w', compress='DEFLATE', tiled=True,
                **new_meta) as dst:
            for i in range(1, src.count):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs,
                    dst_transform=transform,
                    dst_crs=dst_crs,
                    resampling=Resampling.nearest,
                    src_nodata=-28762
                )
예제 #6
0
def test_resample_default_invert_proj(method):
    """Nearest and bilinear should produce valid results
    with the default Env
    """

    with rasterio.open("tests/data/world.rgb.tif") as src:
        source = src.read(1)
        profile = src.profile.copy()

    dst_crs = {"init": "epsg:32619"}

    # Calculate the ideal dimensions and transformation in the new crs
    dst_affine, dst_width, dst_height = calculate_default_transform(
        src.crs, dst_crs, src.width, src.height, *src.bounds
    )

    profile["height"] = dst_height
    profile["width"] = dst_width

    out = np.empty(shape=(dst_height, dst_width), dtype=np.uint8)

    out = np.empty(src.shape, dtype=np.uint8)
    reproject(
        source,
        out,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=dst_affine,
        dst_crs=dst_crs,
        resampling=method,
    )

    assert out.mean() > 0
예제 #7
0
def test_reproject_gcps(rgb_byte_profile):
    """Reproject using ground control points for the source"""
    source = np.ones((3, 800, 800), dtype=np.uint8) * 255
    out = np.zeros(
        (3, rgb_byte_profile["height"], rgb_byte_profile["height"]), dtype=np.uint8
    )
    src_gcps = [
        GroundControlPoint(row=0, col=0, x=156113, y=2818720, z=0),
        GroundControlPoint(row=0, col=800, x=338353, y=2785790, z=0),
        GroundControlPoint(row=800, col=800, x=297939, y=2618518, z=0),
        GroundControlPoint(row=800, col=0, x=115698, y=2651448, z=0),
    ]
    reproject(
        source,
        out,
        src_crs="epsg:32618",
        gcps=src_gcps,
        dst_transform=rgb_byte_profile["transform"],
        dst_crs=rgb_byte_profile["crs"],
        resampling=Resampling.nearest,
    )

    assert not out.all()
    assert not out[:, 0, 0].any()
    assert not out[:, 0, -1].any()
    assert not out[:, -1, -1].any()
    assert not out[:, -1, 0].any()
예제 #8
0
def test_reproject_ndarray():
    with rasterio.open("tests/data/RGB.byte.tif") as src:
        source = src.read(1)

    dst_crs = dict(
        proj="merc",
        a=6378137,
        b=6378137,
        lat_ts=0.0,
        lon_0=0.0,
        x_0=0.0,
        y_0=0,
        k=1.0,
        units="m",
        nadgrids="@null",
        wktext=True,
        no_defs=True,
    )
    out = np.empty(src.shape, dtype=np.uint8)
    reproject(
        source,
        out,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs=dst_crs,
        resampling=Resampling.nearest,
    )
    assert (out > 0).sum() == 438113
예제 #9
0
def downscale_wrapper( arr, affine, crs, baseline, output_filename, downscaling_operation, post_downscale_function ):
	# rotate
	if ( self.data.ds.lon > 200.0 ).any() == True:
		dat, lons = utils.shiftgrid( 180., self.data.anomalies, self.historical.ds.lon, start=False )
		a,b,c,d,e,f,g,h,i = affine #flip it to the greenwich-centering
		src_transform = affine.Affine( a, b, -180.0, d, e, 180.0 )
	else:
		dat, lons = ( self.data.ds, self.historical.ds.lon )
		src_transform = affine
	
	# reproject / resample
	src_crs = {'init':'epsg:4326'}
	src_nodata = None # DangerTown™
	baseline_meta = baseline.meta
	baseline_meta.update( compress='lzw' )
	output_arr = np.empty_like( baeline.read( 1 ) )

	# TODO: make this function available for manipulation if used for different needs
	reproject( arr, output_arr, src_transform=src_transform, src_crs=src_crs, src_nodata=src_nodata, \
			dst_transform=baseline_meta['affine'], dst_crs=baseline_meta['crs'],\
			dst_nodata=None, resampling=RESAMPLING.cubic_spline, SOURCE_EXTRA=1000 )
	
	# downscale
	return utils.downscale( arr, output_arr, output_filename, downscaling_operation, \
					baseline_meta, post_downscale_function, mask=None, mask_value=0 )
예제 #10
0
def test_reproject_resampling_alpha(method):
    """Reprojection of a source with alpha band succeeds"""
    # Expected count of nonzero pixels for each resampling method, based
    # on running rasterio with each of the following configurations
    expected = {
        Resampling.nearest: 438113,
        Resampling.bilinear: 439280,
        Resampling.cubic: 437888,
        Resampling.cubic_spline: 440475,
        Resampling.lanczos: 436001,
        Resampling.average: 439419,
        Resampling.mode: 437298,
        Resampling.max: 439464,
        Resampling.min: 436397,
        Resampling.med: 437194,
        Resampling.q1: 436397,
        Resampling.q3: 438948,
    }

    with rasterio.open("tests/data/RGBA.byte.tif") as src:
        source = src.read(1)

    out = np.empty(src.shape, dtype=np.uint8)
    reproject(
        source,
        out,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs={"init": "epsg:3857"},
        resampling=method,
    )

    assert np.count_nonzero(out) == expected[method]
예제 #11
0
def test_reproject_multi():
    """Ndarry to ndarray."""
    with rasterio.open("tests/data/RGB.byte.tif") as src:
        source = src.read()
    dst_crs = dict(
        proj="merc",
        a=6378137,
        b=6378137,
        lat_ts=0.0,
        lon_0=0.0,
        x_0=0.0,
        y_0=0,
        k=1.0,
        units="m",
        nadgrids="@null",
        wktext=True,
        no_defs=True,
    )
    destin = np.empty(source.shape, dtype=np.uint8)
    reproject(
        source,
        destin,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs=dst_crs,
        resampling=Resampling.nearest,
    )
    assert destin.any()
예제 #12
0
def test_reproject_multi():
    """Ndarry to ndarray"""
    with rasterio.drivers():
        with rasterio.open('tests/data/RGB.byte.tif') as src:
            source = src.read()
        dst_crs = dict(
            proj='merc',
            a=6378137,
            b=6378137,
            lat_ts=0.0,
            lon_0=0.0,
            x_0=0.0,
            y_0=0,
            k=1.0,
            units='m',
            nadgrids='@null',
            wktext=True,
            no_defs=True)
        destin = numpy.empty(source.shape, dtype=numpy.uint8)
        reproject(
            source,
            destin,
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=DST_TRANSFORM,
            dst_crs=dst_crs,
            resampling=RESAMPLING.nearest)
    assert destin.any()
예제 #13
0
    def to_srs_like(self, rgrid, src_nodata=None, dst_nodata=None,
                    resampling=Resampling.bilinear):
        if src_nodata is None:
            src_nodata = self.fill_value
        if dst_nodata is None:
            dst_nodata = src_nodata
        source = self.copy()
        source.fill_underlying_data(src_nodata)

        # dst_shape = rgrid.shape
        dst_transform = rgrid.gtransform
        dst_crs = rgrid.crs
        destination = rgrid.astype(self.dtype).copy()
        reproject(
            source,
            destination=destination,
            src_transform=self.gtransform,
            src_nodata=src_nodata,
            src_crs=self.crs,
            dst_transform=dst_transform,
            dst_crs=dst_crs,
            resampling=resampling,
            dst_nodata=dst_nodata)

        destination.masked_equal(dst_nodata)
        return destination
예제 #14
0
def test_warp_from_file():
    """File to ndarray"""
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        dst_transform = affine.Affine.from_gdal(-8789636.708, 300.0, 0.0, 2943560.235, 0.0, -300.0)
        dst_crs = dict(
                    proj='merc',
                    a=6378137,
                    b=6378137,
                    lat_ts=0.0,
                    lon_0=0.0,
                    x_0=0.0,
                    y_0=0,
                    k=1.0,
                    units='m',
                    nadgrids='@null',
                    wktext=True,
                    no_defs=True)
        destin = numpy.empty(src.shape, dtype=numpy.uint8)
        reproject(
            rasterio.band(src, 1), 
            destin, 
            dst_transform=dst_transform, 
            dst_crs=dst_crs)
    assert destin.any()
    try:
        import matplotlib.pyplot as plt
        plt.imshow(destin)
        plt.gray()
        plt.savefig('test_warp_from_filereproject.png')
    except:
        pass
def warp_raster_to_template(input_raster_file, template_raster_file, output_file):
    print "reprojecting raster"
    with rasterio.open(input_raster_file) as input_r:
        with rasterio.open(template_raster_file) as template_r:
            input_array = input_r.read(1)
            dest_array = np.zeros((template_r.width, template_r.height), dtype=input_array.dtype)

    reproject(
        input_array,
        dest_array,
        src_transform=input_r.affine,
        src_crs=input_r.crs,
        dst_transform=template_r.affine,
        dst_crs=template_r.crs,
        resampling=Resampling.nearest)

    print "now saving raster"

    with rasterio.open(output_file, 'w',
                  driver = "GTiff",
                  width=template_r.width,
                  height=template_r.height,
                  count=1,
                  dtype=dest_array.dtype,
                  crs=template_r.crs,
                  transform=template_r.affine,
                  nodata=0
                 ) as output:
        output.write(dest_array, indexes=1)
예제 #16
0
	def interp_ds( anom, base, output_filename, src_transform, downscaling_operation, post_downscale_function=None, mask=None, mask_value=0 ):
		'''	
		anom = [numpy.ndarray] 2-d array representing a single monthly timestep of the data to be downscaled. Must also be representative of anomalies.
		base = [str] filename of the corresponding baseline monthly file to use as template and downscale baseline for combining with anomalies.
		output_filename = [str] path to the output file to be created following downscaling
		src_transform = [affine.affine] 6 element affine transform of the input anomalies. [should be greenwich-centered]
		downscaling_operation = [str] one of 'add' or 'mult' depending on absolute or relative delta downscaling.
		post_downscale_function = [function] function that takes as input a single 2-d array and returns a 2-d array in the same shape as the input.  
		mask = [numpy.ndarray] 2-d array showing what values should be masked. Masked=0, unmasked=1. must be same shape as base.
		mask_value = [int] what value to use as the masked values. default=0.

		'''		
		from rasterio.warp import reproject, RESAMPLING
		# reproject / resample
		src_crs = {'init':'epsg:4326'}
		src_nodata = None # DangerTown™
		base = rasterio.open( base )
		baseline_arr = base.read( 1 )
		baseline_meta = base.meta
		baseline_meta.update( compress='lzw' )
		output_arr = np.empty_like( baseline_arr )

		# TODO: make this function available for manipulation if used for different needs
		reproject( anom, output_arr, src_transform=src_transform, src_crs=src_crs, src_nodata=src_nodata, \
				dst_transform=baseline_meta['affine'], dst_crs=baseline_meta['crs'],\
				dst_nodata=None, resampling=RESAMPLING.cubic_spline, SOURCE_EXTRA=1000 )
		# downscale
		return utils.downscale( output_arr, baseline_arr, output_filename, downscaling_operation, \
				baseline_meta, post_downscale_function, mask, mask_value )
예제 #17
0
def test_warp_from_to_file(tmpdir):
    """File to file"""
    tiffname = str(tmpdir.join('foo.tif'))
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        dst_transform = affine.Affine.from_gdal(-8789636.708, 300.0, 0.0, 2943560.235, 0.0, -300.0)
        dst_crs = dict(
                    proj='merc',
                    a=6378137,
                    b=6378137,
                    lat_ts=0.0,
                    lon_0=0.0,
                    x_0=0.0,
                    y_0=0,
                    k=1.0,
                    units='m',
                    nadgrids='@null',
                    wktext=True,
                    no_defs=True)
        kwargs = src.meta.copy()
        kwargs.update(
            transform=dst_transform,
            crs=dst_crs)
        with rasterio.open(tiffname, 'w', **kwargs) as dst:
            for i in (1, 2, 3):
                reproject(rasterio.band(src, i), rasterio.band(dst, i))
예제 #18
0
def test_target_aligned_pixels():
    """Issue 853 has been resolved"""
    with rasterio.open('tests/data/world.rgb.tif') as src:
        source = src.read(1)
        profile = src.profile.copy()

    dst_crs = {'init': 'epsg:3857'}

    with rasterio.Env(CHECK_WITH_INVERT_PROJ=False):
        # Calculate the ideal dimensions and transformation in the new crs
        dst_affine, dst_width, dst_height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)

        dst_affine, dst_width, dst_height = aligned_target(dst_affine, dst_width, dst_height, 100000.0)

        profile['height'] = dst_height
        profile['width'] = dst_width

        out = np.empty(shape=(dst_height, dst_width), dtype=np.uint8)

        reproject(
            source,
            out,
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=dst_affine,
            dst_crs=dst_crs,
            resampling=Resampling.nearest)

        # Check that there is no black borders
        assert out[:, 0].all()
        assert out[:, -1].all()
        assert out[0, :].all()
        assert out[-1, :].all()
예제 #19
0
def test_reproject_resampling(path_rgb_byte_tif, method):
    # Expected count of nonzero pixels for each resampling method, based
    # on running rasterio with each of the following configurations
    expected = {
        Resampling.nearest: 438113,
        Resampling.bilinear: 439280,
        Resampling.cubic: 437888,
        Resampling.cubic_spline: 440475,
        Resampling.lanczos: 436001,
        Resampling.average: 439419,
        Resampling.mode: 437298,
        Resampling.max: 439464,
        Resampling.min: 436397,
        Resampling.med: 437194,
        Resampling.q1: 436397,
        Resampling.q3: 438948
    }

    with rasterio.open(path_rgb_byte_tif) as src:
        source = src.read(1)

    out = np.empty(src.shape, dtype=np.uint8)
    reproject(
        source,
        out,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs={'init': 'EPSG:3857'},
        resampling=method)

    assert np.count_nonzero(out) == expected[method]
예제 #20
0
def test_warp_from_to_file_multi(tmpdir):
    """File to file"""
    tiffname = str(tmpdir.join('foo.tif'))
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        dst_crs = dict(
            proj='merc',
            a=6378137,
            b=6378137,
            lat_ts=0.0,
            lon_0=0.0,
            x_0=0.0,
            y_0=0,
            k=1.0,
            units='m',
            nadgrids='@null',
            wktext=True,
            no_defs=True)
        kwargs = src.meta.copy()
        kwargs.update(
            transform=DST_TRANSFORM,
            crs=dst_crs)
        with rasterio.open(tiffname, 'w', **kwargs) as dst:
            for i in (1, 2, 3):
                reproject(
                    rasterio.band(src, i),
                    rasterio.band(dst, i),
                    num_threads=2)
예제 #21
0
def test_reproject_identity():
    """Reproject with an identity matrix."""
    # note the affines are both positive e, src is identity
    src = np.random.random(25).reshape((1, 5, 5))
    srcaff = Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0)  # Identity
    srccrs = {'init': 'epsg:3857'}

    dst = np.empty(shape=(1, 10, 10))
    dstaff = Affine(0.5, 0.0, 0.0, 0.0, 0.5, 0.0)
    dstcrs = {'init': 'epsg:3857'}

    reproject(
        src, dst,
        src_transform=srcaff,
        src_crs=srccrs,
        dst_transform=dstaff,
        dst_crs=dstcrs,
        resampling=Resampling.nearest)

    # note the affines are both positive e, dst is identity
    src = np.random.random(100).reshape((1, 10, 10))
    srcaff = Affine(0.5, 0.0, 0.0, 0.0, 0.5, 0.0)
    srccrs = {'init': 'epsg:3857'}

    dst = np.empty(shape=(1, 5, 5))
    dstaff = Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0)  # Identity
    dstcrs = {'init': 'epsg:3857'}

    reproject(
        src, dst,
        src_transform=srcaff,
        src_crs=srccrs,
        dst_transform=dstaff,
        dst_crs=dstcrs,
        resampling=Resampling.nearest)
예제 #22
0
def test_resample_no_invert_proj(method):
    """Nearest and bilinear should produce valid results with
    CHECK_WITH_INVERT_PROJ = False
    """
    if not supported_resampling(method):
        pytest.skip()

    with rasterio.Env(CHECK_WITH_INVERT_PROJ=False):
        with rasterio.open('tests/data/world.rgb.tif') as src:
            source = src.read(1)
            profile = src.profile.copy()

        dst_crs = {'init': 'EPSG:32619'}

        # Calculate the ideal dimensions and transformation in the new crs
        dst_affine, dst_width, dst_height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)

        profile['height'] = dst_height
        profile['width'] = dst_width

        out = np.empty(shape=(dst_height, dst_width), dtype=np.uint8)

        # see #614, some resamplin methods succeed but produce blank images
        out = np.empty(src.shape, dtype=np.uint8)
        reproject(
            source,
            out,
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=dst_affine,
            dst_crs=dst_crs,
            resampling=method)

        assert out.mean() > 0
예제 #23
0
def test_reproject_dst_nodata_default():
    """
    If nodata is not provided, destination will be filled with 0
    instead of nodata
    """

    params = default_reproject_params()

    with rasterio.drivers():
        source = numpy.ones((params.width, params.height), dtype=numpy.uint8)
        out = numpy.zeros((params.dst_width, params.dst_height),
                          dtype=source.dtype)
        out.fill(120)  # Fill with arbitrary value

        reproject(
            source,
            out,
            src_transform=params.src_transform,
            src_crs=params.src_crs,
            dst_transform=params.dst_transform,
            dst_crs=params.dst_crs
        )

        assert (out == 1).sum() == 4461
        assert (out == 0).sum() == (params.dst_width *
                                    params.dst_height - 4461)
def resample_to_1km(x):
    """
	template_raster_mask should be a mask in in the res/extent/origin/crs of the 
	existing TEM IEM products.
	"""
    fn, output_filename, template_raster_mask_fn = x
    import rasterio, os
    from rasterio.warp import RESAMPLING, reproject
    import numpy as np

    rst = rasterio.open(fn)
    rst_arr = rst.read(1)

    fn = os.path.basename(fn)
    fn_split = fn.split(".")[0].split("_")

    template_raster_mask = rasterio.open(template_raster_mask_fn)
    template_arr = template_raster_mask.read(1)
    template_meta = template_raster_mask.meta
    template_meta.update(compress="lzw", nodata=rst.nodata, dtype="float32")

    if "transform" in template_meta.keys():
        template_meta.pop("transform")

    output_arr = np.empty_like(template_arr.astype(np.float32))
    output_arr[template_arr == 0] = rst.nodata

    epsg3338 = {"init": "epsg:3338"}
    reproject(
        rst_arr,
        output_arr,
        src_transform=rst.affine,
        src_crs=epsg3338,
        src_nodata=rst.nodata,
        dst_transform=template_raster_mask.affine,
        dst_crs=epsg3338,
        dst_nodata=rst.nodata,
        resampling=RESAMPLING.cubic_spline,
    )  # , num_threads=2

    # # get indexes of non-masked values
    # ind = np.where( output_arr >= 0 )
    # if 'pr_' in fn:
    # 	# truncate the values to 0 decimal places
    # 	new_vals = np.ceil( output_arr[ ind ] )
    # elif 'tas_' in fn:
    # 	# round to 2 decimal places
    # 	new_vals = np.round( output_arr[ ind ], 2 )
    # else:
    # 	AttributeError( 'tool only built to work with tas / pr at this time.' )

    # # pass the prepped values to the output_arr
    # output_arr[ ind ] = new_vals

    with rasterio.open(output_filename, "w", **template_meta) as out:
        output_arr[template_arr == 0] = rst.nodata
        out.write(output_arr, 1)
    return output_filename
예제 #25
0
def project_raster(src_raster, dst_raster, dst_crs,
                   resampling=1, resolution=None, num_threads=2):
    """Reproject a raster from one coordinate system to another using Rasterio
    code from: https://github.com/mapbox/rasterio/blob/master/docs/reproject.rst

    Parameters
    ----------
    src_raster : str
        Filename of source raster.
    dst_raster : str
        Filename of reprojected (destination) raster.
    dst_crs : str
        Coordinate system of reprojected raster.
        Examples:
            'EPSG:26715'
    resampling : int (see rasterio source code: https://github.com/mapbox/rasterio/blob/master/rasterio/enums.py)
        nearest = 0
        bilinear = 1
        cubic = 2
        cubic_spline = 3
        lanczos = 4
        average = 5
        mode = 6
        gauss = 7
        max = 8
        min = 9
        med = 10
        q1 = 11
        q3 = 12
    resolution : tuple of floats (len 2)
        cell size of the output raster
        (x resolution, y resolution)
    """
    rasterio = import_rasterio() # check for rasterio
    from rasterio.warp import calculate_default_transform, reproject

    with rasterio.open(src_raster) as src:
        affine, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds, resolution=resolution)
        kwargs = src.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': affine,
            'affine': affine,
            'width': width,
            'height': height
        })
        with rasterio.open(dst_raster, 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.affine,
                    src_crs=src.crs,
                    dst_transform=affine,
                    dst_crs=dst_crs,
                    resampling=resampling,
                    num_threads=num_threads)
def resample_to_1km( x, template_raster_mask ):
	'''
	template_raster_mask should be a mask in in the res/extent/origin/crs of the 
	existing TEM IEM products.
	'''
	import rasterio, os
	from rasterio.warp import RESAMPLING, reproject
	import numpy as np

	fn = os.path.basename( x )
	fn_split = fn.split( '.' )[0].split( '_' ) 
	if '_cru_' in fn:
		output_path = os.path.dirname( x ).replace( '/cru_ts31/', '/IEM/cru_ts31/' ) # hardwired!
		fn_parts = ['variable', 'metric', 'model_1', 'model_2', 'kind', 'month', 'year']
		fn_dict = dict( zip( fn_parts, fn_split ) )
		fn_dict.update( scenario='historical', model='cru_ts31' )
	else:
		output_path = os.path.dirname( x ).replace( '/ar5/', '/IEM/ar5/' ) # hardwired!
		fn_parts = ['variable', 'metric', 'model', 'scenario', 'ensemble', 'month', 'year']
		fn_dict = dict( zip( fn_parts, fn_split ) )

	try:
		if not os.path.exists( output_path ):
			os.makedirs( output_path )
	except:
		pass

	fn_switch = { 'cld':'_'.join([ 'cld','mean','pct','iem',fn_dict['model'],fn_dict['scenario'],fn_dict['month'], fn_dict['year'] ]) + '.tif',
		'vap':'_'.join(['vap','mean','hPa','iem', fn_dict['model'],fn_dict['scenario'],fn_dict['month'], fn_dict['year'] ]) + '.tif', 
		'tas':'_'.join(['tas','mean','C','iem',fn_dict['model'],fn_dict['scenario'],fn_dict['month'], fn_dict['year'] ]) + '.tif',
		'hur':'_'.join(['hur','mean','pct','iem',fn_dict['model'],fn_dict['scenario'],fn_dict['month'], fn_dict['year'] ]) + '.tif' }

	output_filename = os.path.join( output_path, fn_switch[ fn_dict[ 'variable' ] ] )

	rst = rasterio.open( x )
	rst_arr = rst.read( 1 )

	template_arr = template_raster_mask.read( 1 )
	template_meta = template_raster_mask.meta
	template_meta.update( compress='lzw', nodata=rst.nodata )
	
	if 'transform' in template_meta.keys():
		template_meta.pop( 'transform' )

	output_arr = np.empty_like( template_arr.astype( np.float32 ) )
	output_arr[ template_arr == 0 ] = rst.nodata

	src_crs = {'init':'epsg:3338'}
	dst_crs = {'init':'epsg:3338'}

	reproject( rst_arr, output_arr, src_transform=rst.affine, src_crs=src_crs, src_nodata=rst.nodata, \
			dst_transform=template_raster_mask.affine, dst_crs=dst_crs,\
			dst_nodata=rst.nodata, resampling=RESAMPLING.cubic_spline, num_threads=2 )

	with rasterio.open( output_filename, 'w', **template_meta ) as out:
		output_arr[ template_arr == 0 ] = rst.nodata
		out.write( output_arr, 1 )
	return output_filename
예제 #27
0
def test_reproject_dst_crs_none():
    with pytest.raises(CRSError):
        reproject(
            np.ones((2, 2)),
            np.zeros((2, 2)),
            src_transform=Affine.identity(),
            dst_transform=Affine.identity(),
            src_crs=WGS84_crs,
        )
def main(red, green, blue, pan, weight,out):
	weight = float(weight)
	r, g, b, p = (rio.open(f) for f in [red, green, blue, pan])
	rgb_o = (r, g, b)
	
	color_meta, pan_meta = b.meta, p.meta
	w, h = pan_meta['width'], pan_meta['height']
	print(w, h)
	pan_meta.update(photometric='rgb', count=3)
	
	with rio.open(out, 'w', **pan_meta) as dest:
		for wind in make_windows(w, h):
			# print(wind, half_window(wind))
			pan = p.read_band(1, window=wind).astype(matht)
			print 'pan after', pan.shape
			rgb = list(x.read_band(1, window=half_window(wind)).astype(matht) for x in rgb_o)
			for i, c in enumerate(rgb):
				x = np.empty(pan.shape)
				reproject(
					c, x,
					src_transform=color_meta['transform'],
					src_crs=color_meta['crs'],
					dst_transform=pan_meta['transform'],
					dst_crs=pan_meta['crs'],
					resampling=RESAMPLING.bilinear)
 
				rgb[i] = x

			sudo_pan = (rgb[0] + rgb[1] + rgb[2]*weight)/(2+weight)


			print weight
			print 'RESAMPLING.bilinear'

			# # print 'pan min',np.nanmin(pan)
			# # print 'pan max', np.nanmax(pan)
			# # print 'pan mean', np.mean(pan) 

			# # print 'sudo min',np.nanmin(sudo_pan)
			# # print 'sudo max', np.nanmax(sudo_pan)
			# # print 'sudo mean', np.mean(sudo_pan) 
			ratio = pan / sudo_pan
			print sudo_pan.shape
			print ratio.shape
			print pan.shape
			# # print 'ratio min',np.nanmin(ratio)
			# # print 'ratio max', np.nanmax(ratio)
			# # print 'ratio mean', np.mean(ratio)   # ratio .9 to 1.1
 
			# for i, data in enumerate(rgb):
			# 	done = (data * ratio).astype(np.uint16)
			# 	print(np.amax(done)) # for debugging, whether overflow. typically 40%, 30k
			# 	# print(done.shape)
			# 	# print(wind, w, h)
			# 	# print done[0]
			# 	dest.write_band(i+1, done, window=wind)
	print('that was cool and fun')
예제 #29
0
def process_chunk_task(task):
    """
    Chunks the image into tile_dim x tile_dim tiles,
    and saves them to the target folder (s3 or local)

    Returns the extent of the output raster.
    """

    creation_options = {
        "driver": "GTiff",
        "crs": "EPSG:3857",
        "tiled": True,
        "compress": "deflate",
        "predictor":   2, # 3 for floats, 2 otherwise
        "sparse_ok": True
    }

    with rasterio.open(task.source_uri, "r") as src:
        meta = src.meta.copy()
        meta.update(creation_options)
        meta.update(task.target_meta)
        
        cols = meta["width"]
        rows = meta["height"]

        tmp_path = "/vsimem/" + get_filename(task.target)

        with rasterio.open(tmp_path, "w", **meta) as tmp:
            # Reproject the src dataset into image tile.
            warped = []
            for bidx in src.indexes:
                source = rasterio.band(src, bidx)
                warped.append(numpy.zeros((cols, rows), dtype=meta['dtype']))

                warp.reproject(
                    source=source,
                    src_nodata=0,
                    destination=warped[bidx - 1],
                    dst_transform=meta["transform"],
                    dst_crs=meta["crs"],
                    # resampling=RESAMPLING.bilinear
                )

            # check for chunks containing only zero values
            if not any(map(lambda b: b.any(), warped)):
                return False

            # write out our warped data to the vsimem raster
            for bidx in src.indexes:
                tmp.write_band(bidx, warped[bidx - 1])

    contents = bytearray(virtual_file_to_buffer(tmp_path))

    write_bytes_to_target(task.target, contents)
    return True
def resample_to_1km( x ):
	'''
	template_raster_mask should be a mask in in the res/extent/origin/crs of the 
	existing TEM IEM products.
	'''
	fn, output_filename, template_raster_mask_fn = x
	import rasterio, os
	from rasterio.warp import RESAMPLING, reproject
	import numpy as np

	rst = rasterio.open( fn )
	rst_arr = rst.read( 1 )

	fn = os.path.basename( fn )
	fn_split = fn.split( '.' )[0].split( '_' ) 

	template_raster_mask = rasterio.open( template_raster_mask_fn )
	template_arr = template_raster_mask.read( 1 )
	template_meta = template_raster_mask.meta
	template_meta.update( compress='lzw', nodata=rst.nodata, dtype='float32' )
	
	if 'transform' in template_meta.keys():
		template_meta.pop( 'transform' )

	output_arr = np.empty_like( template_arr.astype( np.float32 ) )
	output_arr[ template_arr == 0 ] = rst.nodata

	epsg3338 = {'init':'epsg:3338'}
	reproject( rst_arr, output_arr, src_transform=rst.affine, src_crs=epsg3338, src_nodata=rst.nodata, \
			dst_transform=template_raster_mask.affine, dst_crs=epsg3338,\
			dst_nodata=rst.nodata, resampling=RESAMPLING.cubic_spline ) # , num_threads=2

	# REQUIRES UPDATING! 
	# this is where we would need to truncate the data to 2 decimal places for the tas
	#  and we need have a dtype diff I think for pr = int32 and tas = float32
	# this needs to match the AR4 data! check the CKAN_Data data.

	# get indexes of non-masked values
	ind = np.where( output_arr >= 0 )
	if 'pr_' in fn:
		# truncate the values to 0 decimal places
		new_vals = np.ceil( output_arr[ ind ] )
	elif 'tas_' in fn:
		# round to 2 decimal places
		new_vals = np.round( output_arr[ ind ], 2 )
	else:
		AttributeError( 'tool only built to work with tas / pr at this time.' )

	# pass the prepped values to the output_arr
	output_arr[ ind ] = new_vals

	with rasterio.open( output_filename, 'w', **template_meta ) as out:
		output_arr[ template_arr == 0 ] = rst.nodata
		out.write( output_arr, 1 )
	return output_filename
예제 #31
0
def test_reproject_dst_nodata_default(options, expected):
    """If nodata is not provided, destination will be filled with 0."""
    params = default_reproject_params()

    with rasterio.Env(**options):
        source = np.ones((params.width, params.height), dtype=np.uint8)
        out = np.zeros((params.dst_width, params.dst_height),
                       dtype=source.dtype)
        out.fill(120)  # Fill with arbitrary value

        reproject(source,
                  out,
                  src_transform=params.src_transform,
                  src_crs=params.src_crs,
                  dst_transform=params.dst_transform,
                  dst_crs=params.dst_crs)

        assert (out == 1).sum() == expected
        assert (out == 0).sum() == (params.dst_width * params.dst_height -
                                    expected)
예제 #32
0
def test_reproject_dst_alpha(path_rgb_msk_byte_tif):
    """Materialization of external mask succeeds"""

    with rasterio.open(path_rgb_msk_byte_tif) as src:

        nrows, ncols = src.shape

        dst_arr = np.zeros((src.count + 1, nrows, ncols), dtype=np.uint8)

        reproject(
            rasterio.band(src, src.indexes),
            dst_arr,
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=DST_TRANSFORM,
            dst_crs={"init": "epsg:3857"},
            dst_alpha=4,
        )

        assert dst_arr[3].any()
예제 #33
0
def test_reproject_out_of_bounds():
    """Using EPSG code is not appropriate for the transform.

    Should return blank image.
    """
    with rasterio.open("tests/data/RGB.byte.tif") as src:
        source = src.read(1)

    dst_crs = {"init": "epsg:32619"}
    out = np.zeros(src.shape, dtype=np.uint8)
    reproject(
        source,
        out,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs=dst_crs,
        resampling=Resampling.nearest,
    )
    assert not out.any()
예제 #34
0
    def read_sentinel2_data(self, input_file, params):
        print("read_sentinel2_data", input_file)
        input_dir = os.path.join(input_file, "GRANULE")
        sub_directories = utils.get_immediate_subdirectories(input_dir)
        image_dir = os.path.join(input_dir, sub_directories[0], "IMG_DATA")

        input_bands = params['bands']
        # input_bands = ['B02', 'B03', 'B04', 'B08',  # 10m
        #                'B05', 'B06', 'B07', 'B8A', 'B11', 'B12',  # 20m
        #                'B01', 'B09', 'B10']  # 60m

        num_bands = len(input_bands)
        scale_factor = 10000.0  #read from metadata?

        band_paths = self.get_band_paths(input_file, self.params['bands'])
        for band_ind, img_filename in enumerate(band_paths):
            print("Reading", img_filename)
            with rasterio.open(img_filename) as ds:
                img = ds.read()
                if band_ind == 0:  # First band need to be 10m
                    tmparr = np.empty_like(img)
                    ds10 = ds
                    aff10 = ds.transform
                    img_stack = np.zeros(
                        (img.shape[1], img.shape[2], num_bands))
                    img_stack[:, :, band_ind] = img.squeeze() / scale_factor
                elif input_bands[band_ind] == "B03" or input_bands[
                        band_ind] == "B04" or input_bands[
                            band_ind] == "B08":  # 10m
                    img_stack[:, :, band_ind] = img.squeeze() / scale_factor
                else:
                    reproject(img,
                              tmparr,
                              src_transform=ds.transform,
                              dst_transform=aff10,
                              src_crs=ds.crs,
                              dst_crs=ds.crs,
                              resampling=Resampling.cubic_spline)
                    img_stack[:, :, band_ind] = tmparr.squeeze() / scale_factor

        return (img_stack, ds10)
예제 #35
0
def create_ortho_mask(in_path, dst_path, dst_crs=None):
    """Create a mask on an orthophotography and write the output in the specified crs"""
    # Read the input raster
    with rasterio.open(in_path) as src:
        r, g, b, a = src.read()
        profile = src.profile
        bounds = src.bounds

    # Build the mask
    tot = r + g + b
    tot[tot > 200] = 255
    tot[tot <= 200] = 0

    # There is only one band in the output
    profile["count"] = 1

    # If dst_crs is define, we transform the raster to the destination crs
    if dst_crs:
        # Calculate the ideal dimensions and transformation in the new crs
        dst_affine, dst_width, dst_height = calculate_default_transform(
            profile["crs"], dst_crs, profile["width"], profile["height"], *bounds)

        # Update the profile
        profile['crs'] = dst_crs
        profile['transform'] = dst_affine
        profile['affine'] = dst_affine
        profile['width'] = dst_width
        profile['height'] = dst_height

        # Reproject with the default resampling method
        reproject(
            source=tot,
            src_crs=profile["crs"],
            src_transform=profile["affine"],
            destination=tot,
            dst_transform=dst_affine,
            dst_crs=dst_crs)

    # Write the output raster
    with rasterio.open(dst_path, "w", **profile) as dest:
        dest.write(tot, 1)
예제 #36
0
def dt_whole_year(year):
    population_file_path = POP_DATA_SRC / 'nasa_grid' / 'count' / f'population_{year}_quartres.tif'

    nasa_giss = xr.open_dataset(str(nasa_giss_anom))

    t_anomaly = nasa_giss.sel(time=f'{year}').air.mean(
        dim='time').values.astype('float32')

    with rasterio.open(str(population_file_path)) as pop:
        print(pop.meta)
        pop_meta = pop.meta
        pop_trns = pop.transform
        population = pop.read(1)

    common_crs = pop_meta['crs']

    newarr = np.empty(shape=population.shape)
    print('Reprojecting')

    reproject(t_anomaly,
              newarr,
              src_transform=nasa_trns,
              dst_transform=pop_trns,
              src_crs=common_crs,
              dst_crs=common_crs,
              resample=Resampling.bilinear)

    indicator = population * newarr

    with rasterio.open(str(DATA_SRC / 'lancet' /
                           f'nasa_dt_indicator_{year}.tif'),
                       'w',
                       driver='GTiff',
                       height=indicator.shape[0],
                       width=indicator.shape[1],
                       count=1,
                       dtype=indicator.dtype,
                       crs=common_crs,
                       transform=pop_trns,
                       compress='lzw') as new_dataset:
        new_dataset.write(indicator, 1)
예제 #37
0
    def reproject(self, dst_crs, fp=None, interpolation='nearest'):

        if dst_crs == 'utm':
            dst_crs = get_utm_zone(self.crs, self.transform,
                                   (self.height, self.width))

        # get temporary filepath if such is not provided
        tmp_file = False if fp is not None else True
        if fp is None:
            fp = '{tmp}/reprojected_{crs}/{directory}/{name}.tif'.format(
                tmp=TMP_DIR,
                crs=dst_crs,
                directory=random_name(),
                name=self.name)
        os.makedirs(os.path.dirname(fp), exist_ok=True)

        # calculate params of new reprojected Band
        transform, width, height = calculate_default_transform(
            self.crs, dst_crs, self.width, self.height, *self.bounds)
        kwargs = self.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': transform,
            'width': width,
            'height': height
        })

        # reproject
        with rasterio.open(fp, 'w', **kwargs) as dst:
            reproject(source=rasterio.band(self._band, 1),
                      destination=rasterio.band(dst, 1),
                      src_transform=self.transform,
                      src_crs=self.crs,
                      dst_transform=transform,
                      dst_crs=dst_crs,
                      resampling=getattr(Resampling, interpolation))

        # new band
        band = Band(fp)
        band._tmp_file = tmp_file  # file will be automatically removed when `Band` instance will be deleted
        return band
예제 #38
0
def calc_difference(ndvi_tile1, ndvi_tile2, output):
    """This function calculates the difference of two rasters
    Parameter: Two ndvi calculated rasters, output raster"""

    #open dataset and get Affine transformation and bounding properties
    with rio.open(ndvi1) as src1:
        meta = src1.meta.copy()
        transform = src1.meta["transform"]
        x = meta['width']
        y = meta['height']
        band1 = src1.read()

    #open dataset
    with rio.open(ndvi2) as src2:
        #read the band as ndarray with the same dimension of src1
        band2 = src2.read(out_shape=(src1.height, src1.width),
                          resampling=rio.enums.Resampling.bilinear)
        #create destination for reprojection of src2
        dst_crs = {'init': 'EPSG:32632'}
        proj_band2 = np.empty(src1.shape, dtype=np.float32)
        #reproject the src2 to match src1
        warp.reproject(band2,
                       destination=proj_band2,
                       src_transform=src2.transform,
                       src_crs=src2.crs,
                       dst_transform=transform,
                       dst_crs=dst_crs)

    #calculate difference between reprojected band2 and band1
    difference = np.subtract(proj_band2, band1)
    #create outfile
    outfile = output
    #write outfile with the properties and resolution of src1
    with rio.open(outfile, 'w', **meta) as dst:
        dst.write(difference,
                  window=rio.windows.Window(col_off=0,
                                            row_off=0,
                                            width=x,
                                            height=y))

    return outfile
예제 #39
0
def test_reproject_view():
    """Source views are reprojected properly"""
    with rasterio.open("tests/data/RGB.byte.tif") as src:
        source = src.read(1)

    window = windows.Window(100, 100, 500, 500)
    # window = windows.get_data_window(source)
    reduced_array = source[window.toslices()]
    reduced_transform = windows.transform(window, src.transform)

    # Assert that we're working with a view.
    assert reduced_array.base is source

    dst_crs = dict(
        proj="merc",
        a=6378137,
        b=6378137,
        lat_ts=0.0,
        lon_0=0.0,
        x_0=0.0,
        y_0=0,
        k=1.0,
        units="m",
        nadgrids="@null",
        wktext=True,
        no_defs=True,
    )

    out = np.empty(src.shape, dtype=np.uint8)

    reproject(
        reduced_array,
        out,
        src_transform=reduced_transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs=dst_crs,
        resampling=Resampling.nearest,
    )

    assert (out > 0).sum() == 299199
def reproject_raster(inpath, outpath, new_crs):
    """
    Reproject/define projection of studyregion raster
    
    Parameters
    ----------
    inpath : string
        filepath of input raster image
    outpath : string 
        filepath to save output raster
    new_crs : dict
        new projected crs

    Returns
    -------
    output raster
 
    """
    # CRS for web meractor
    dst_crs = new_crs
    with rasterio.open(inpath) as src:
        transform, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)
        kwargs = src.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': transform,
            'width': width,
            'height': height
        })
        with rasterio.open(outpath, 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(source=rasterio.band(src, i),
                          destination=rasterio.band(dst, i),
                          src_transform=src.transform,
                          src_crs=src.crs,
                          dst_transform=transform,
                          dst_crs=dst_crs,
                          resampling=Resampling.nearest)
        raster_proj = rasterio.open(outpath)
        return raster_proj
예제 #41
0
def rescale(root, name, template="hand.tif"):
    '''
    Rescale a raster based on the
    :param root: root directory
    :param name: name of raster to be rescaled
    :param template: Name of file used for rescale template. Defaults to Hand in the output folder.
    :return: none
    '''
    with rasterio.open(os.path.join(root, template)) as mask_file:
        # with rasterio.open(os.path.join(root, 'demfill.tif')) as mask2:
        shape = [{
            'type':
            'Polygon',
            'coordinates': [[(mask_file.bounds.left, mask_file.bounds.top),
                             (mask_file.bounds.left, mask_file.bounds.bottom),
                             (mask_file.bounds.right, mask_file.bounds.bottom),
                             (mask_file.bounds.right, mask_file.bounds.top)]]
        }]

        with rasterio.open(os.path.join(root, "temp.tif")) as src:
            # resample_raster(src, upscale_factor, huc, name)
            transform, width, height = calculate_default_transform(
                src.crs, mask_file.crs, mask_file.width, mask_file.height,
                *mask_file.bounds)
            kwargs = src.meta.copy()
            kwargs.update({
                'crs': mask_file.crs,
                'transform': transform,
                'width': width,
                'height': height,
            })
            with rasterio.open(os.path.join(root, name), 'w', **kwargs) as dst:
                for i in range(1, src.count + 1):
                    reproject(source=rasterio.band(src, i),
                              destination=rasterio.band(dst, i),
                              src_transform=src.transform,
                              src_crs=src.crs,
                              dst_transform=transform,
                              dst_crs=mask_file.crs,
                              resampling=Resampling.bilinear)
    return None
예제 #42
0
def reprojectA(id):
    dst_crs = 'EPSG:4326'
    with rasterio.open(id + '.tif') as src:
        transform, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)
        kwargs = src.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': transform,
            'width': width,
            'height': height
        })
        with rasterio.open('reprojected.tif', 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(source=rasterio.band(src, i),
                          destination=rasterio.band(dst, i),
                          src_transform=src.transform,
                          src_crs=src.crs,
                          dst_transform=transform,
                          dst_crs=dst_crs,
                          resampling=Resampling.nearest)
예제 #43
0
def render_image(renderer, data, filename, scale=1, reproject_kwargs=None):
    if reproject_kwargs is not None:

        with rasterio.drivers():
            out = numpy.empty(shape=reproject_kwargs['dst_shape'],
                              dtype=data.dtype)
            out.fill(data.fill_value)
            reproject(data, out, **reproject_kwargs)
            # Reapply mask
            data = numpy.ma.masked_array(out, mask=out == data.fill_value)

    resampling = ANTIALIAS
    if renderer.name == 'unique':
        resampling = NEAREST

    img = renderer.render_image(data)
    if scale != 1:
        img = img.resize(
            (numpy.array(data.shape[::-1]) * scale).astype(numpy.uint),
            resampling)
    img.save(filename)
예제 #44
0
def test_reproject_nodata_nan():
    params = default_reproject_params()

    with rasterio.Env():
        source = np.ones((params.width, params.height), dtype=np.float32)
        out = np.zeros((params.dst_width, params.dst_height),
                       dtype=source.dtype)
        out.fill(120)  # Fill with arbitrary value

        reproject(source,
                  out,
                  src_transform=params.src_transform,
                  src_crs=params.src_crs,
                  src_nodata=np.nan,
                  dst_transform=params.dst_transform,
                  dst_crs=params.dst_crs,
                  dst_nodata=np.nan)

        assert (out == 1).sum() == 6215
        assert np.isnan(out).sum() == (params.dst_width * params.dst_height -
                                       6215)
예제 #45
0
def reproject_raster(raster_path, dst_crs, outpul_file):
    with rasterio.open(raster_path) as src:
        transform, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)
        kwargs = src.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': transform,
            'width': width,
            'height': height
        })

        with rasterio.open(outpul_file, 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(source=rasterio.band(src, i),
                          destination=rasterio.band(dst, i),
                          src_transform=src.transform,
                          src_crs=src.crs,
                          dst_transform=transform,
                          dst_crs=dst_crs,
                          resampling=Resampling.nearest)
예제 #46
0
def interpolate_clouds_to_10m(file_clouds):
    print('interpolate_clouds_to_10m:\n')
    print('Open files, reserve memory:\n')
    dataset_clouds = rio.open(file_clouds)
    clouds = dataset_clouds.read(1)
    clouds10 = np.empty(shape=(int(round(clouds.shape[0] * 2)),
                               int(round(clouds.shape[1] * 2))))
    print('define transformation:\n')

    aff = dataset_clouds.transform
    newaff = Affine(aff[0] / 2, aff[1], aff[2], aff[3], aff[4] / 2, aff[5])
    print('Reproject: \n')
    reproject(clouds,
              clouds10,
              src_transform=aff,
              dst_transform=newaff,
              src_crs=dataset_clouds.crs,
              dst_crs=dataset_clouds.crs,
              resampling=Resampling.nearest)

    return (clouds10)
예제 #47
0
def test_reproject_dst_nodata():
    """Affirm resolution of issue #1395"""
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        source = src.read(1)

    dst_crs = {'init': 'EPSG:3857'}
    out = np.empty(src.shape, dtype=np.float32)
    reproject(
        source,
        out,
        src_transform=src.transform,
        src_crs=src.crs,
        dst_transform=DST_TRANSFORM,
        dst_crs=dst_crs,
        src_nodata=0,
        dst_nodata=np.nan,
        resampling=Resampling.nearest)

    assert (out > 0).sum() == 438113
    assert out[0, 0] != 0
    assert np.isnan(out[0, 0])
예제 #48
0
파일: sh_add.py 프로젝트: maiti21/eo-learn
    def _reproject(self, eopatch, src_raster):
        """
        Reprojects the raster data from Geopedia's CRS (POP_WEB) to EOPatch's CRS.
        """
        height, width = src_raster.shape

        dst_raster = np.ones((height, width), dtype=self.raster_dtype)

        src_bbox = transform_bbox(eopatch.bbox, CRS.POP_WEB)
        src_transform = transform.from_bounds(*src_bbox, width=width, height=height)

        dst_bbox = eopatch.bbox
        dst_transform = transform.from_bounds(*dst_bbox, width=width, height=height)

        warp.reproject(src_raster, dst_raster,
                       src_transform=src_transform, src_crs={'init': CRS.ogc_string(CRS.POP_WEB)},
                       src_nodata=0,
                       dst_transform=dst_transform, dst_crs={'init': CRS.ogc_string(eopatch.bbox.crs)},
                       dst_nodata=self.no_data_val)

        return dst_raster
예제 #49
0
def SetCRSRes(srcpath, dstpath, dst_crs, dst_transform, dst_width, dst_height):
    with rasterio.open(srcpath) as src:
        transform, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)
        kwargs = src.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': dst_transform,
            'width': dst_width,
            'height': dst_height
        })

        with rasterio.open(dstpath, 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(source=rasterio.band(src, i),
                          destination=rasterio.band(dst, i),
                          src_transform=src.affine,
                          src_crs=src.crs,
                          dst_transform=dst_transform,
                          dst_crs=dst_crs,
                          resampling=Resampling.nearest)
예제 #50
0
def test_reproject_nodata_nan(options, expected):

    with rasterio.Env(**options):
        params = uninvertable_reproject_params()
        source = np.ones((params.width, params.height), dtype=np.float32)
        out = np.zeros((params.dst_width, params.dst_height), dtype=source.dtype)
        out.fill(120)  # Fill with arbitrary value

        reproject(
            source,
            out,
            src_transform=params.src_transform,
            src_crs=params.src_crs,
            src_nodata=np.nan,
            dst_transform=params.dst_transform,
            dst_crs=params.dst_crs,
            dst_nodata=np.nan,
        )

        assert (out == 1).sum() == expected
        assert np.isnan(out).sum() == (params.dst_width * params.dst_height - expected)
예제 #51
0
def test_warp_from_to_file(tmpdir):
    """File to file."""
    tiffname = str(tmpdir.join('foo.tif'))
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        dst_crs = dict(proj='merc',
                       a=6378137,
                       b=6378137,
                       lat_ts=0.0,
                       lon_0=0.0,
                       x_0=0.0,
                       y_0=0,
                       k=1.0,
                       units='m',
                       nadgrids='@null',
                       wktext=True,
                       no_defs=True)
        kwargs = src.meta.copy()
        kwargs.update(transform=DST_TRANSFORM, crs=dst_crs)
        with rasterio.open(tiffname, 'w', **kwargs) as dst:
            for i in (1, 2, 3):
                reproject(rasterio.band(src, i), rasterio.band(dst, i))
예제 #52
0
def test_reproject_identity_src():
    """Reproject with an identity like source matrices."""
    src = np.random.random(25).reshape((1, 5, 5))
    dst = np.empty(shape=(1, 10, 10))
    dstaff = Affine(0.5, 0.0, 0.0, 0.0, 0.5, 0.0)
    crs = {'init': 'epsg:3857'}

    src_affines = [
        Affine(1.0, 0.0, 0.0, 0.0, 1.0, 0.0),  # Identity both positive
        Affine(1.0, 0.0, 0.0, 0.0, -1.0, 0.0),  # Identity with negative e
    ]

    for srcaff in src_affines:
        # reproject expected to not raise any error in any of the srcaff
        reproject(src,
                  dst,
                  src_transform=srcaff,
                  src_crs=crs,
                  dst_transform=dstaff,
                  dst_crs=crs,
                  resampling=Resampling.nearest)
예제 #53
0
def test_warp_from_file():
    """File to ndarray."""
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        dst_crs = dict(proj='merc',
                       a=6378137,
                       b=6378137,
                       lat_ts=0.0,
                       lon_0=0.0,
                       x_0=0.0,
                       y_0=0,
                       k=1.0,
                       units='m',
                       nadgrids='@null',
                       wktext=True,
                       no_defs=True)
        destin = np.empty(src.shape, dtype=np.uint8)
        reproject(rasterio.band(src, 1),
                  destin,
                  dst_transform=DST_TRANSFORM,
                  dst_crs=dst_crs)
    assert destin.any()
예제 #54
0
def reprojected_by_rio( src : str, dst : str, dst_crs : int = 4326 ) -> None:

    """ Reproject raster with rasterio """

    # Get src dataset
    src_dataset = rio.open(src, 'r')

    # get crs
    src_crs    = src_dataset.crs
    dst_crs    = 'EPSG:%d' % dst_crs

    # get src width x height
    src_width  = src_dataset.width
    src_height = src_dataset.height

    # get src bounds
    src_bounds = src_dataset.bounds

    # get transform
    src_transform                        = src_dataset.transform
    dst_transform, dst_width, dst_height = calculate_default_transform(src_crs, dst_crs, src_width, src_height, *src_bounds)

    # get profile
    src_profile = src_dataset.profile
    dst_profile = src_profile.copy()
    dst_profile.update(crs=dst_crs, width=dst_width, height=dst_height, transform=dst_transform)

    # make reprojection
    with rio.open(dst, 'w', **dst_profile) as dst_dataset:

        for i in range(1, src_dataset.count+1):

            reproject(
                source=rio.band(src_dataset, i),
                destination=rio.band(dst_dataset, i),
                src_transform=src_transform,
                dst_transform=dst_transform,
                src_crs=src_crs,
                dst_crs=dst_crs
            )
예제 #55
0
def raster_resample(raster_name, save_as_filename, epsg, upscale_factor):
    """
    raster resampling code to reduce or increase the no.of pixels in a given
    raster. Upscale factor determines whether it is upscaled or downscaled.
    Upscale has lesser cell size and downscale has higher cell size.
    :param raster_name: raster filename
    :param save_as_filename: save as file name
    :param epsg: epsg code for reprojection
    :param upscale_factor: the factor for upscaling or downscaling
    :return: None
    """
    try:
        with rasterio.open(raster_name) as dem:
            dst_crs = 'EPSG:' + str(epsg)

            data = dem.read(out_shape=(dem.count,
                                       int(dem.height * upscale_factor),
                                       int(dem.width * upscale_factor)),
                            resampling=Resampling.bilinear)

            transform = dem.transform * dem.transform.scale(
                (dem.width / data.shape[-1]), (dem.height / data.shape[-2]))
            kwargs = dem.meta.copy()
            kwargs.update({
                'crs': dem.crs,
                'transform': transform,
                'width': dem.width * upscale_factor,
                'height': dem.height * upscale_factor
            })
            with rasterio.open(save_as_filename, 'w', **kwargs) as dst:
                for i in range(1, dem.count + 1):
                    reproject(source=rasterio.band(dem, i),
                              destination=rasterio.band(dst, i),
                              src_transform=dem.transform,
                              src_crs=dem.crs,
                              dst_crs=dst_crs,
                              dst_transform=transform,
                              resampling=Resampling.bilinear)
    except IOError:
        print('Error in opening the raster, check for location/availability')
예제 #56
0
def test_resample_default_invert_proj(method):
    """Nearest and bilinear should produce valid results
    with the default Env
    """

    with rasterio.open("tests/data/world.rgb.tif") as src:
        source = src.read(1)
        profile = src.profile

    dst_crs = "EPSG:32619"

    # Calculate the ideal dimensions and transformation in the new crs
    dst_affine, dst_width, dst_height = calculate_default_transform(
        src.crs, dst_crs, src.width, src.height, *src.bounds
    )

    profile["height"] = dst_height
    profile["width"] = dst_width

    out = np.empty(shape=(dst_height, dst_width), dtype=np.uint8)

    # GDAL 1.11 needs to have this config option set on to match the
    # default results in later versions.
    if gdal_version.major == 1:
        options = dict(CHECK_WITH_INVERT_PROJ=True)
    else:
        options = {}

    with rasterio.Env(**options):
        reproject(
            source,
            out,
            src_transform=src.transform,
            src_crs=src.crs,
            dst_transform=dst_affine,
            dst_crs=dst_crs,
            resampling=method,
        )

    assert out.mean() > 0
예제 #57
0
파일: misc.py 프로젝트: weizushuai/SMAP
def sub_n_reproj(input_mat, kwargs_sub, sub_window, output_crs):
    # Get the georeference and bounding parameters of subset image
    # kwargs_sub = input_ds.meta.copy()
    kwargs_sub.update({
        'height':
        sub_window.height,
        'width':
        sub_window.width,
        'transform':
        rasterio.windows.transform(sub_window, kwargs_sub['transform'])
    })

    # Generate subset rasterio dataset
    input_ds_subset = MemoryFile().open(**kwargs_sub)
    # mat_sub = input_ds.read(1, window=sub_window)
    input_mat = np.expand_dims(input_mat, axis=0)
    input_ds_subset.write(input_mat)

    # Reproject a dataset
    transform_reproj, width_reproj, height_reproj = \
        calculate_default_transform(input_ds_subset.crs, output_crs,
                                    input_ds_subset.width, input_ds_subset.height, *input_ds_subset.bounds)
    kwargs_reproj = input_ds_subset.meta.copy()
    kwargs_reproj.update({
        'crs': output_crs,
        'transform': transform_reproj,
        'width': width_reproj,
        'height': height_reproj
    })

    output_ds = MemoryFile().open(**kwargs_reproj)
    reproject(source=rasterio.band(smap_sm_1km_subset, 1),
              destination=rasterio.band(output_ds, 1),
              src_transform=smap_sm_1km_subset.transform,
              src_crs=smap_sm_1km_subset.crs,
              dst_transform=transform,
              dst_crs=dst_crs,
              resampling=Resampling.nearest)

    return output_ds
예제 #58
0
def reproject_GSWE(
    GSWE_file_list,
    target_crs='+proj=utm +zone=30 +ellps=WGS84 +datum=WGS84 +units=m +no_defs '
):
    """Summary
	
	Args:
	    GSWE_file_list (TYPE): Description
	    target_crs (str, optional): Description
	"""
    try:
        os.makedirs("./Reprojected")
    except FileExistsError:
        # directory already exists
        pass

    ## Reporject GSWE exports from WGS to target_crs
    for gsw in GSWE_file_list:
        head, tail = os.path.split(gsw)
        with rio.open(gsw) as src:
            transform, width, height = calculate_default_transform(
                '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', target_crs,
                src.width, src.height, *src.bounds)
            kwargs = src.meta.copy()
            kwargs.update({
                'crs': target_crs,
                'transform': transform,
                'width': width,
                'height': height,
                'compress': 'lzw'
            })
            with rio.open('./Reprojected/' + tail[0:-4] + 'UTM.tif', 'w',
                          **kwargs) as dst:
                reproject(source=rio.band(src, 1),
                          destination=rio.band(dst, 1),
                          src_transform=src.transform,
                          src_crs=src.crs,
                          dst_transform=transform,
                          dst_crs=target_crs,
                          resampling=Resampling.nearest)
예제 #59
0
def test_resample_no_invert_proj(method):
    """Nearest and bilinear should produce valid results with
    CHECK_WITH_INVERT_PROJ = False
    """
    if not supported_resampling(method):
        pytest.skip()

    if method in (Resampling.bilinear, Resampling.cubic,
                  Resampling.cubic_spline, Resampling.lanczos):
        pytest.xfail(
            reason="Some resampling methods succeed but produce blank images. "
            "See https://github.com/mapbox/rasterio/issues/614")

    with rasterio.Env(CHECK_WITH_INVERT_PROJ=False):
        with rasterio.open('tests/data/world.rgb.tif') as src:
            source = src.read(1)
            profile = src.profile.copy()

        dst_crs = {'init': 'EPSG:32619'}

        # Calculate the ideal dimensions and transformation in the new crs
        dst_affine, dst_width, dst_height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)

        profile['height'] = dst_height
        profile['width'] = dst_width

        out = np.empty(shape=(dst_height, dst_width), dtype=np.uint8)

        # see #614, some resampling methods succeed but produce blank images
        out = np.empty(src.shape, dtype=np.uint8)
        reproject(source,
                  out,
                  src_transform=src.transform,
                  src_crs=src.crs,
                  dst_transform=dst_affine,
                  dst_crs=dst_crs,
                  resampling=method)

        assert out.mean() > 0
예제 #60
0
def _tile_worker(tile):
    """
    For each tile, and given an open rasterio src, plus a`global_args` dictionary
    with attributes of `base_val`, `interval`, and a `writer_func`,
    warp a continous single band raster to a 512 x 512 mercator tile,
    then encode this tile into RGB.

    Parameters
    -----------
    tile: list
        [x, y, z] indices of tile

    Returns
    --------
    tile, buffer
        tuple with the input tile, and a bytearray with the data encoded into
        the format created in the `writer_func`

    """
    x, y, z = tile

    bounds = [
        c for i in (mercantile.xy(*mercantile.ul(x, y + 1, z)),
                    mercantile.xy(*mercantile.ul(x + 1, y, z))) for c in i
    ]

    toaffine = transform.from_bounds(*bounds + [512, 512])

    out = np.empty((512, 512), dtype=src.meta['dtype'])

    reproject(rasterio.band(src, 1),
              out,
              dst_transform=toaffine,
              dst_crs="init='epsg:3857'",
              resampling=RESAMPLING.bilinear)

    out = data_to_rgb(out, global_args['base_val'], global_args['interval'])

    return tile, global_args['writer_func'](out, global_args['kwargs'].copy(),
                                            toaffine)