Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 635859242
  • Loading branch information
Google Earth Engine Authors authored and naschmitz committed May 21, 2024
1 parent e9cc342 commit 39688b0
Show file tree
Hide file tree
Showing 8 changed files with 3,393 additions and 61 deletions.
2 changes: 2 additions & 0 deletions python/ee/_cloud_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def request( # pylint: disable=invalid-name
except requests.exceptions.ChunkedEncodingError as encoding_error:
# This is not a one-to-one match, but it's close enough.
raise ConnectionError(encoding_error) from encoding_error
except requests.exceptions.Timeout as timeout_error:
raise TimeoutError(timeout_error) from timeout_error
headers = dict(response.headers)
headers['status'] = response.status_code
content = response.content
Expand Down
5 changes: 5 additions & 0 deletions python/ee/apitestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def InitializeApi(self, should_mock: bool = True):
ee.data.getDownloadId = self._MockDownloadUrl
ee.data.getThumbId = self._MockThumbUrl
ee.data.getTableDownloadId = self._MockTableDownload
# pylint: disable-next=protected-access
ee.deprecation._FetchDataCatalogStac = self._MockFetchDataCatalogStac
ee.Initialize(None, '')

# We are mocking the url here so the unit tests are happy.
Expand All @@ -96,6 +98,9 @@ def _MockTableDownload(self, params: Dict[str, Any]) -> Dict[str, str]:
self.last_table_call = {'url': '/table', 'data': params}
return {'docid': '5', 'token': '6'}

def _MockFetchDataCatalogStac(self) -> Dict[str, Any]:
return {}


def _GenerateCloudApiResource(mock_http: Any, raw: Any) -> discovery.Resource:
"""Returns a Cloud API resource for testing."""
Expand Down
2 changes: 1 addition & 1 deletion python/ee/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ def toBigQuery(
description: Human-readable name of the task.
table: The fully-qualifed BigQuery destination table with
"project_id.dataset_id.table_id" format.
overwrite: [Not yet supported.] Whether the existing table should be
overwrite: Whether the existing table should be
overwritten by the results of this export.
The `overwrite` and `append` parameters cannot be `true`
simultaneously. The export fails if the table already exists and both
Expand Down
23 changes: 13 additions & 10 deletions python/ee/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,11 +1256,12 @@ def getDownloadId(params: Dict[str, Any]) -> Dict[str, str]:
possible values:
image - The image to download.
- name: a base name to use when constructing filenames. Only applicable
when format is "ZIPPED_GEO_TIFF" (default) or filePerBand is true.
Defaults to the image id (or "download" for computed images) when
format is "ZIPPED_GEO_TIFF" or filePerBand is true, otherwise a
random character string is generated. Band names are appended when
filePerBand is true.
when format is "ZIPPED_GEO_TIFF" (default),
"ZIPPED_GEO_TIFF_PER_BAND", or filePerBand is true. Defaults to the
image id (or "download" for computed images) when format is
"ZIPPED_GEO_TIFF", "ZIPPED_GEO_TIFF_PER_BAND", or filePerBand is
true, otherwise a random character string is generated. Band names
are appended when filePerBand is true.
- bands: a description of the bands to download. Must be an array of
band names or an array of dictionaries, each with the
following keys:
Expand All @@ -1285,10 +1286,13 @@ def getDownloadId(params: Dict[str, Any]) -> Dict[str, str]:
and crs_transform are specified.
- filePerBand: whether to produce a separate GeoTIFF per band (boolean).
Defaults to true. If false, a single GeoTIFF is produced and all
band-level transformations will be ignored.
band-level transformations will be ignored. Note that this is
ignored if the format is "ZIPPED_GEO_TIFF" or
"ZIPPED_GEO_TIFF_PER_BAND".
- format: the download format. One of:
"ZIPPED_GEO_TIFF" (GeoTIFF file(s) wrapped in a zip file, default),
"GEO_TIFF" (GeoTIFF file), "NPY" (NumPy binary format).
"ZIPPED_GEO_TIFF" (GeoTIFF file wrapped in a zip file, default),
"ZIPPED_GEO_TIFF_PER_BAND" (Multiple GeoTIFF files wrapped in a
zip file), "GEO_TIFF" (GeoTIFF file), "NPY" (NumPy binary format).
If "GEO_TIFF" or "NPY", filePerBand and all band-level
transformations will be ignored. Loading a NumPy output results in
a structured array.
Expand Down Expand Up @@ -2102,8 +2106,7 @@ def setAssetAcl(assetId: str, aclUpdate: Union[str, Dict[str, Any]]) -> None:
Args:
assetId: The ID of the asset to set the ACL on.
aclUpdate: The updated ACL for the asset. Must be formatted like the
value returned by getAssetAcl but without "owners".
aclUpdate: The updated ACL.
"""
# The ACL may be a string by the time it gets to us. Sigh.
if isinstance(aclUpdate, str):
Expand Down
4 changes: 3 additions & 1 deletion python/ee/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ def _IssueAssetDeprecationWarning(asset: DeprecatedAsset) -> None:
)
removal_date = asset.removal_date
if removal_date:
formatted_date = removal_date.strftime('%B %-d, %Y')
# %d gives a zero-padded day. Remove the leading zero. %-d is incompatible
# with Windows.
formatted_date = removal_date.strftime('%B %d, %Y').replace(' 0', ' ')
warning = warning + f' by {formatted_date}.'
else:
warning = warning + '.'
Expand Down
67 changes: 36 additions & 31 deletions python/ee/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
from ee import geometry


def _parse_dimensions(dimensions: Any) -> Sequence[Any]:
"""Parses a dimensions specification into a one or two element list."""
if ee_types.isNumber(dimensions):
return [dimensions]
elif isinstance(dimensions, str):
# Unpack WIDTHxHEIGHT
return [int(x) for x in dimensions.split('x')]
elif isinstance(dimensions, (list, tuple)) and 1 <= len(dimensions) <= 2:
return dimensions

raise ee_exception.EEException(
'Invalid dimensions {}.'.format(dimensions))


class Image(element.Element):
"""An object to represent an Earth Engine image."""

Expand Down Expand Up @@ -105,6 +119,10 @@ def reset(cls) -> None:
apifunction.ApiFunction.clearApi(cls)
cls._initialized = False

@staticmethod
def name() -> str:
return 'Image'

# pylint: disable-next=useless-parent-delegation
def getInfo(self) -> Optional[Any]:
"""Fetch and return information about this image.
Expand All @@ -120,7 +138,7 @@ def getMapId(self, vis_params: Optional[Any] = None) -> Dict[str, Any]:
"""Fetch and return a map ID dictionary, suitable for use in a Map overlay.
Args:
vis_params: The visualization parameters. See ee.data.getMapId.
vis_params: The visualization parameters. See ee.data.getMapId.
Returns:
A map ID dictionary as described in ee.data.getMapId.
Expand Down Expand Up @@ -434,11 +452,12 @@ def getDownloadURL(self, params: Optional[Dict[str, Any]] = None) -> str:
params: An object containing download options with the following
possible values:
- name: a base name to use when constructing filenames. Only applicable
when format is "ZIPPED_GEO_TIFF" (default) or filePerBand is true.
Defaults to the image id (or "download" for computed images) when
format is "ZIPPED_GEO_TIFF" or filePerBand is true, otherwise a
random character string is generated. Band names are appended when
filePerBand is true.
when format is "ZIPPED_GEO_TIFF" (default),
"ZIPPED_GEO_TIFF_PER_BAND" or filePerBand is true. Defaults to the
image id (or "download" for computed images) when format is
"ZIPPED_GEO_TIFF", "ZIPPED_GEO_TIFF_PER_BAND", or filePerBand is
true, otherwise a random character string is generated. Band names
are appended when filePerBand is true.
- bands: a description of the bands to download. Must be an array of
band names or an array of dictionaries, each with the
following keys:
Expand All @@ -463,10 +482,13 @@ def getDownloadURL(self, params: Optional[Dict[str, Any]] = None) -> str:
and crs_transform are specified.
- filePerBand: whether to produce a separate GeoTIFF per band (boolean).
Defaults to true. If false, a single GeoTIFF is produced and all
band-level transformations will be ignored.
band-level transformations will be ignored. Note that this is
ignored if the format is "ZIPPED_GEO_TIFF" or
"ZIPPED_GEO_TIFF_PER_BAND".
- format: the download format. One of:
"ZIPPED_GEO_TIFF" (GeoTIFF file(s) wrapped in a zip file, default),
"GEO_TIFF" (GeoTIFF file), "NPY" (NumPy binary format).
"ZIPPED_GEO_TIFF" (GeoTIFF file wrapped in a zip file, default),
"ZIPPED_GEO_TIFF_PER_BAND" (Multiple GeoTIFF files wrapped in a
zip file), "GEO_TIFF" (GeoTIFF file), "NPY" (NumPy binary format).
If "GEO_TIFF" or "NPY", filePerBand and all band-level
transformations will be ignored. Loading a NumPy output results in
a structured array.
Expand Down Expand Up @@ -513,8 +535,8 @@ def getThumbURL(self, params: Optional[Dict[str, Any]] = None) -> str:
computed by proportional scaling.
region - (ee.Geometry, GeoJSON, list of numbers, list of points)
Geospatial region of the image to render. By default, the whole
image. If given a list of min lon, min lat, max lon, max lat,
a planar rectangle is created. If given a list of points a
image. If given a list of min lon, min lat, max lon, max lat,
a planar rectangle is created. If given a list of points a
planar polygon is created.
format - (string) Either 'png' or 'jpg'.
Expand Down Expand Up @@ -672,7 +694,7 @@ def expression(self, expression: Any, map_: Optional[Any] = None) -> Image:
accessed like image.band_name or image[0].
Both b() and image[] allow multiple arguments, to specify multiple bands,
such as b(1, 'name', 3). Calling b() with no arguments, or using a variable
such as b(1, 'name', 3). Calling b() with no arguments, or using a variable
by itself, returns all bands of the image.
Args:
Expand Down Expand Up @@ -730,6 +752,7 @@ def clip(self, clip_geometry: Any) -> Image:
clip_geometry = geometry.Geometry(clip_geometry)
except ee_exception.EEException:
pass # Not an ee.Geometry or GeoJSON. Just pass it along.

return apifunction.ApiFunction.call_('Image.clip', self, clip_geometry)

def rename(self, names: Any, *args) -> Image:
Expand All @@ -739,7 +762,7 @@ def rename(self, names: Any, *args) -> Image:
Args:
names: An array of strings specifying the new names for the
bands. Must exactly match the number of bands in the image.
bands. Must exactly match the number of bands in the image.
*args: Band names as varargs.
Returns:
Expand All @@ -756,21 +779,3 @@ def rename(self, names: Any, *args) -> Image:
'names': names
}
return apifunction.ApiFunction.apply_('Image.rename', algorithm_args)

@staticmethod
def name() -> str:
return 'Image'


def _parse_dimensions(dimensions: Any) -> Sequence[Any]:
"""Parses a dimensions specification into a one or two element list."""
if ee_types.isNumber(dimensions):
return [dimensions]
elif isinstance(dimensions, str):
# Unpack WIDTHxHEIGHT
return [int(x) for x in dimensions.split('x')]
elif isinstance(dimensions, (list, tuple)) and 1 <= len(dimensions) <= 2:
return dimensions

raise ee_exception.EEException(
'Invalid dimensions {}.'.format(dimensions))
36 changes: 24 additions & 12 deletions python/ee/tests/deprecation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import contextlib
from typing import Any, Dict
import warnings
import unittest

from absl.testing import parameterized

Expand Down Expand Up @@ -48,6 +47,20 @@
'gee:replacement_id': 'replacement_id',
'gee:removal_date': '2024-07-01T00:00:00Z',
},
{
'href': 'https://example.test/two_digit_date.json',
'title': 'two_digit_date',
'deprecated': True,
'gee:replacement_id': 'replacement_id',
'gee:removal_date': '2024-01-31T00:00:00Z',
},
{
'href': 'https://example.test/invalid_date.json',
'title': 'invalid_date',
'deprecated': True,
'gee:replacement_id': 'replacement_id',
'gee:removal_date': '20240131',
},
{
'href': 'https://example.test/learn_more_url_only.json',
'title': 'learn_more_url_only',
Expand Down Expand Up @@ -83,6 +96,15 @@
r'Attention required for date_only! You are using a deprecated asset.\n'
r'To ensure continued functionality, please update it by July 1, 2024.'
),
'two_digit_date': (
r'Attention required for two_digit_date! You are using a deprecated'
r' asset.\nTo ensure continued functionality, please update it by'
r' January 31, 2024.'
),
'invalid_date': (
r'Attention required for invalid_date! You are using a deprecated'
r' asset.\nTo ensure continued functionality, please update it\.'
),
'learn_more_url_only': (
r'Attention required for learn_more_url_only! You are using a'
r' deprecated asset.\nTo ensure continued functionality, please update'
Expand Down Expand Up @@ -123,38 +145,28 @@ def test_no_warnings_thrown_second_arg(self):
with self.assertDoesNotWarn():
FakeClass().some_function('some-value', 'valid-asset')

@unittest.skip('Does not work on github')
@parameterized.named_parameters(
('deprecated_asset', 'deprecated_asset'),
('date_and_learn_more', 'date_and_learn_more'),
('date_only', 'date_only'),
('learn_more_url_only', 'learn_more_url_only'),
)
@parameterized.named_parameters((k, k) for k in _EXPECTED_WARNINGS.keys())
def test_warning_thrown_args_init(self, asset_id: str):
with self.assertWarnsRegex(
DeprecationWarning, _EXPECTED_WARNINGS[asset_id]
):
FakeClass(asset_id, 'some-value')

@unittest.skip('Does not work on github')
def test_warning_thrown_args_instance_method(self):
asset = 'deprecated_asset'
with self.assertWarnsRegex(DeprecationWarning, _EXPECTED_WARNINGS[asset]):
FakeClass().some_function('some-value', asset)

@unittest.skip('Does not work on github')
def test_warning_thrown_kwargs_init(self):
asset = 'deprecated_asset'
with self.assertWarnsRegex(DeprecationWarning, _EXPECTED_WARNINGS[asset]):
FakeClass(arg1=asset)

@unittest.skip('Does not work on github')
def test_warning_thrown_kwargs_instance_method(self):
asset = 'deprecated_asset'
with self.assertWarnsRegex(DeprecationWarning, _EXPECTED_WARNINGS[asset]):
FakeClass().some_function(arg2=asset)

@unittest.skip('Does not work on github')
def test_same_warning_not_thrown(self):
# Verifies the same warning message is not thrown twice.
asset = 'deprecated_asset'
Expand Down
Loading

0 comments on commit 39688b0

Please sign in to comment.