0

I have a shapefile I made from a list of longitude and latitudes. I extracted the latitudes and longitudes in arrays following this link. I then try to iterate over the arrays using this:

for (var i = 0; i < lats.length; i++) {
  print('lat',lats[i]);
  print('long',longs[i]);
  var point1 = ee.Geometry.Point([lats[i],longs[i]]);
  Map.addLayer(point1, {color: 'cyan'}, 'geodesic polygon');
}

but for some reason both lats.length and lats[i] (similarly for longs) are undefined. I need to iterate over longs and lats array and mark the points as (latitude, longitude) on my map.

How can I sort this issue?

EDIT: Here is the link to my script.

2
  • It would be great if you could share the link to your actual script that is/isn't working. Jun 3 '19 at 16:24
  • @NishantaKhanal I have added the link to my script.
    – Prachi
    Jun 4 '19 at 6:19
1

The issue here is that you are trying to access server side values as client side values. Processes in GEE are just stacked invocations in client side until a client request something from the server. So, the shapefile that you uploaded will be on server and not in client which needs to be accessed through the APIs that GEE provides.

ee.FeatureCollection.aggregate_array is supposed to return a ee.List object (a server side object) by extracting properties of each feature into a list. So you can not access it as basic javascript array. for example lats.length and lats[i]

[Note: I am assuming you are using lats = featureCollection.aggregate_array('latitude')]

To iterate through the ee.List object you can explore iterate or map function.

But seeing how you just want to add the shapefile to the map, you can simply do

Map.addLayer(featureCollection);
4
  • Thank for the answer, but in my script on adding Map.addLayer(table); shows nothing on the map. I have updated my answer with the link to my script. I just want to see the points marked on my map, but Map.addLayer does not work for me.
    – Prachi
    Jun 4 '19 at 6:21
  • if you do Map.centerObject(table); you can see that your features are properly loading and somewhere in the center of ocean North of Finland. I'm guessing the coordinates in your feature might be wrong or maybe your lat and long was switched when creating the shapefile. Jun 4 '19 at 7:58
  • Removing other parts of your code, if i just want to view your points in map, i can do that in just ` var table = ee.FeatureCollection("users/prachisingh220/exp"); Map.addLayer(table,{},"asdasd"); Map.centerObject(table); ` you can see this in action here Jun 4 '19 at 8:05
  • Yes, you are right. My latitudes and longitudes got interchanged while processing the table. I can see them on the map now but you would observe I have an ID parameter as well in my table corresponding to each coordinate. Is there no way I can get the id marked along with each point? I need to individually label each point into one of three classes which is why I was using iteration in my original code. If I can ID for each point, I can correspond to the class, otherwise it is impossible this way.
    – Prachi
    Jun 5 '19 at 10:53
1

You possibly did swap around your lattitudes and longitudes while preprocessing the table. Here is how to swap around the lattitude and longitude and add the points to the map:

// load table
var table = ee.FeatureCollection("users/prachisingh220/exp");
// swap around the lattitude and longitude
table = table.map(function(feat){
  var lat = feat.get('LONGITUDE');
  var lon = feat.get('LATITUDE');
  return ee.Feature(ee.Geometry.Point([lon, lat]), 
          {LATITUDE: lat, LONGITUDE: lon, ID: feat.get('ID')});
});
print(table)
Map.addLayer(table); Map.centerObject(table);

Link code

5
  • Yes, this works for swapping the longitude and latitude :) But is there no way I can get the id marked along with each point? You would observe I have an ID parameter as well in my table corresponding to each coordinate. I need to individually label each point into one of three classes which is why I was using iteration in my original code. ID of each point would help me correspond the point to the class.
    – Prachi
    Jun 5 '19 at 10:51
  • Width 'style' you can maybe link the size of the point to the features' ID and then have an idea of which one is which? link code (just a simple suggestion, adding text as labels in the GEE is far more difficult)
    – Kuik
    Jun 5 '19 at 19:57
  • Though having different sizes can be a solution for three points, I have 100 points like these. It would be extremely difficult differentiating 100 points of different sizes.
    – Prachi
    Jun 6 '19 at 4:45
  • True, I don't have solution for that. But if you would click in the inspector on the points, the feature you clicked on will appear under 'objects'
    – Kuik
    Jun 6 '19 at 6:13
  • Oh I wasn't aware of the inspector feature. I can make this work for now. Thank you!
    – Prachi
    Jun 6 '19 at 6:21

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.