pyosv.io.reader

  1from ..utils.paths import get_path_gui
  2
  3import matplotlib.pyplot as plt
  4import numpy as np
  5import rasterio
  6import netCDF4
  7
  8
  9def load(path : str) -> [np.ndarray or dict, dict, list]:
 10    '''
 11        Load an image and its metadata given its path.
 12
 13        Supported data format
 14
 15        RASTERIO_EXTENSIONS   = ['.tif', '.tiff', '.geotiff']  
 16        MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg', 'jp2']
 17        NETCDF4_EXTENSIONS    = ['.nc']
 18
 19        Returns always data in channel last format.
 20
 21        If image extension is in MATPLOTLIB_EXTENSIONS, metadata and bouns will be None.
 22        If image extension is in NETCDF4_EXTENSIONS, metadata and bounds will be None.
 23        
 24        Parameters:
 25        -----------
 26            - path : str
 27                position of the image, if None the function will ask for the image path using a menu
 28
 29        Returns:
 30        --------
 31            - data : np.ndarray or list
 32                WxHxB image, with W width, H height and B bands
 33
 34            - metadata : dict
 35                dictionary containing image metadata
 36            
 37            - bounds : list
 38                list containing geo bounds
 39        
 40        Usage:
 41        ------
 42        ```python
 43            img = load(None)
 44        ``` 
 45        or
 46        ```python
 47            img = load("path/to/image.png")
 48        ``` 
 49
 50        Output:
 51        -------
 52        ```
 53        (
 54            array([[[5872., 5532., 5516., ...,    0.,    0., 1024.],  
 55                    [5872., 5588., 5451., ...,    0.,    0., 1024.],  
 56                    [5872., 5606., 5333., ...,    0.,    0., 1024.],  
 57                    ...,  
 58                    [2672., 2602., 2368., ...,    0.,    0., 1024.],  
 59                    [2672., 2689., 2394., ...,    0.,    0., 1024.],  
 60                    [2672., 2705., 2431., ...,    0.,    0., 1024.]],  
 61                    ...,  
 62                    [[1571., 1318., 1167., ...,    0.,    0.,    0.],  
 63                    [1571., 1206., 1113., ...,    0.,    0.,    0.],  
 64                    [1571., 1230., 1094., ...,    0.,    0.,    0.],  
 65                    ...,  
 66                    [1330., 1044.,  837., ...,    0.,    0.,    0.],  
 67                    [1330., 1045.,  842., ...,    0.,    0.,    0.],  
 68                    [1330., 1032.,  833., ...,    0.,    0.,    0.]]]),  
 69            
 70            {'driver': 'GTiff', 'dtype': 'float64', 'nodata': None, 'width': 1043, 'height': 1040, 'count': 16, 'crs': CRS.from_epsg(32632), 'transform': Affine(10.0, 0.0, 638640.0,
 71       0.0, -10.0, 5084590.0), 'blockxsize': 256, 'blockysize': 256, 'tiled': True, 'compress': 'lzw', 'interleave': 'pixel'},  
 72
 73       BoundingBox(left=638640.0, bottom=5074190.0, right=649070.0, top=5084590.0))  
 74        )
 75
 76        ```
 77    '''
 78    
 79    
 80    RASTERIO_EXTENSIONS   = ['.tif', '.tiff', '.geotiff']
 81    MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg', 'jp2']
 82    NETCDF4_EXTENSIONS    = ['.nc']
 83    NUMPY_EXTENSIONS      = ['.npy', '.npz']
 84    
 85    
 86    if path is None:
 87        path = get_path_gui()
 88    
 89    if any(frmt in path for frmt in RASTERIO_EXTENSIONS):
 90        with rasterio.open(path) as src:
 91            data = src.read()
 92            metadata = src.profile
 93            bounds = src.bounds
 94        data = np.moveaxis(data, 0, -1)
 95    elif any(frmt in path for frmt in MATPLOTLIB_EXTENSIONS):
 96        data = plt.imread(path)
 97        metadata = None
 98        bounds = None
 99    elif any(frmt in path for frmt in NETCDF4_EXTENSIONS):
