Example #1
0
def test_window_fromslices(col_off, row_off, col_stop, row_stop):
    """Empty and non-empty absolute windows from slices, tuples, or lists
    are valid"""

    # Constrain windows to >= 0 in each dimension
    assume(col_stop >= col_off)
    assume(row_stop >= row_off)

    rows = (row_off, row_stop)
    cols = (col_off, col_stop)
    expected = (col_off, row_off, col_stop - col_off, row_stop - row_off)

    assert np.allclose(
        Window.from_slices(rows=slice(*rows), cols=slice(*cols)).flatten(),
        expected
    )

    assert np.allclose(
        Window.from_slices(rows=rows, cols=cols).flatten(),
        expected
    )

    assert np.allclose(
        Window.from_slices(rows=list(rows), cols=list(cols)).flatten(),
        expected
    )
Example #2
0
def test_window_fromslices_negative_start_missing_dim_err():
    """Should raise error if width or height are not provided"""

    with pytest.raises(WindowError):
        Window.from_slices(rows=(-10, 4), cols=(0, 4))

    with pytest.raises(WindowError):
        Window.from_slices(rows=(0, 4), cols=(-10, 4))
Example #3
0
def test_window_class_toslices():
    """Test Window.toslices"""
    window = Window(row_off=0, col_off=1, num_rows=100, num_cols=200)
    yslice, xslice = window.toslices()
    assert yslice.start == 0
    assert yslice.stop == 100
    assert xslice.start == 1
    assert xslice.stop == 201
Example #4
0
def test_window_fromslices_implicit_err():
    """ height and width are required if stop index is None; failing to
    provide them will result in error"""

    with pytest.raises(WindowError):
        Window.from_slices(rows=(1, None), cols=(1, 4))

    with pytest.raises(WindowError):
        Window.from_slices(rows=(1, 4), cols=(1, None))
Example #5
0
def test_window_fromslices_negative_stop():
    # TODO: Should negative stops even allowed??  Limited to boundless case?
    assert np.allclose(
        Window.from_slices(rows=(-4, -1), cols=(0, 4), height=10).flatten(),
        (0, 6, 4, 3)
    )

    assert np.allclose(
        Window.from_slices(rows=(0, 4), cols=(-4, -1), width=10).flatten(),
        (6, 0, 3, 4)
    )
Example #6
0
def test_window_fromslices_stops_lt_starts():
    """Should produce empty windows if stop indexes are less than start
    indexes"""

    assert np.allclose(
        Window.from_slices(rows=(4, 2), cols=(0, 4)).flatten(),
        (0, 4, 4, 0)
    )

    assert np.allclose(
        Window.from_slices(rows=(0, 4), cols=(4, 2)).flatten(),
        (4, 0, 0, 4)
    )
Example #7
0
def test_window_fromslices_invalid_rows_cols():
    """Should raise error if rows or cols  are not slices, lists, or tuples
    of length 2"""

    invalids = (
        np.array([0, 4]),  # wrong type, but close
        '04',  # clearly the wrong type but right length
        (1, 2, 3)  # wrong length
    )

    for invalid in invalids:
        with pytest.raises(WindowError):
            Window.from_slices(rows=invalid, cols=(0, 4))

        with pytest.raises(WindowError):
            Window.from_slices(rows=(0, 4), cols=invalid)
Example #8
0
def test_data_window_maskedarray():
    """Get window of masked arr."""
    arr = np.ones((3, 3))
    arr[0, :] = 0
    arr = np.ma.masked_array(arr, arr == 0)
    window = get_data_window(arr)
    assert window == Window.from_slices((1, 3), (0, 3))
Example #9
0
def test_window_fromslices_negative_start():
    # TODO: if passing negative start, what are valid values for stop?
    assert np.allclose(
        Window.from_slices(rows=(-4, None), cols=(0, 4), height=10).flatten(),
        (0, 6, 4, 4)
    )

    assert np.allclose(
        Window.from_slices(rows=(0, 4), cols=(-4, None), width=10).flatten(),
        (6, 0, 4, 4)
    )

    assert np.allclose(
        Window.from_slices(rows=(-6, None), cols=(-4, None),
                           height=8, width=10).flatten(),
        (6, 2, 4, 6)
    )
