A Standard Exploration of Google Earth Engine Feature Collections
Published: 2023-11-23 18:22:02

In Google Earth Engine, a `Feature` is an object with a `geometry` property storing a Geometry object and a properties property storing a dictionary of other properties. 
A `FeatureCollection` is a [Groups of combined related features](https://developers.google.com/earth-engine/guides/feature_collections). 
FeatureCollection objects contains features. Naturally, `FeatureCollections` contain geometry and properties, and can also contain other collections.

**GeoJSON** is a data format for encoding a range of geospatial data structures. It supports _Point_, _Polygon, LineString, MultiLineString_ and _MultiPolygon_ geometries. 

As a [Google Earth Engine](https://earthengine.google.com/) Developer you might wish to combine features. The feature combining operation, allows for additional geospatial operations on the entire set such as filtering, sorting and rendering. 
Individual geometries can also be turned into a `FeatureCollection` a single Feature. 

***Ways to Create a FeatureCollection in GEE***
***1.Geometry Points***
A FeatureCollection can be created using individual geometries. The geometries can be turned into can a FeatureCollection of just one Feature. An example is the code below.

```
// Make a feature with some properties.
var feature = ee.Feature(ee.Geometry.Point([37.393616042129715, -0.012801305697836253]))
  .set('County', 'Meru').set('Meru1', 'Settings');
```
**2. GeometryPolygons **
Another way to create a `FeatureCollection` in GEE is using GeometryPolygons. This technique calls for drawing a Polygon on the map then convert the polygon into a Geometry using the `ee.Geometry.Polygon` with Geometries put in a dictionary format. The code prints the geometry properties using the `print()` function the adds the polygon into a map using the `Map.addLayer()` method. 

The code below plots th polygon of Nairobi county..

```
// Creating Nairobi Polygon with Geometries
var Nairobi = 
ee.Geometry.Polygon(
        [[[36.668518385879715, -1.2396225227250073],
          [36.695984206192215, -1.354380436474334],
          [36.822326979629715, -1.3928215346108865],
          [36.915710768692215, -1.359872059659389],
          [36.978882155410965, -1.313192869399109],
          [36.976135573379715, -1.2829882209698302],
          [37.028320631973465, -1.2994635284940157],
          [37.075012526504715, -1.3077011420427207],
          [37.105224928848465, -1.2610209794600944],
          [37.061279616348465, -1.2061020716359079],
          [36.998108229629715, -1.2390535525537143],
          [36.970642409317215, -1.2170859434783168],
          [36.918457350723465, -1.2115940131238145],
          [36.885498366348465, -1.1841341953591218],
          [36.836059889785965, -1.2061020716359079],
          [36.792114577285965, -1.1951181554616812],
          [36.745422682754715, -1.228069770585397],
          [36.701477370254715, -1.2610209794600944]]]);
print (Nairobi)
Map.addLayer(Nairobi)
```
Featurecollection can be added to a map directly with `Map.addLayer()` function.
By default, map visualization displays the vectors with a solid black lines and semi-opaque black fill. However, it is possible to render the vectors in a color by specifying the `.color` parameter. 

**3. Using Shapefile**
A **shapefile** is a simple, non-topological format for storing the geometric location and attribute information of geographic features. Within a shapefile geographic features can be represented by lines, points, or polygons/areas. The workspace containing shapefiles may also contain dBASE tables, which can store additional attributes that can be joined to a shapefile's features. 
A geographic location-based shapefile can be imported into Earth Engine and be used as a `FeatureCollection`. Boundary or administrative shapefile is a good example to demonstrate how to use shapefile as shapefile. [Global Administrative Areas (GADM) 3.6](https://data.apps.fao.org/map/catalog/static/search?format=shapefile) vector dataset series which includes distinct datasets representing administrative boundaries for all countries in the world. The code below demonstrates how to import FAO’s - GADM 3.6 - Country boundaries (level 0) into Google Earth Engine and use it as a FeatureCollection. The Country boundaries are positioned at level 0 in the GADM 3.6 dataset. In this regard, our code below will focus on Kenya. 

```
///////////////////Creating FeatureCollection from Dataset /////////////
var admin0 = ee.FeatureCollection('FAO/GAUL_SIMPLIFIED_500m/2015/level2');
var Kenya = admin0.filter(ee.Filter.eq('ADM0_NAME', 'Kenya'));
print(Kenya)
Map.centerObject(Kenya)
Map.addLayer(Kenya)
```

**Visualizing FeatureCollections**
Once all the operations are completed, it is time to visualize our `FeatureCollection`. Like images, geometries and features, FeatureCollections can be visualized on a map using the `Map.addLayer()` function. While images are visualized based on the pixel values, feature collections use feature properties/attributes. Vector layers are added on map by assigning a value to the _red, green_ and _blue_ channels for individual pixel on the screen based on the geometry and attributes of the features.

The following functions are used to visualize a vector on map:

- `Map.addLayer`: As with raster layers, you can add a FeatureCollection to the Map by specifying visualization parameters. This method supports only one visualization parameter: color. All features are rendered with the specified color.
- `draw`: This function supports the parameters _pointRadius_ and _strokeWidth_ in addition to color. It renders all features of the layer with the specified parameters.
- `paint`: This is a more powerful function that can render each feature with a different color and width based on the values in the specified property.
- `style`: This is the most versatile function. It can apply a different style to each feature, including _color_, _pointSize, pointShape, width, fillColor,_ and _lineType_.  

**Filtering a FeatureCollection**
FeatureCollection filtering techniques resembles that used in ImageCollections. We can filter by Date(), Bounds() among other filtering techniques. While filtering, we use the `featureCollection.filter()` method. 

The code below shows how to apply the filtering technique 

```
/////////////////////Filtering FeatureCollection///////////////
// Load watersheds from a data table.
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')
//  Convert 'areasqkm' property from string to number.
  .map(function(feature){
    var num = ee.Number.parse(feature.get('areasqkm'));
    return feature.set('areasqkm', num);
  });

// Define a region roughly covering the continental US.
var continentalUS = ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29);

// Filter the table geographically: only watersheds in the continental US.
var filtered = sheds.filterBounds(continentalUS);

// Check the number of watersheds after filtering for location.
print('Count after filter:', filtered.size());

// Filter to get only larger continental US watersheds.
var largeSheds = filtered.filter(ee.Filter.gt('areasqkm', 25000));
// Check the number of watersheds after filtering for size and location.
print('Count after filtering by size:', largeSheds.size());
```

**Extracting Metadata from a FeatureCollection**
It is possible to extract information from a FeatureCollection like count of features, statistical description, or even perform  mathematical computations on a FeatureCollection.
Let’s say we want to extract for information/metadata from a FeatureCollection using a code in Earth Engine..

```
//////////////////////Extracting FeatureCollection Metadata/////////////
// Load watersheds from a data table.
var Kenya = admin0.filter(ee.Filter.eq('ADM0_NAME', 'Kenya'));
var sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06')
  // Filter to the continental US.
  //.filterBounds(RoI)
  .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29))
  // Convert 'areasqkm' property from string to number.
  .map(function(feature){
    var num = ee.Number.parse(feature.get('Areasqkm'));
    return feature.set('Areasqkm', num);
  });

// Display the table and print its first element.
Map.addLayer(sheds, {}, 'watersheds');
print('First watershed', sheds.first());

// Print the number of watersheds.
print('Count:', sheds.size());

// Print stats for an area property.
print('Area statistic:', sheds.aggregate_stats('Areasqkm'));
```

We have done some of the critical operations required on a FeatureCollection and I believe this helps..any question or comment regarding this topic can be shared in the commend section..