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:

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:

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)

3) Import as Geoimage

raster10m=rastereasy.open('./data/results/shp2raster/raster10m.tif')
raster5m=rastereasy.open('./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]
../_images/38643017c562789f89c077cfb955f94b2d6913d70e54de55f1ecaf8e42fc77f4.png ../_images/78324ff8f066872b6af4b3e5c8f956880ab426d1a4a18a69afa23e347668746e.png

4) 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')
../_images/9ca605828ad7346effa638a5461afd3e64b8d0b730663ec4f9338fa29e5a5923.png ../_images/00297f8f056f018d80eb2af478958dc39af79f79e6a74539f041430145c73212.png
G5_B2=rastereasy.open('./data/demo/tostack/G5_B2.tif')
G5_B3=rastereasy.open('./data/demo/tostack/G5_B3.tif')
G5_B4=rastereasy.open('./data/demo/tostack/G5_B4.tif')
G5_B8=rastereasy.open('./data/demo/tostack/G5_B8.tif')
ROI_g5=rastereasy.open('./data/demo/data_shp/ROI_g5.tif')
ROI_g5.visu()
../_images/0a91b55b3b749eafcf7f3a08ccbe309a4c800af34a14a5c7c17a680188e8bd2a.png

5) 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.open('./data/demo/tostack/G5_B2.tif')
im_stack = im.stack(rastereasy.open('./data/demo/tostack/G5_B3.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.open('./data/demo/tostack/G5_B4.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.open('./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')
../_images/cfd54e88599d77fe76bbec261e7a863f02445dec7b57662750ddaa9e57dc3c96.png ../_images/6a2484e0789d47ccc0733ac624c60f23f900c2b90f401a704f6b00726d92f1bb.png

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

keep_size=False
value=4

#Read a stack
im=rastereasy.open('./data/demo/tostack/G5_B2.tif')
im.stack(rastereasy.open('./data/demo/tostack/G5_B3.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.open('./data/demo/tostack/G5_B4.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.open('./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}
../_images/2229318945a2d85a4fe47c64ad45dc53692b2ccadc1bf9168b320405c2b6d962.png ../_images/6ace665f85a0a2acda57e2167fc32071f3511071f5cfa35f1e13732a45413b8a.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.open('./data/demo/tostack/G5_B2.tif')
im_stack = im.stack(rastereasy.open('./data/demo/tostack/G5_B3.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.open('./data/demo/tostack/G5_B4.tif'),reformat_names=True)
im_stack = im_stack.stack(rastereasy.open('./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')
../_images/cfd54e88599d77fe76bbec261e7a863f02445dec7b57662750ddaa9e57dc3c96.png ../_images/6a2484e0789d47ccc0733ac624c60f23f900c2b90f401a704f6b00726d92f1bb.png

2) With modifying the image

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

#Read a stack
im=rastereasy.open('./data/demo/tostack/G5_B2.tif')
im.stack(rastereasy.open('./data/demo/tostack/G5_B3.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.open('./data/demo/tostack/G5_B4.tif'),reformat_names=True, inplace=True)
im.stack(rastereasy.open('./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}
../_images/21895ed05e2db900fc72b484fd09bd50310468171e622ddc30a0e8652bbbc105.png ../_images/b0bf34cd0e71ae3e1d25048d4220742e47fa6ed68e2d55f0a966d23654fa0588.png