1

I am trying to combine two images one containing various bands of the sentinel 2 mission and the other one is created from a feature collection containing two types of polygons that have the values "test" of 0 and 1 as can be seen here:

Polygons converted to map

The combined image should then be exported following the tutorial here (https://colab.research.google.com/github/google/earthengine-api/blob/master/python/examples/ipynb/UNET_regression_demo.ipynb#scrollTo=FyRpvwENxE-A)

Here is my code:

RESPONSE = 'test'

trainingMap = ee.FeatureCollection('users/xxx/xxx')
trainingMapReduced = trainingMap.reduceToImage([RESPONSE], 'max');

# combine the images
featureStack = ee.Image.cat([
  image.select(BANDS),
  trainingMapReduced.select([RESPONSE])
]).float()

# consider a kernel of KERNEL_SIZE x KERNEL_SIZE around each pixel
list = ee.List.repeat(1, KERNEL_SIZE)
lists = ee.List.repeat(list, KERNEL_SIZE)
kernel = ee.Kernel.fixed(KERNEL_SIZE, KERNEL_SIZE, lists)

arrays = featureStack.neighborhoodToArray(kernel)

# load training and evaluation areas

trainingPolys = ee.FeatureCollection('users/xxx/trainingRegion')
evalPolys = ee.FeatureCollection('users/xxx/testingRegion')

trainingPolysList = trainingPolys.toList(trainingPolys.size())
evalPolysList = trainingPolys.toList(trainingPolys.size())

# These numbers determined experimentally.
n = 100 # Number of shards in each polygon.
N = 2000 # Total sample size in each polygon.

# Export all the training data (in many pieces), with one task 
# per geometry.
for g in range(trainingPolys.size().getInfo()):
  geomSample = ee.FeatureCollection([])
  for i in range(n):
    sample = arrays.sample(
      region = ee.Feature(trainingPolysList.get(g)).geometry(), 
      scale = 30, 
      numPixels = N / n, # Size of the shard.
      seed = i,
      tileScale = 8
    )
    geomSample = geomSample.merge(sample)

  desc = TRAINING_BASE + '_g' + str(g)
  task = ee.batch.Export.table.toDrive(
    collection = geomSample,
    description = desc, 
    folder = BUCKET, 
    fileNamePrefix = FOLDER + '_' + desc,
    fileFormat = 'TFRecord',
    selectors = BANDS + [RESPONSE]
  )
  task.start()

The task starts but it fails with the following error message "Error: Image.select: Pattern 'test' did not match any bands."

When I use

featureStack = image.addBands(trainingMapReduced, [RESPONSE])

instead of

featureStack = ee.Image.cat([
  image.select(BANDS),
  trainingMapReduced.select([RESPONSE])
]).float()

I get the error message "Error: Image.addBands: Cannot add band 'test' because it doesn't appear in srcImg. "

The print(trainingMapReduced) looks like this:

ee.Image({ "type": "Invocation", "arguments": { "input": { "type": "Invocation", "arguments": { "collection": { "type": "Invocation", "arguments": { "tableId": "users/xxx/xxx" }, "functionName": "Collection.loadTable" }, "properties": [ "test" ], "reducer": { "type": "Invocation", "arguments": {}, "functionName": "Reducer.max" } }, "functionName": "Collection.reduceToImage" }, "bandSelectors": [ "test" ] }, "functionName": "Image.select" })

Does anybody have an idea how to solve this error?

0

I don't think ee.FeatureCollection.reduceToImage() names the bands based on the property/field they were reduced from. My guess is that trainingMapReduced only has one band named 0. Maybe try:

  image.select(BANDS),
  trainingMapReduced.select([0])
]).float()

You could also just do print(trainingMapReduced) to see what the names of the bands are.

|improve this answer|||||
  • Hi Nick thank you for your reply, I have printed the trainingMapReduced and it does contain "bandSelectors": [ "test" ] as can be seen at the bottom of my question. Its strange the error is only thrown when I start the tasks with (task.start()) and then look at the task status. Before that everything runs without an error. – dude Oct 24 '19 at 8:46
0

The problem was that the max reducer renames the bandname to "max" It had to be renamed again.

trainingMap = ee.FeatureCollection('users/xxx/xxx')
trainingMapReduced = trainingMap.reduceToImage([RESPONSE], 'max')
trainingMapReduced = trainingMapReduced.rename(RESPONSE)
|improve this answer|||||

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.