コード例 #1
0
def main(npy_in,tif_out,tif_like):
    
    arr_in = np.load(npy_in)
    locs = arr_in[:,:2].astype(np.int)

    ## "un-flatten" to array shape of basin raster
    # get correct shape and coordinates
    with rs.drivers():
        with rs.open(tif_like) as src:
            profile = src.profile
            w,h = (src.width, src.height)
            aff = src.affine
            ndv = src.get_nodatavals()
            x,y = [i.astype(np.int) for i in (~aff * (locs[:,0],locs[:,1]))]

    # construct 3d array
    arr_out = np.ones((w,h,arr_in.shape[1]-2),dtype=np.float32)
    arr_out[:] = ndv
    arr_out[x,y,:] = arr_in[:,2:]
    arr_out = np.swapaxes(arr_out,0,2)

    # update output profile to get correct # of bands and compression mechanism
    profile.update(
        count = arr_out.shape[0],
        compress = 'DEFLATE')


    ## output Tiff file
    with rs.drivers():
        with rs.open(tif_out, 'w', **profile) as dst:
            dst.write(arr_out)
コード例 #2
0
def test_drivers():
    with rasterio.drivers() as m:
        assert driver_count() > 0
        assert type(m) == DriverManager
        
        n = rasterio.drivers()
        assert driver_count() > 0
        assert type(n) == DummyManager
コード例 #3
0
ファイル: _gdal.py プロジェクト: valpasq/yatsm
 def _src(self):
     """ An optionally memoized generator on time series datasets
     """
     if self.keep_open:
         if not hasattr(self, '_src_open'):
             with rasterio.drivers():
                 self._src_open = [rasterio.open(f, 'r') for
                                   f in self.df['filename']]
         for _src in self._src_open:
             yield _src
     else:
         with rasterio.drivers():
             for f in self.df['filename']:
                 yield rasterio.open(f, 'r')
コード例 #4
0
def main(infile, outfile, with_threads=False):
    
    with rasterio.drivers():

        # Open the source dataset.
        with rasterio.open(infile) as src:

            # Create a destination dataset based on source params. The
            # destination will be tiled, and we'll "process" the tiles
            # concurrently.

            meta = src.meta
            del meta['transform']
            meta.update(affine=src.affine)
            meta.update(blockxsize=256, blockysize=256, tiled='yes')
            with rasterio.open(outfile, 'w', **meta) as dst:

                loop = asyncio.get_event_loop()
                
                # With the exception of the ``yield from`` statement,
                # process_window() looks like callback-free synchronous
                # code. With a coroutine, we can keep the read, compute,
                # and write statements close together for
                # maintainability. As in the concurrent-cpu-bound.py
                # example, all of the speedup is provided by
                # distributing raster computation across multiple
                # threads. The difference here is that we're submitting
                # jobs to the thread pool asynchronously.

                @asyncio.coroutine
                def process_window(window):
                    
                    # Read a window of data.
                    data = src.read(window=window)
                    
                    # We run the raster computation in a separate thread
                    # and pause until the computation finishes, letting
                    # other coroutines advance.
                    #
                    # The _example.compute function modifies no Python
                    # objects and releases the GIL. It can execute
                    # concurrently.
                    result = numpy.zeros(data.shape, dtype=data.dtype)
                    if with_threads:
                        yield from loop.run_in_executor(
                                            None, compute, data, result)
                    else:
                        compute(data, result)
                    
                    # Write the result.
                    for i, arr in enumerate(result, 1):
                        dst.write_band(i, arr, window=window)

                # Queue up the loop's tasks.
                tasks = [asyncio.Task(process_window(window)) 
                         for ij, window in dst.block_windows(1)]
                
                # Wait for all the tasks to finish, and close.
                loop.run_until_complete(asyncio.wait(tasks))
                loop.close()
コード例 #5
0
def test_sieve_mask():
    """Test proper behavior of mask image, if passed int sieve"""

    with rasterio.drivers():
        shape = (20, 20)
        image = numpy.zeros(shape, dtype=rasterio.ubyte)
        image[5:15, 5:15] = 1
        image[1:3, 1:3] = 2

        # Blank mask has no effect, only areas smaller than size will be
        # removed
        mask = numpy.ones(shape, dtype=rasterio.bool_)
        sieved_image = ftrz.sieve(image, 100, mask=mask)
        truth = numpy.zeros_like(image)
        truth[5:15, 5:15] = 1
        assert numpy.array_equal(sieved_image, truth)

        # Only areas within the overlap of the mask and values will be kept
        mask = numpy.ones(shape, dtype=rasterio.bool_)
        mask[7:10, 7:10] = False
        sieved_image = ftrz.sieve(image, 100, mask=mask)
        truth = numpy.zeros_like(image)
        truth[7:10, 7:10] = 1
        assert numpy.array_equal(sieved_image, truth)

        # mask of other type than rasterio.bool_ should fail
        mask = numpy.zeros(shape, dtype=rasterio.uint8)
        with pytest.raises(ValueError):
            ftrz.sieve(image, 100, mask=mask)
コード例 #6
0
def test_sieve():
    """Test sieving a 10x10 feature from an ndarray."""

    image = numpy.zeros((20, 20), dtype=rasterio.ubyte)
    image[5:15, 5:15] = 1

    # An attempt to sieve out features smaller than 100 should not change the
    # image.
    with rasterio.drivers():
        sieved_image = ftrz.sieve(image, 100)
        assert numpy.array_equal(sieved_image, image)

    # Setting the size to 100 should leave us an empty, False image.
    with rasterio.drivers():
        sieved_image = ftrz.sieve(image, 101)
        assert not sieved_image.any()
コード例 #7
0
def visualize_labels(geo_tiff, nlcd):
    """Make a picture with color-coded labels as pixels

    :param geo_tiff: path to GeoTiff file
    :param nlcd: path to NLCD .img file
    """
    print "Getting labels..."
    labels = get_labels_tif(geo_tiff, nlcd)
    with rasterio.drivers():
        with rasterio.open(geo_tiff) as src:
            width = src.width
            height = src.height
    rgb = np.zeros((height, width, 3))

    print "Getting colors for labels..."
    for col in range(0, width):
        for row in range(0, height):
            label = labels[row + col*height]
            r, g, b = const.RGB_LABELS[label]
            rgb[(row, col, 0)] = r
            rgb[(row, col, 1)] = g
            rgb[(row, col, 2)] = b

    # show image
    print "Showing image now..."
    plt.imshow(rgb)
    plt.show()
コード例 #8
0
def get_labels_lat_lon(coords, nlcd):
    """Gets the labels corresponding to coordinates

    1. Transforms (latitude, longitude) to the coordinate reference system
       used in the NLCD data set (Alber Conical Equal Area (ACEA))
    2. Transforms ACEA coordinates to pixels in the raster data set
    3. Queries the labels for those pixels
       None if one of the indeces of the pixel is out-of-bounds

    :param coords: list of (latitude, longitude) tuples
    :param main_folder: path to folder where the data folder is found
    :return: list: list containing the labels corresponding to each coordinate
                   None for coordinates not in the NLCD data set
    """
    labels = []
    # transform lat, lon to Albers Conical Equal Area (ACEA)
    acea = pyproj.Proj(ACEA_PROJ4)
    acea_coords = [(acea(lon, lat)) for lat, lon in coords]
    # open NLCD raster data set
    with rasterio.drivers():
        with rasterio.open(nlcd) as src:
            # linear transformation between ACEA coordinates and pixels
            rev = ~src.affine
            # transform ACEA to pixel coordinates
            pixels = [tuple(int(round(i)) for i in rev*coord) for coord in acea_coords]
            for col, row in pixels:
                if col < 0 or col >= src.width or row < 0 or row >= src.height:
                    labels.append(None)
                else:
                    window = ((row, row+1), (col, col+1))
                    labels.append(src.read(1, window=window)[0,0])
    return labels
コード例 #9
0
ファイル: test_features.py プロジェクト: aashish24/rasterio
def test_rasterize_invalid_out_dtype(basic_geometry):
    """ A non-supported data type for out should raise an exception """

    out = numpy.zeros(DEFAULT_SHAPE, dtype=numpy.int64)
    with rasterio.drivers():
        with pytest.raises(ValueError):
            rasterize([basic_geometry], out=out)
