def test_is_image_format(self): for ext in ['tif', 'tiff', 'jpg', 'jpeg', 'png', 'jp2']: mime_type = MimeType.from_string(ext) self.assertTrue( MimeType.is_image_format(mime_type), msg="Expected MIME type {} to be an image format".format( mime_type.value))
def test_is_image_format(self): image_format_extensions = ('tif', 'tiff', 'jpg', 'jpeg', 'tif', 'tiff', 'png', 'jpg') for ext in image_format_extensions: mime_type = MimeType(MimeType.canonical_extension(ext)) self.assertTrue( MimeType.is_image_format(mime_type), msg="Expected MIME type {} to be an image format".format( mime_type.value))
def test_extension_and_from_string(self): extension_pairs = (('tif', MimeType.TIFF), ('jpeg', MimeType.JPG), ('h5', MimeType.HDF), ('hdf5', MimeType.HDF)) for ext, mime_type in extension_pairs: parsed_mime_type = MimeType.from_string(ext) self.assertEqual(mime_type, parsed_mime_type) for mime_type in MimeType: if not mime_type.is_tiff_format(): self.assertEqual(MimeType.from_string(mime_type.extension), mime_type) with self.assertRaises(ValueError): MimeType.from_string('unknown ext')
def test_canonical_extension(self): extension_pairs = (('tiff', 'tiff'), ('tif', 'tiff'), ('jpg', 'jpg'), ('jpeg', 'jpg'), ('png', 'png'), ('jp2', 'jp2'), ('txt', 'txt'), ('h5', 'hdf'), ('hdf', 'hdf'), ('hdf5', 'hdf')) for ext, canon in extension_pairs: res = MimeType.canonical_extension(ext) self.assertEqual(canon, res, msg="Expected {}, got {}".format(canon, res))
def _decode(self, file, path): """Loads from a file and decodes content.""" file_format = MimeType(fs.path.splitext(path)[1].strip(".")) if file_format is MimeType.NPY: return np.load(file) if file_format is MimeType.GPKG: dataframe = gpd.read_file(file) if dataframe.crs is not None: # Trying to preserve a standard CRS and passing otherwise try: with warnings.catch_warnings(): warnings.simplefilter("ignore", category=SHUserWarning) dataframe.crs = CRS(dataframe.crs).pyproj_crs() except ValueError: pass if "TIMESTAMP" in dataframe: dataframe.TIMESTAMP = pd.to_datetime(dataframe.TIMESTAMP) return dataframe if file_format in [MimeType.JSON, MimeType.GEOJSON]: json_data = json.load(file) if self.feature_type is FeatureType.BBOX: return Geometry.from_geojson(json_data).bbox return json_data if file_format is MimeType.PICKLE: warnings.warn( f"File {self.path} with data of type {self.feature_type} is in pickle format which is deprecated " "since eo-learn version 1.0. Please re-save this EOPatch with the new eo-learn version to " "update the format. In newer versions this backward compatibility will be removed.", EODeprecationWarning, ) data = pickle.load(file) # There seems to be an issue in geopandas==0.8.1 where unpickling GeoDataFrames, which were saved with an # old geopandas version, loads geometry column into a pandas.Series instead geopandas.GeoSeries. Because # of that it is missing a crs attribute which is only attached to the entire GeoDataFrame if isinstance(data, GeoDataFrame) and not isinstance( data.geometry, GeoSeries): data = data.set_geometry("geometry") return data raise ValueError(f"Unsupported data type for file {path}.")
def test_get_string(self): type_string_pairs = ((MimeType.PNG, 'image/png'), (MimeType.JPG, 'image/jpeg'), (MimeType.TIFF, 'image/tiff'), (MimeType.TIFF_d32f, 'image/tiff;depth=32f'), (MimeType.JSON, 'application/json'), (MimeType.CSV, 'text/csv'), (MimeType.ZIP, 'application/zip'), (MimeType.HDF, 'application/x-hdf'), (MimeType.XML, 'application/xml'), (MimeType.TXT, 'text/plain')) for img_type, img_str in type_string_pairs: res = MimeType.get_string(img_type) self.assertEqual(img_str, res, msg="Expected {}, got {}".format(img_str, res))
def test_has_value(self): self.assertTrue(MimeType.has_value('tiff;depth=32f')) self.assertFalse(MimeType.has_value('unknown value'))