Ejemplo n.º 1
0
def gen_color_file(input_file):
    fp, temp_file = tempfile.mkstemp(suffix='.txt')

    dem = DEM(input_file)
    dem.open()
    phase_data = dem.height_band.ReadAsArray()

    max_ph = np.nanmax(phase_data)
    min_ph = np.nanmin(phase_data)
    range_ph = max_ph-min_ph
    colors = ['black', 'blue', 'yellow', 'orange', 'red', 'white']
    with open(temp_file, 'w') as f:
        for i, c in enumerate(colors[:-1]):
            f.write(str(int(min_ph + (i + 1)*range_ph/len(colors))) +
                    ' ' + c + '\n')
        f.write(str(int(max_ph - range_ph/len(colors))) +
                ' ' + colors[-1] + '\n')
    os.close(fp)
    return temp_file
Ejemplo n.º 2
0
def dem_or_ifg(data_path):
    """ 
    Whether tif is a dem or an interferogram
    
    :param: data_path: xxxxx
    
    :return xxxx 
    """
    ds = gdal.Open(data_path)
    md = ds.GetMetadata()
    if 'DATE' in md:  # ifg
        return Ifg(data_path)
    else:
        return DEM(data_path)
Ejemplo n.º 3
0
def dem_or_ifg(data_path):
    """
    Returns an Ifg or DEM class object from input geotiff file.

    :param str data_path: file path name

    :return: Interferogram or DEM object from input file
    :rtype: Ifg or DEM class object
    """
    ds = gdal.Open(data_path)
    md = ds.GetMetadata()
    if 'DATE' in md:  # ifg
        return Ifg(data_path)
    else:
        return DEM(data_path)
Ejemplo n.º 4
0
    def parse(self, string):
        """
        override of :py:meth:`DictParam.parse`.
        """
        dct = super(RasterParam, self).parse(string)
        raster_type = dct['type']
        path = dct['path']

        if raster_type == 'DEM':
            return DEM(path)
        elif raster_type == 'Ifg':
            return Ifg(path)
        elif raster_type == 'Incidence':
            return Incidence(path)
        else:  # pragma: no cover
            raise luigi.parameter.UnknownParameterException(
                'rasterBase must be an inscance DEM, '
                'Ifg or Incidence is valid')
Ejemplo n.º 5
0
    def test_output_datatype(self):
        """Test resampling method using a scaling factor of 4"""
        scale = 4  # assumes square cells
        self.ifgs.append(DEM(SML_TEST_DEM_TIF))
        self.ifg_paths = [i.data_path for i in self.ifgs] + [SML_TEST_DEM_TIF]
        cext = self._custom_extents_tuple()
        xlooks = ylooks = scale
        prepare_ifgs(self.ifg_paths,
                     CUSTOM_CROP,
                     xlooks,
                     ylooks,
                     thresh=1.0,
                     user_exts=cext)

        for i in self.ifg_paths:
            mlooked_ifg = mlooked_path(i, xlooks, CUSTOM_CROP)
            ds1 = DEM(mlooked_ifg)
            ds1.open()
            ds2 = DEM(i)
            ds2.open()
            self.assertEqual(
                ds1.dataset.GetRasterBand(1).DataType,
                ds2.dataset.GetRasterBand(1).DataType)
            ds1 = ds2 = None
Ejemplo n.º 6
0
class DEMTests(unittest.TestCase):
    'Unit tests to verify operations on GeoTIFF format DEMs'

    def setUp(self):
        self.ras = DEM(SML_TEST_DEM_TIF)

    def test_create_raster(self):
        # validate header path
        self.assertTrue(os.path.exists(self.ras.data_path))

    def test_headers_as_attr(self):
        self.ras.open()
        attrs = ['ncols', 'nrows', 'x_first', 'x_step', 'y_first', 'y_step' ]

        # TODO: are 'projection' and 'datum' attrs needed?
        for a in attrs:
            self.assertTrue(getattr(self.ras, a) is not None)

    def test_is_dem(self):
        self.ras = DEM(join(SML_TEST_TIF, 'geo_060619-061002_unw.tif'))
        self.assertFalse(hasattr(self.ras, 'datum'))

    def test_open(self):
        self.assertTrue(self.ras.dataset is None)
        self.ras.open()
        self.assertTrue(self.ras.dataset is not None)
        self.assertTrue(isinstance(self.ras.dataset, Dataset))

        # ensure open cannot be called twice
        self.assertRaises(RasterException, self.ras.open)

    def test_band(self):
        # test accessing bands with open and unopened datasets
        try:
            _ = self.ras.height_band
            self.fail("Should not be able to access band without open dataset")
        except RasterException:
            pass

        self.ras.open()
        data = self.ras.height_band.ReadAsArray()
        self.assertEqual(data.shape, (72, 47))
Ejemplo n.º 7
0
 def test_is_dem(self):
     self.ras = DEM(join(SML_TEST_TIF, 'geo_060619-061002_unw.tif'))
     self.assertFalse(hasattr(self.ras, 'datum'))
Ejemplo n.º 8
0
 def setUp(self):
     self.ras = DEM(SML_TEST_DEM_TIF)
Ejemplo n.º 9
0
    def test_multilook(self):
        """Test resampling method using a scaling factor of 4"""
        scale = 4  # assumes square cells
        self.ifgs.append(DEM(SML_TEST_DEM_TIF))
        self.ifg_paths = [i.data_path for i in self.ifgs]
        cext = self._custom_extents_tuple()
        xlooks = ylooks = scale
        prepare_ifgs(self.ifg_paths,
                     CUSTOM_CROP,
                     xlooks,
                     ylooks,
                     thresh=1.0,
                     user_exts=cext)

        for n, ipath in enumerate([self.exp_files[3], self.exp_files[7]]):
            i = Ifg(ipath)
            i.open()
            self.assertEqual(i.dataset.RasterXSize, 20 / scale)
            self.assertEqual(i.dataset.RasterYSize, 28 / scale)

            # verify resampling
            path = join(PREP_TEST_TIF, "%s.tif" % n)
            ds = gdal.Open(path)
            src_data = ds.GetRasterBand(2).ReadAsArray()
            exp_resample = multilooking(src_data, scale, scale, thresh=0)
            self.assertEqual(exp_resample.shape, (7, 5))
            assert_array_almost_equal(exp_resample, i.phase_band.ReadAsArray())
            ds = None
            i.close()
            os.remove(ipath)

        # verify DEM has been correctly processed
        # ignore output values as resampling has already been tested for phase
        exp_dem_path = join(SML_TEST_DEM_DIR,
                            'roipac_test_trimmed_4rlks_3cr.tif')
        self.assertTrue(exists(exp_dem_path))
        orignal_dem = DEM(SML_TEST_DEM_TIF)
        orignal_dem.open()
        dem_dtype = orignal_dem.dataset.GetRasterBand(1).DataType
        orignal_dem.close()
        dem = DEM(exp_dem_path)
        dem.open()

        # test multilooked dem is of the same datatype as the original dem tif
        self.assertEqual(dem_dtype, dem.dataset.GetRasterBand(1).DataType)

        self.assertEqual(dem.dataset.RasterXSize, 20 / scale)
        self.assertEqual(dem.dataset.RasterYSize, 28 / scale)
        data = dem.height_band.ReadAsArray()
        self.assertTrue(data.ptp() != 0)
        # close ifgs
        dem.close()
        for i in self.ifgs:
            i.close()
        os.remove(exp_dem_path)