Пример #1
0
    def test_geotransform(self):
        """
        Test geotransform and spatialref keywords.

        Notes
        -----
        Spatial reference cannot be compared due to different versions of WKT1 strings.
        """
        data = np.ones((1, 100, 100), dtype=np.float32)

        geotrans = (3000000.0, 500.0, 0.0, 1800000.0, 0.0, -500.0)
        sref = ('PROJCS["Azimuthal_Equidistant",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["Azimuthal_Equidistant"],'
                'PARAMETER["latitude_of_center",53],'
                'PARAMETER["longitude_of_center",24],'
                'PARAMETER["false_easting",5837287.81977],'
                'PARAMETER["false_northing",2121415.69617],'
                'UNIT["metre",1,AUTHORITY["EPSG","9001"]]]')

        with GeoTiffFile(self.filepath, mode='w', geotrans=geotrans,
                         sref=sref) as src:
            src.write(data)

        with GeoTiffFile(self.filepath) as src:
            ds_tags = src.read_tags(1)

        self.assertEqual(ds_tags['geotransform'], geotrans)
Пример #2
0
    def test_decoding_multi_band(self):
        """
        Test decoding of multi band data.

        """
        data = np.ones((5, 100, 100), dtype=np.float32)

        with GeoTiffFile(self.filepath, mode='w') as src:
            src.write(data,
                      scale_factor=[1, 2, 1, 1, 3],
                      add_offset=[0, 3, 0, 0, 0])

        with GeoTiffFile(self.filepath, auto_decode=False) as src:
            ds = src.read(band=1)
            np.testing.assert_array_equal(ds, data[0, :, :])
            ds = src.read(band=2)
            np.testing.assert_array_equal(ds, data[1, :, :])
            ds = src.read(band=5)
            np.testing.assert_array_equal(ds, data[4, :, :])

        data[1, :, :] = data[1, :, :] * 2 + 3
        data[4, :, :] = data[4, :, :] * 3

        with GeoTiffFile(self.filepath, auto_decode=True) as src:
            ds = src.read(band=1)
            np.testing.assert_array_equal(ds, data[0, :, :])
            ds = src.read(band=2)
            np.testing.assert_array_equal(ds, data[1, :, :])
            ds = src.read(band=5)
            np.testing.assert_array_equal(ds, data[4, :, :])
Пример #3
0
    def test_read_write_1_band(self):
        """
        Test a write and read of a single band data.
        """
        data = np.ones((1, 100, 100), dtype=np.float32)

        with GeoTiffFile(self.filepath, mode='w') as src:
            src.write(data)

        with GeoTiffFile(self.filepath) as src:
            ds = src.read()

        np.testing.assert_array_equal(ds, data[0, :, :])
Пример #4
0
    def test_read_write_multi_band(self):
        """
        Test a write and read of a multi band data.
        """
        data = np.ones((5, 100, 100), dtype=np.float32)

        with GeoTiffFile(self.filepath, mode='w') as src:
            src.write(data)

        with GeoTiffFile(self.filepath) as src:
            for band in np.arange(data.shape[0]):
                ds = src.read(band=band + 1)
                np.testing.assert_array_equal(ds, data[band, :, :])
Пример #5
0
    def test_read_write_specific_band(self):
        """
        Test a write and read of a specific data.
        """
        data = np.ones((100, 100), dtype=np.float32)

        with GeoTiffFile(self.filepath, mode='w', n_bands=10) as src:
            src.write(data, band=5)
            src.write(data, band=10)

        with GeoTiffFile(self.filepath) as src:
            ds = src.read(band=5)
            np.testing.assert_array_equal(ds, data)
            ds = src.read(band=10)
            np.testing.assert_array_equal(ds, data)
Пример #6
0
    def test_tags(self):
        """
        Test meta data tags.
        """
        data = np.ones((1, 100, 100), dtype=np.float32)

        metadata = {'attr1': '123', 'attr2': 'test'}
        tags = {'description': 'helloworld', 'metadata': metadata}

        with GeoTiffFile(self.filepath, mode='w', tags=tags) as src:
            src.write(data)

        with GeoTiffFile(self.filepath) as src:
            ds_tags = src.read_tags(1)

        self.assertEqual(ds_tags['metadata'], tags['metadata'])
        self.assertNotEqual(ds_tags['description'], tags['description'])