Example #10
0
def test_window_fromslices_implicit(abs_off, imp_off, stop, dim):
    """ providing None for start index will default to 0
    and providing None for stop index will default to width or height """

    assume(stop >= abs_off)
    assume(dim >= imp_off)

    absolute = (abs_off, stop)
    implicit_start = (None, stop)  # => (0, stop)
    implicit_stop = (imp_off, None)  # => (implicit_offset, dim)
    implicit_both = (None, None)  # => (implicit_offset, dim)

    # Implicit start indexes resolve to 0
    assert np.allclose(
        Window.from_slices(rows=implicit_start, cols=absolute).flatten(),
        (abs_off, 0, stop - abs_off, stop)
    )

    assert np.allclose(
        Window.from_slices(rows=absolute, cols=implicit_start).flatten(),
        (0, abs_off, stop, stop - abs_off)
    )

    # Implicit stop indexes resolve to dim (height or width)
    assert np.allclose(
        Window.from_slices(
            rows=implicit_stop, cols=absolute, height=dim).flatten(),
        (abs_off, imp_off, stop - abs_off, dim - imp_off)
    )

    assert np.allclose(
        Window.from_slices(
            rows=absolute, cols=implicit_stop, width=dim).flatten(),
        (imp_off, abs_off, dim - imp_off, stop - abs_off)
    )

    # Both can be implicit
    assert np.allclose(
        Window.from_slices(
            rows=implicit_both, cols=implicit_both,
            width=dim, height=dim).flatten(),
        (0, 0, dim, dim)
    )
Example #11
0
def test_window_float(path_rgb_byte_tif):
    """Test window float values"""
    with rasterio.open(path_rgb_byte_tif) as src:
        left, bottom, right, top = src.bounds
        dx, dy = src.res
        height = src.height
        width = src.width

        assert_window_almost_equals(from_bounds(
            left, top - 400, left + 400, top, src.transform,
            height, width), Window.from_slices((0, 400 / src.res[1]), (0, 400 / src.res[0])))
Example #12
0
def clip(indir, chip_csv, outdir,
         image_pattern, chip_pattern, shape, driver):
    """ Output image chips listed in a CSV file

    \b
    CSV file expects the following columns:
        * idx (int): index of the chip
        * name (str): name of chip land cover
        * x (float): upper left X coordinate of chip
        * y (float): upper left Y coordinate of chip

    """
    # Handle 1 or 2 inputs
    if not len(shape):
        shape = None
    else:
        shape = (shape[0], shape[0]) if len(shape) == 1 else shape

    indir, chip_csv, outdir = Path(indir), Path(chip_csv), Path(outdir)
    outdir.mkdir(parents=True, exist_ok=True)

    # Chip info
    chips = pd.read_csv(chip_csv)

    # Input images
    images = list(indir.glob(image_pattern))

    for chip in chips.itertuples():
        _chip = dict(zip(chip._fields, chip))
        _chip['Index'] += 1  # index on 1
        for image in images:
            # Format output filename
            _chip['input'] = image.name
            out_image = outdir.joinpath(chip_pattern.format(**_chip))
            # Make sure output directory exists
            out_image.parent.mkdir(parents=True, exist_ok=True)

            with rasterio.open(str(image)) as src:
                # Formulate chip bounds
                col, row = map(int, ~src.transform * (chip.x, chip.y))
                window = Window.from_offlen(col, row, shape[0], shape[1])

                # Form output kwargs
                out_kwargs = src.meta.copy()
                out_kwargs['driver'] = driver
                out_kwargs['width'] = shape[0]
                out_kwargs['height'] = shape[1]
                out_kwargs['transform'] = src.window_transform(window)

                click.echo('Writing output for image: {}'
                           .format(out_image.name))
                with rasterio.open(str(out_image), 'w', **out_kwargs) as dst:
                    dst.write(src.read(window=window))
Example #13
0
def test_window_fromslices_boundless(col_off, row_off, col_stop, row_stop):

    # Constrain windows to >= 0 in each dimension
    assume(col_stop >= col_off)
    assume(row_stop >= row_off)

    assert np.allclose(
        Window.from_slices(
            rows=(-row_off, row_stop), cols=(col_off, col_stop),
            boundless=True).flatten(),
        (col_off, -row_off, col_stop - col_off, row_stop + row_off)
    )
Example #14
0
def test_window_from_bounds(path_rgb_byte_tif):
    # TODO: break this test up.
    with rasterio.open(path_rgb_byte_tif) as src:
        left, bottom, right, top = src.bounds
        dx, dy = src.res
        height = src.height
        width = src.width

        assert_window_almost_equals(from_bounds(
            left + EPS, bottom + EPS, right - EPS, top - EPS, src.transform,
            height, width), Window.from_slices((0, height), (0, width)))

        assert_window_almost_equals(from_bounds(
            left, top - 2 * dy - EPS, left + 2 * dx - EPS, top, src.transform,
            height, width), Window.from_slices((0, 2), (0, 2)))

        # boundless
        assert_window_almost_equals(
            from_bounds(left - 2 * dx, top - 2 * dy, left + 2 * dx,
                        top + 2 * dy, src.transform, height=height,
                        width=width),
            Window.from_slices((-2, 2), (-2, 2), boundless=True, height=height,
                               width=width))
Example #15
0
def to_tif(mask_poly,
           tif_file,
           layer='ahn3_05m_dtm',
           cell_size=0.5,
           src_nodata=None,
           overviews=[5, 25]):
    '''
    Download an ahn-layer clipped by a shapely-polygon

    Parameters
    ----------    
    mask_poly: shapely polygon geometry
        Polygon used as clipping mask
    tif_file: str, file object or pathlib.Path object
        Location of the tif-file to be stored
    layer: str, optional
        NGR WCS layer to be downloaded. By default 'ahn3_05m_dtm'
    cell_size: int,float
        Cell_size in which the layer will be downloaded and stored
    src_nodata: int, float, optional
        Over-write the nodata value returned by the WCS. Usefull @ ahn2, as 
        nodata is not provided in gtiff profile
    overviews: list, optional
        Specify a list of raster-overviews in m. E.g.: overviews=[5,25] and 
        cell_size=0.5 will create 2 overviews with a cell size of 5m and 25m. 
        With the same overviews and cell_size=5 only an overview of 25m will 
        be included
    '''

    url = 'https://geodata.nationaalgeoregister.nl/{}/wcs?'.format(
        layer[:layer.find('_')])
    wcs = WebCoverageService(url, version='1.0.0', timeout=timeout)

    # see if the mask_poly is within the boundary of the wcs layer
    bboxes = wcs.contents[layer].boundingboxes
    xmin, ymin, xmax, ymax = [
        item['bbox'] for item in bboxes if item['nativeSrs'] == 'EPSG:28992'
    ][0]
    wcs_poly = Polygon([(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin),
                        (xmin, ymin)])
    if not wcs_poly.intersects(mask_poly):
        print('Bounds of poly-mask do not intersect with bounds of wcs-layer')

    else:
        bounds = list(mask_poly.bounds)
        # xmin, ymin rounddown 2 cellsize
        bounds[0], bounds[2] = [
            round(bounds[idx] / cell_size - cell_size) * cell_size
            for idx in [0, 2]
        ]
        # xmax, ymax rounddown 2 cellsize
        bounds[1], bounds[3] = [
            round(bounds[idx] / cell_size + cell_size) * cell_size
            for idx in [1, 3]
        ]

        profile = {
            'driver':
            'GTiff',
            'dtype':
            dtype,
            'nodata':
            -32768,
            'width':
            int((bounds[2] - bounds[0]) / cell_size),
            'height':
            int((bounds[3] - bounds[1]) / cell_size),
            'count':
            1,
            'crs':
            'epsg:28992',
            'BIGTIFF':
            "IF_SAFER",
            'transform':
            Affine(cell_size, 0.0, bounds[0], 0.0, -cell_size, bounds[3]),
            'tiled':
            True,
            'interleave':
            'band',
            'compress':
            'deflate',
            'predictor':
            2,
            'blockxsize':
            256,
            'blockysize':
            256
        }

        cols = int(np.ceil((bounds[2] - bounds[0]) / cell_size / max_size))
        rows = int(np.ceil((bounds[3] - bounds[1]) / cell_size / max_size))

        window_width = int((bounds[2] - bounds[0]) / cols / cell_size)
        window_height = int((bounds[3] - bounds[1]) / rows / cell_size)

        with rasterio.open(tif_file, 'w', **profile) as dst:
            dst.scales = [0.01]
            for row in range(rows):
                for col in range(cols):
                    xmin = bounds[0] + (col * window_width * cell_size)
                    ymax = bounds[3] - (row * window_height * cell_size)
                    xmax = xmin + (window_width * cell_size)
                    ymin = ymax - (window_height * cell_size)

                    bound_poly = Polygon([(xmin, ymin), (xmin, ymax),
                                          (xmax, ymax), (xmax, ymin),
                                          (xmin, ymin)])
                    if bound_poly.intersects(mask_poly):
                        print('NGR download: (row: {}/{}, col: {}/{})'.format(
                            row + 1, rows, col + 1, cols))

                        attempt = 1
                        succeed = False

                        while attempt <= attempts and not succeed:
                            try:
                                requestbbox = (xmin, ymin, xmax, ymax)
                                requestwidth = window_width
                                requestheight = window_height

                                gc = wcs.getCoverage(identifier=layer,
                                                     bbox=requestbbox,
                                                     format='GEOTIFF_FLOAT32',
                                                     width=requestwidth,
                                                     height=requestheight,
                                                     crs='EPSG:28992')
                                with MemoryFile(gc) as memfile:
                                    with memfile.open() as src:
                                        data = src.read(1)
                                        if src_nodata == None:
                                            src_nodata = src.profile['nodata']
                                        data = np.where(
                                            data == src_nodata, nodata,
                                            (data * 100).astype(
                                                rasterio.int16))
                                        if not bound_poly.within(mask_poly):
                                            geometry = bound_poly.intersection(
                                                mask_poly)
                                            mask = rasterio.features.rasterize(
                                                [(geometry, 1)],
                                                out_shape=data.shape,
                                                transform=src.
                                                profile['transform'],
                                                fill=0,
                                                all_touched=True,
                                                dtype=dtype)
                                            data = np.where(
                                                mask == 1, data, nodata)
                                succeed = True
                            except Exception as e:
                                print(
                                    'FAILED ATTEMPT ({}/{}): {} RETRYING 5 SECS'
                                    .format(attempt, attempts, e))
                                attempt += 1
                                time.sleep(5)
                                pass

                        dst.write(data.astype(rasterio.int16),
                                  window=Window(col * window_width,
                                                row * window_height,
                                                window_width, window_height),
                                  indexes=1)

            if not overviews == None:
                print('creating overviews')
                factors = [
                    int(size / cell_size) for size in overviews
                    if size > cell_size
                ]
                dst.build_overviews(factors, Resampling.average)
                dst.update_tags(ns='rio_overview', resampling='average')
