Basic Usage
Install¶
Installation is easy just grab it form pip
In [ ]:
Copied!
!pip install virtughan==0.7.0
!pip install virtughan==0.7.0
Setup¶
Define the parameters for your computation
Use this for visual image timeseries
formula="band1"
band1="visual"
band2=None
operation=None
timeseries=True
In [9]:
Copied!
# Define the bounding box
bbox = [-17.942562103271488,28.569682805210718,-17.839050292968754,28.64689695054486] # cumbre vieja volcano spain area
# Define the parameters
start_date = "2021-10-01"
end_date = "2021-12-30"
cloud_cover = 10
formula = "(band1-band2)/(band1+band2)" # NBR
band1 = "nir08"
band2 = "swir22"
operation = "max"
output_dir = "virtughan_output"
timeseries = True
workers = 1 # no of parallel workers
smart_filter=False
# Define the bounding box
bbox = [-17.942562103271488,28.569682805210718,-17.839050292968754,28.64689695054486] # cumbre vieja volcano spain area
# Define the parameters
start_date = "2021-10-01"
end_date = "2021-12-30"
cloud_cover = 10
formula = "(band1-band2)/(band1+band2)" # NBR
band1 = "nir08"
band2 = "swir22"
operation = "max"
output_dir = "virtughan_output"
timeseries = True
workers = 1 # no of parallel workers
smart_filter=False
Cleanup¶
Lets clear if there are previous output in the dir
In [10]:
Copied!
import shutil , os
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
import shutil , os
if os.path.exists(output_dir):
shutil.rmtree(output_dir)
Example compute on time dimension¶
You can run computation now , You can visualize the results or download them from the files directly after computation is done
In [11]:
Copied!
from virtughan.engine import VirtughanProcessor
processor = VirtughanProcessor(
bbox,
start_date,
end_date,
cloud_cover,
formula,
band1,
band2,
operation,
timeseries,
output_dir,
workers=workers,
smart_filter=smart_filter,
cmap="RdYlGn"
)
processor.compute()
from virtughan.engine import VirtughanProcessor
processor = VirtughanProcessor(
bbox,
start_date,
end_date,
cloud_cover,
formula,
band1,
band2,
operation,
timeseries,
output_dir,
workers=workers,
smart_filter=smart_filter,
cmap="RdYlGn"
)
processor.compute()
Engine starting... Searching STAC ..... Total scenes found: 11 Scenes covering input area: 11 Scenes after removing overlaps: 6 Computing Band Calculation: 100%|██████████| 6/6 [00:07<00:00, 1.27s/it] Aggregating results... Saving aggregated result with colormap... Creating GIF and zipping TIFF files... Saved timeseries GIF to virtughan_output/output.gif Saved intermediate images ZIP to virtughan_output/tiff_files.zip
In [12]:
Copied!
from IPython.display import Image
Image(open(os.path.join(output_dir,'output.gif'),'rb').read())
from IPython.display import Image
Image(open(os.path.join(output_dir,'output.gif'),'rb').read())
Out[12]:
Example on the fly tile computation¶
Here computation will be done alike google earth engine , you zoom out do computation , zoom in do computation based on your formula and resolution will differ accordingly as resampling will happen
In [15]:
Copied!
import mercantile
from PIL import Image
from io import BytesIO
from virtughan.tile import TileProcessor
# Define the parameters
lat, lon = 28.28139, 83.91866
zoom_level = 12
x, y, z = mercantile.tile(lon, lat, zoom_level)
tile_processor = TileProcessor()
image_bytes, feature = await tile_processor.cached_generate_tile(
x=x,
y=y,
z=z,
start_date="2020-01-01",
end_date="2025-01-01",
cloud_cover=30,
band1="red",
band2="nir",
formula="(band2-band1)/(band2+band1)",
colormap_str="RdYlGn",
)
image = Image.open(BytesIO(image_bytes))
print(f"Tile: {x}_{y}_{z}")
print(f"Date: {feature['properties']['datetime']}")
print(f"Cloud Cover: {feature['properties']['eo:cloud_cover']}%")
image.save(f'tile_{x}_{y}_{z}.png')
import mercantile
from PIL import Image
from io import BytesIO
from virtughan.tile import TileProcessor
# Define the parameters
lat, lon = 28.28139, 83.91866
zoom_level = 12
x, y, z = mercantile.tile(lon, lat, zoom_level)
tile_processor = TileProcessor()
image_bytes, feature = await tile_processor.cached_generate_tile(
x=x,
y=y,
z=z,
start_date="2020-01-01",
end_date="2025-01-01",
cloud_cover=30,
band1="red",
band2="nir",
formula="(band2-band1)/(band2+band1)",
colormap_str="RdYlGn",
)
image = Image.open(BytesIO(image_bytes))
print(f"Tile: {x}_{y}_{z}")
print(f"Date: {feature['properties']['datetime']}")
print(f"Cloud Cover: {feature['properties']['eo:cloud_cover']}%")
image.save(f'tile_{x}_{y}_{z}.png')
Tile: 3002_1712_12 Date: 2024-12-30T05:10:56.883000Z Cloud Cover: 1.140427%
In [16]:
Copied!
from IPython.display import Image as display_image
display_image(f'tile_{x}_{y}_{z}.png')
from IPython.display import Image as display_image
display_image(f'tile_{x}_{y}_{z}.png')
Out[16]:
Extract Sentinel-2 Images for your AOI¶
This example showcase how you can select the band you want and extract sentinel-2 images just for your area of interest
In [17]:
Copied!
import os
bbox = [83.84765625, 28.22697003891833, 83.935546875, 28.304380682962773]
start_date = "2024-12-15"
end_date = "2024-12-31"
cloud_cover = 30
bands_list = ["blue","green","red", "nir","swir16","swir22"] # order of the bands will result order in the resulting output
output_dir = "./sentinel_images"
workers = 1
os.makedirs(output_dir, exist_ok=True)
import os
bbox = [83.84765625, 28.22697003891833, 83.935546875, 28.304380682962773]
start_date = "2024-12-15"
end_date = "2024-12-31"
cloud_cover = 30
bands_list = ["blue","green","red", "nir","swir16","swir22"] # order of the bands will result order in the resulting output
output_dir = "./sentinel_images"
workers = 1
os.makedirs(output_dir, exist_ok=True)
In [18]:
Copied!
from virtughan.extract import ExtractProcessor, VALID_BANDS
print("Available bands: ", VALID_BANDS)
extractor = ExtractProcessor(
bbox,
start_date,
end_date,
cloud_cover,
bands_list,
output_dir,
workers=workers,
)
extractor.extract()
from virtughan.extract import ExtractProcessor, VALID_BANDS
print("Available bands: ", VALID_BANDS)
extractor = ExtractProcessor(
bbox,
start_date,
end_date,
cloud_cover,
bands_list,
output_dir,
workers=workers,
)
extractor.extract()
Available bands: {'red': 'Red - 10m', 'green': 'Green - 10m', 'blue': 'Blue - 10m', 'nir': 'NIR 1 - 10m', 'swir22': 'SWIR 2.2μm - 20m', 'rededge2': 'Red Edge 2 - 20m', 'rededge3': 'Red Edge 3 - 20m', 'rededge1': 'Red Edge 1 - 20m', 'swir16': 'SWIR 1.6μm - 20m', 'wvp': 'Water Vapour (WVP)', 'nir08': 'NIR 2 - 20m', 'aot': 'Aerosol optical thickness (AOT)', 'coastal': 'Coastal - 60m', 'nir09': 'NIR 3 - 60m'} Extracting bands... Total scenes found: 4 Scenes covering input area: 4 Scenes after removing overlaps: 4 Filter from : 2024-12-15 to : 2024-12-30 Selecting 1 image per 4 days Scenes after applying smart filter: 4 Extracting Bands: 0%| | 0/4 [00:00<?, ?it/s]Stacking Bands... Extracting Bands: 25%|██▌ | 1/4 [00:09<00:28, 9.67s/it]Stacking Bands... Extracting Bands: 50%|█████ | 2/4 [00:19<00:19, 9.55s/it]Stacking Bands... Extracting Bands: 75%|███████▌ | 3/4 [00:29<00:09, 9.78s/it]Stacking Bands... Extracting Bands: 100%|██████████| 4/4 [00:37<00:00, 9.28s/it]
In [18]:
Copied!