Skip to content
Advertisement

Is there another way to convert ee.Number to float except getInfo()?

Hello friends!

Summarization: I got a ee.FeatureCollection containing around 8500 ee.Point-objects. I would like to calculate the distance of these points to a given coordinate, lets say (0.0, 0.0). For this i use the function geopy.distance.distance() (ref: https://geopy.readthedocs.io/en/latest/#module-geopy.distance). As input the the function takes 2 coordinates in the form of 2 tuples containing 2 floats.

Problem: When i am trying to convert the coordinates in form of an ee.List to float, i always use the getinfo() function. I know this is a callback and it is very time intensive but i don’t know another way to extract them. Long story short: To extract the data as ee.Number it takes less than a second, if i want them as float it takes more than an hour. Is there any trick to fix this?

Code:

fc_containing_points = ee.FeatureCollection('projects/ee-philadamhiwi/assets/Flensburg_100') #ee.FeatureCollection
list_containing_points = fc_containing_points.toList(fc_containing_points.size()) #ee.List
fc_containing_points_length = fc_containing_points.size() #ee.Number
for index in range(fc_containing_points_length.getInfo()): #i need to convert ee.Number to int
  point_tmp = list_containing_points.get(i) #ee.ComputedObject
  point = ee.Feature(point_tmp) #transform ee.ComputedObject to ee.Feature
  coords = point.geometry().coordinates() #ee.List containing 2 ee.Numbers 
  #when i run the loop with this function without the next part 
  #i got all the data i want as ee.Number in under 1 sec
  coords_as_tuple_of_ints = (coords.getInfo()[1],coords.getInfo()[0]) #tuple containing 2 floats
  #when i add this part to the function it takes hours

PS: This is my first question, pls be patient with me.

Advertisement

Answer

I would use .map instead of your looping. This stays server side until you export the table (or possibly do a .getInfo on the whole thing)

fc_containing_points = ee.FeatureCollection('projects/eephiladamhiwi/assets/Flensburg_100') 

fc_containing_points.map(lambda feature: feature.set("distance_to_point", feature.distance(ee.Feature(ee.Geometry.Point([0.0,0.0])))

# Then export using ee.batch.Export.Table.toXXX or call getInfo

(An alternative might be to useee.Image.paint to convert the target point to an image then, use ee.Image.distance to calculate the distance to the point (as an image), then use reduceRegions over the feature collection with all points but 1) you can only calculate distance to a certain distance and 2) I don’t think it would be any faster.)

To comment on your code, you are probably aware loops (especially client side loops) are frowned upon in GEE (primarily for the performance reasons you’ve run into) but also note that any time you call .getInfo on a server side object it incurs a performance cost. So this line

coords_as_tuple_of_ints = (coords.getInfo()[1],coords.getInfo()[0])

Would take roughly double the time as this

coords_client = coords.getInfo()
coords_as_tuple_of_ints = (coords_client[1],coords_client[0]) 

Finally, you could always just export your entire feature collection to a shapefile (using ee.batch.Export.Table.... as above) and do all the operations using geopy locally.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement