From 276214350b9ef643f3bcefb4eefd5d15046cb452 Mon Sep 17 00:00:00 2001 From: Rambaud Pierrick <12rambau@users.noreply.github.com> Date: Tue, 27 Jan 2026 07:30:25 +0000 Subject: [PATCH 1/2] feat: sort featurecollection by area --- geetools/ee_feature_collection.py | 26 +++++ tests/test_FeatureCollection.py | 9 ++ .../serialized_test_area_sort.yml | 96 +++++++++++++++++++ .../test_FeatureCollection/test_area_sort.yml | 5 + 4 files changed, 136 insertions(+) create mode 100644 tests/test_FeatureCollection/serialized_test_area_sort.yml create mode 100644 tests/test_FeatureCollection/test_area_sort.yml diff --git a/geetools/ee_feature_collection.py b/geetools/ee_feature_collection.py index 7c28eb0f..544a9917 100644 --- a/geetools/ee_feature_collection.py +++ b/geetools/ee_feature_collection.py @@ -767,3 +767,29 @@ def fromGeoInterface( # create the feature collection return ee.FeatureCollection(data) + + def areaSort(self, ascending: bool = True) -> ee.FeatureCollection: + """Sort the features in the collection by area. + + Args: + ascending: Whether to sort in ascending order. + + Returns: + The sorted collection. + + Examples: + .. code-block:: python + + import ee + + fc = ee.FeatureCollection("FAO/GAUL/2015/level0").limit(5) + fc = fc.geetools.areaSort() + fc.aggregate_array("ADM0_NAME").getInfo() + """ + # generate an area property + name = "__geetools_area__" + fc = self._obj.map(lambda feat: feat.set(name, feat.geometry().area(1))) + + # sort by area and remove the property from the output + properties = fc.first().propertyNames().remove(name) + return fc.sort(name, ascending).map(lambda feat: feat.select(properties)) diff --git a/tests/test_FeatureCollection.py b/tests/test_FeatureCollection.py index 1e42c036..28b7bfa8 100644 --- a/tests/test_FeatureCollection.py +++ b/tests/test_FeatureCollection.py @@ -362,3 +362,12 @@ def gdfZ(self): ], } return gpd.GeoDataFrame.from_features(data["features"]) + + class TestAreaSort: + """Test the ``areaSort`` method.""" + + def test_area_sort(self, ee_list_regression): + fc = ee.FeatureCollection("FAO/GAUL/2015/level0").limit(5) + fc = fc.geetools.areaSort() + property = fc.aggregate_array("ADM0_NAME") + ee_list_regression.check(property) diff --git a/tests/test_FeatureCollection/serialized_test_area_sort.yml b/tests/test_FeatureCollection/serialized_test_area_sort.yml new file mode 100644 index 00000000..2e0a3544 --- /dev/null +++ b/tests/test_FeatureCollection/serialized_test_area_sort.yml @@ -0,0 +1,96 @@ +result: '0' +values: + '0': + functionInvocationValue: + arguments: + collection: + functionInvocationValue: + arguments: + baseAlgorithm: + functionDefinitionValue: + argumentNames: + - _MAPPING_VAR_1_0 + body: '1' + collection: + functionInvocationValue: + arguments: + ascending: + constantValue: true + collection: + valueReference: '3' + key: + valueReference: '2' + functionName: Collection.limit + functionName: Collection.map + property: + constantValue: ADM0_NAME + functionName: AggregateFeatureCollection.array + '1': + functionInvocationValue: + arguments: + input: + argumentReference: _MAPPING_VAR_1_0 + propertySelectors: + functionInvocationValue: + arguments: + element: + valueReference: '2' + list: + functionInvocationValue: + arguments: + element: + functionInvocationValue: + arguments: + collection: + valueReference: '3' + functionName: Collection.first + functionName: Element.propertyNames + functionName: List.remove + functionName: Feature.select + '2': + constantValue: __geetools_area__ + '3': + functionInvocationValue: + arguments: + baseAlgorithm: + functionDefinitionValue: + argumentNames: + - _MAPPING_VAR_0_0 + body: '4' + collection: + functionInvocationValue: + arguments: + collection: + functionInvocationValue: + arguments: + tableId: + constantValue: FAO/GAUL/2015/level0 + functionName: Collection.loadTable + limit: + constantValue: 5 + functionName: Collection.limit + functionName: Collection.map + '4': + functionInvocationValue: + arguments: + key: + valueReference: '2' + object: + argumentReference: _MAPPING_VAR_0_0 + value: + functionInvocationValue: + arguments: + geometry: + functionInvocationValue: + arguments: + feature: + argumentReference: _MAPPING_VAR_0_0 + functionName: Feature.geometry + maxError: + functionInvocationValue: + arguments: + value: + constantValue: 1 + functionName: ErrorMargin + functionName: Geometry.area + functionName: Element.set diff --git a/tests/test_FeatureCollection/test_area_sort.yml b/tests/test_FeatureCollection/test_area_sort.yml new file mode 100644 index 00000000..732f3230 --- /dev/null +++ b/tests/test_FeatureCollection/test_area_sort.yml @@ -0,0 +1,5 @@ +- Montenegro +- Taiwan +- Serbia +- South Sudan +- Sudan From b03655a7635d3979606c4e92cfe90b8244f0e854 Mon Sep 17 00:00:00 2001 From: Rambaud Pierrick <12rambau@users.noreply.github.com> Date: Tue, 27 Jan 2026 07:34:16 +0000 Subject: [PATCH 2/2] docs: execute the documentation --- geetools/ee_feature_collection.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/geetools/ee_feature_collection.py b/geetools/ee_feature_collection.py index 544a9917..c4f58551 100644 --- a/geetools/ee_feature_collection.py +++ b/geetools/ee_feature_collection.py @@ -778,9 +778,12 @@ def areaSort(self, ascending: bool = True) -> ee.FeatureCollection: The sorted collection. Examples: - .. code-block:: python + .. jupyter-execute:: + + import ee, geetools + from geetools.utils import initialize_documentation - import ee + initialize_documentation() fc = ee.FeatureCollection("FAO/GAUL/2015/level0").limit(5) fc = fc.geetools.areaSort()