0

I have an ee.FeatureCollection called 'datasubset' that contains the following columns: ID, LATITUDE, LONGITUDE, WIDTH, HEIGHT, and VALUE. This is a subset of the data I am working on in Ayeyarwady Region, Myanmar, with only 55 features.

ID refers to the unique feature identifier, an integer from 1 to 55. LATITUDE & LONGITUDE refer to the geographical coordinates of each feature. WIDTH & HEIGHT refer to the dimensions of rectangular plots for each feature, units are in kilometers. VALUE refers to a derived index of crop suitability, ranging from 0 to 100. See attached image below for a snapshot of the ee.FeatureCollection metadata in my GEE assets folder. I have made this asset public for you to view in GEE.

My first objective is to plot rectangles of varying WIDTH and HEIGHT values. With the help of @kkrao (see this link to the previous post for a separate question), I was able to do this. The result is an ee.FeatureCollection containing rectangles of varying dimensions.

My second objective is to add original input data for ID and VALUE to the rectangles I created, but I have been unsuccessful here. The ee.Geometry.Rectangles I created contain no information on ID and VALUE, which I want to preserve too, they contain 0 properties from the input dataset. This is what is printed onto the console: columns: Object (0 properties).

In my attempts, I have been trying to append the ID and VALUE data from the input ee.FeatureCollection, particularly using the .set() and .get() functions, but these have not worked out for me. I get errors in the Console which I do not understand.

Below is my current script attempt, and here is the link to the GEE script for the Code Editor. I believe I need help for lines 48 - 49. But am open to suggestions in other parts too.

//  Load point data
var points = ee.FeatureCollection('users/earthorbgis/datasubset');
print(points,'points');

//  Add coordinates of required rectangle around each point as separate properties to the points Feature Collection
var rectCoords = points.map(function(f) {
  var dist = ee.Number(f.get("HEIGHT")).divide(2).multiply(1000);
  var yBoundbox = f.buffer(dist).bounds().geometry() //bounding box for height
  
  var dist = ee.Number(f.get("WIDTH")).divide(2).multiply(1000);
  var xBoundbox = f.buffer(dist).bounds().geometry() //bounding box for width

  // return the list of coordinates
  var listCoords = ee.Array.cat(xBoundbox.coordinates(), 1);// obtain coordinates as a single list
  // get the X and Y -coordinates
  var xCoords = listCoords.slice(1, 0, 1);
  
  var listCoords = ee.Array.cat(yBoundbox.coordinates(), 1); 
  var yCoords = listCoords.slice(1, 1, 2);
  
  // reduce the arrays to find the max (or min) value
  var xMin = xCoords.reduce('min', [0]).get([0,0]); //reduce list to obtain min X
  var xMax = xCoords.reduce('max', [0]).get([0,0]);
  var yMin = yCoords.reduce('min', [0]).get([0,0]);
  var yMax = yCoords.reduce('max', [0]).get([0,0]);
  
  var f = f.set("south", yMin); // paste min longitude into the peroprty "south" of the point
  var f = f.set("north", yMax);
  var f = f.set("west", xMin);
  var f = f.set("east", xMax);
  
  return f; 
});


//  Using the rectangle coordinates from each point's property, draw rectangles
function createRect(f){
  var west = f.get("west")
  var rect = ee.Geometry.Rectangle(ee.List([f.get("west"),f.get("south"), f.get("east"), f.get("north")]))

//  HERE!! ( in lines 46 - 47)
//  I am trying to incorporate lines of code into this function
//  So that it not only draws the rectangle of custom dimension
//  But also add the initial properties on ID and VALUE

//  rect = rect.set('ID',f.get("ID")); 
//  rect = rect.set('VALUE',f.get("VALUE"));
//  but they do not work
  return ee.Feature(rect)
}

// apply the function defined above
var rects = rectCoords.map(createRect);
print(rects,'rects'); // Here in the console, it says 'columns: Object (0 properties)

//  View layers
Map.addLayer(rects);
Map.centerObject(rects,12);

//  Export as Shapefile and inspect in QGIS
Export.table.toDrive(rects,'rects','rects_folder','rects','SHP');

Here is the snapshot of the input data metadata: enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.