Skip to content

Commit

Permalink
Merge pull request #25 from leftfield-geospatial/fix_masking
Browse files Browse the repository at this point in the history
Fix masking
  • Loading branch information
dugalh authored Jul 12, 2024
2 parents 1bf7cbd + 7fc00ed commit d8650f4
Show file tree
Hide file tree
Showing 12 changed files with 911 additions and 473 deletions.
5 changes: 4 additions & 1 deletion geedim/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@


class BaseImage:
_float_nodata = float('nan')
_float_nodata = float('-inf')
_desc_width = 50
_default_resampling = ResamplingMethod.near
_ee_max_tile_size = 32
Expand Down Expand Up @@ -984,6 +984,9 @@ def download(

def download_tile(tile):
"""Download a tile and write into the destination GeoTIFF."""
# Note: due to an Earth Engine bug (https://issuetracker.google.com/issues/350528377), tile_array is
# float64 for uint32 and int64 exp_image types. The tile_array nodata value is as it should be
# though, so conversion to the exp_image / GeoTIFF type happens ok below.
tile_array = tile.download(session=session, bar=bar)
with out_lock:
out_ds.write(tile_array, window=tile.window)
Expand Down
194 changes: 124 additions & 70 deletions geedim/mask.py

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions geedim/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ def _download_to_array(self, url: str, session: requests.Session = None, bar: tq
with utils.suppress_rio_logs(), env, MemoryFile(gtiff_bytes) as mem_file:
with mem_file.open() as ds:
array = ds.read()
if (array.dtype == np.dtype('float32')) or (array.dtype == np.dtype('float64')):
# GEE sets nodata to -inf for float data types, (but does not populate the nodata field).
# rasterio won't allow nodata=-inf, so this is a workaround to change nodata to nan at
# source.
array[np.isinf(array)] = np.nan
return array

def download(
Expand Down
24 changes: 6 additions & 18 deletions geedim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import time
from contextlib import contextmanager
from threading import Thread
from typing import Tuple, Optional
from typing import Optional, Tuple

import ee
import numpy as np
Expand Down Expand Up @@ -164,7 +164,7 @@ def get_bounds(filename: pathlib.Path, expand: float = 5):
return src_bbox_wgs84


def get_projection(image, min_scale=True):
def get_projection(image: ee.Image, min_scale: bool = True) -> ee.Projection:
"""
Get the min/max scale projection of image bands. Server side - no calls to getInfo().
Adapted from from https://github.com/gee-community/gee_tools, MIT license.
Expand All @@ -186,23 +186,11 @@ def get_projection(image, min_scale=True):
raise TypeError('image is not an instance of ee.Image')

bands = image.bandNames()
scales = bands.map(lambda band: image.select(ee.String(band)).projection().nominalScale())
projs = bands.map(lambda band: image.select(ee.String(band)).projection())
projs = projs.sort(scales)

compare = ee.Number.lte if min_scale else ee.Number.gte
init_proj = image.select(0).projection()

def compare_scale(name, prev_proj):
"""Server side comparison of band scales"""
prev_proj = ee.Projection(prev_proj)
prev_scale = prev_proj.nominalScale()

curr_proj = image.select([name]).projection()
curr_scale = ee.Number(curr_proj.nominalScale())

condition = compare(curr_scale, prev_scale)
comp_proj = ee.Algorithms.If(condition, curr_proj, prev_proj)
return ee.Projection(comp_proj)

return ee.Projection(bands.iterate(compare_scale, init_proj))
return ee.Projection(projs.get(0) if min_scale else projs.get(-1))


class Spinner(Thread):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ readme = 'README.rst'
requires-python = '>=3.6'
dependencies = [
'numpy>=1.19',
'rasterio>=1.1',
'rasterio>=1.3',
'click>=8',
'tqdm>=4.6',
'earthengine-api>=0.1.2',
Expand Down
Loading

0 comments on commit d8650f4

Please sign in to comment.