This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
251 lines (232 loc) · 7.13 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import asyncio
import warnings
from collections import OrderedDict
from datetime import date, datetime
from typing import Iterable, List
import environ
import folium
import numpy as np
import pandas as pd
import rasterio
from google.protobuf import timestamp_pb2
from grpclib.client import Channel
from grpclib.config import Configuration
from rasterio.enums import Resampling
from rasterio.io import MemoryFile
from shapely import wkt
from shapely.geometry import mapping
from tqdm import tqdm
from snapearth.api.v1.database_grpc import DatabaseProductServiceStub
from snapearth.api.v1.database_pb2 import ListSegmentationRequest, SegmentationResponse
# Europe bounding box
EUROPE_COORDINATES = wkt.loads(
"POLYGON((-10.61 71.16, 44.85 71.16, 44.85 35.97, -10.61 35.97, -10.61 71.16))",
)
MAP_CENTER = mapping(EUROPE_COORDINATES.centroid)
CATEGORY_TO_RGB = OrderedDict(
{
0: (255, 255, 255),
111: (230, 0, 77),
112: (255, 0, 0),
121: (204, 77, 242),
122: (204, 0, 0),
123: (230, 204, 204),
124: (230, 204, 230),
131: (166, 0, 204),
132: (166, 77, 0),
133: (255, 77, 255),
141: (255, 166, 255),
142: (255, 230, 255),
211: (255, 255, 168),
212: (255, 255, 0),
213: (230, 230, 0),
221: (230, 128, 0),
222: (242, 166, 77),
223: (230, 166, 0),
231: (230, 230, 77),
241: (255, 230, 166),
242: (255, 230, 77),
243: (230, 204, 77),
244: (242, 204, 166),
311: (128, 255, 0),
312: (0, 166, 0),
313: (77, 255, 0),
321: (204, 242, 77),
322: (166, 255, 128),
323: (166, 230, 77),
324: (166, 242, 0),
331: (230, 230, 230),
332: (204, 204, 204),
333: (204, 255, 204),
334: (0, 0, 0),
335: (166, 230, 204), # Not used for classification
411: (166, 166, 255),
412: (77, 77, 255),
421: (204, 204, 255),
422: (230, 230, 255),
423: (166, 166, 230),
511: (0, 204, 242),
512: (128, 242, 230),
521: (0, 255, 166),
522: (166, 255, 230),
523: (230, 242, 255),
},
)
@environ.config(prefix="SNAPEARTH")
class DemoConfig:
@environ.config
class GRPC:
host: str = environ.var()
port: int = environ.var(converter=int)
max_receive_message_length: int = environ.var(converter=int)
max_send_message_length: int = environ.var(converter=int)
keepalive_timeout_ms: int = environ.var(converter=int)
use_ssl: bool = environ.bool_var()
max_results: int = environ.var(default=1, converter=int)
grpc: GRPC = environ.group(GRPC)
async def request_earthsignature(
host: str,
port: int,
use_ssl: bool,
geom: str,
start_date: date,
end_date: date,
product_ids: List[str],
categories: List[str],
n_results: int,
) -> Iterable[SegmentationResponse]:
min_time = datetime.min.time()
config = Configuration(
http2_connection_window_size=2000000000,
http2_stream_window_size=2000000000,
)
async with Channel(
host=host,
port=port,
ssl=use_ssl,
config=config,
) as channel:
stub = DatabaseProductServiceStub(channel)
geom = geom if not geom == "" else geom
request = ListSegmentationRequest(
wkt=geom,
start_date=timestamp_pb2.Timestamp().FromDatetime(
datetime.combine(start_date, min_time),
),
end_date=timestamp_pb2.Timestamp().FromDatetime(
datetime.combine(end_date, min_time),
),
product_ids=product_ids,
categories=categories,
n_results=n_results,
)
return await stub.ListSegmentation(request)
def read_inmemory(segmentation, width=None, height=None, resampling=None, dtype=None):
with MemoryFile(segmentation) as memfile:
with memfile.open(
mode="r",
count=1,
width=width,
height=height,
resampling=resampling,
dtype=dtype,
) as dataset:
return dataset.read()
def segmentation_to_image(segmentation, cloud_mask, out_dtype=np.ubyte):
array = read_inmemory(segmentation, dtype=rasterio.uint16).squeeze()
# cloud_array = read_inmemory(
# cloud_mask,
# height=array.shape[0],
# width=array.shape[1],
# resampling=Resampling.nearest,
# dtype=rasterio.ubyte,
# ).squeeze()
categories = np.unique(array)
image = np.empty((array.shape[0], array.shape[1], 3), dtype=out_dtype)
for category in categories:
mask = array == category
try:
image[mask] = CATEGORY_TO_RGB[category]
except KeyError:
warnings.warn(f"Category {category} not found")
image[mask] = (255, 255, 255)
# image[cloud_array] = (255, 255, 255) # Remove cloud areas
return image
def plot_responses(
host,
port,
use_ssl,
geom,
start_date,
end_date,
product_ids,
categories,
n_results,
):
product_ids = product_ids.value.split(",") if product_ids.value else None
categories = categories.value.split(",") if categories.value else None
responses = asyncio.run(
request_earthsignature(
host,
port,
use_ssl,
geom.value,
start_date.value,
end_date.value,
product_ids,
categories,
n_results.value,
),
)
map_ = folium.Map(
location=MAP_CENTER["coordinates"][::-1],
zoom_start=3,
crs="EPSG3857",
)
data = []
for response in tqdm(responses):
geom = wkt.loads(response.wkt)
folium.GeoJson(geom).add_to(map_)
centroid = mapping(geom.centroid)
bounds = geom.bounds
bounds = (
(bounds[1], bounds[0]),
(bounds[3], bounds[2]),
)
image = segmentation_to_image(response.segmentation, response.cloud_mask)
folium.raster_layers.ImageOverlay(
image,
bounds=bounds,
name=response.product_id,
overlay=True,
control=False,
opacity=0.8,
).add_to(map_)
names = [
"creation_date",
"publication_date",
"product_id",
"quicklook_url",
"cloud_cover",
"browse_url",
"download_url",
]
responses = [
response.creation_date.ToDatetime(),
response.publication_date.ToDatetime(),
response.product_id,
response.quicklook,
response.cloud_cover,
response.browse_url,
response.download_url,
]
df = pd.DataFrame({"names": names, "values": responses})
html = df.to_html(
classes="table table-striped table-hover table-condensed table-responsive",
)
folium.Marker(
location=centroid["coordinates"][::-1],
tooltip=f"{response.product_id}",
popup=folium.Popup(html),
).add_to(map_)
return map_