コード例 #10
0
ファイル: test_features.py プロジェクト: aashish24/rasterio
def test_sieve_large(basic_image):
    """
    Setting the size larger than size of feature should leave us an empty image.
    """

    with rasterio.drivers():
        assert not numpy.any(sieve(basic_image, basic_image.sum() + 1))
コード例 #11
0
ファイル: test_features.py プロジェクト: aashish24/rasterio
def test_shapes(basic_image):
    """ Test creation of shapes from pixel values """

    with rasterio.drivers():
        results = list(shapes(basic_image))

        assert len(results) == 2

        shape, value = results[0]
        assert shape == {
            'coordinates': [
                [(2, 2), (2, 5), (5, 5), (5, 2), (2, 2)]
            ],
            'type': 'Polygon'
        }
        assert value == 1

        shape, value = results[1]
        assert shape == {
            'coordinates': [
                [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
                [(2, 2), (5, 2), (5, 5), (2, 5), (2, 2)]
            ],
            'type': 'Polygon'
        }
        assert value == 0
コード例 #12
0
ファイル: test_features.py プロジェクト: aashish24/rasterio
def test_rasterize_unsupported_dtype(basic_geometry):
    """ Unsupported types should all raise exceptions """

    with rasterio.drivers():
        unsupported_types = (
            ('int8', -127),
            ('int64', 20439845334323),
            ('float16', -9343.232)
        )

        for dtype, default_value in unsupported_types:
            with pytest.raises(ValueError):
                rasterize(
                    [basic_geometry],
                    out_shape=DEFAULT_SHAPE,
                    default_value=default_value,
                    dtype=dtype
                )

            with pytest.raises(ValueError):
                rasterize(
                    [(basic_geometry, default_value)],
                    out_shape=DEFAULT_SHAPE,
                    dtype=dtype
                )
コード例 #13
0
ファイル: test_features.py プロジェクト: aashish24/rasterio
def test_rasterize_supported_dtype(basic_geometry):
    """ Supported data types should return valid results """

    with rasterio.drivers():
        supported_types = (
            ('int16', -32768),
            ('int32', -2147483648),
            ('uint8', 255),
            ('uint16', 65535),
            ('uint32', 4294967295),
            ('float32', 1.434532),
            ('float64', -98332.133422114)
        )

        for dtype, default_value in supported_types:
            truth = numpy.zeros(DEFAULT_SHAPE, dtype=dtype)
            truth[2:4, 2:4] = default_value

            result = rasterize(
                [basic_geometry],
                out_shape=DEFAULT_SHAPE,
                default_value=default_value,
                dtype=dtype
            )
            assert numpy.array_equal(result, truth)
            assert numpy.dtype(result.dtype) == numpy.dtype(truth.dtype)

            result = rasterize(
                [(basic_geometry, default_value)],
                out_shape=DEFAULT_SHAPE
            )
            if numpy.dtype(dtype).kind == 'f':
                assert numpy.allclose(result, truth)
            else:
                assert numpy.array_equal(result, truth)
コード例 #14
0
ファイル: utilities.py プロジェクト: kapadia/geoblend
def get_mask(srcpath):
    """
    Read the mask for a given filepath. It is assumed
    that the 4th band corresponds to a mask. If a 4th
    band does not exist, a square mask is generated with
    border pixels marked out.

    :param srcpath:
    """

    with rio.drivers():
        with rio.open(srcpath) as src:
            count = src.count

            if count < 4:
                mask = np.ones(src.shape, dtype=np.uint8)
                mask[0, :] = 0
                mask[-1, :] = 0
                mask[:, 0] = 0
                mask[:, -1] = 0
            else:
                mask = src.read(4).astype(np.bool)
                mask = binary_erosion(mask, disk(3)).astype(np.uint8)

                # height, width = src.shape
                # h2, w2 = height / 2, width / 2
                # mask = np.zeros(src.shape, dtype=np.uint8)
                # r = 600
                # mask[h2 - r: h2 + r + 1, w2 - r: w2 + r + 1] = disk(r)

    return mask
コード例 #15
0
ファイル: tool.py プロジェクト: robintw/rasterio
def main(banner, srcfile):
    
    with rasterio.drivers(), rasterio.open(srcfile) as src:
        
        def show(source):
            """Show a raster using matplotlib.

            The raster may be either an ndarray or a (dataset, bidx)
            tuple.
            """
            import matplotlib.pyplot as plt
            if isinstance(source, tuple):
                arr = source[0].read_band(source[1])
            else:
                arr = source
            plt.imshow(arr)
            plt.gray()
            plt.show()

        def stats(source):
            """Return a tuple with raster min, max, and mean.
            """
            if isinstance(source, tuple):
                arr = source[0].read_band(source[1])
            else:
                arr = source
            return Stats(numpy.min(arr), numpy.max(arr), numpy.mean(arr))

        code.interact(
            banner, local=dict(locals(), np=numpy, rio=rasterio))
    
    return 1
コード例 #16
0
def _get_raster_array_gt(raster):
    with rasterio.drivers():
        with rasterio.open(raster, 'r') as src:
            affine = src.affine
            gt = affine.to_gdal()
            arr = src.read(1)
    return arr, gt
コード例 #17
0
def test_options(tmpdir):
    """Test that setting CPL_DEBUG=True results in GDAL debug messages.
    """
    logger = logging.getLogger('GDAL')
    logger.setLevel(logging.DEBUG)
    logfile1 = str(tmpdir.join('test_options1.log'))
    fh = logging.FileHandler(logfile1)
    logger.addHandler(fh)
    
    # With CPL_DEBUG=True, expect debug messages from GDAL in
    # logfile1
    with rasterio.drivers(CPL_DEBUG=True):
        with rasterio.open("rasterio/tests/data/RGB.byte.tif") as src:
            pass

    log = open(logfile1).read()
    assert "GDAL: GDALOpen(rasterio/tests/data/RGB.byte.tif" in log
    
    # The GDAL env above having exited, CPL_DEBUG should be OFF.
    logfile2 = str(tmpdir.join('test_options2.log'))
    fh = logging.FileHandler(logfile2)
    logger.addHandler(fh)

    with rasterio.open("rasterio/tests/data/RGB.byte.tif") as src:
        pass
    
    # Expect no debug messages from GDAL.
    log = open(logfile2).read()
    assert "GDAL: GDALOpen(rasterio/tests/data/RGB.byte.tif" not in log
コード例 #18
0
ファイル: test_warp.py プロジェクト: clembou/rasterio
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()
コード例 #19
0
ファイル: test_crs.py プロジェクト: clembou/rasterio
def test_write_3857(tmpdir):
    src_path = str(tmpdir.join('lol.tif'))
    subprocess.call([
        'gdalwarp', '-t_srs', 'EPSG:3857', 
        'tests/data/RGB.byte.tif', src_path])
    dst_path = str(tmpdir.join('wut.tif'))
    with rasterio.drivers():
        with rasterio.open(src_path) as src:
            with rasterio.open(dst_path, 'w', **src.meta) as dst:
                assert dst.crs == {'init': 'epsg:3857'}
    info = subprocess.check_output([
        'gdalinfo', dst_path])
    assert """PROJCS["WGS 84 / Pseudo-Mercator",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["scale_factor",1],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    EXTENSION["PROJ4","+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  +no_defs"],
    AUTHORITY["EPSG","3857"]]""" in info.decode('utf-8')
コード例 #20
0
ファイル: test_warp.py プロジェクト: clembou/rasterio
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
コード例 #21
0
ファイル: test_warp.py プロジェクト: clembou/rasterio
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)
コード例 #22
0
def main(raster_file, vector_file, driver, mask_value):
    
    with rasterio.drivers():
        
        with rasterio.open(raster_file) as src:
            image = src.read_band(1)
        
        if mask_value is not None:
            mask = image == mask_value
        else:
            mask = None
        
        results = (
            {'properties': {'raster_val': v}, 'geometry': s}
            for i, (s, v) 
            in enumerate(
                shapes(image, mask=mask, transform=src.affine)))

        with fiona.open(
                vector_file, 'w', 
                driver=driver,
                crs=src.crs,
                schema={'properties': [('raster_val', 'int')],
                        'geometry': 'Polygon'}) as dst:
            dst.writerecords(results)
    
    return dst.name
コード例 #23
0
ファイル: features.py プロジェクト: AsgerPetersen/rasterio
def shapes(image, mask=None, connectivity=4, transform=IDENTITY):
    """Yields a (shape, image_value) pair for each feature in the image.
    
    The shapes are GeoJSON-like dicts and the image values are ints.
    
    Features are found using a connected-component labeling algorithm.

    The image must be of unsigned 8-bit integer (rasterio.byte or
    numpy.uint8) data type. If a mask is provided, pixels for which the
    mask is `False` will be excluded from feature generation.
    """
    if np.dtype(image.dtype) != np.dtype(rasterio.ubyte):
        raise ValueError("Image must be dtype uint8/ubyte")

    if mask is not None and np.dtype(mask.dtype) != np.dtype(rasterio.bool_):
        raise ValueError("Mask must be dtype rasterio.bool_")

    if connectivity not in (4, 8):
        raise ValueError("Connectivity Option must be 4 or 8")

    transform = guard_transform(transform)

    with rasterio.drivers():
        for s, v in _shapes(image, mask, connectivity, transform.to_gdal()):
            yield s, v
コード例 #24
0
ファイル: conftest.py プロジェクト: aashish24/rasterio
def pixelated_image_file(tmpdir, pixelated_image):
    """
    A basic raster file with a 10x10 array for testing sieve functions.
    Contains data from pixelated_image.

    Returns
    -------

    string
        Filename of test raster file
    """

    from affine import Affine
    import rasterio

    image = pixelated_image

    outfilename = str(tmpdir.join('pixelated_image.tif'))
    kwargs = {
        "crs": {'init': 'epsg:4326'},
        "transform": Affine.identity(),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": image.shape[1],
        "height": image.shape[0],
        "nodata": 255
    }
    with rasterio.drivers():
        with rasterio.open(outfilename, 'w', **kwargs) as out:
            out.write_band(1, image)

    return outfilename
