3

I would like to turn a MultiPoint geometry to a feature collection with each point as its own feature. How come the following code will not do that?

var points = /* color: #d63000 */ee.Geometry.MultiPoint(
    [[1.3634359574461996, 31.636034304440155],
     [3.6046468949461996, 31.148372138748694],
     [-0.9656656050538004, 29.172742327143503],
     [2.8575765824461996, 29.172742327143503],
     [0.7042562699461996, 30.506858885801098],
     [-1.6248452925538004, 30.884738935660458],
     [-3.8221109175538004, 27.62655429384725],
     [2.2862875199461996, 25.781544169671214]]);

// var mp = ee.FeatureCollection(points)
// print(mp)
var mp = ee.FeatureCollection(points.map(function(p){
  var point = ee.Feature(ee.Geometry.Point(p), {})
  return point
}))
print(mp)

This code returns the following error message: "points.map is not a function"

EDITS: The following code will not work either:

var points = /* color: #d63000 */ee.Geometry.MultiPoint(
    [[1.3634359574461996, 31.636034304440155],
     [3.6046468949461996, 31.148372138748694],
     [-0.9656656050538004, 29.172742327143503],
     [2.8575765824461996, 29.172742327143503],
     [0.7042562699461996, 30.506858885801098],
     [-1.6248452925538004, 30.884738935660458],
     [-3.8221109175538004, 27.62655429384725],
     [2.2862875199461996, 25.781544169671214]]);

var pointses = points.geometries()
print(pointses)
var mp2 = ee.FeatureCollection(pointses)
print(mp2)

and returns this error: "Collection: Geometries cannot have their properties modified or be placed into collections."

which leads me to believe that a FeatureCollection cannot be created from a set of points.

1 Answer 1

7

Change points to be a list, rather than a MultiPoint Geometry. There is no map function for MultiPoint Geometries, but there is one for lists:

var points = /* color: #d63000 */ee.List(
    [[1.3634359574461996, 31.636034304440155],
     [3.6046468949461996, 31.148372138748694],
     [-0.9656656050538004, 29.172742327143503],
     [2.8575765824461996, 29.172742327143503],
     [0.7042562699461996, 30.506858885801098],
     [-1.6248452925538004, 30.884738935660458],
     [-3.8221109175538004, 27.62655429384725],
     [2.2862875199461996, 25.781544169671214]]);

// var mp = ee.FeatureCollection(points)
// print(mp)
var mp = ee.FeatureCollection(points.map(function(p){
  var point = ee.Feature(ee.Geometry.Point(p), {})
  return point
}))
print(mp)

A suggested edit to this question also points out that you may use the .coordinates() method to cast your coordinates to a mappable form:

var points = /* color: #d63000 */ee.Geometry.MultiPoint(
    [[1.3634359574461996, 31.636034304440155],
     [3.6046468949461996, 31.148372138748694],
     [-0.9656656050538004, 29.172742327143503],
     [2.8575765824461996, 29.172742327143503],
     [0.7042562699461996, 30.506858885801098],
     [-1.6248452925538004, 30.884738935660458],
     [-3.8221109175538004, 27.62655429384725],
     [2.2862875199461996, 25.781544169671214]]);

// var mp = ee.FeatureCollection(points)
// print(mp)
var mp = ee.FeatureCollection(points.coordinates().map(function(p){
  var point = ee.Feature(ee.Geometry.Point(p), {})
  return point
}))
print(mp)
4
  • Is there a way of not having to do this "by hand"?
    – Raimundo
    Commented Apr 27, 2021 at 18:07
  • It's not clear what you mean "by hand." It seems pretty automated to me...
    – Jon
    Commented Apr 27, 2021 at 21:47
  • Thanks for the answer! Because of the nature of my project, I'm drawing and re-drawing the points on the map based on further analysis (there are many points). So I would like to avoid to have to copy the code and change to ee.List. By automated I mean that directly takes the MultiPoint and convert to a List.
    – Raimundo
    Commented Apr 27, 2021 at 21:59
  • You might take a look at developers.google.com/earth-engine/apidocs/… and other functions for Multipoint...I'm pretty sure you should be able to get your Multipoint converted to a List using a couple functions. Open a new question on this site if you can't solve it!
    – Jon
    Commented Apr 28, 2021 at 14:33

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.