Example #16
0
def test_window_toranges(col_off, row_off, width, height):
    """window.toranges() should match inputs"""

    assert np.allclose(
        Window(col_off, row_off, width, height).toranges(),
        ((row_off, row_off + height), (col_off, col_off + width)))
Example #17
0
 def test_partial_intersecting_window_is_not_contained(self):
     raster = GeoRaster2(image=np.full((1, 20, 30), 12),
                         affine=Affine.identity(),
                         crs=WEB_MERCATOR_CRS)
     window = Window(-5, 5, 10, 25)
     self.assertFalse(raster._window_contained_in_raster(window))
Example #18
0
 def test_larger_window_is_not_contained(self):
     raster = GeoRaster2(image=np.full((1, 20, 30), 12),
                         affine=Affine.identity(),
                         crs=WEB_MERCATOR_CRS)
     window = Window(-1, -5, 35, 35)
     self.assertFalse(raster._window_contained_in_raster(window))
Example #19
0
def test_window_from_offlen():
    """from_offlen classmethod works."""
    with pytest.warns(RasterioDeprecationWarning):
        assert Window.from_offlen(2, 0, 1, 1) == Window.from_slices((0, 1), (2, 3))
Example #20
0
 def test_size_pass(self):
     """Test size property"""
     centr_ras = Centroids()
     centr_ras.set_raster_file(HAZ_DEMO_FL, window=Window(0, 0, 50, 60))
     self.assertEqual(centr_ras.size, 50 * 60)
Example #21
0
def test_data_window_novalid():
    """Get window of arr with nodata."""
    arr = np.ones((3, 3))
    arr[:, :] = 0
    window = get_data_window(arr, nodata=0)
    assert window == Window.from_slices((0, 0), (0, 0))
Example #22
0
def test_data_window_full():
    """Get window of entirely valid data array."""
    arr = np.ones((3, 3))
    window = get_data_window(arr)
    assert window == Window.from_slices((0, 3), (0, 3))
Example #23
0
def test_read_with_window_class(path_rgb_byte_tif):
    """Reading subset with Window class works"""
    with rasterio.open(path_rgb_byte_tif) as src:
        subset = src.read(1, window=Window(0, 0, 10, 10))
        assert subset.shape == (10, 10)
Example #24
0
def test_window_from_slices():
    """from_slices classmethod works."""
    assert Window.from_slices((0, 1), (2, 3)) == Window.from_slices((0, 1),
                                                                    (2, 3))
Example #25
0
def test_window_class_nonintersects():
    """Windows do not intersect"""
    assert not intersect(Window(0, 0, 10, 10), Window(10, 10, 10, 10))
Example #26
0
def test_window_class_intersects_list():
    """A list of Windows intersect"""
    assert intersect([Window(0, 0, 10, 10), Window(8, 8, 10, 10)])
Example #27
0
def test_window_class_intersects():
    """Windows intersect"""
    assert intersect(Window(0, 0, 10, 10), Window(8, 8, 10, 10))
Example #28
0
def test_window_bounds_north_up():
    transform = Affine.translation(0.0, 10.0) * Affine.scale(
        1.0, -1.0) * Affine.identity()
    assert_window_almost_equals(from_bounds(0, 0, 10, 10, transform, 10, 10),
                                Window(0, 0, 10, 10))
Example #29
0
def test_window_from_slices():
    """from_slices classmethod works."""
    assert Window.from_slices((0, 1), (2, 3)) == Window.from_slices((0, 1), (2, 3))
Example #30
0
def test_data_window_novalid():
    """Get window of arr with nodata."""
    arr = np.ones((3, 3))
    arr[:, :] = 0
    window = get_data_window(arr, nodata=0)
    assert window == Window.from_slices((0, 0), (0, 0))
Example #31
0
def test_window_repr():
    assert str(Window(0, 1, 4, 2)) == ('Window(col_off=0, row_off=1, width=4, '
                                       'height=2)')
Example #32
0
def test_data_window_nodata_3d():
    """Get window of 3d arr with nodata."""
    arr = np.ones((3, 3, 3))
    arr[:, 0, :] = 0
    window = get_data_window(arr, nodata=0)
    assert window == Window.from_slices((1, 3), (0, 3))
Example #33
0
def test_window_from_offlen():
    """from_offlen classmethod works."""
    assert Window.from_offlen(2, 0, 1, 1) == ((0, 1), (2, 3))
Example #34
0
def test_window_union():
    """Window union works."""
    window = union(Window(0, 0, 1, 1), Window(1, 1, 2, 2))
    assert window == Window.from_slices((0, 3), (0, 3))
Example #35
0
    # Define the size of each image chip (NxN)
    image_size = 64

    # Get a list of all of the GEOTIFFS
    tiff_paths = find_image_tiffs(tiff_dir, '\\*.tif')

    # Loop through the GeoTiffs
    print('Trimming rasters...')
    for cur_path in tiff_paths:

        with rasterio.open(cur_path) as src:

            # Define the window
            window = Window(col_off=0,
                            row_off=0,
                            width=image_size,
                            height=image_size)

            # Get the metadata
            kwargs = src.meta.copy()
            kwargs.update({
                'height':
                window.height,
                'width':
                window.width,
                'transform':
                rasterio.windows.transform(window, src.transform)
            })

            with rasterio.open(cur_path, 'w', **kwargs) as dst:
                dst.write(src.read(window=window))
Example #36
0
def test_no_intersection():
    """Non intersecting windows raises error."""
    with pytest.raises(WindowError):
        intersection(Window(0, 0, 1, 1), Window(1, 1, 2, 2))
Example #37
0
 def test_entire_raster_window_is_not_contained(self):
     raster = GeoRaster2(image=np.full((1, 20, 30), 12),
                         affine=Affine.identity(),
                         crs=WEB_MERCATOR_CRS)
     window = Window(0, 0, 30, 20)
     self.assertTrue(raster._window_contained_in_raster(window))
Example #38
0
def test_data_window_nodata_3d():
    """Get window of 3d arr with nodata."""
    arr = np.ones((3, 3, 3))
    arr[:, 0, :] = 0
    window = get_data_window(arr, nodata=0)
    assert window == Window.from_slices((1, 3), (0, 3))
Example #39
0
    def _read_fluid_selection(self, chunks, selector, fields, size):
        rv = {}
        chunks = list(chunks)

        if isinstance(selector, GridSelector):
            if not (len(chunks) == len(chunks[0].objs) == 1):
                raise RuntimeError
            g = chunks[0].objs[0]

            if g.id in self._cached_fields:
                gf = self._cached_fields[g.id]
                rv.update(gf)

            if len(rv) == len(fields): return rv
            src = rasterio.open(g.filename, "r")
            for field in fields:
                if field in rv:
                    self._hits += 1
                    continue
                self._misses += 1
                ftype, fname = field

                # Read in the band/field
                rv[(ftype,
                    fname)] = src.read(int(fname)).astype(self._field_dtype)

            if self._cache_on:
                self._cached_fields.setdefault(g.id, {})
                self._cached_fields[g.id].update(rv)
            return rv

        if size is None:
            size = sum(
                (g.count(selector) for chunk in chunks for g in chunk.objs))
        for field in fields:
            ftype, fname = field
            fsize = size
            rv[field] = np.empty(fsize, dtype="float64")
        ind = 0

        for chunk in chunks:
            src = None
            for g in chunk.objs:
                if g.filename is None: continue
                if src is None:
                    src = rasterio.open(g.filename, "r")
                gf = self._cached_fields.get(g.id, {})
                nd = 0

                # Determine the window dimensions of a given selector
                left_edge, width = g._get_rasterio_window(selector)

                # Build Rasterio window-read format
                rasterio_wr_dim = Window(left_edge[0], left_edge[1], width[0],
                                         width[1])

                for field in fields:
                    # only for cached gridded objects
                    if field in gf:

                        # Add third dimension to numpy array
                        for dim in range(len(gf[field].shape), 3):
                            gf[field] = np.expand_dims(gf[field], dim)

                        nd = g.select(selector, gf[field], rv[field], ind)

                        self._hits += 1
                        continue

                    self._misses += 1

                    ftype, fname = field

                    # Perform Rasterio window read
                    data = src.read(int(fname), window=rasterio_wr_dim).astype(
                        self._field_dtype)

                    for dim in range(len(data.shape), 3):
                        data = np.expand_dims(data, dim)

                    if self._cache_on:
                        self._cached_fields.setdefault(g.id, {})
                        self._cached_fields[g.id][field] = data

                    nd = g.select(selector, data, rv[field], ind)

                ind += nd

        return rv
