Skip to content

Commit 307efba

Browse files
schwehrGoogle Earth Engine Authors
authored andcommitted
Add Collection.distance.
PiperOrigin-RevId: 651917385
1 parent 74f2c9f commit 307efba

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

python/ee/collection.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313

1414
from ee import _utils
1515
from ee import apifunction
16+
from ee import computedobject
1617
from ee import deprecation
1718
from ee import ee_date
1819
from ee import ee_exception
20+
from ee import ee_number
1921
from ee import element
2022
from ee import filter as ee_filter
2123
from ee import function
2224
from ee import geometry as ee_geometry
25+
from ee import image
26+
27+
_NumberType = Union[float, ee_number.Number, computedobject.ComputedObject]
2328

2429

2530
class Collection(element.Element):
@@ -60,6 +65,35 @@ def reset(cls) -> None:
6065
def name() -> str:
6166
return 'Collection'
6267

68+
def distance(
69+
self,
70+
searchRadius: Optional[_NumberType] = None,
71+
maxError: Optional[_NumberType] = None,
72+
) -> image.Image:
73+
"""Returns a distance image for the collection.
74+
75+
Produces a DOUBLE image where each pixel is the distance in meters from the
76+
pixel center to the nearest Point, LineString, or polygonal boundary in the
77+
collection. Note distance is also measured within interiors of polygons.
78+
Pixels that are not within 'searchRadius' meters of a geometry will be
79+
masked out.
80+
81+
Distances are computed on a sphere, so there is a small error proportional
82+
to the latitude difference between each pixel and the nearest geometry.
83+
84+
Args:
85+
searchRadius: Maximum distance in meters from each pixel to look for
86+
edges. Pixels will be masked unless there are edges within this
87+
distance.
88+
maxError: Maximum reprojection error in meters, only used if the input
89+
polylines require reprojection. If '0' is provided, then this operation
90+
will fail if projection is required.
91+
"""
92+
93+
return apifunction.ApiFunction.call_(
94+
'Collection.distance', self, searchRadius, maxError
95+
)
96+
6397
@staticmethod
6498
def elementType() -> Type[element.Element]:
6599
"""Returns the type of the collection's elements."""
@@ -274,4 +308,3 @@ def sort(self, prop: str, ascending: Optional[bool] = None) -> Any:
274308
args['ascending'] = ascending
275309
return self._cast(
276310
apifunction.ApiFunction.apply_('Collection.limit', args))
277-

python/ee/tests/featurecollection_test.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def make_expression_graph(
5353
}
5454
}
5555

56-
class FeatureCollectionTestCase(apitestcase.ApiTestCase):
56+
class FeatureCollectionTest(apitestcase.ApiTestCase):
5757

5858
def test_constructors(self):
5959
"""Verifies that constructors understand valid parameters."""
@@ -242,6 +242,24 @@ def test_copy_properties(self):
242242
result = json.loads(expression.serialize())
243243
self.assertEqual(expect, result)
244244

245+
def test_distance(self):
246+
# Inherited from Collection.distance.
247+
features = ee.FeatureCollection('a')
248+
search_radius = 1.1
249+
max_error = 2.2
250+
expect = make_expression_graph({
251+
'arguments': {
252+
'features': FEATURES_A,
253+
'searchRadius': {'constantValue': search_radius},
254+
'maxError': {'constantValue': max_error},
255+
},
256+
# Not FeatureCollection.
257+
'functionName': 'Collection.distance',
258+
})
259+
expression = features.distance(search_radius, max_error)
260+
result = json.loads(expression.serialize())
261+
self.assertEqual(expect, result)
262+
245263
def test_inverse_distance(self):
246264
a_range = 2
247265
property_name = 'property name'

python/ee/tests/imagecollection_test.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def make_expression_graph(
5555
}
5656

5757

58-
class ImageCollectionTestCase(apitestcase.ApiTestCase):
58+
class ImageCollectionTest(apitestcase.ApiTestCase):
5959

6060
def test_image_collection_constructors(self):
6161
"""Verifies that constructors understand valid parameters."""
@@ -211,6 +211,25 @@ def test_copy_properties(self):
211211
result = json.loads(expression.serialize())
212212
self.assertEqual(expect, result)
213213

214+
def test_distance(self):
215+
# Inherited from Collection.distance.
216+
features = ee.ImageCollection('a')
217+
search_radius = 1.1
218+
max_error = 2.2
219+
expect = make_expression_graph({
220+
'arguments': {
221+
'features': IMAGES_A,
222+
'searchRadius': {'constantValue': search_radius},
223+
'maxError': {'constantValue': max_error},
224+
},
225+
# Not FeatureCollection.
226+
'functionName': 'Collection.distance',
227+
})
228+
expression = features.distance(search_radius, max_error)
229+
result = json.loads(expression.serialize())
230+
self.assertEqual(expect, result)
231+
232+
214233
def test_forma_trend(self):
215234
covariates = ee.ImageCollection('b')
216235
window_size = 3

0 commit comments

Comments
 (0)