-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
59 lines (53 loc) · 1.85 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import rasterio
from rasterio.warp import reproject, Resampling
import requests
import json
def process_raster(data, nodata):
data = data.astype('float32')
data[data == nodata] = np.nan
return data
def read_raster(file_path):
with rasterio.open(file_path) as src:
data = src.read(1)
profile = src.profile
nodata = src.nodata
transform = src.transform
return data, profile, nodata, transform
def read_raster_files(raster_paths):
rasters_data = []
for path in raster_paths:
data, profile, nodata, transform = read_raster(path)
rasters_data.append((data, profile, nodata, transform, path))
return rasters_data
def resample_raster(src_data, src_transform, src_crs, dst_shape, dst_transform, dst_crs):
resampled_data = np.empty(dst_shape, dtype='float32')
reproject(
source=src_data,
destination=resampled_data,
src_transform=src_transform,
src_crs=src_crs,
dst_transform=dst_transform,
dst_crs=dst_crs,
resampling=Resampling.nearest
)
return resampled_data
def predict(scoring_uri, api_key, input_data):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
response = requests.post(scoring_uri, data=json.dumps(input_data), headers=headers)
if response.status_code != 200:
raise Exception(f"Request failed with status code {response.status_code}: {response.text}")
return response.json()
def process_predictions(predictions, raster_paths):
# Process the predictions to create a map or any other output
results = []
for i, prediction in enumerate(predictions):
raster_path = raster_paths[i]
results.append({
'raster_path': raster_path,
'prediction': prediction
})
return results