コード例 #25
0
ファイル: test_rio_merge.py プロジェクト: aashish24/rasterio
def test_data_dir_2(tmpdir):
    kwargs = {
        "crs": {'init': 'epsg:4326'},
        "transform": (-114, 0.2, 0, 46, 0, -0.1),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": 10,
        "height": 10
        # these files have undefined nodata.
    }

    with rasterio.drivers():

        with rasterio.open(str(tmpdir.join('b.tif')), 'w', **kwargs) as dst:
            data = numpy.zeros((10, 10), dtype=rasterio.uint8)
            data[0:6, 0:6] = 255
            dst.write_band(1, data)

        with rasterio.open(str(tmpdir.join('a.tif')), 'w', **kwargs) as dst:
            data = numpy.zeros((10, 10), dtype=rasterio.uint8)
            data[4:8, 4:8] = 254
            dst.write_band(1, data)

    return tmpdir
コード例 #26
0
ファイル: gdal.py プロジェクト: mhearne-usgs/grid
 def getFileGeoDict(cls,filename):
     """Get the spatial extent, resolution, and shape of grid inside ESRI grid file.
     :param filename:
        File name of ESRI grid file.
     :returns:
        - GeoDict specifying spatial extent, resolution, and shape of grid inside ESRI grid file.
        - xvar array specifying X coordinates of data columns
        - yvar array specifying Y coordinates of data rows
     :raises DataSetException:
       When the file contains a grid with more than one band.
     """
     geodict = {}
     with rasterio.drivers():
         with rasterio.open(filename) as src:
             aff = src.affine
             geodict['xdim'] = aff.a
             geodict['ydim'] = -1*aff.e
             geodict['xmin'] = aff.xoff + geodict['xdim']/2.0
             geodict['ymax'] = aff.yoff - geodict['ydim']/2.0
                             
             shp = src.shape
             if len(shp) > 2:
                 raise DataSetException('Cannot support grids with more than one band')
             geodict['nrows'] = src.height
             geodict['ncols'] = src.width
             geodict['xmax'] = geodict['xmin'] + (geodict['ncols']-1)*geodict['xdim']
             geodict['ymin'] = geodict['ymax'] - (geodict['nrows']-1)*geodict['ydim']
     xvar = np.arange(geodict['xmin'],geodict['xmax']+geodict['xdim'],geodict['xdim'])
     yvar = np.arange(geodict['ymin'],geodict['ymax']+geodict['ydim'],geodict['ydim'])
     return (geodict,xvar,yvar)
