Skip to content

Commit 11d316e

Browse files
Merge pull request #210 from geo-engine/skip-empty-times
Skip-empty-times
2 parents 40f799b + 42e3081 commit 11d316e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

geoengine/raster.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ def spatial_partition(self) -> gety.SpatialPartition2D:
213213
def spatial_resolution(self) -> gety.SpatialResolution:
214214
return self.geo_transform.spatial_resolution()
215215

216+
def is_empty(self) -> bool:
217+
''' Returns true if the tile is empty'''
218+
num_pixels = self.size_x * self.size_y
219+
num_nulls = self.data.null_count
220+
return num_pixels == num_nulls
221+
216222
@staticmethod
217223
def from_ge_record_batch(record_batch: pa.RecordBatch) -> RasterTile2D:
218224
'''Create a RasterTile2D from an Arrow record batch recieved from the Geo Engine'''

geoengine/raster_workflow_rio_writer.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,37 @@ def __create_new_dataset(self, query: QueryRectangle):
168168
**self.rio_kwargs
169169
)
170170

171+
for i, b in enumerate(self.bands, start=1):
172+
b_n = b.name
173+
b_m = str(b.measurement)
174+
rio_dataset.update_tags(i, band_name=b_n, band_measurement=b_m)
175+
171176
self.current_dataset = rio_dataset
172177

173-
async def query_and_write(self, query: QueryRectangle):
174-
''' Query the raster workflow and write the tiles to the dataset.'''
178+
async def query_and_write(self, query: QueryRectangle, skip_empty_times=True):
179+
'''
180+
Query the raster workflow and write the resulting tiles to a GDAL dataset per timeslice.
181+
182+
:param query: The QueryRectangle to write to GDAL dataset(s)
183+
:param skip_empty_times: Skip timeslices where all pixels are empty/nodata
184+
'''
175185

176186
self.create_tiling_geo_transform_width_height(query)
177187

178-
assert self.workflow is not None, "The workflow must be set"
188+
assert self.bands is not None, "The bands must be set"
189+
bands = list(range(0, len(self.bands)))
179190

191+
assert self.workflow is not None, "The workflow must be set"
180192
try:
181-
async for tile in self.workflow.raster_stream(query):
193+
async for tile in self.workflow.raster_stream(query, bands=bands):
182194
if self.current_time != tile.time:
183195
self.close_current_dataset()
184196
self.current_time = tile.time
197+
198+
if tile.is_empty() and skip_empty_times:
199+
continue
200+
201+
if self.current_dataset is None:
185202
self.__create_new_dataset(query)
186203

187204
assert self.current_time == tile.time, "The time of the current dataset does not match the tile"

0 commit comments

Comments
 (0)