Пример #7
0
    def write_from_xr(self, ds, dir_path):
        """
        Write data set into raster time stack.

        Parameters
        ----------
        ds : xarray.Dataset
            Input data set.
        """
        time_var = ds['time'].to_index()

        if time_var.dtype == np.float64:
            time_var = pd.to_datetime(time_var,
                                      unit='D',
                                      origin=pd.Timestamp('1900-01-01'))

        timestamps = time_var.strftime('%Y%m%d%H%M%S')
        var_dict = {}

        for name in ds.variables:
            if name == 'time':
                continue
            ds_attrs = ds[name].attrs.keys()
            nodata = [ds[name].attrs["fill_value"]
                      ] if "fill_value" in ds_attrs else [-9999]
            scale_factor = [ds[name].attrs["scale_factor"]
                            ] if "scale_factor" in ds_attrs else [1.]
            add_offset = [ds[name].attrs["add_offset"]
                          ] if "add_offset" in ds_attrs else [0.]

            filepaths = []
            for i, timestamp in enumerate(timestamps):
                filename = '{:}_{:}'.format(timestamp, name)
                filepath = os.path.join(dir_path, '{:}.tif'.format(filename))
                filepaths.append(filepath)

                with GeoTiffFile(filepath, mode='w', n_bands=1) as src:
                    src.write(ds[name].isel(time=i).values,
                              band=1,
                              nodataval=nodata,
                              scale_factor=scale_factor,
                              add_offset=add_offset)

            bands = [1] * len(filepaths)
            var_dict[name] = pd.DataFrame(
                {
                    'filepath': filepaths,
                    'band': bands,
                    'nodataval': nodata,
                    'scale_factor': scale_factor,
                    'add_offset': add_offset
                },
                index=ds['time'].to_index())

        return var_dict
Пример #8
0
    def _open(self):

        if self.inventory is not None:
            ref_filepath = self.inventory[
                self.inventory.notnull()]['filepath'][0]
            with GeoTiffFile(ref_filepath, mode='r') as geotiff:
                self.sref = geotiff.sref
                self.geotrans = geotiff.geotrans
                self.metadata = geotiff.metadata
                self.dtype = geotiff.dtype
                self.shape = (len(self.inventory), geotiff.shape[-2],
                              geotiff.shape[-1])

            self.vrt = self._build_stack(self.inventory)
Пример #9
0
    def test_gdal_translate(self):
        with GeoTiffFile(self.filename, mode='w') as gt_file:
            gt_file.write(self.data)
        # configure options
        options = {
            '-of': 'GTiff',
            '-co': 'COMPRESS=LZW',
            '-mo': ['parent_data_file="%s"' % os.path.basename(self.filename)],
            '-outsize': ('50%', '50%'),
            '-ot': 'Byte'
        }
        succeed, output = call_gdal_util("gdal_translate",
                                         src_files=self.filename,
                                         dst_file=self.filename_gdal_trans,
                                         options=options)

        assert succeed
Пример #10
0
    def write(self,
              data,
              filepath,
              band=None,
              encoder=None,
              nodataval=None,
              encoder_kwargs=None,
              ct=None):
        """
        Write data into raster file.

        Parameters
        ----------
        data : numpy.ndarray (3d)
            Raster data set, either as single image (2d) or as stack (3d).
            Dimensions [band, x, y]
        band : int, optional
            Band number (starting with 1). If band number is provided,
            raster data set has to be 2d.
            Default: Raster data set is 3d and all bands a written at
            the same time.
        encoder : function
            Encoding function expecting a NumPy array as input.
        encoder_kwargs : dict, optional
            Keyword arguments for the encoder.
        nodataval : tuple or list, optional
            List of no data values for each band.
            Default: -9999 for each band.
        ct : tuple or list, optional
            List of color tables for each band.
            Default: No color tables set.
        """
        if data.ndim != 3:
            err_msg = "Array needs to have 3 dimensions [band, width, height]"
            raise ValueError(err_msg)

        with GeoTiffFile(filepath, mode='w') as geotiff:
            geotiff.write(data,
                          band=band,
                          encoder=encoder,
                          nodataval=nodataval,
                          encoder_kwargs=encoder_kwargs,
                          ct=ct)
