Skip to content

Commit

Permalink
Feature, FeatureCollection, Image, and ImageCollection copyProperties…
Browse files Browse the repository at this point in the history
…: add testing

PiperOrigin-RevId: 636566626
  • Loading branch information
schwehr authored and Google Earth Engine Authors committed May 23, 2024
1 parent c1a78df commit 23db943
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
32 changes: 32 additions & 0 deletions python/ee/tests/feature_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
'arguments': {'crs': {'constantValue': EPSG_4326}},
}
}
# ee.Feature(None).serialize())['values']['0']
FEATURE_NONE_GRAPH = {
'functionInvocationValue': {
'functionName': 'Feature',
'arguments': {},
}
}
# ee.Feature(None, {'a': 'b'}).serialize())['values']['0']
FEATURE_A_GRAPH = {
'functionInvocationValue': {
'functionName': 'Feature',
'arguments': {'metadata': {'constantValue': {'a': 'b'}}},
}
}


def right_maxerror_proj(function_name: str) -> Dict[str, Any]:
Expand Down Expand Up @@ -250,6 +258,30 @@ def test_convex_hull(self):
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

def test_copy_properties(self):
source = ee.Feature(None, {'a': 'b'})
properties = ['c', 'd']
exclude = ['e', 'f']
expect = make_expression_graph({
'arguments': {
'destination': FEATURE_NONE_GRAPH,
'source': FEATURE_A_GRAPH,
'properties': {'constantValue': properties},
'exclude': {'constantValue': exclude},
},
# Note this is Element rather than ImageCollection
'functionName': 'Element.copyProperties',
})
expression = ee.Feature(None).copyProperties(source, properties, exclude)
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

# expression = ee.Feature('a').copyProperties(
# source=source, properties=properties, exclude=exclude
# )
# result = json.loads(expression.serialize())
# self.assertEqual(expect, result)

def test_cutLines(self):
expect = make_expression_graph({
'arguments': {
Expand Down
48 changes: 43 additions & 5 deletions python/ee/tests/featurecollection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def make_expression_graph(
}
}

FEATURES_A = {
'functionInvocationValue': {
'functionName': 'Collection.loadTable',
'arguments': {'tableId': {'constantValue': 'a'}},
}
}

FEATURES_B = {
'functionInvocationValue': {
'functionName': 'Collection.loadTable',
'arguments': {'tableId': {'constantValue': 'b'}},
}
}

class FeatureCollectionTestCase(apitestcase.ApiTestCase):

Expand Down Expand Up @@ -203,8 +216,34 @@ def test_cluster(self):
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

def test_copy_properties(self):
source = ee.FeatureCollection('b')
properties = ['c', 'd']
exclude = ['e', 'f']
expect = make_expression_graph({
'arguments': {
'destination': FEATURES_A,
'source': FEATURES_B,
'properties': {'constantValue': properties},
'exclude': {'constantValue': exclude},
},
# Note this is Element rather than ImageCollection
'functionName': 'Element.copyProperties',
})
expression = ee.FeatureCollection('a').copyProperties(
source, properties, exclude
)
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

expression = ee.FeatureCollection('a').copyProperties(
source=source, properties=properties, exclude=exclude
)
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

def test_inverse_distance(self):
range = 2
a_range = 2
property_name = 'property name'
mean = 3
std_dev = 4
Expand All @@ -213,7 +252,7 @@ def test_inverse_distance(self):
expect = make_expression_graph({
'arguments': {
'collection': FEATURES_ONE,
'range': {'constantValue': range},
'range': {'constantValue': a_range},
'propertyName': {'constantValue': property_name},
'mean': {'constantValue': mean},
'stdDev': {'constantValue': std_dev},
Expand All @@ -228,13 +267,13 @@ def test_inverse_distance(self):
'functionName': 'FeatureCollection.inverseDistance',
})
expression = ee.FeatureCollection(ee.Feature(None)).inverseDistance(
range, property_name, mean, std_dev, gamma, reducer
a_range, property_name, mean, std_dev, gamma, reducer
)
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

expression = ee.FeatureCollection(ee.Feature(None)).inverseDistance(
range=range,
range=a_range,
propertyName=property_name,
mean=mean,
stdDev=std_dev,
Expand Down Expand Up @@ -356,6 +395,5 @@ def test_random_points(self):
self.assertEqual(expect, result)



if __name__ == '__main__':
unittest.main()
27 changes: 26 additions & 1 deletion python/ee/tests/imagecollection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ def test_combine(self):
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

def test_copy_properties(self):
source = ee.ImageCollection('b')
properties = ['c', 'd']
exclude = ['e', 'f']
expect = make_expression_graph({
'arguments': {
'destination': IMAGES_A,
'source': IMAGES_B,
'properties': {'constantValue': properties},
'exclude': {'constantValue': exclude},
},
# Note this is Element rather than ImageCollection
'functionName': 'Element.copyProperties',
})
expression = ee.ImageCollection('a').copyProperties(
source, properties, exclude
)
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

expression = ee.ImageCollection('a').copyProperties(
source=source, properties=properties, exclude=exclude
)
result = json.loads(expression.serialize())
self.assertEqual(expect, result)

def test_forma_trend(self):
covariates = ee.ImageCollection('b')
window_size = 3
Expand Down Expand Up @@ -376,6 +402,5 @@ def test_to_bands(self):
self.assertEqual(expect, result)



if __name__ == '__main__':
unittest.main()

0 comments on commit 23db943

Please sign in to comment.