javascriptpythonpython-2.7google-earth-engine

Translating Google Earth Engine to Python API: Spatial joins


I am currently translating my GEE from Javascript to the Python API. One initial problem I have encountered is with spatial joins. I essentially have a shapefile and a series of random points, and I am trying to produce a feature collection which reports the values from the shapefile for each point. The syntax is as follows:


#1. A shapefile of sub-Saharan Africa, which I made open for public usage 
SSA = ee.FeatureCollection('users/salem043/Africa_Districts')

#2. 100 random points within the SSA shapefile
points = ee.FeatureCollection.randomPoints(SSA, 100)

#3. The properties I want to retain from the SSA shapefile (admin. districts)
properties = ["ADM0","ADM1", "ADM2"]

#4. This spatial filter function which used to work in Javascript
spatialFilter = ee.Filter.intersects({leftField: '.geo', rightField: '.geo'})

#5. Using the spatial filter and the join command I then create a feature var 

joinAll = ee.Join.saveAll('matched').apply(points, SSA, spatialFilter)


For the SSA shapefile, the link is: https://code.earthengine.google.com/?asset=users/salem043/Africa_Districts

The error I receive happens in Step 4: Python reports that "name 'leftField' is not defined". If you can please help me figure out how to convert the Javascript in Step 4 (and Step 5, if that is going to be a problem too) into Python, I would be most grateful!


Solution

  • With Python, you need to surround dict keys with".
    Replace
    spatialFilter = ee.Filter.intersects({leftField: '.geo', rightField: '.geo'})
    by
    spatialFilter = ee.Filter.intersects({"leftField": '.geo', "rightField": '.geo'})