def test_rastermetadata_cache(self): # GDAL helper functions for generating VRT landsat = Landsat(self.metadata_set) # get a numpy.ndarray from bands for specified epl_grpc # 'nir', 'swir1', 'swir2' band_numbers = [Band.NIR, Band.SWIR1, Band.SWIR2] scaleParams = [[0.0, 40000.0], [0.0, 40000.0], [0.0, 40000.0]] nda = landsat.fetch_imagery_array( band_numbers, scaleParams, polygon_boundary_wkb=self.taos_shape.wkb, spatial_resolution_m=120) self.assertIsNotNone(nda) self.assertEqual((902, 648, 3), nda.shape) band_numbers = [Band.RED, Band.BLUE, Band.GREEN] nda = landsat.fetch_imagery_array( band_numbers, scaleParams, polygon_boundary_wkb=self.taos_shape.wkb, spatial_resolution_m=120) self.assertIsNotNone(nda) self.assertEqual((902, 648, 3), nda.shape) band_numbers = [Band.RED, Band.BLUE, Band.GREEN] nda = landsat.fetch_imagery_array(band_numbers, scaleParams, spatial_resolution_m=120) self.assertIsNotNone(nda) self.assertNotEqual((902, 648, 3), nda.shape)
def test_ndvi_taos(self): code = """import numpy as np def ndvi_numpy(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, buf_radius, gt, **kwargs): with np.errstate(divide = 'ignore', invalid = 'ignore'): factor = float(kwargs['factor']) output = np.divide((in_ar[1] - in_ar[0]), (in_ar[1] + in_ar[0])) output[np.isnan(output)] = 0.0 # shift range from -1.0-1.0 to 0.0-2.0 output += 1.0 # scale up from 0.0-2.0 to 0 to 255 by multiplying by 255/2 # https://stackoverflow.com/a/1735122/445372 output *= factor/2.0 # https://stackoverflow.com/a/10622758/445372 # in place type conversion out_ar[:] = output.astype(np.int16, copy=False)""" code2 = """import numpy as np def ndvi_numpy2(in_ar, out_ar, xoff, yoff, xsize, ysize, raster_xsize, raster_ysize, buf_radius, gt, **kwargs): with np.errstate(divide = 'ignore', invalid = 'ignore'): output = (in_ar[1] - in_ar[0]) / (in_ar[1] + in_ar[0]) output[np.isnan(output)] = 0.0 out_ar[:] = output""" landsat = Landsat(self.metadata_set) scale_params = [[0, DataType.UINT16.range_max, -1.0, 1.0]] pixel_function_details = FunctionDetails( name="ndvi_numpy", band_definitions=[Band.RED, Band.NIR], code=code, arguments={"factor": DataType.UINT16.range_max}, data_type=DataType.UINT16) nda = landsat.fetch_imagery_array( [pixel_function_details], scale_params=scale_params, polygon_boundary_wkb=self.taos_shape.wkb, output_type=DataType.FLOAT32) self.assertIsNotNone(nda) self.assertGreaterEqual(1.0, nda.max()) self.assertLessEqual(-1.0, nda.min()) pixel_function_details = FunctionDetails( name="ndvi_numpy2", band_definitions=[Band.RED, Band.NIR], code=code2, data_type=DataType.FLOAT32) nda2 = landsat.fetch_imagery_array( [pixel_function_details], polygon_boundary_wkb=self.taos_shape.wkb, output_type=DataType.FLOAT32) self.assertIsNotNone(nda2) self.assertGreaterEqual(1.0, nda2.max()) self.assertLessEqual(-1.0, nda2.min())
def test_file_creation(self): landsat = Landsat(self.metadata_set[0]) # get a numpy.ndarray from bands for specified epl_grpc band_numbers = [Band.RED, Band.GREEN, Band.BLUE] scale_params = [[0.0, 65535], [0.0, 65535], [0.0, 65535]] file_name = landsat.fetch_file(band_numbers, scale_params, self.taos_shape.wkb, spatial_resolution_m=480) self.assertGreater(len(file_name), 0)
def test_cutline(self): # GDAL helper functions for generating VRT landsat = Landsat(self.metadata_set[0]) # get a numpy.ndarray from bands for specified epl_grpc band_numbers = [Band.RED, Band.GREEN, Band.BLUE] scale_params = [[0.0, 65535], [0.0, 65535], [0.0, 65535]] nda = landsat.fetch_imagery_array(band_numbers, scale_params, self.taos_shape.wkb, spatial_resolution_m=480) self.assertIsNotNone(nda)
def test_mosaic(self): # GDAL helper functions for generating VRT landsat = Landsat(self.metadata_set) # get a numpy.ndarray from bands for specified epl_grpc band_numbers = [Band.RED, Band.GREEN, Band.BLUE] scale_params = [[0.0, 65535], [0.0, 65535], [0.0, 65535]] nda = landsat.fetch_imagery_array( band_numbers, scale_params, envelope_boundary=self.taos_shape.bounds) self.assertIsNotNone(nda) self.assertEqual((1804, 1295, 3), nda.shape)
def test_vrt_with_alpha(self): landsat = Landsat(self.metadata_set) # get a numpy.ndarray from bands for specified epl_grpc band_numbers = [Band.RED, Band.GREEN, Band.BLUE, Band.ALPHA] scaleParams = [[0.0, 40000], [0.0, 40000], [0.0, 40000]] nda = landsat.fetch_imagery_array( band_numbers, scaleParams, envelope_boundary=self.taos_shape.bounds, output_type=DataType.UINT16, spatial_resolution_m=120) self.assertIsNotNone(nda)
def test_mosaic_cutline(self): # GDAL helper functions for generating VRT landsat = Landsat(self.metadata_set) # get a numpy.ndarray from bands for specified epl_grpc # 'nir', 'swir1', 'swir2' band_numbers = [Band.NIR, Band.SWIR1, Band.SWIR2] scaleParams = [[0.0, 40000.0], [0.0, 40000.0], [0.0, 40000.0]] nda = landsat.fetch_imagery_array( band_numbers, scaleParams, polygon_boundary_wkb=self.taos_shape.wkb) self.assertIsNotNone(nda) self.assertEqual((1804, 1295, 3), nda.shape)
def test_ndarray(self): # sql_filters = ['scene_id="LC80270312016188LGN00"'] landsat_filters = LandsatQueryFilters() landsat_filters.scene_id.set_value("LC80270312016188LGN00") metadata_rows = self.metadata_service.search( SpacecraftID.UNKNOWN_SPACECRAFT, data_filters=landsat_filters) metadata_set = list(metadata_rows) landsat = Landsat(metadata_set) data = landsat.fetch_imagery_array( band_definitions=[Band.RED, Band.GREEN, Band.BLUE, Band.ALPHA], spatial_resolution_m=960) self.assertEqual(data.shape, (249, 245, 4)) self.assertEqual(data.dtype, np.uint8)
def test_datatypes(self): landsat = Landsat(self.metadata_set) # get a numpy.ndarray from bands for specified epl_grpc band_numbers = [Band.RED, Band.GREEN, Band.BLUE] scaleParams = [[0.0, 40000], [0.0, 40000], [0.0, 40000]] for data_type in DataType: if data_type == DataType.CFLOAT32 or data_type == DataType.CFLOAT64 or data_type == DataType.UNKNOWN_GDAL: continue nda = landsat.fetch_imagery_array( band_numbers, scaleParams, envelope_boundary=self.taos_shape.bounds, output_type=data_type, spatial_resolution_m=240) self.assertIsNotNone(nda) self.assertGreaterEqual(data_type.range_max, nda.max()) self.assertLessEqual(data_type.range_min, nda.min())
def test_band_enum(self): self.assertTrue(True) d_start = date(2016, 7, 20) d_end = date(2016, 7, 28) landsat_filters = LandsatQueryFilters() landsat_filters.scene_id.set_value("LC80390332016208LGN00") landsat_filters.acquired.set_range(start=d_start, end=d_end) rows = self.metadata_service.search(SpacecraftID.LANDSAT_8, limit=1, data_filters=landsat_filters) rows = list(rows) metadata = rows[0] landsat = Landsat(metadata) scale_params = [[0.0, 65535], [0.0, 65535], [0.0, 65535]] # nda = landsat.__get_ndarray(band_numbers, metadata, scale_params) nda = landsat.fetch_imagery_array([Band.RED, Band.GREEN, Band.BLUE], scale_params=scale_params, spatial_resolution_m=240) self.assertIsNotNone(nda) nda2 = landsat.fetch_imagery_array([4, 3, 2], scale_params=scale_params, spatial_resolution_m=240) np.testing.assert_almost_equal(nda, nda2)
def test_pixel_function_vrt_1(self): metadata_service = MetadataService() # TODO figure out what's wrong here for AWS self.assertTrue(True) utah_box = (-112.66342163085938, 37.738141282210385, -111.79824829101562, 38.44821130413263) d_start = date(2016, 7, 20) d_end = date(2016, 7, 28) landsat_filters = LandsatQueryFilters() landsat_filters.cloud_cover.set_value(5) landsat_filters.acquired.set_range(start=d_start, end=d_end) landsat_filters.aoi.set_bounds(*utah_box) rows = metadata_service.search(SpacecraftID.LANDSAT_8, limit=10, data_filters=landsat_filters) rows = list(rows) self.assertEqual(len(rows), 2) # # # metadata_row = ['LC80390332016208LGN00', '', 'LANDSAT_8', 'OLI_TIRS', '2016-07-26', # # '2016-07-26T18:14:46.9465460Z', 'PRE', 'N/A', 'L1T', 39, 33, 1.69, # # 39.96962, 37.81744, -115.27267, -112.56732, 1070517542, # # 'gs://gcp-public-data-landsat/LC08/PRE/039/033/LC80390332016208LGN00'] metadata = rows[0] # GDAL helper functions for generating VRT landsat = Landsat([metadata]) # get a numpy.ndarray from bands for specified epl_grpc band_numbers = [4, 3, 2] scale_params = [[0.0, 65535], [0.0, 65535], [0.0, 65535]] nda = landsat.fetch_imagery_array(band_numbers, scale_params) self.assertEqual(nda.shape, (3861, 3786, 3))