0

I have imported a CSV file with just one column. Each row of the column has multiple coordinates required to draw a polygon. (This was collected on the ground)

I have used the below code to extract the coordinates from the CSV as a list

var poly = table2.aggregate_array('wkt')  //wkt being the column name

Now essentially, I would want to loop through the list, create and display the polygons on the map.

I am looking for something like this:

var newPolygon = ee.Geometry.Polygon(poly[i])

If there is a simpler way to use the coordinates to plot polygons can you let me know?

1

Without more information on the CRS of your coordinates, and their construction., it will be difficult to create a working solution, but I'll make assumptions along the way and see if you can adapt it to your solution. What you'll want to do is map a function to your list that will take each element of the list (the polygon coordinates) and make a geometry with it. Something like this.

var makeFeatureFromGeometry = function(coords){
         var newGeometry = ee.Geometry.Polygon(coords, CRS); // you will have to supply the CRS of the coordinates
         return ee.Feature(newGeometry);
}
var myFeatureCollection = ee.FeatureCollection(poly.map(makeFeatureFromGeometry));

Map.addLayer(myFeatureCollection);

Try this and if you receive error messages, I'll see if I can help you create a working solution.

  • Thanks for the response @Sean! This was exactly the gap in my understanding. However, I am now facing an error with coords, as it is of type string and I am unable to convert it to list despite trying out a lot of functions. The error I am getting while constructing the polygon is: "GeometryConstructors.Polygon, argument 'coordinates': Invalid type. Expected: List<Object>. Actual: String." – Abheeshta Rao Feb 14 at 9:28
  • My coordinates are of the format: [[76.47441303,11.91755698],[76.47439336,11.91753608],[76.47435523,11.91751293],[76.47429992,11.91750973],[76.47426618,11.91756832],[76.47436792,11.91753208],[76.47452594,11.91749775],[76.47441303,11.91755698]] This is accepted while directly using it in ee.Geometry.polygon But not while mapping it is as variable coors – Abheeshta Rao Feb 14 at 9:52
  • Hi Abheeshta, do the following: print(poly) and look in the console what appears. It might be a text string, which is why you can't read it with the function I wrote. It is expecting a list... – Sean Roulet Feb 15 at 23:13
  • Yes, it is a text string. When I try to parse it to a list using JSON.parse it throws an error saying "element e is not found at position 0" – Abheeshta Rao Feb 17 at 1:46
  • Look in the docs of the code editor, under ee.String. perhaps if you cast it to an ee.String() you will then be able to use the functions there to parse it. I've never used wkt with Earth Engine, so I'm afraid I'm not much help. – Sean Roulet Feb 17 at 10:17

Your Answer

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

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