Example #1
0
    def random_grid(self, width, height, count, units='native'):
        """Create a dataframe that defines a random set of samples.

        The width and height are given in pixels and multiplied by the
        pixel size of the raster to create samples at the native
        resolution of the raster.

        Note: width and height unrelated to the DL model inputs.

        Args:
          width (int): sample size in pixels
          height (int): sample size in pixels
          units (str): units applied to sample sizes ('native' or 'pixels')
          count (int): number of samples

        Returns:
          (GeoDataframe)
        """

        if not self.src:
            raise RuntimeError('source not set or failed to open')

        if units == 'pixels':
            dims = width * self.src.res[0], height * self.src.res[1]
        elif units == 'native':
            dims = width, height
        else:
            raise ValueError('units must be "native" or "pixels"')

        gdf = grid.random_grid(*self.src.bounds, *dims, count)
        gdf.crs = self.src.crs
        return gdf
Example #2
0
    def random_grid(self, width, height, count, units='native'):
        """Create a dataframe that defines a random set of samples.

        When units='native', width and height are given in projection
        units (i.e. meters, feet, etc). The number of pixels within this
        area would depend on the pixel size. If units='pixel', width
        and height are multiplied by the pixel size to compute the
        sample boundaries.

        Note: width and height are unrelated to the DL model inputs.

        Args:
          width (int): sample size in pixels
          height (int): sample size in pixels
          units (str): units applied to sample sizes ('native' or 'pixels')
          count (int): number of samples

        Returns:
          (GeoDataframe)
        """

        if not self.src:
            raise RuntimeError('source not set or failed to open')

        if units == 'pixels':
            dims = width * self.src.res[0], height * self.src.res[1]
        elif units == 'native':
            dims = width, height
        else:
            raise ValueError('units must be "native" or "pixels"')

        gdf = grid.random_grid(*self.src.bounds, *dims, count)
        gdf.crs = self.src.crs
        return gdf
Example #3
0
    def random_grid(self, count, width=0, height=0):
        """Create a dataframe that defines a random set of samples.

        The width and height are given in pixels and multiplied by the
        pixel size of the raster to create samples at the native
        resolution of the raster.

        Args:
          width (int): sample size in pixels
          height (int): sample size in pixels
          count (int): number of samples

        Returns:
          (GeoDataframe)
        """

        width = width if width else self.width
        height = height if height else self.height
        if width < 1 or height < 1:
            raise ValueError('width and height must be specified')

        if not self.src:
            raise RuntimeError('source not set or failed to open')

        dims = width * self.src.res[0], height * self.src.res[1]
        gdf = grid.random_grid(*self.src.bounds, *dims, count)
        gdf.crs = self.src.crs
        return gdf
Example #4
0
def test_random_grid():
    bounds, _, _ = grid.raster_meta('data/small.tif')
    size = (bounds[2]-bounds[0], bounds[3]-bounds[1])
    size = [i/10 for i in size]

    df = grid.random_grid(*bounds, *size, count=100)
    assert len(df) == 100
    assert df.total_bounds[0] >= bounds[0]
    assert df.total_bounds[1] >= bounds[1]
    assert df.total_bounds[2] <= bounds[2]
    assert df.total_bounds[3] <= bounds[3]