def test_img_read(self): for test_case in self.test_cases: with self.subTest(msg=test_case.filename): file_path = os.path.join(self.INPUT_FOLDER, test_case.filename) img = read_data(file_path) self.assertEqual( img.shape, test_case.shape, 'Expected shape {}, got {}'.format(test_case.shape, img.shape)) self.assertAlmostEqual(np.mean(img), test_case.mean, delta=1e-4, msg='Expected mean {}, got {}'.format( test_case.mean, np.mean(img))) new_file_path = os.path.join(self.OUTPUT_FOLDER, test_case.filename) write_data(new_file_path, img) new_img = read_data(new_file_path) if not test_case.filename.endswith('jpg'): self.assertTrue( np.array_equal(img, new_img), msg="Original and new image are not the same")
def test_img_read(self): for test_case in self.test_cases: with self.subTest(msg=test_case.filename): file_path = os.path.join(self.INPUT_FOLDER, test_case.filename) img = read_data(file_path) self.assertEqual( img.shape, test_case.shape, 'Expected shape {}, got {}'.format(test_case.shape, img.shape)) if test_case.filename != 'img.jpg' or python_implementation( ) != 'PyPy': self.assertAlmostEqual( np.mean(img), test_case.mean, delta=1e-4, msg='Expected mean {}, got {}'.format( test_case.mean, np.mean(img))) self.assertTrue(img.flags['WRITEABLE'], msg='Obtained numpy array is not writeable') new_file_path = os.path.join(self.OUTPUT_FOLDER, test_case.filename) write_data(new_file_path, img) new_img = read_data(new_file_path) if not test_case.filename.endswith('jpg'): self.assertTrue( np.array_equal(img, new_img), msg="Original and new image are not the same")
def test_import_tiff_intersecting(self): path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../example_data/import-tiff-test2.tiff') mask_feature = FeatureType.MASK_TIMELESS, 'TEST_TIF' mask_type, mask_name = mask_feature no_data_value = 1.0 task = ImportFromTiff(mask_feature, path, image_dtype=np.float64, no_data_value=no_data_value) task.execute(self.eopatch) tiff_img = read_data(path) self.assertTrue( np.array_equal(tiff_img[-6:, :3, :], self.eopatch[mask_type][mask_name][:6, -3:, :]), msg='Imported tiff data should be the same as original') feature_dtype = self.eopatch[mask_type][mask_name].dtype self.assertEqual( feature_dtype, np.float64, msg='Feature should have dtype numpy.float64 but {} found'.format( feature_dtype)) self.eopatch[mask_type][mask_name][:6, -3:, :] = no_data_value unique_values = list( np.unique(self.eopatch[mask_type][mask_name][:6, -3:, :])) self.assertEqual(unique_values, [no_data_value], msg='No data values should all be equal to {}'.format( no_data_value))
def setUpClass(cls): geojson = read_data(os.path.join(cls.INPUT_FOLDER, 'cies_islands.json')) cls.area = shape(geojson) cls.test_cases = [ cls.SplitterTestCase('BBoxSplitter', BBoxSplitter([cls.area], CRS.WGS84, 5, reduce_bbox_sizes=True), bbox_len=19), cls.SplitterTestCase('OsmSplitter', OsmSplitter([cls.area], CRS.WGS84, 15, reduce_bbox_sizes=True), bbox_len=24), cls.SplitterTestCase('TileSplitter', TileSplitter( [cls.area], CRS.WGS84, ('2017-10-01', '2018-03-01'), tile_split_shape=40, data_source=DataSource.SENTINEL2_L1C, reduce_bbox_sizes=True), bbox_len=13) ]
def test_import_tiff_intersecting(test_eopatch, example_data_path): path = os.path.join(example_data_path, "import-tiff-test2.tiff") feature = FeatureType.DATA_TIMELESS, "TEST_TIF" no_data_value = 1.0 task = ImportFromTiffTask(feature, path, image_dtype=np.float64, no_data_value=no_data_value) task(test_eopatch) tiff_img = read_data(path) assert_array_equal( tiff_img[-6:, :3, :], test_eopatch[feature][:6, -3:, :], err_msg="Imported tiff data should be the same as original", ) feature_dtype = test_eopatch[feature].dtype assert feature_dtype == np.float64, f"Feature should have dtype numpy.float64 but {feature_dtype} found" test_eopatch[feature][:6, -3:, :] = no_data_value unique_values = list(np.unique(test_eopatch[feature][:6, -3:, :])) assert unique_values == [ no_data_value ], f"No data values should all be equal to {no_data_value}"
def setUpClass(cls): super().setUpClass() geojson = read_data(os.path.join(cls.INPUT_FOLDER, 'cies_islands.json')) cls.area = shapely.geometry.shape(geojson) bbox_grid = [BBox((x / 10, y / 100, (x + 1) / 10, (y + 1) / 100), CRS.WGS84) for x, y in itertools.product(range(-90, -87), range(4200, 4250))] cls.test_cases = [ cls.SplitterTestCase('BBoxSplitter', BBoxSplitter([cls.area], CRS.WGS84, 5, reduce_bbox_sizes=True), bbox_len=19), cls.SplitterTestCase('OsmSplitter', OsmSplitter([cls.area], CRS.WGS84, 15, reduce_bbox_sizes=True), bbox_len=24), cls.SplitterTestCase('TileSplitter', TileSplitter([cls.area], CRS.WGS84, ('2017-10-01', '2018-03-01'), tile_split_shape=40, data_source=DataSource.SENTINEL2_L1C, reduce_bbox_sizes=True), bbox_len=13), cls.SplitterTestCase('CustomGridSplitter', CustomGridSplitter([cls.area], CRS.WGS84, bbox_grid, bbox_split_shape=(3, 4), reduce_bbox_sizes=False), bbox_len=41), cls.SplitterTestCase('UTMGridSplitter', UtmGridSplitter([cls.area], CRS.WGS84, bbox_size=(1200, 1200)), bbox_len=16), cls.SplitterTestCase('UTMZoneSplitter', UtmZoneSplitter([cls.area], CRS.WGS84, bbox_size=(1000, 1000)), bbox_len=19) ]
def test_img_read(self): W, H = 2048, 2048 formats = [('tif', 13577.49), ('jpg', 52.41), ('png', 52.34), ('jp2', 47.09)] for ext, exp_mean in formats: with self.subTest(msg=ext): filename = os.path.join(self.INPUT_FOLDER, 'ml.{}'.format(ext)) img = read_data(filename) self.assertEqual(np.shape(img), (W, H, 3) if ext != 'jp2' else (343, 343, 3), "{} image dimension mismatch".format(ext)) self.assertAlmostEqual(np.mean(img), exp_mean, delta=1e-1, msg="{} image has incorrect values".format(ext)) new_filename = os.path.join(self.OUTPUT_FOLDER, os.path.split(filename)[-1]) write_data(new_filename, img) new_img = read_data(new_filename) if ext != 'jpg': self.assertTrue(np.array_equal(img, new_img), msg="Original and new image are not the same")
def load_input_sources(): """ Loads input sources and returns a dictionary with source IDs and source classes """ sources_filename = os.path.join(os.path.dirname(__file__), 'data', 'input_sources.json') sources_list = [ Source.load(payload) for payload in to_python(read_data(sources_filename))['sources'] ] return {source.id: source for source in sources_list}
def test_import_tiff_subset(self): path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../example_data/import-tiff-test1.tiff') mask_feature = FeatureType.MASK_TIMELESS, 'TEST_TIF' mask_type, mask_name = mask_feature task = ImportFromTiff(mask_feature, path) task.execute(self.eopatch) tiff_img = read_data(path) self.assertTrue(np.array_equal(tiff_img[20: 53, 21: 54], self.eopatch[mask_type][mask_name][..., 0]), msg='Imported tiff data should be the same as original')
def test_import_tiff_subset(test_eopatch, example_data_path): path = os.path.join(example_data_path, "import-tiff-test1.tiff") mask_feature = FeatureType.MASK_TIMELESS, "TEST_TIF" task = ImportFromTiffTask(mask_feature, path) task(test_eopatch) tiff_img = read_data(path) assert_array_equal( tiff_img[20:53, 21:54], test_eopatch[mask_feature][..., 0], err_msg="Imported tiff data should be the same as original", )
def _load_data(self, filename): """ Load data from file """ data = read_data( os.path.join(os.path.dirname(os.path.realpath(__file__)), filename)) self.source_dict = {} for source_info in data['sources']: source = SourceType(source_info['source_type']) source_info = to_python(source_info) if source is SourceType.S2_L1C_ARCHIVE: self.source_dict[source_info['id']] = Source(**source_info) elif source is SourceType.GEOPEDIA_WB: self.source_dict[source_info['id']] = Source(**source_info) else: raise NotImplementedError( 'Support for source {} is not implemented') self.campaign_collection = {} for campaign_info in data['campaigns']: campaign_info = to_python(campaign_info) # TODO: add parsing of Box for local back-end if 'bbox' not in campaign_info: campaign_info['bbox'] = BBox(bbox=((0, 0), (0, 0)), crs=CRS(4326)) for param in ['input_source', 'output_source']: campaign_info[param] = self.source_dict[campaign_info[param] ['id']] self.campaign_collection[campaign_info['id']] = Campaign( **campaign_info) self.user_collection = { user_info['id']: User(**to_python(user_info)) for user_info in data['users'] }
def load_truth(self): return [tuple(item) for item in read_data(self.get_filename())]
import numpy as np from shapely.geometry import shape, Polygon, MultiLineString from sentinelhub import SHConfig from sentinelhub import BBoxSplitter, OsmSplitter, TileSplitter, CustomGridSplitter, UtmZoneSplitter, UtmGridSplitter from sentinelhub import BBox, read_data, CRS, DataCollection # imports for visualisations import matplotlib.pyplot as plt from matplotlib.patches import Polygon as plt_polygon from mpl_toolkits.basemap import Basemap INPUT_FILE = './data/Hawaii.json' geo_json = read_data(INPUT_FILE) hawaii_area = shape(geo_json["features"][0]["geometry"]) print(type(hawaii_area)) def show_area(area_shape, area_buffer=0.3): """ Args: area_shape: area_buffer: Returns: None (plotting function) """ fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111)