Filter the data of the specified attribute information after importing the vector layer
After the vector layer is introduced, gee users often choose a small piece of the vector layer for research (for example, the vector layer of China's provinces is introduced, but the research area only involves Anhui Province). This will use Filter function, filter out Anhui Province from the Chinese vector layer.
Of course, the simpler method is to filter out Anhui Province from ArcGIS or other software, export it, and then import it into Geemap, but this is time-consuming and labor-intensive.
Generally, users have two needs: ① Select a small area
from a large area as the experimental area; ② Select multiple small areas from a large area as the experimental area
Attribute table information of Hengyang City:
(Here, take the selection of Hengnan County from Hengyang City, Hunan Province as an example)
1. Select a small area from the large area as the experimental area
1. The first method:
Use the .filterMetadata('PYNAME','equals','Hengnan Xian') command:
it refers to the vector area whose metadata PYNAME is equal to Hengnan Xian.
Of course, you can also use other attribute information to filter, just change the field name in the function
var HengYangShi= ee.FeatureCollection(HY);
var HengnanXian= HengYangShi.filterMetadata('PYNAME','equals','Hengnan Xian');
Among them, HY is the vector layer of Hengyang City introduced.
2. The second method:
Query the position of the point in Hengnan County in advance, and introduce a point in Hengnan County,
var Roi_HengnanXian = ee.Geometry.Point(114,30)
Or draw a point in Hengnan County by hand in Geemap
, and then use the following command (the point is included in the polygon) to filter the vector area of Hengnan County from Hengyang City.
//用point筛选HengYangShi
var HY = table.filterBounds(Roi_HengnanXian).geometry();
The result is shown in the figure below
3. Other methods
If using the first method will save a lot of work, there are other methods besides the above two methods.
2. Select multiple small areas from a large area as the experimental area
1. Use attribute information to filter multiple areas
The table in the figure below is the Hengyang vector map imported in the figure above
var HengYangShi = ee.FeatureCollection(table);
var Name_List = ee.List( ['Hengdong Xian','Hengyang Xian','Qidong Xian','Hengnan Xian'] );
var Inlist_Filter = ee.Filter.inList( 'PYNAME', Name_List);
var List_Features = HengYangShi.filter( Inlist_Filter );
Map.addLayer(List_Features)
Filter the vector region in the PYNAME field containing all the elements in the list by the name list built.
The result is shown in the figure,
2. Draw a polygonal area by hand
As shown in the figure below, first draw a polygonal area by hand.
At this time, a hand-painted polygon appears in the resource bar
, and then use .filterBounds() to filter the area where the large area intersects with the hand-painted polygon as a test area
var HY = table.filterBounds(geometry).geometry();
Map.addLayer(HY)
3. Other methods
Of course, there are other screening methods, including using geospatial variables to perform operations such as Boolean operations, mathematical operations, and sorting.
.filterMetadata() usage notes
filterMetadata(name, operator, value)
Shortcut for filtering collections by metadata.
This operation is equivalent to .filter(ee.Filter.metadata(…)).
Returns the filtered collection.
Parameters:
this:collection (Collection):
The Collection instance.
name (String): The name of the property to filter.
operator (String): The name of the comparison operator.
Other possible values: "equals", "less_than", "greater_than", "not_equals", "not_less_than", "not_greater_than", "starts_with", "ends_with", "not_starts_with", "not_ends_with", "contains"
,
" not_contains”.
value (Object): The value to compare.
Returns: Collection
.inList() usage instructions
ee.Filter.inList(leftField, rightValue, rightField, leftValue)
Filter the metadata contained in the pre-constructed list.
Returns the constructed filter.
Parameters:
leftField (String, optional):
The selector for the left operand. If leftValue is specified, it should not be specified.
rightValue (List|Object, optional):
The value of the right operand. Should not be specified if rightField is specified.
The value of the right operand. If rightField is specified, it should not be specified
rightField (String, optional):
selector for the right operand. If rightValue is specified,
leftValue (List|Object, optional):
the value of the left operand should not be specified. If leftField is specified, it should not be specified.
Returns: Filter