1

I have a CSV where one column contains state codes, for example "ND", and the other contains lists, for example [1,2,3]. I'd like to import this CSV into GEE, join list values to their respective state boundaries, use the boundaries to clip an image, and then select image values from their respective state list. I've been able to do this by essentially recreating the CSV as a feature collection like this:

var states_list = ee.FeatureCollection([ 
  ee.Feature(null, {STUSPS: 'ND', l: [39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]}), 
  ee.Feature(null, {STUSPS: 'SD', l: [60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76]}) 
  ]);

I'd like to be able to use the imported CSV though, but I need to cast the csv list from a string to a GEE integer list. I can't figure out how to do it. Here's a link to GEE example. For simplicity I left out the code that processes the images.

https://code.earthengine.google.com/03eeac5c05e14403e970fafeeb3b58b1

1 Answer 1

1

Using decodeJSON() on the string representation of the list will convert it. For example:

var features = ee.FeatureCollection([ 
  ee.Feature(null, {STUSPS: 'ND', l: "[39, 40, 41, 42, 43, 44]"}), 
  ee.Feature(null, {STUSPS: 'SD', l: "[60, 61, 62, 63, 64, 65]"}) 
  ]);

features = features.map(function(f) {
  var list = f.getString('l').decodeJSON()
  return f.set('list', list) 
})

If the string representation isn't proper JSON (ie: maybe you're missing the brackets), you can touch up the string before decoding it.

1
  • I definitely would not have figured that out -- thanks so much!
    – Kevin
    Jun 3, 2021 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.