Example #40
0
def test_intersection():
    """Window intersection works."""
    window = intersection(Window(0, 0, 10, 10), Window(8, 8, 12, 12))
    assert window == Window.from_slices((8, 10), (8, 10))
    def _get_bbox(self, ds, j, i,
                  all_boxes):
        """
        处理每个tile输入模型的返回结果
        """
        transf = ds.transform

        height = ds.height
        width = ds.width

        try:
            one_pixel = ds.res[0]
        except:
            one_pixel = 1
        block_xmin = j * self.tile_offset
        block_ymin = i * self.tile_offset
        if (j == -1) & (i == -1):
            block_xmin = 0
            block_ymin = 0
            block = np.zeros([3, ds.height, ds.width], dtype=np.uint8)
            img = ds.read(window=Window(block_xmin, block_ymin, ds.width, ds.height))

        else:
            block_xmax = block_xmin + self.blocksize
            block_ymax = block_ymin + self.blocksize
            if height <= block_ymax:
                block_ymin = height - self.blocksize
            if width <= block_xmax:
                block_xmin = width - self.blocksize
            block = np.zeros([3, self.blocksize, self.blocksize], dtype=np.uint8)
            img = ds.read(window=Window(block_xmin, block_ymin, self.blocksize, self.blocksize))

        block[:, :img.shape[1], :img.shape[2]] = img[:3, :, :]
        block = reshape_as_image(block)
        block = cv2.cvtColor(block, cv2.COLOR_RGB2BGR)

        blobs, im_scales = self._get_blobs(block)
        im_blob = blobs['data']
        blobs['im_info'] = np.array([im_blob.shape[1], im_blob.shape[2], im_scales[0]],
                                    dtype=np.float32)
        # 执行预测
        _, scores, bbox_pred, rois = self.sess.run([self.score, self.prob, self.pred, self.rois],
                                                   feed_dict={self.im_data: blobs['data'],
                                                              self.im_info: blobs[
                                                                  'im_info']})
        self.sess.graph.finalize()
        # 计算包围框
        boxes = rois[:, 1:5] / im_scales[0]
        scores = np.reshape(scores, [scores.shape[0], -1])
        bbox_pred = np.reshape(bbox_pred, [bbox_pred.shape[0], -1])

        if True:
            # Apply bounding-box regression deltas
            box_deltas = bbox_pred
            pred_boxes = self._bbox_transform_inv(boxes, box_deltas)
            pred_boxes = self._clip_boxes(pred_boxes, block.shape)
        else:
            # Simply repeat the boxes, once for each class
            pred_boxes = np.tile(boxes, (1, scores.shape[1]))
        # # 遍历预测出的所有类别
        for cls_ind, cls in enumerate(self.category_names[0:]):
            cls_ind += 1  # 过滤了背景框
            cls_boxes = pred_boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
            keep = self.nms(dets, one_pixel)
            dets = dets[keep, :]
            inds = np.where(dets[:, -1] >= self.score_thresh)[0]
            # 将准确率大于阈值且用户需要的类别结果提取保存出来
            if len(inds) > 0 and str(cls) in self.category_name:
                # 输入为影像文件时,输出GeoJSON文件
                for s in inds:
                    bbox = dets[s, :5]
                    if ds.crs is None:
                        xmin = round(float(bbox[0]), 4) + block_xmin
                        ymin = round(float(bbox[1]), 4) + block_ymin
                        xmax = round(float(bbox[2]), 4) + block_xmin
                        ymax = round(float(bbox[3]), 4) + block_ymin
                        score_single_bbox = round(float(bbox[4]), 4)
                    else:
                        coord_min = transform.xy(transf, bbox[1] + float(block_ymin),
                                                 bbox[0] + float(block_xmin))
                        coord_max = transform.xy(transf, bbox[3] + float(block_ymin),
                                                 bbox[2] + float(block_xmin))

                        xmin = coord_min[0]
                        ymin = coord_max[1]
                        xmax = coord_max[0]
                        ymax = coord_min[1]
                        score_single_bbox = bbox[4]
                    all_boxes.append(
                        [xmin, ymin, xmax, ymax, score_single_bbox, str(cls)])

        return all_boxes
