google-earth-engine – How to Change System Index of FeatureCollection in GEE

google-earth-enginesf

I have a FeatureCollection with points loaded from an asset (shapefile). Since the system:index is empty, gee assigns a weird index to each feature (e.g. 000000000001, 0000000000a). This causes problems if I want to some mapping and extracting over the FeatureCollection. Can I change the system:index to a sequence from 0 to length of the features?

var asset_points = ee.FeatureCollection("users/slisovski/AssetPoints");
// Map.addLayer(asset_points, {color: 'FF0000'}, "coordinates");

print(asset_points);
var id01 = ee.Feature(asset_points.first()).get("system:index");
print(id01);

var len = asset_points.toList(10000).length().subtract(1);
var idList = ee.List.sequence(0,len);

var assetID = asset_points.set('system:index', idList);
print(assetID);

https://code.earthengine.google.com/bfcfa65f704505889f2bc4d16c909f37

I created the shapefile in R. Maybe I can change this code to make sure that the system:index is sensible?

library(sf)
tab     <- data.frame(ID = paste0("ID_", letters[1:20]), Longitude = runif(20, 110, 150), Latitude = runif(20, 20, 80))
data_sf <- tab %>% st_as_sf(.,coords=c("Longitude", "Latitude"),crs = st_crs(world))
st_write(data_sf, dsn = "AssetPoints.shp", driver = "ESRI Shapefile")

Best Answer

You could do that in the GEE after uploading for example as follows:

// create 100 random points
var asset_points = ee.FeatureCollection.randomPoints(geometry, 100);

// make a list of length of features
var idList = ee.List.sequence(0,asset_points.size().subtract(1));

// featureCollection to a List
var list = asset_points.toList(asset_points.size());

// set the system:index
var assetID = ee.FeatureCollection(idList.map(function(newSysIndex){
  var feat = ee.Feature(list.get(newSysIndex));
  // format number to string (system:index must be a string)
  var indexString = ee.Number(newSysIndex).format('%03d')
  return feat.set('system:index', indexString, 'ID', indexString);
}));
// print lis of system:index
print('list system:index OLD',asset_points.aggregate_array('system:index'));
print('list system:index NEW',assetID.aggregate_array('system:index'));

link code. As your FC was not shared, I made a dummy one. It should work similar for your FC.

Related Question