import rastereasy

Dealing with shapefiles, ROI, …

1) Read shapefile

Two main functions : rastereasy.shpfiles.shp2raster and rastereasy.shpfiles.shp2geoim

help(rastereasy.shpfiles.shp2raster)
help(rastereasy.shpfiles.shp2geoim)
Help on function shp2raster in module rastereasy.rastereasy:

shp2raster(shapefile_path, dest_name, attribute='code', resolution=10, nodata=0)
    Convert a shapefile to a GeoTIFF raster file.

    Parameters
    ----------
    shapefile_path : str
        Path to the input shapefile.
    dest_name : str
        Path to save the output raster file.
    attribute : str, optional
        Attribute field in the shapefile to assign values to each pixel.
        Default is 'code'.
    resolution : float, optional
        Spatial resolution of the output raster in meters/degrees.
        Default is 10.
    nodata : int or float, optional
        Value to assign to areas outside the shapes.
        Default is 0.

    Notes
    -----
    - The `shapefile_path` should be the full path to a shapefile (.shp) on the disk.
    - To get the attributes of a shapefile, see :meth:`shpfiles.get_shapefile_attributes`
    - The output raster will be written in GeoTIFF format to the path specified by `dest_name`.

    Examples
    --------
    >>> shpfiles.shp2raster("landcover.shp", "landcover.tif", attribute='landtype', resolution=5)

Help on function shp2geoim in module rastereasy.rastereasy:

shp2geoim(shapefile_path, attribute='code', resolution=10, nodata=0)
    Convert a shapefile to a Geoimage object.

    Parameters
    ----------
    shapefile_path : str
        Path to the input shapefile.
    attribute : str, optional
        Attribute field in the shapefile to assign values to each pixel.
        Default is 'code'.
    resolution : float, optional
        Spatial resolution of the output raster in meters/degrees.
        Default is 10.
    nodata : int or float, optional
        Value to assign to areas outside the shapes.
        Default is 0.

    Returns
    -------
    Geoimage
        A Geoimage object containing the rasterized data.

    Notes
    -----
    - The `shapefile_path` should be the full path to a shapefile (.shp) on the disk.
    - The `attribute` field will be assigned to each pixel in the rasterized Geoimage.
    - To get the attributes of a shapefile, see :meth:`shpfiles.get_shapefile_attributes`
    - The `resolution` sets the size of each pixel in the output image.

    Examples
    --------
    >>> geo_img = shpfiles.shp2geoim("landcover.shp", attribute='landtype', resolution=5)

2) Convert shapefile to tif

shapefile_path = './data/demo/data_shp/Roi_G5.shp'
rastereasy.shpfiles.shp2raster(shapefile_path,'./data/results/shp2raster/raster5m.tif',resolution=5)
rastereasy.shpfiles.shp2raster(shapefile_path,'./data/results/shp2raster/raster10m.tif',resolution=10)

2) Import as Geoimage

raster10m=rastereasy.Geoimage('./data/results/shp2raster/raster10m.tif')
raster5m=rastereasy.Geoimage('./data/results/shp2raster/raster5m.tif')
raster5m.info()
raster10m.info()
print(raster10m.unique())
print(raster5m.unique())
raster10m.visu()
raster5m.visu()
- Size of the image:
   - Rows (height): 3649
   - Cols (width): 5097
   - Bands: 1
- Spatial resolution: 5.000057261870794  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60953547, -2.57061505)
- Driver: GTiff
- Data type: int32
- Projection system: EPSG:32630
- Nodata: 0.0

- Given names for spectral bands: 
   {'1': 1}


- Size of the image:
   - Rows (height): 1824
   - Cols (width): 2548
   - Bands: 1
- Spatial resolution: 10.002076869605746  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60951283, -2.57058196)
- Driver: GTiff
- Data type: int32
- Projection system: EPSG:32630
- Nodata: 0.0

- Given names for spectral bands: 
   {'1': 1}


[0 1 2 3 4 5 6 7]
[0 1 2 3 4 5 6 7]
<Figure size 640x480 with 0 Axes>
../_images/d749633ac3de3effeb5f567ad0372896812bf0b2dc5e7069cb869af446651909.png
<Figure size 640x480 with 0 Axes>
../_images/6318b82e20c2aa6b4b4985a951e0237fd13c4d49dba849ac3840f786099ffd18.png

3) Convert shapefile to geoimage

georaster5=rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=5)
georaster10=rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=10)
georaster5.visu(colorbar=True)    
georaster10.visu(colorbar=True)
georaster5.save('./data/results/shp2raster/raster5m_fromgeoraster.tif')
georaster10.save('./data/results/shp2raster/raster10m_fromgeoraster.tif')
<Figure size 640x480 with 0 Axes>
../_images/75473141dd1870d0a6eed2de497cdf9dc8bd89e2602d72ee2eed0529dcdcfd5c.png
<Figure size 640x480 with 0 Axes>
../_images/e75cabf9b8e274c79a5ab3fecbc10bcec84ccb3072222d6970b63869b2e1e089.png
G5_B2=rastereasy.Geoimage('./data/demo/tostack/G5_B2.tif')
G5_B3=rastereasy.Geoimage('./data/demo/tostack/G5_B3.tif')
G5_B4=rastereasy.Geoimage('./data/demo/tostack/G5_B4.tif')
G5_B8=rastereasy.Geoimage('./data/demo/tostack/G5_B8.tif')
ROI_g5=rastereasy.Geoimage('./data/demo/data_shp/ROI_g5.tif')
ROI_g5.visu()
<Figure size 640x480 with 0 Axes>
../_images/16ba775abc0778a4e0a1e5f004a9aa1fc6ce8c69cd3ed797662dd48f6b2ab96a.png