Example #42
0
def geometry_window(dataset, shapes, pad_x=0, pad_y=0, north_up=True,
                    rotated=False, pixel_precision=3):
    """Calculate the window within the raster that fits the bounds of the
    geometry plus optional padding.  The window is the outermost pixel indices
    that contain the geometry (floor of offsets, ceiling of width and height).

    If shapes do not overlap raster, a WindowError is raised.

    Parameters
    ----------
    dataset: dataset object opened in 'r' mode
        Raster for which the mask will be created.
    shapes: iterable over geometries.
        A geometry is a GeoJSON-like object or implements the geo interface.
        Must be in same coordinate system as dataset.
    pad_x: float
        Amount of padding (as fraction of raster's x pixel size) to add to left
        and right side of bounds.
    pad_y: float
        Amount of padding (as fraction of raster's y pixel size) to add to top
        and bottom of bounds.
    north_up: bool
        If True (default), the origin point of the raster's transform is the
        northernmost point and y pixel values are negative.
    rotated: bool
        If true, some rotation terms exist in the dataset transform (this
        requires special attention.)
    pixel_precision: int
        Number of places of rounding precision for evaluating bounds of shapes.

    Returns
    -------
    window: rasterio.windows.Window instance
    """

    if pad_x:
        pad_x = abs(pad_x * dataset.res[0])

    if pad_y:
        pad_y = abs(pad_y * dataset.res[1])

    if not rotated:
        all_bounds = [bounds(shape, north_up=north_up) for shape in shapes]
        lefts, bottoms, rights, tops = zip(*all_bounds)

        left = min(lefts) - pad_x
        right = max(rights) + pad_x

        if north_up:
            bottom = min(bottoms) - pad_y
            top = max(tops) + pad_y
        else:
            bottom = max(bottoms) + pad_y
            top = min(tops) - pad_y
    else:
        # get the bounds in the pixel domain by specifying a transform to the bounds function
        all_bounds_px = [bounds(shape, transform=~dataset.transform) for shape in shapes]
        # get left, right, top, and bottom as above
        lefts, bottoms, rights, tops = zip(*all_bounds_px)
        left = min(lefts) - pad_x
        right = max(rights) + pad_x
        top = min(tops) - pad_y
        bottom = max(bottoms) + pad_y
        # do some clamping if there are any values less than zero or greater than dataset shape
        left = max(0, left)
        top = max(0, top)
        right = min(dataset.shape[1], right)
        bottom = min(dataset.shape[0], bottom)
        # convert the bounds back to the CRS domain
        left, top = (left, top) * dataset.transform
        right, bottom = (right, bottom) * dataset.transform

    window = dataset.window(left, bottom, right, top)
    window_floored = window.round_offsets(op='floor', pixel_precision=pixel_precision)
    w = math.ceil(window.width + window.col_off - window_floored.col_off)
    h = math.ceil(window.height + window.row_off - window_floored.row_off)
    window = Window(window_floored.col_off, window_floored.row_off, w, h)

    # Make sure that window overlaps raster
    raster_window = Window(0, 0, dataset.width, dataset.height)

    # This will raise a WindowError if windows do not overlap
    window = window.intersection(raster_window)

    return window
Example #43
0
def sliding_windows(size, width, height):
    """Slide a window of +size+ pixels"""
    for i in range(0, height, size):
        for j in range(0, width, size):
            yield Window(j, i, min(width - j, size), min(height - i, size))
Example #44
0
def test_window_hashable():
    a = Window(0, 0, 10, 10)
    b = Window(0, 0, 10, 10)
    c = Window(8, 8, 12, 12)
    assert hash(a) == hash(b)
    assert hash(a) != hash(c)
Example #45
0
def geometry_window(raster,
                    shapes,
                    pad_x=0,
                    pad_y=0,
                    north_up=True,
                    pixel_precision=3):
    """Calculate the window within the raster that fits the bounds of the 
    geometry plus optional padding.  The window is the outermost pixel indices
    that contain the geometry (floor of offsets, ceiling of width and height).
    
    If shapes do not overlap raster, a WindowError is raised.

    Parameters
    ----------
    raster: rasterio RasterReader object
        Raster for which the mask will be created.
    shapes: iterable over geometries.
        A geometry is a GeoJSON-like object or implements the geo interface.
        Must be in same coordinate system as raster.
    pad_x: float
        Amount of padding (as fraction of raster's x pixel size) to add to left 
        and right side of bounds.
    pad_y: float
        Amount of padding (as fraction of raster's y pixel size) to add to top 
        and bottom of bounds.
    north_up: bool
        If True (default), the origin point of the raster's transform is the 
        northernmost point and y pixel values are negative.
    pixel_precision: int
        Number of places of rounding precision for evaluating bounds of shapes.

    Returns
    -------
    window: rasterio.windows.Window instance
    """

    if pad_x:
        pad_x = abs(pad_x * raster.res[0])

    if pad_y:
        pad_y = abs(pad_y * raster.res[1])

    all_bounds = [bounds(shape, north_up=north_up) for shape in shapes]
    lefts, bottoms, rights, tops = zip(*all_bounds)

    left = min(lefts) - pad_x
    right = max(rights) + pad_x

    if north_up:
        bottom = min(bottoms) - pad_y
        top = max(tops) + pad_y
    else:
        bottom = max(bottoms) + pad_y
        top = min(tops) - pad_y

    window = raster.window(left, bottom, right, top)
    window = window.round_offsets(op='floor', pixel_precision=pixel_precision)
    window = window.round_shape(op='ceil', pixel_precision=pixel_precision)

    # Make sure that window overlaps raster
    raster_window = Window(0, 0, raster.height, raster.width)

    # This will raise a WindowError if windows do not overlap
    window = window.intersection(raster_window)

    return window
