1

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?

1 Answer 1

1

Paint the points into an empty image:

ee.Image().double().paint(points, "label")
1
  • This does the job of creating an image I can view correctly on GEE map. I also want to plot and save the image as PNG so I export the image as GeoTif using Export.image.toDrive, but in the resulting GeoTif, the entire polygon region is rotated. Any idea what I am missing?
    – Mohamad
    Commented Jan 16, 2022 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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