コード例 #27
0
def test_mask_crop(runner, tmpdir, basic_feature, pixelated_image):
    """
    In order to test --crop option, we need to use a transform more similar to
    a normal raster, with a negative y pixel size.
    """

    image = pixelated_image
    outfilename = str(tmpdir.join("pixelated_image.tif"))
    kwargs = {
        "crs": {"init": "epsg:4326"},
        "transform": Affine(1, 0, 0, 0, -1, 0),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": image.shape[1],
        "height": image.shape[0],
        "nodata": 255,
    }
    with rasterio.drivers():
        with rasterio.open(outfilename, "w", **kwargs) as out:
            out.write_band(1, image)

    output = str(tmpdir.join("test.tif"))

    truth = numpy.zeros((4, 3))
    truth[1:3, 0:2] = 1

    result = runner.invoke(
        features.mask, [outfilename, output, "--crop", "--geojson-mask", "-"], input=json.dumps(basic_feature)
    )

    assert result.exit_code == 0
    assert os.path.exists(output)
    with rasterio.open(output) as out:
        assert numpy.array_equal(truth, out.read(1, masked=True).filled(0))
コード例 #28
0
ファイル: ndvi.py プロジェクト: harryjfowen/landsat-util
    def write_band(self, output_band, output_file, image_data):
        # colormaps will overwrite our transparency masks so we will manually
        # create three RGB bands

        self.output("Applying ColorMap", normal=True, arrow=True)
        self.cmap[0] = (0, 0, 0, 255)

        v_manual_colormap = numpy.vectorize(self.manual_colormap, otypes=[numpy.uint8])
        rgb_bands = []
        for i in range(3):
            rgb_bands.append(v_manual_colormap(output_band, i))

        with rasterio.drivers(GDAL_TIFF_INTERNAL_MASK=True):
            with rasterio.open(output_file, 'w', driver='GTiff',
                               width=image_data['shape'][1],
                               height=image_data['shape'][0],
                               count=3,
                               dtype=numpy.uint8,
                               nodata=0,
                               photometric='RGB',
                               transform=image_data['dst_transform'],
                               crs=self.dst_crs) as output:

                for i in range(3):
                    output.write_band(i+1, rgb_bands[i])

            self.output("Writing to file", normal=True, color='green', indent=1)
        return output_file
コード例 #29
0
ファイル: test_rio_warp.py プロジェクト: aashish24/rasterio
def test_warp_reproject_like(runner, tmpdir):
    likename = str(tmpdir.join('like.tif'))
    kwargs = {
        "crs": {'init': 'epsg:4326'},
        "transform": (-106.523, 0.001, 0, 39.6395, 0, -0.001),
        "count": 1,
        "dtype": rasterio.uint8,
        "driver": "GTiff",
        "width": 10,
        "height": 10,
        "nodata": 0
    }

    with rasterio.drivers():
        with rasterio.open(likename, 'w', **kwargs) as dst:
            data = numpy.zeros((10, 10), dtype=rasterio.uint8)
            dst.write_band(1, data)

    srcname = 'tests/data/shade.tif'
    outputname = str(tmpdir.join('test.tif'))
    result = runner.invoke(warp.warp, [srcname, outputname,
                                       '--like', likename])
    assert result.exit_code == 0
    assert os.path.exists(outputname)

    with rasterio.open(outputname) as output:
        assert output.crs == {'init': 'epsg:4326'}
        assert numpy.allclose([0.001, 0.001], [output.affine.a, -output.affine.e])
        assert output.width == 10
        assert output.height == 10