Example #46
0
def test_window_flatten(col_off, row_off, width, height):
    """Flattened window should match inputs"""

    assert np.allclose(
        Window(col_off, row_off, width, height).flatten(),
        (col_off, row_off, width, height))
Example #47
0
def test_data_window_full():
    """Get window of entirely valid data array."""
    arr = np.ones((3, 3))
    window = get_data_window(arr)
    assert window == Window.from_slices((0, 3), (0, 3))
Example #48
0
def test_window_class_todict():
    """Test Window.todict"""
    window = Window(row_off=0, col_off=1, num_rows=100, num_cols=200)
    assert window.todict() == {
        'col_off': 1, 'num_cols': 200, 'num_rows': 100, 'row_off': 0}
Example #49
0
def test_intersection():
    """Window intersection works."""
    window = intersection(Window(0, 0, 10, 10), Window(8, 8, 12, 12))
    assert window == Window.from_slices((8, 10), (8, 10))
Example #50
0
def test_window_bounds_south_up():
    identity = Affine.identity()
    assert_window_almost_equals(from_bounds(0, 10, 10, 0, identity, 10, 10),
                                Window(0, 0, 10, 10))
Example #51
0
def test_window_union():
    """Window union works."""
    window = union(Window(0, 0, 1, 1), Window(1, 1, 2, 2))
    assert window == Window.from_slices((0, 3), (0, 3))
Example #52
0
def test_round_window_to_full_blocks_error():
    with pytest.raises(WindowError):
        round_window_to_full_blocks(Window(0, 0, 10, 10),
                                    block_shapes=[(1, 1), (2, 2)])
Example #53
0
def test_round_window_already_at_edge(path_alpha_tif):
    with rasterio.open(path_alpha_tif) as src:
        block_shapes = src.block_shapes
        test_window = ((256, 512), (512, 768))
        rounded_window = round_window_to_full_blocks(test_window, block_shapes)
        assert rounded_window == Window.from_slices(*test_window)
Example #54
0
def test_round_window_already_at_edge(path_alpha_tif):
    with rasterio.open(path_alpha_tif) as src:
        block_shapes = src.block_shapes
        test_window = ((256, 512), (512, 768))
        rounded_window = round_window_to_full_blocks(test_window, block_shapes)
        assert rounded_window == Window.from_slices(*test_window)
Example #55
0
def process_tile(tile):
    """Process a single MBTiles tile

    Parameters
    ----------
    tile : mercantile.Tile

    Returns
    -------

    tile : mercantile.Tile
        The input tile.
    bytes : bytearray
        Image bytes corresponding to the tile.

    """
    global base_kwds, resampling, src

    # Get the bounds of the tile.
    ulx, uly = mercantile.xy(
        *mercantile.ul(tile.x, tile.y, tile.z))
    lrx, lry = mercantile.xy(
        *mercantile.ul(tile.x + 1, tile.y + 1, tile.z))

    kwds = base_kwds.copy()
    kwds['transform'] = transform_from_bounds(ulx, lry, lrx, uly,
                                              kwds['width'], kwds['height'])
    src_nodata = kwds.pop('src_nodata', None)
    dst_nodata = kwds.pop('dst_nodata', None)

    warnings.simplefilter('ignore')

    with MemoryFile() as memfile:

        with memfile.open(**kwds) as tmp:

            # determine window of source raster corresponding to the tile
            # image, with small buffer at edges
            try:
                west, south, east, north = transform_bounds(TILES_CRS, src.crs, ulx, lry, lrx, uly)
                tile_window = window_from_bounds(west, south, east, north, transform=src.transform)
                adjusted_tile_window = Window(
                    tile_window.col_off - 1, tile_window.row_off - 1,
                    tile_window.width + 2, tile_window.height + 2)
                tile_window = adjusted_tile_window.round_offsets().round_shape()

                # if no data in window, skip processing the tile
                if not src.read_masks(1, window=tile_window).any():
                    return tile, None

            except ValueError:
                log.info("Tile %r will not be skipped, even if empty. This is harmless.", tile)

            reproject(rasterio.band(src, tmp.indexes),
                      rasterio.band(tmp, tmp.indexes),
                      src_nodata=src_nodata,
                      dst_nodata=dst_nodata,
                      num_threads=1,
                      resampling=resampling)

        return tile, memfile.read()
Example #56
0
def test_round_offsets_no_op_error():
    with pytest.raises(WindowError):
        Window(0, 0, 1, 1).round_offsets(op='lolwut')