Search code examples
javascriptrgoogle-earth-enginergee

Using rgee and fastDistanceTransform with a featureCollection


I have successfully extracted distance to nearest human settlement with rgee function fastDistanceTransform, where the original layer is a single image (DLR/WSF/WSF2015/v1). I want to repeat this with distance to nearest road, but the road dataset is a featureCollection and not an image.

I have successfully converted to image in the gee code editor with:

var dataset = ee.FeatureCollection('TIGER/2016/Roads')
.map(function(feature){
    var num = ee.Number.parse(feature.get('linearid'));
    return feature.set('linearid', num);
});

  var roads = dataset
  .reduceToImage({
    properties: ['linearid'],
    reducer: ee.Reducer.first()
});

Map.setCenter(-99.976, 40.38, 5);
Map.addLayer(roads, {
});

But I cannot get it to repeat the same in the R package rgee. I am using code:

road = ee$FeatureCollection("TIGER/2016/Roads")$
  map(function(feature) { num = ee$Number$parse(feature$get('linearid'))
  return(feature$set('linearid', num)) })
road2 = road$reduceToImage(properties=road$select('linearid'), reducer=ee$Reducer$first())
Map$addLayer(road2, {}, "road2")

But I get the error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ee.ee_exception.EEException: Collection.reduceToImage, argument 'properties': Invalid type.
Expected type: List<String>.
Actual type: FeatureCollection.

Can anyone help me fix this error so I can continue using 'road2' as an image? I think it has something to do with this (https://github.com/r-spatial/rgee/issues/232) "It seems that passing an ee.Image object in Javascript immediately changes it to a List"


Solution

  • I don't know the R language, but it looks like you made a mistake in your translation.

      var roads = dataset
      .reduceToImage({
        properties: ['linearid'],
        reducer: ee.Reducer.first()
    });
    

    Here the properties argument to reduceToImage is a list of strings.

    road2 = road$reduceToImage(properties=road$select('linearid'), reducer=ee$Reducer$first())
    

    Here you are not constructing a list, but instead calling road$select. So, the result is an image, not the list of strings that reduceToImage wants.

    Maybe you want properties=list('linearid') ? Again, I don't know R or rgee, so I can't say whether this is correct.