コード例 #30
0
def rasterize(shapes,
              out_shape=None,
              fill=0,
              out=None,
              output=None,
              transform=IDENTITY,
              all_touched=False,
              default_value=1,
              dtype=None):
    """
    Returns an image array with input geometries burned in.

    Parameters
    ----------
    shapes : iterable of (geometry, value) pairs or iterable over
        geometries. `geometry` can either be an object that implements
        the geo interface or GeoJSON-like object.
    out_shape : tuple or list
        Shape of output numpy ndarray.
    fill : int or float, optional
        Used as fill value for all areas not covered by input
        geometries.
    out : numpy ndarray, optional
        Array of same shape and data type as `image` in which to store
        results.
    output : older alias for `out`, will be removed before 1.0.
    transform : Affine transformation object, optional
        Transformation from pixel coordinates of `image` to the
        coordinate system of the input `shapes`. See the `transform`
        property of dataset objects.
    all_touched : boolean, optional
        If True, all pixels touched by geometries will be burned in.  If
        false, only pixels whose center is within the polygon or that
        are selected by Bresenham's line algorithm will be burned in.
    default_value : int or float, optional
        Used as value for all geometries, if not provided in `shapes`.
    dtype : rasterio or numpy data type, optional
        Used as data type for results, if `out` is not provided.

    Returns
    -------
    out : numpy ndarray
        Results

    Notes
    -----
    Valid data types for `fill`, `default_value`, `out`, `dtype` and
    shape values are rasterio.int16, rasterio.int32, rasterio.uint8,
    rasterio.uint16, rasterio.uint32, rasterio.float32,
    rasterio.float64.

    """

    valid_dtypes = ('int16', 'int32', 'uint8', 'uint16', 'uint32', 'float32',
                    'float64')

    def format_invalid_dtype(param):
        return '{0} dtype must be one of: {1}'.format(param,
                                                      ', '.join(valid_dtypes))

    def format_cast_error(param, dtype):
        return '{0} cannot be cast to specified dtype: {1}'.format(
            param, dtype)

    if fill != 0:
        fill_array = np.array([fill])
        if not validate_dtype(fill_array, valid_dtypes):
            raise ValueError(format_invalid_dtype('fill'))

        if dtype is not None and not can_cast_dtype(fill_array, dtype):
            raise ValueError(format_cast_error('fill', dtype))

    if default_value != 1:
        default_value_array = np.array([default_value])
        if not validate_dtype(default_value_array, valid_dtypes):
            raise ValueError(format_invalid_dtype('default_value'))

        if dtype is not None and not can_cast_dtype(default_value_array,
                                                    dtype):
            raise ValueError(format_cast_error('default_vaue', dtype))

    if dtype is not None and np.dtype(dtype).name not in valid_dtypes:
        raise ValueError(format_invalid_dtype('dtype'))

    valid_shapes = []
    shape_values = []
    for index, item in enumerate(shapes):
        if isinstance(item, (tuple, list)):
            geom, value = item
        else:
            geom = item
            value = default_value
        geom = getattr(geom, '__geo_interface__', None) or geom

        #not isinstance(geom, dict) or
        if 'type' in geom or 'coordinates' in geom:
            valid_shapes.append((geom, value))
            shape_values.append(value)

        else:
            raise ValueError(
                'Invalid geometry object at index {0}'.format(index))

    if not valid_shapes:
        raise ValueError('No valid geometry objects found for rasterize')

    shape_values = np.array(shape_values)

    if not validate_dtype(shape_values, valid_dtypes):
        raise ValueError(format_invalid_dtype('shape values'))

    if dtype is None:
        dtype = get_minimum_dtype(np.append(shape_values, fill))

    elif not can_cast_dtype(shape_values, dtype):
        raise ValueError(format_cast_error('shape values', dtype))

    if output is not None:
        warnings.warn("The 'output' keyword arg has been superceded by 'out' "
                      "and will be removed before Rasterio 1.0.",
                      FutureWarning,
                      stacklevel=2)  # pragma: no cover

    out = out if out is not None else output
    if out is not None:
        if np.dtype(out.dtype).name not in valid_dtypes:
            raise ValueError(format_invalid_dtype('out'))

        if not can_cast_dtype(shape_values, out.dtype):
            raise ValueError(format_cast_error('shape values', out.dtype.name))

    elif out_shape is not None:
        out = np.empty(out_shape, dtype=dtype)
        out.fill(fill)

    else:
        raise ValueError('Either an output shape or image must be provided')

    transform = guard_transform(transform)

    with rasterio.drivers():
        _rasterize(valid_shapes, out, transform.to_gdal(), all_touched)

    return out