100        data = netCDF4.Dataset(path, 'r')
101        metadata = None
102        bounds = None
103    elif any(frmt in path for frmt in NUMPY_EXTENSIONS):
104        data = np.load(path)
105        metadata = None
106        bounds = None
107    else:
108        data = None
109        metadata = None
110        bounds = None
111        raise Exception('Error: file can not be opened or format not supported!')
112        
113    return data, metadata, bounds
def load(path: str) -> [<class 'numpy.ndarray'>, <class 'dict'>, <class 'list'>]:
 10def load(path : str) -> [np.ndarray or dict, dict, list]:
 11    '''
 12        Load an image and its metadata given its path.
 13
 14        Supported data format
 15
 16        RASTERIO_EXTENSIONS   = ['.tif', '.tiff', '.geotiff']  
 17        MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg', 'jp2']
 18        NETCDF4_EXTENSIONS    = ['.nc']
 19
 20        Returns always data in channel last format.
 21
 22        If image extension is in MATPLOTLIB_EXTENSIONS, metadata and bouns will be None.
 23        If image extension is in NETCDF4_EXTENSIONS, metadata and bounds will be None.
 24        
 25        Parameters:
 26        -----------
 27            - path : str
 28                position of the image, if None the function will ask for the image path using a menu
 29
 30        Returns:
 31        --------
 32            - data : np.ndarray or list
 33                WxHxB image, with W width, H height and B bands
 34
 35            - metadata : dict
 36                dictionary containing image metadata
 37            
 38            - bounds : list
 39                list containing geo bounds
 40        
 41        Usage:
 42        ------
 43        ```python
 44            img = load(None)
 45        ``` 
 46        or
 47        ```python
 48            img = load("path/to/image.png")
 49        ``` 
 50
 51        Output:
 52        -------
 53        ```
 54        (
 55            array([[[5872., 5532., 5516., ...,    0.,    0., 1024.],  
 56                    [5872., 5588., 5451., ...,    0.,    0., 1024.],  
 57                    [5872., 5606., 5333., ...,    0.,    0., 1024.],  
 58                    ...,  
 59                    [2672., 2602., 2368., ...,    0.,    0., 1024.],  
 60                    [2672., 2689., 2394., ...,    0.,    0., 1024.],  
 61                    [2672., 2705., 2431., ...,    0.,    0., 1024.]],  
 62                    ...,  
 63                    [[1571., 1318., 1167., ...,    0.,    0.,    0.],  
 64                    [1571., 1206., 1113., ...,    0.,    0.,    0.],  
 65                    [1571., 1230., 1094., ...,    0.,    0.,    0.],  
 66                    ...,  
 67                    [1330., 1044.,  837., ...,    0.,    0.,    0.],  
 68                    [1330., 1045.,  842., ...,    0.,    0.,    0.],  
 69                    [1330., 1032.,  833., ...,    0.,    0.,    0.]]]),  
 70            
 71            {'driver': 'GTiff', 'dtype': 'float64', 'nodata': None, 'width': 1043, 'height': 1040, 'count': 16, 'crs': CRS.from_epsg(32632), 'transform': Affine(10.0, 0.0, 638640.0,
 72       0.0, -10.0, 5084590.0), 'blockxsize': 256, 'blockysize': 256, 'tiled': True, 'compress': 'lzw', 'interleave': 'pixel'},  
 73
 74       BoundingBox(left=638640.0, bottom=5074190.0, right=649070.0, top=5084590.0))  
 75        )
 76
 77        ```
 78    '''
 79    
 80    
 81    RASTERIO_EXTENSIONS   = ['.tif', '.tiff', '.geotiff']
 82    MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg', 'jp2']
 83    NETCDF4_EXTENSIONS    = ['.nc']
 84    NUMPY_EXTENSIONS      = ['.npy', '.npz']
 85    
 86    
 87    if path is None:
 88        path = get_path_gui()
 89    
 90    if any(frmt in path for frmt in RASTERIO_EXTENSIONS):
 91        with rasterio.open(path) as src:
 92            data = src.read()
 93            metadata = src.profile
 94            bounds = src.bounds
 95        data = np.moveaxis(data, 0, -1)
 96    elif any(frmt in path for frmt in MATPLOTLIB_EXTENSIONS):
 97        data = plt.imread(path)
 98        metadata = None
 99        bounds = None
100    elif any(frmt in path for frmt in NETCDF4_EXTENSIONS):
101        data = netCDF4.Dataset(path, 'r')
102        metadata = None
103        bounds = None
104    elif any(frmt in path for frmt in NUMPY_EXTENSIONS):
105        data = np.load(path)
106        metadata = None
107        bounds = None
108    else:
109        data = None
110        metadata = None
111        bounds = None
112        raise Exception('Error: file can not be opened or format not supported!')
113        
114    return data, metadata, bounds

Load an image and its metadata given its path.

Supported data format

RASTERIO_EXTENSIONS = ['.tif', '.tiff', '.geotiff']
MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg', 'jp2'] NETCDF4_EXTENSIONS = ['.nc']

Returns always data in channel last format.

If image extension is in MATPLOTLIB_EXTENSIONS, metadata and bouns will be None. If image extension is in NETCDF4_EXTENSIONS, metadata and bounds will be None.

Parameters:


 - path : str
     position of the image, if None the function will ask for the image path using a menu

Returns:


 - data : np.ndarray or list
     WxHxB image, with W width, H height and B bands

 - metadata : dict
     dictionary containing image metadata

 - bounds : list
     list containing geo bounds

Usage:


    img = load(None)
 

or

    img = load("path/to/image.png")
 

Output:


 (
     array([[[5872., 5532., 5516., ...,    0.,    0., 1024.],  
             [5872., 5588., 5451., ...,    0.,    0., 1024.],  
             [5872., 5606., 5333., ...,    0.,    0., 1024.],  
             ...,  
             [2672., 2602., 2368., ...,    0.,    0., 1024.],  
             [2672., 2689., 2394., ...,    0.,    0., 1024.],  
             [2672., 2705., 2431., ...,    0.,    0., 1024.]],  
             ...,  
             [[1571., 1318., 1167., ...,    0.,    0.,    0.],  
             [1571., 1206., 1113., ...,    0.,    0.,    0.],  
             [1571., 1230., 1094., ...,    0.,    0.,    0.],  
             ...,  
             [1330., 1044.,  837., ...,    0.,    0.,    0.],  
             [1330., 1045.,  842., ...,    0.,    0.,    0.],  
             [1330., 1032.,  833., ...,    0.,    0.,    0.]]]),  

     {'driver': 'GTiff', 'dtype': 'float64', 'nodata': None, 'width': 1043, 'height': 1040, 'count': 16, 'crs': CRS.from_epsg(32632), 'transform': Affine(10.0, 0.0, 638640.0,
0.0, -10.0, 5084590.0), 'blockxsize': 256, 'blockysize': 256, 'tiled': True, 'compress': 'lzw', 'interleave': 'pixel'},  

BoundingBox(left=638640.0, bottom=5074190.0, right=649070.0, top=5084590.0))  
 )