Thursday, 4 January 2018

Extracting coordinates of pixels inside polygon in Google Earth Engine as single feature




IMPORTS:


var geometry = /* color: #d63000 */ee.Geometry.Polygon(
[[[-67.76435852050781, -22.213792811479802],
[-67.76641845703125, -22.220785143658368],
[-67.75646209716797, -22.228412744835992],
[-67.74410247802734, -22.230319580297703],
[-67.7420425415039, -22.221420792936158],
[-67.74341583251953, -22.212203596469955],
[-67.74787902832031, -22.207753698686023],
[-67.75543212890625, -22.213474969917876]]]);


CODE I have tried :


var fg_points = ee.Feature(geometry);

I thought if we give geometry as a feature it will select all points in that polygon but it isn't.


I have used randomPoints command but it is not evenly selecting all the points what I want is at least a point from each pixel. and the randomPoints command is selecting the same points again and again.



Answer



var bounds = ee.Geometry.Polygon(
[[[-67.76435852050781, -22.213792811479802],
[-67.76641845703125, -22.220785143658368],

[-67.75646209716797, -22.228412744835992],
[-67.74410247802734, -22.230319580297703],
[-67.7420425415039, -22.221420792936158],
[-67.74341583251953, -22.212203596469955],
[-67.74787902832031, -22.207753698686023],
[-67.75543212890625, -22.213474969917876]]]);

var l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")
.filterBounds(bounds);


// TEST IMAGE
var first = ee.Image(l8.first()).clip(bounds)

// get image projection
var proj = first.select([0]).projection()

// get coordinates image
var latlon = ee.Image.pixelLonLat().reproject(proj)
Map.addLayer(first, {bands:['B1'], min:0, max:500}, 'Image')


// put each lon lat in a list
var coords = latlon.select(['longitude', 'latitude'])
.reduceRegion({
reducer: ee.Reducer.toList(),
geometry: bounds,
scale: 30
})

// get lat & lon
var lat = ee.List(coords.get('latitude'))

var lon = ee.List(coords.get('longitude'))

// zip them. Example: zip([1, 3],[2, 4]) --> [[1, 2], [3,4]]
var point_list = lon.zip(lat)

// Create points
var mp = ee.Geometry.MultiPoint(point_list)
Map.addLayer(mp,{}, 'Points')

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...