コード例 #31
0
def shapes(ctx, input, precision, indent, compact, projection, sequence,
           use_rs, geojson_type, bands, bidx, sampling, with_nodata):
    """Writes features of a dataset out as GeoJSON. It's intended for
    use with single-band rasters and reads from the first band.
    """
    # These import numpy, which we don't want to do unless it's needed.
    import numpy
    import rasterio.features
    import rasterio.warp

    verbosity = ctx.obj['verbosity'] if ctx.obj else 1
    logger = logging.getLogger('rio')
    dump_kwds = {'sort_keys': True}
    if indent:
        dump_kwds['indent'] = indent
    if compact:
        dump_kwds['separators'] = (',', ':')
    stdout = click.get_text_stream('stdout')

    # This is the generator for (feature, bbox) pairs.
    class Collection(object):
        def __init__(self):
            self._xs = []
            self._ys = []

        @property
        def bbox(self):
            return min(self._xs), min(self._ys), max(self._xs), max(self._ys)

        def __call__(self):
            with rasterio.open(input) as src:
                img = None
                nodata_mask = None
                if bands:
                    if sampling == 1:
                        img = src.read_band(bidx)
                        transform = src.transform
                    # Decimate the band.
                    else:
                        img = numpy.zeros(
                            (src.height // sampling, src.width // sampling),
                            dtype=src.dtypes[src.indexes.index(bidx)])
                        img = src.read_band(bidx, img)
                        transform = src.affine * Affine.scale(float(sampling))
                if not bands or not with_nodata:
                    if sampling == 1:
                        nodata_mask = src.read_mask()
                        transform = src.transform
                    # Decimate the mask.
                    else:
                        nodata_mask = numpy.zeros(
                            (src.height // sampling, src.width // sampling),
                            dtype=numpy.uint8)
                        nodata_mask = src.read_mask(nodata_mask)
                        transform = src.affine * Affine.scale(float(sampling))

                bounds = src.bounds
                xs = [bounds[0], bounds[2]]
                ys = [bounds[1], bounds[3]]
                if projection == 'geographic':
                    xs, ys = rasterio.warp.transform(src.crs,
                                                     {'init': 'epsg:4326'}, xs,
                                                     ys)
                if precision >= 0:
                    xs = [round(v, precision) for v in xs]
                    ys = [round(v, precision) for v in ys]
                self._xs = xs
                self._ys = ys

                kwargs = {'transform': transform}
                # Default is to exclude nodata features.
                if nodata_mask is not None:
                    kwargs['mask'] = (nodata_mask == 255)
                if img is None:
                    img = nodata_mask
                for g, i in rasterio.features.shapes(img, **kwargs):
                    if projection == 'geographic':
                        g = rasterio.warp.transform_geom(
                            src.crs,
                            'EPSG:4326',
                            g,
                            antimeridian_cutting=True,
                            precision=precision)
                    xs, ys = zip(*coords(g))
                    yield {
                        'type': 'Feature',
                        'id': str(i),
                        'properties': {
                            'val': i
                        },
                        'bbox': [min(xs), min(ys),
                                 max(xs), max(ys)],
                        'geometry': g
                    }

    if not sequence:
        geojson_type = 'collection'

    try:
        with rasterio.drivers(CPL_DEBUG=verbosity > 2):
            write_features(stdout,
                           Collection(),
                           sequence=sequence,
                           geojson_type=geojson_type,
                           use_rs=use_rs,
                           **dump_kwds)
        sys.exit(0)
    except Exception:
        logger.exception("Failed. Exception caught")
        sys.exit(1)
コード例 #32
-1
def test_drivers():
    with rasterio.drivers() as m:
        assert driver_count() > 0
        assert type(m) == GDALEnv
        
        n = rasterio.drivers()
        assert driver_count() > 0
        assert type(n) == GDALEnv