4) Select interesting areas with extract_from_shapefile

1) Without modifying the image

Read a stack (for the example. im can be an image)

im=rastereasy.Geoimage('./data/demo/tostack/G5_B2.tif')
im_stack = im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B3.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B4.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B8.tif'),reformat_names=True)
im_stack.info()
- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}
keep_size=True
value=3
ime=im_stack.extract_from_shapefile(shapefile_path,value,keep_size=keep_size)
ime.info()
- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630
- Nodata: 0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}
(rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=im.resolution)==value).visu(title='shapefile = %d'%value)
if keep_size is True:
    ((ime-im)==0).visu(1,title='extracted areas')
else:
    ime,imc = rastereasy.extract_common_areas(ime,im,resolution=None)
    ((ime-imc)==0).visu(1,title='extracted areas')
<Figure size 640x480 with 0 Axes>
../_images/4af9fe9cab686685263c6224005af295b780448074808951f44cf57932662ea6.png
<Figure size 640x480 with 0 Axes>
../_images/aff771486b466ecac992ecd7e851093268c4966c4393057748650b55715d96ac.png

2) With modifying the image (option inplace=True)

keep_size=False
value=4

#Read a stack
im=rastereasy.Geoimage('./data/demo/tostack/G5_B2.tif')
im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B3.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B4.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B8.tif'),reformat_names=True, inplace=True)
imoriginal=im.copy()
imoriginal.info()

im.extract_from_shapefile(shapefile_path,value,keep_size=keep_size,inplace=True)
im.save('./data/results/shp2raster/extract4.tif')
im.info()
(rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=im.resolution)==value).visu(title='shapefile = %d'%value)
if keep_size is True:
    ((imoriginal-im)==0).visu(1,title='extracted areas')
else:
    ime,imc = rastereasy.extract_common_areas(imoriginal,im,resolution=None)
    ((ime-imc)==0).visu(1,title='extracted areas')
- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}


- Size of the image:
   - Rows (height): 1824
   - Cols (width): 2548
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60943270, -2.57056349)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630
- Nodata: 0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}
<Figure size 640x480 with 0 Axes>
../_images/80ec4546c0f8556e9c3a60168375296363921f987efc559f1e98de09d727febc.png
<Figure size 640x480 with 0 Axes>
../_images/16ed6c04e7c86bb9afa59928c7332659c8c4966495e57ebc9b8b8eefc75504a4.png

3) By having charged the shapefile

1) Without modifying the image

#Read a stack
shapefile_path = './data/demo/data_shp/Roi_G5.shp'

shp=rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=10)
im=rastereasy.Geoimage('./data/demo/tostack/G5_B2.tif')
im_stack = im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B3.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B4.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B8.tif'),reformat_names=True)
im_stack.info()
- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}
keep_size=True
value=3
ime=im_stack.extract_from_shapeimage(shp,value,keep_size=keep_size)
ime.save('./data/results/shp2raster//extract3.tif')
ime.info()
- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630
- Nodata: 0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}
(rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=im.resolution)==value).visu(title='shapefile = %d'%value)
if keep_size is True:
    ((ime-im)==0).visu(1,title='extracted areas')
else:
    ime,imc = rastereasy.extract_common_areas(ime,im,resolution=None)
    ((ime-imc)==0).visu(1,title='extracted areas')
<Figure size 640x480 with 0 Axes>
../_images/4af9fe9cab686685263c6224005af295b780448074808951f44cf57932662ea6.png
<Figure size 640x480 with 0 Axes>
../_images/aff771486b466ecac992ecd7e851093268c4966c4393057748650b55715d96ac.png

2) With modifying the image

shp=rastereasy.shpfiles.shp2geoim(shapefile_path,resolution=3)
keep_size=True
value=4

#Read a stack
im=rastereasy.Geoimage('./data/demo/tostack/G5_B2.tif')
im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B3.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B4.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.Geoimage('./data/demo/tostack/G5_B8.tif'),reformat_names=True, inplace=True)
imoriginal=im.copy()
imoriginal.info()

im.extract_from_shapeimage(shp,value,keep_size=keep_size,inplace=True)
im.save('./data/results/shp2raster//extract4.tif')
im.info()
(shp==value).visu(title='shapefile = %d'%value)
if keep_size is True:
    ((imoriginal-im)==0).visu(1,title='extracted areas')
else:
    ime,imc = rastereasy.extract_common_areas(imoriginal,im,resolution=None)
    ((ime-imc)==0).visu(1,title='extracted areas')
- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}


- Size of the image:
   - Rows (height): 2370
   - Cols (width): 3144
   - Bands: 4
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (47.60615500, -2.57780166)
- Driver: GTiff
- Data type: uint16
- Projection system: EPSG:32630
- Nodata: 0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4}
<Figure size 640x480 with 0 Axes>
../_images/df2ac9d6fe80e8c36cd745ee81c2d4d71df8e232880eed9ae25d6146f23afb63.png
<Figure size 640x480 with 0 Axes>
../_images/f8ef5abc182859f56c5fea8228c45c78fc1278325de8074d6006cc5ab35efbb7.png