Пример #11
0
    def write(self, ds):
        """
        Write data set into raster time stack.

        Parameters
        ----------
        ds : xarray.Dataset
            Input data set.
        """
        time_var = ds['time'].to_index()

        if time_var.dtype == np.float64:
            time_var = pd.to_datetime(time_var,
                                      unit='D',
                                      origin=pd.Timestamp('1900-01-01'))

        time_stamps = time_var.strftime('%Y%m%d%H%M%S')
        var_dict = {}

        for name in ds.variables:
            if name == 'time':
                continue

            filenames = []
            for i, time_stamp in enumerate(time_stamps):

                fn = '{:}_{:}'.format(time_stamp, name)
                filename = os.path.join(self.out_path, '{:}.tif'.format(fn))
                filenames.append(filename)

                with GeoTiffFile(filename, mode='w', count=1) as src:
                    src.write(ds[name].isel(time=i).values, band=1)

            band = np.arange(1, len(filenames) + 1)
            var_dict[name] = pd.DataFrame(
                {
                    'filenames': filenames,
                    'band': band
                }, index=time_var)

        return var_dict
Пример #12
0
def setup_gt_test_data():
    """
    Creates test data as single-time and single-band GeoTIFF files.

    Returns
    -------
    list of str
        List of GeoTIFF test data filepaths.
    list of datetime
        List of timestamps as datetime objects.
    """

    root_dirpath = os.path.join(dirpath_test(), 'data', 'Sentinel-1_CSAR')

    # create target folders
    dirpath = os.path.join(root_dirpath, 'IWGRDH', 'preprocessed', 'datasets', 'resampled', 'T0101', 'EQUI7_EU500M')

    timestamps = [datetime(2016, 1, 1), datetime(2016, 2, 1), datetime(2017, 1, 1), datetime(2017, 2, 1)]

    var_names = ["SIG0", "GMR-"]
    pols = ["VV", "VH"]
    directions = ["A", "D"]
    tilenames = ["E048N012T6", "E042N012T6"]
    filename_fmt = "D{}_000000--_{}-----_S1AIWGRDH1{}{}_146_T0101_EU500M_{}.tif"
    combs = itertools.product(var_names, pols, directions, timestamps, tilenames)

    rows, cols = np.meshgrid(np.arange(0, 1200), np.arange(0, 1200))
    data = (rows + cols).astype(float)
    equi7 = Equi7Grid(500)

    filepaths = []
    for comb in combs:
        var_name = comb[0]
        pol = comb[1]
        direction = comb[2]
        timestamp = comb[3]
        tilename = comb[4]
        filename = filename_fmt.format(timestamp.strftime("%Y%m%d"), var_name, pol, direction, tilename)
        if var_name == "SIG0":
            var_dirpath = os.path.join(dirpath, tilename, "sig0")
        elif var_name == "GMR-":
            var_dirpath = os.path.join(dirpath, tilename, "gmr")
        else:
            raise Exception("Variable name {} unknown.".format(var_name))

        if not os.path.exists(var_dirpath):
            os.makedirs(var_dirpath)
        filepath = os.path.join(var_dirpath, filename)
        filepaths.append(filepath)

        if not os.path.exists(filepath):
            tile_oi = equi7.EU.tilesys.create_tile(name=tilename)
            tags = {'metadata': {'direction': direction}}
            gt_file = GeoTiffFile(filepath, mode='w', count=1, geotransform=tile_oi.geotransform(),
                                  spatialref=tile_oi.get_geotags()['spatialreference'], tags=tags)

            data_i = data + timestamps.index(timestamp)
            gt_file.write(data_i, band=1, nodata=[-9999])
            gt_file.close()

    timestamps = [pd.Timestamp(timestamp.strftime("%Y%m%d")) for timestamp in timestamps]

    return filepaths, timestamps