Skip to content

Commit 2ef3c0e

Browse files
authored
feat: sort featurecollection by area (#482)
2 parents 49f4831 + b03655a commit 2ef3c0e

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

geetools/ee_feature_collection.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,32 @@ def fromGeoInterface(
767767

768768
# create the feature collection
769769
return ee.FeatureCollection(data)
770+
771+
def areaSort(self, ascending: bool = True) -> ee.FeatureCollection:
772+
"""Sort the features in the collection by area.
773+
774+
Args:
775+
ascending: Whether to sort in ascending order.
776+
777+
Returns:
778+
The sorted collection.
779+
780+
Examples:
781+
.. jupyter-execute::
782+
783+
import ee, geetools
784+
from geetools.utils import initialize_documentation
785+
786+
initialize_documentation()
787+
788+
fc = ee.FeatureCollection("FAO/GAUL/2015/level0").limit(5)
789+
fc = fc.geetools.areaSort()
790+
fc.aggregate_array("ADM0_NAME").getInfo()
791+
"""
792+
# generate an area property
793+
name = "__geetools_area__"
794+
fc = self._obj.map(lambda feat: feat.set(name, feat.geometry().area(1)))
795+
796+
# sort by area and remove the property from the output
797+
properties = fc.first().propertyNames().remove(name)
798+
return fc.sort(name, ascending).map(lambda feat: feat.select(properties))

tests/test_FeatureCollection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,12 @@ def gdfZ(self):
362362
],
363363
}
364364
return gpd.GeoDataFrame.from_features(data["features"])
365+
366+
class TestAreaSort:
367+
"""Test the ``areaSort`` method."""
368+
369+
def test_area_sort(self, ee_list_regression):
370+
fc = ee.FeatureCollection("FAO/GAUL/2015/level0").limit(5)
371+
fc = fc.geetools.areaSort()
372+
property = fc.aggregate_array("ADM0_NAME")
373+
ee_list_regression.check(property)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
result: '0'
2+
values:
3+
'0':
4+
functionInvocationValue:
5+
arguments:
6+
collection:
7+
functionInvocationValue:
8+
arguments:
9+
baseAlgorithm:
10+
functionDefinitionValue:
11+
argumentNames:
12+
- _MAPPING_VAR_1_0
13+
body: '1'
14+
collection:
15+
functionInvocationValue:
16+
arguments:
17+
ascending:
18+
constantValue: true
19+
collection:
20+
valueReference: '3'
21+
key:
22+
valueReference: '2'
23+
functionName: Collection.limit
24+
functionName: Collection.map
25+
property:
26+
constantValue: ADM0_NAME
27+
functionName: AggregateFeatureCollection.array
28+
'1':
29+
functionInvocationValue:
30+
arguments:
31+
input:
32+
argumentReference: _MAPPING_VAR_1_0
33+
propertySelectors:
34+
functionInvocationValue:
35+
arguments:
36+
element:
37+
valueReference: '2'
38+
list:
39+
functionInvocationValue:
40+
arguments:
41+
element:
42+
functionInvocationValue:
43+
arguments:
44+
collection:
45+
valueReference: '3'
46+
functionName: Collection.first
47+
functionName: Element.propertyNames
48+
functionName: List.remove
49+
functionName: Feature.select
50+
'2':
51+
constantValue: __geetools_area__
52+
'3':
53+
functionInvocationValue:
54+
arguments:
55+
baseAlgorithm:
56+
functionDefinitionValue:
57+
argumentNames:
58+
- _MAPPING_VAR_0_0
59+
body: '4'
60+
collection:
61+
functionInvocationValue:
62+
arguments:
63+
collection:
64+
functionInvocationValue:
65+
arguments:
66+
tableId:
67+
constantValue: FAO/GAUL/2015/level0
68+
functionName: Collection.loadTable
69+
limit:
70+
constantValue: 5
71+
functionName: Collection.limit
72+
functionName: Collection.map
73+
'4':
74+
functionInvocationValue:
75+
arguments:
76+
key:
77+
valueReference: '2'
78+
object:
79+
argumentReference: _MAPPING_VAR_0_0
80+
value:
81+
functionInvocationValue:
82+
arguments:
83+
geometry:
84+
functionInvocationValue:
85+
arguments:
86+
feature:
87+
argumentReference: _MAPPING_VAR_0_0
88+
functionName: Feature.geometry
89+
maxError:
90+
functionInvocationValue:
91+
arguments:
92+
value:
93+
constantValue: 1
94+
functionName: ErrorMargin
95+
functionName: Geometry.area
96+
functionName: Element.set
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- Montenegro
2+
- Taiwan
3+
- Serbia
4+
- South Sudan
5+
- Sudan

0 commit comments

Comments
 (0)