You have two question. The first one is why the color pallet is not showing in the exported image.
Actually, you export an one-banded ('gray-scale') image and the visualization you provided in the web API of GEE is only for the visualization there. In a program you open the GeoTiff (e.g. ArcMap), you will have to set the visualization parameters again, as the program just reads the values of each pixels and usually by default give it a gray scale.
Your seconds question relates to the geometry. You present the image on the Map in the API clipped to 'region', while you export the image using 'roi'. This explains why it is different.
I would recommed to clip the image by region (which is a feature collection, so use clipToCollection), and export the image by the bounding rectangle of that feature collection.
Export.image.toDrive({
image: annualNDVI.clipToCollection(region),
description: 'CTG_NDVi',
scale: 30,
region: ee.Feature(region.first()).bounds(),
fileFormat: 'GeoTIFF',
maxPixels: 1e12,
formatOptions: {
cloudOptimized: true
},
skipEmptyTiles: true
});
Furthermore, you could scale the image pixel values to 0-255 (8-bit integers), knowing that NDVI ranges between -1 and 1, to save storage space and increase your download speed:
var toBytes = annualNDVI.add(1).multiply(122.5).toByte();
link script
The overall flow is, you mask the image, convert to array, then sample the arrays at points. Here's a sketch of the solution in Python (where classes
is an image in which each pixel stores an integer label 0, 1, 2..., composite
is some multi-band image of predictor bands and KERNEL
is whatever shape you want:
# Get only patches completely covered by the kernel.
connectedMask = classes.mask().reduceNeighborhood(
reducer=ee.Reducer.min(),
kernel=KERNEL,
)
# Convert to an array per pixel
arrays = composite.addBands(classes).neighborhoodToArray(KERNEL)
# Sample the arrays
sample = arrays.updateMask(connectedMask).sample(
numPixels=1000,
region=SOME_GEOMETRY,
scale=30)
# Export the result to a table.
task = ee.batch.Export.table.toCloudStorage(
collection=sample,
description='Sample Export',
fileNamePrefix='foo',
bucket=outputBucket,
fileFormat='TFRecord',
selectors=listOfBands)
task.start()
Best Answer
That function still works. As far as I can tell, it's deprecated from the ee.Image docs because it extends beyond just images at this point.
For example, you can use
Element.geometry()
to find the geometry of a FeatureCollection as well: