Last active
January 29, 2024 05:27
-
-
Save krishnaglodha/c60c9b2c04a68f29b0f5a20ad29dc934 to your computer and use it in GitHub Desktop.
Getting Satellite date based on stac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GDAL==3.4.3 | |
odc-stac==0.3.8 | |
pystac-client==0.7.5 | |
rasterio==1.3.9 | |
numpy==1.26.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pystac_client import Client | |
from odc.stac import load | |
import odc.geo | |
from datetime import date, timedelta | |
from osgeo import gdal | |
import rasterio as rio | |
from rasterio import mask as msk | |
today = date.today() # get today's date | |
today_string = today.strftime("%Y-%m-%d") # convert in string | |
collection = "sentinel-2-l2a" # Satellite name -> Checkout other available satellite IDs at https://earth-search.aws.element84.com/v1/collections | |
feature_geometry = { | |
"coordinates": [ | |
[ | |
[ | |
73.05683851936746, | |
19.33321254579718 | |
], | |
[ | |
73.05664412469514, | |
19.33253384375915 | |
], | |
[ | |
73.05713011137598, | |
19.33198354273874 | |
], | |
[ | |
73.05714955084323, | |
19.3329832548836 | |
], | |
[ | |
73.05683851936746, | |
19.33321254579718 | |
] | |
] | |
], | |
"type": "Polygon" | |
} # GeoJSON feature geometry | |
client = Client.open("https://earth-search.aws.element84.com/v1") # Pystac client init | |
try: | |
search = client.search(collections=[collection], intersects=feature_geometry ,datetime=today_string) # Find all data for today's date | |
data = load(search.items() ,geopolygon=feature_geometry, groupby="solar_day", chunks={}) # group collection based on day | |
data["ndvi"] = (data.nir - data.red ) / (data.red + data.nir) # calculate ndvi | |
odc.geo.xr.write_cog(data['ndvi'],fname=f"ndvi_old.tiff", overwrite=True) # write ndvi to tiff in original CRS | |
# Path to your input GeoTIFF file | |
input_file = f"ndvi_old.tiff" | |
# Path to save the reprojected GeoTIFF file | |
output_file = f"ndvi_reprojected.tiff" | |
# Define the target CRS (EPSG:4326) | |
dst_crs = 'EPSG:4326' | |
input_raster = gdal.Open(input_file) | |
output_raster = output_file | |
warp = gdal.Warp(output_raster, input_raster, dstSRS=dst_crs) # Convert tiff to EPSG:4326 | |
raster = rio.open(output_file) | |
with raster as src: | |
out_image, out_transform = msk.mask( | |
src, [feature_geometry], crop=True | |
) # clip the tiff as per geometry | |
out_meta = src.meta.copy() | |
out_meta.update( | |
{ | |
"driver": "GTiff", | |
"height": out_image.shape[1], | |
"width": out_image.shape[2], | |
"transform": out_transform, | |
"nodata": 0, | |
} | |
) | |
farmpath = (f"ndvi.tiff") | |
with rio.open(farmpath, "w", **out_meta) as dest: | |
dest.write(out_image) | |
dest.close() # write clipped tiff to ndvi.tiff | |
except: | |
print('no data found for the date') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment