Exemplo n.º 1
0
 def test_pixelscale_metres(self):
     scale = 0.00025
     shape = (4000, 4000)
     origin = (150.0, -34.0)
     ggb = GriddedGeoBox(shape, origin, pixelsize=(scale, scale))
     (size_x, size_y) = ggb.get_pixelsize_metres(xy=(0, 0))
     self.assertAlmostEqual(size_x, 23.0962, places=4)
     self.assertAlmostEqual(size_y, 27.7306, places=4)
Exemplo n.º 2
0
 def test_create(self):
     scale = 0.00025
     shape = (3, 2)
     origin = (150.0, -34.0)
     corner = (shape[1] * scale + origin[0], origin[1] - shape[0] * scale)
     ggb = GriddedGeoBox(shape, origin)
     assert ggb is not None
     self.assertEqual(shape, ggb.shape)
     self.assertEqual(origin, ggb.origin)
     self.assertEqual(corner, ggb.corner)
Exemplo n.º 3
0
    def test_all_pixelscale_metres(self):
        scale = 0.00025
        shape = (4000, 4000)
        origin = (150.0, -34.0)
        ggb = GriddedGeoBox(shape, origin, pixelsize=(scale, scale))
        size_array = ggb.get_all_pixelsize_metres()

        self.assertEqual(len(size_array), 4000)
        (size_x, size_y) = size_array[0]
        self.assertAlmostEqual(size_x, 23.0962, places=4)
        self.assertAlmostEqual(size_y, 27.7306, places=4)
        (size_x, size_y) = size_array[3999]
        self.assertAlmostEqual(size_x, 22.8221, places=4)
        self.assertAlmostEqual(size_y, 27.7351, places=4)
Exemplo n.º 4
0
    def no_test_ggb_subsets(self):
        # get Land/Sea data file for this bounding box
        utmZone = 56
        utmDataPath = '/g/data/v10/eoancillarydata/Land_Sea_Rasters/WORLDzone%d.tif' % (utmZone,)

        # read the data for the Flinders islet region
        with rio.open(utmDataPath) as ds:

            # get the gridded box for the full data extent
            datasetGGB = GriddedGeoBox.from_dataset(ds)

            flindersGGB = getFlindersIsletGGB()
            scale = 0.00025
            shapeYX = (3, 3)
            origin = (150.0, -34.0)
            corner = (shapeYX[1] * scale + origin[0], origin[1] - shapeYX[0] * scale)
            ggb = GriddedGeoBox(shapeYX, origin)
            assert ggb is not None
            self.assertEqual(shapeYX, ggb.shape)
            self.assertEqual(origin, ggb.origin)
            self.assertEqual(corner, ggb.corner)
Exemplo n.º 5
0
    def test_agdc_copy(self):
        scale = 0.00025
        shape = (4000, 4000)
        origin = (150.0, -34.0)

        corner = (shape[1] * scale + 150, -34 - shape[0] * scale)
        ggb = GriddedGeoBox(shape, origin, pixelsize=(scale, scale))
        # print "ggb=%s" % str(ggb)
        assert ggb is not None
        self.assertEqual(shape, ggb.shape)
        self.assertEqual(corner, ggb.corner)

        # now get UTM equilavent

        utm_ggb = ggb.copy(crs="EPSG:32756")
        # print "utm_ggb=%s" % str(utm_ggb)

        self.assertAlmostEqual(utm_ggb.origin[0], 222908.70452156663)
        self.assertAlmostEqual(utm_ggb.origin[1], 6233785.283900621)
        self.assertAlmostEqual(utm_ggb.corner[0], 317483.90638409054)
        self.assertAlmostEqual(utm_ggb.corner[1], 6125129.365269075)
        self.assertAlmostEqual(utm_ggb.pixelsize[0], 23.643800465630978)
        self.assertAlmostEqual(utm_ggb.pixelsize[1], 27.16397965788655)
Exemplo n.º 6
0
    def z_axis_stats(self, out_fname=None, raster_bands=None):
        """
        Compute statistics over the z-axis of the StackedDataset.
        An image containing 14 raster bands, each describing a
        statistical measure:

            * 1. Sum
            * 2. Mean
            * 3. Valid Observations
            * 4. Variance
            * 5. Standard Deviation
            * 6. Skewness
            * 7. Kurtosis
            * 8. Max
            * 9. Min
            * 10. Median (non-interpolated value)
            * 11. Median Index (zero based index)
            * 12. 1st Quantile (non-interpolated value)
            * 13. 3rd Quantile (non-interpolated value)
            * 14. Geometric Mean

        :param out_fname:
            A string containing the full file system path name of the
            image containing the statistical outputs.

        :param raster_bands:
            If raster_bands is None (Default) then statistics will be
            calculated across all bands. Otherwise raster_bands can be
            a list containing the raster bands of interest. This can be
            sequential or non-sequential.

        :return:
            An instance of StackedDataset referencing the stats file.
        """
        # Check if the image tiling has been initialised
        if self.n_tiles == 0:
            self.init_tiling()

        # Get the band names for the stats file
        band_names = [
            'Sum', 'Mean', 'Valid Observations', 'Variance',
            'Standard Deviation', 'Skewness', 'Kurtosis', 'Max', 'Min',
            'Median (non-interpolated value)',
            'Median Index (zero based index)',
            '1st Quantile (non-interpolated value)',
            '3rd Quantile (non-interpolated value)', 'Geometric Mean'
        ]

        # out number of bands
        out_nb = 14

        # Construct the output image file to contain the result
        if out_fname is None:
            out_fname = pjoin(self.fname, '_z_axis_stats')

        geobox = GriddedGeoBox(shape=(self.lines, self.samples),
                               origin=(self.geotransform[0],
                                       self.geotransform[3]),
                               pixelsize=(self.geotransform[1],
                                          self.geotransform[5]),
                               crs=self.projection)

        outds = TiledOutput(out_fname,
                            self.samples,
                            self.lines,
                            out_nb,
                            geobox,
                            no_data=numpy.nan,
                            dtype=gdal.GDT_Float32)

        # Write the band names
        for i in range(out_nb):
            outds.out_bands[i].SetDescription(band_names[i])

        # If we have None, set to read all bands
        if raster_bands is None:
            raster_bands = range(1, self.bands + 1)

        # Check that we have an iterable
        if not isinstance(raster_bands, collections.Sequence):
            msg = 'raster_bands is not a list but a {}'
            msg = msg.format(type(raster_bands))
            raise TypeError(msg)

        # Loop over every tile
        for tile_n in range(self.n_tiles):
            tile = self.get_tile(tile_n)
            subset = self.read_tile(tile, raster_bands)
            stats = bulk_stats(subset, no_data=self.no_data)
            outds.write_tile(stats, tile)

        outds.close()

        return StackedDataset(out_fname)