Rasters

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

rasters

May 2, 2024

[47]: import rasterio


import os

[48]: path = os.getcwd()+ '\\dados/cbers.tif'

[16]: os.getcwd()

[16]: 'C:\\Users\\Aurora\\Desktop\\Gabriel\\PYTHON\\Jupyter_PyGis'

[49]: raster_base = rasterio.open(path)

[26]: raster_base.name

[26]: 'C:/Users/Aurora/Desktop/Gabriel/PYTHON/Jupyter_PyGis/dados/cbers.tif'

[27]: raster_base.mode

[27]: 'r'

[50]: raster_base.count

[50]: 4

[28]: raster_base.closed

[28]: False

[51]: raster_base.close()

[59]: with rasterio.open(path, 'r') as dst:


band1 = dst.read(1)
band2 = dst.read(2)
band3 = dst.read(3)
band4 = dst.read(4)
profile = dst.profile

[53]: dst.closed

1
[53]: True

[60]: profile

[60]: {'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': 1929, 'height':
1943, 'count': 4, 'crs': CRS.from_epsg(32721), 'transform': Affine(8.0, 0.0,
734856.0,
0.0, -8.0, 7189156.0), 'blockysize': 1, 'tiled': False, 'interleave':
'pixel'}

[65]: with rasterio.open('exported.tif','w',


driver = 'Gtiff',
dtype = rasterio.float32,
height = 1943,
width = 1929,
count = 1,
crs = '+proj=utm +zone=21 +south +datum=WGS84 +units=m␣
↪+no_defs',

transform = (8.0, 0.0, 734856.0, 0.0, -8.0, 7189156.0)


) as dst:
dst.write(band1,1)

[62]: profile['count'] = 1

[63]: profile

[63]: {'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': 1929, 'height':
1943, 'count': 1, 'crs': CRS.from_epsg(32721), 'transform': Affine(8.0, 0.0,
734856.0,
0.0, -8.0, 7189156.0), 'blockysize': 1, 'tiled': False, 'interleave':
'pixel'}

[67]: with rasterio.open('exported1.tif','w', **profile) as dst:


dst.write(band1,1)

[68]: import matplotlib.pyplot as plt


from rasterio.plot import show

[93]: plt.figure(figsize=(5,5))
show(band1, cmap='gray', transform = profile['transform'])

2
[93]: <Axes: >

[89]: fig, (ax1,ax2,ax3) = plt.subplots(1,3, figsize=(21,7))


show(band1, ax=ax1, cmap='gray', transform = profile['transform'], title='Banda␣
↪01')

show(band2, ax=ax2, cmap='Greens', transform = profile['transform'],␣


↪title='Banda 02')

show(band3, ax=ax3, cmap='Reds', transform = profile['transform'], title='Banda␣


↪03')

[89]: <Axes: title={'center': 'Banda 03'}>

3
[92]: result = band1*band2
show(result, cmap='grey', transform = profile['transform'], title='Result')

[92]: <Axes: title={'center': 'Result'}>

[94]: import numpy as np

4
[95]: # Função para normalizar valor
def normalize(array):
'''Normaliza variavel numpy em escala de 0.0 a 1.0'''
array_min, array_max = array.min(), array.max()
return((array - array_min)/(array_max - array_min))

[98]: nir = normalize(band4)


r = normalize(band3)
g = normalize(band2)
b = normalize(band1)

[103]: rgb = np.dstack((r,g,b))


plt.figure(figsize=(21,9))
plt.imshow(rgb)

[103]: <matplotlib.image.AxesImage at 0x22aeb9f8380>

5
[107]: plt.figure(figsize=(21,9))
show([r,g,b], transform=profile['transform'])

[107]: <Axes: >

[30]: import rasterio


import os
import matplotlib.pyplot as plt
from rasterio.plot import show

def normalize(array):

6
'''Normaliza variavel numpy em escala de 0.0 a 1.0'''
array_min, array_max = array.min(), array.max()

return((array - array_min)/(array_max - array_min))

def cbers_raster(raster):

with rasterio.open(raster, 'r') as dst:


band1 = dst.read(1)
band2 = dst.read(2)
band3 = dst.read(3)
band4 = dst.read(4)
profile = dst.profile

return band1, band2, band3, band4, profile

# C:\\Users\\Aurora\\Desktop\\Gabriel\\PYTHON\\Jupyter_PyGis\\dados\\cbers.tif␣
↪Pois o notebook não está na mesma pasta

def cbers_index():
band1, band2, band3, band4, profile = cbers_raster(raster)

# Normalizar os Dados
ir = normalize(band4)
r = normalize(band3)
g = normalize(band2)
b = normalize(band1)

# Criando Indíces
ndvi = (ir-r)/(ir+r) # Vigor da Vegetação
ndwi = (g-ir)/(g+ir) # Massas D'água

return ndvi, ndwi, profile

def plot_cbers():
ndvi, ndwi, profile = cbers_index()

fig, (ax1,ax2) = plt.subplots(1,2, figsize=(21,9))


show(ndvi, ax=ax1, title='NDVI', transform=profile['transform'],␣
↪cmap='Greens')

show(ndwi, ax=ax2, title='NDWI', transform=profile['transform'],␣


↪cmap='Blues')

return

7
[31]: raster = 'C:
↪\\Users\\Aurora\\Desktop\\Gabriel\\PYTHON\\Jupyter_PyGis\\dados\\cbers.tif'

plot_cbers()

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

You might also like