Commit 0e843de5 authored by Heisenberg5124's avatar Heisenberg5124
Browse files

Merge remote-tracking branch 'origin/backend' into frontend

parents dbab9ed0 15b0ccdd
This diff is collapsed.
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"coordinates":[[[9.16145374315991,48.78558930650067],[9.16145374315991,48.77109004651268],[9.18585591426276,48.77109004651268],[9.18585591426276,48.78558930650067],[9.16145374315991,48.78558930650067]]],"type":"Polygon"}}]}
\ No newline at end of file
# Preprocess AOI and Download Satellite Imagery from Microsoft Planetary Computer
# Requirements: pip install geopandas rasterio pystac-client planetary-computer requests
import geopandas as gpd
import rasterio
from rasterio.mask import mask
import json
import os
from pystac_client import Client
import planetary_computer as pc
import requests
import matplotlib.pyplot as plt
# --- CONFIG ---
AOI_PATH = "map.geojson" # Your AOI file (exported from geojson.io or Google Earth)
BUFFER_METERS = 200 # Buffer distance in meters
OUTPUT_DIR = "processed" # Output directory for processed files
COLLECTION = "sentinel-2-l2a" # or "landsat-8-c2-l2"
DATE_RANGE = [
("2016-06-01", "2016-08-31"), # Past
("2025-06-01", "2025-08-31") # Present
]
MAX_CLOUD = 10 # %
# --- STEP 1: Buffer AOI ---
def buffer_aoi(aoi_path, buffer_meters, output_path):
aoi = gpd.read_file(aoi_path)
aoi_buffered = aoi.to_crs(epsg=3857).buffer(buffer_meters).to_crs(aoi.crs)
aoi_buffered_gdf = gpd.GeoDataFrame(geometry=aoi_buffered)
aoi_buffered_gdf.to_file(output_path, driver="GeoJSON")
print(f"Buffered AOI saved to {output_path}")
return output_path
# --- STEP 2: Download Image from MPC ---
def download_best_image(aoi_geojson, collection, date_range, max_cloud, out_path):
# Load AOI geometry
with open(aoi_geojson) as f:
geojson = json.load(f)
geometry = geojson['features'][0]['geometry']
# Search MPC STAC
catalog = Client.open("https://planetarycomputer.microsoft.com/api/stac/v1")
search = catalog.search(
collections=[collection],
intersects=geometry,
datetime=f"{date_range[0]}/{date_range[1]}",
query={"eo:cloud_cover": {"lt": max_cloud}}
)
items = list(search.get_items())
if not items:
print(f"No images found for {date_range}")
return None
# Pick the least cloudy image
items.sort(key=lambda x: x.properties.get("eo:cloud_cover", 100))
item = items[0]
asset = item.assets["visual"] if "visual" in item.assets else list(item.assets.values())[0]
signed_href = pc.sign(asset.href)
# Download the image
print(f"Downloading {signed_href} ...")
r = requests.get(signed_href, stream=True)
with open(out_path, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Downloaded image to {out_path}")
return out_path
# --- STEP 3: Display Images Side by Side ---
def show_images_side_by_side(image_paths):
fig, axes = plt.subplots(1, 2, figsize=(16, 8))
for i, img_path in enumerate(image_paths):
with rasterio.open(img_path) as src:
img = src.read([1, 2, 3]) # RGB bands
img = img.transpose(1, 2, 0)
# Normalize for display
img = (img - img.min()) / (img.max() - img.min())
axes[i].imshow(img)
axes[i].set_title(f"Image {i+1}")
axes[i].axis('off')
plt.tight_layout()
plt.show()
if __name__ == "__main__":
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Download images only, skip buffer and clipping
image_paths = []
for i, drange in enumerate(DATE_RANGE):
out_img = os.path.join(OUTPUT_DIR, f"raw_{i+1}.tif")
img_path = download_best_image(AOI_PATH, COLLECTION, drange, MAX_CLOUD, out_img)
if img_path:
image_paths.append(img_path)
if len(image_paths) == 2:
show_images_side_by_side(image_paths)
print("\nAll done! You now have raw rasters in the 'processed' folder and can view them side by side.")
{
"type": "FeatureCollection",
"name": "map_buffered",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 9.166994209422283, 48.784091968852827 ], [ 9.178154201230427, 48.784091968852827 ], [ 9.178330301820953, 48.784086268546858 ], [ 9.178504706466526, 48.78406922252217 ], [ 9.178675735555073, 48.784040994929839 ], [ 9.178841741982971, 48.784001857598447 ], [ 9.179001127017576, 48.783952187416872 ], [ 9.179152355693873, 48.783892462705673 ], [ 9.179293971597041, 48.783823258611697 ], [ 9.179424610888514, 48.783745241570486 ], [ 9.179543015440506, 48.783659162889563 ], [ 9.179648044952451, 48.783565851514489 ], [ 9.179738687932733, 48.783466206047081 ], [ 9.179814071439905, 48.783361186092904 ], [ 9.179873469489584, 48.783251803021074 ], [ 9.179916310046078, 48.783139110225356 ], [ 9.179942180531409, 48.783024192980392 ], [ 9.179950831798665, 48.782908157990647 ], [ 9.179950831798665, 48.778299983780748 ], [ 9.179942180531409, 48.778183937869152 ], [ 9.179916310046078, 48.77806900927871 ], [ 9.179873469489584, 48.777956304846001 ], [ 9.179814071439905, 48.777846909994913 ], [ 9.179738687932733, 48.777741878282676 ], [ 9.179648044952451, 48.777642221252542 ], [ 9.179543015440506, 48.777548898690974 ], [ 9.179424610888514, 48.777462809382932 ], [ 9.179293971597041, 48.777384782454767 ], [ 9.179152355693873, 48.77731556938766 ], [ 9.179001127017576, 48.777255836778998 ], [ 9.178841741982971, 48.777206159921221 ], [ 9.178675735555073, 48.777167017260076 ], [ 9.178504706466526, 48.777138785785802 ], [ 9.178330301820953, 48.777121737401508 ], [ 9.178154201230427, 48.777116036303902 ], [ 9.166994209422283, 48.777116036303902 ], [ 9.166818108831755, 48.777121737401508 ], [ 9.166643704186182, 48.777138785785802 ], [ 9.166472675097637, 48.777167017260076 ], [ 9.166306668669737, 48.777206159921221 ], [ 9.166147283635134, 48.777255836778998 ], [ 9.165996054958837, 48.77731556938766 ], [ 9.16585443905567, 48.777384782454767 ], [ 9.165723799764194, 48.777462809382932 ], [ 9.165605395212204, 48.777548898690974 ], [ 9.165500365700257, 48.777642221252542 ], [ 9.165409722719977, 48.777741878282676 ], [ 9.165334339212803, 48.777846909994913 ], [ 9.165274941163126, 48.777956304846001 ], [ 9.165232100606632, 48.77806900927871 ], [ 9.165206230121299, 48.778183937869152 ], [ 9.165197578854045, 48.778299983780748 ], [ 9.165197578854045, 48.782908157990647 ], [ 9.165206230121299, 48.783024192980392 ], [ 9.165232100606632, 48.783139110225356 ], [ 9.165274941163126, 48.783251803021074 ], [ 9.165334339212803, 48.783361186092904 ], [ 9.165409722719977, 48.783466206047081 ], [ 9.165500365700257, 48.783565851514489 ], [ 9.165605395212204, 48.783659162889563 ], [ 9.165723799764194, 48.783745241570486 ], [ 9.16585443905567, 48.783823258611697 ], [ 9.165996054958837, 48.783892462705673 ], [ 9.166147283635134, 48.783952187416872 ], [ 9.166306668669737, 48.784001857598447 ], [ 9.166472675097637, 48.784040994929839 ], [ 9.166643704186182, 48.78406922252217 ], [ 9.166818108831755, 48.784086268546858 ], [ 9.166994209422283, 48.784091968852827 ] ] ] } }
]
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment