I have a polygon and I want to extract both the value and coordinates of every pixel of a landcover raster within that polygon into a CSV spreadsheet. I will use that to perform computations outside GEE that result in a label assigned to each pixel (an extra column in the CSV). What I want to do is to both plot an image of the pixel values outside a map (e.g., using Matplotlib) and to view the image on GEE.
For this, I use sampleRegions
to give me a featureCollection of all pixels within the polygon, export that, and add the new label column to it. However, I don’t know how to visualize the resulting updated featureCollection as an image. How can I know where each pixel goes in the image?
Here is my code (I assign a random label to each pixel for demonstration):
import ee
ee.Initialize()
from random import random
landcover = ee.ImageCollection('USGS/NLCD_RELEASES/2016_REL').filter(ee.Filter.eq('system:index', '2016')).first().select('landcover')
roi = ee.FeatureCollection(ee.Geometry.Polygon(
[[[-121.214213, 39.102459],
[-121.214213, 39.109467],
[-121.199295, 39.109467],
[-121.199295, 39.102459],
[-121.214213, 39.102459]]]))
points1 = landcover.sampleRegions(collection=roi, scale=30, geometries=True)
# I export this featureCollection as a CSV, compute a label for each point, and add the label to the points
# I assign a random label to demonstrate
def addLabel(point):
return point.set({'label':random()})
points2 = points1.map(addLabel)
How can this updated featureCollection be visualized as an image with "label" as its pixel values?