pythongoogle-earthgoogle-earth-engine

combining filters in Google Earth Engine from feature collection


I don't think I have seen this asked and unfortunately this is probably a simple answer that I am getting stuck with.

I have a feature collection and I am simply trying to filter two properties from that feature collection simultaneously. The feature collections are a list of projects located across the USA and so I am trying to visually display two such projects at the same time. I have no problem filtering one of the projects out of the feature collection and displaying it in Google Earth Engine but I am having trouble trying to filter a second project area and display it at the same time as the first project area.

My code in Python 3 Jupyter Notebook looks like this (of course after having initialized GEE and so on)

I tried using Filter.and based on the notes from GEE which states to use and as a way to grab multiple filtered information but I must be doing something wrong so below is what I tried at first but this didnt work.

How can I rewrite this to properly grab the two projects from the FeatureCollection at the same time?

Thanks in advance for the help and the time.

fc = (
ee.FeatureCollection("File location here")
.filter('PROJ A == "PROJECT NAME"')
.filter('PROJ B == "PROJECT MAME"')
)

# Draw the visualization

wtmap = geemap.Map()
# set our initial map parameters for USA
center_lat = 40
center_lon = -100
zoomlevel = 5

# Initialize the map
wtmap = geemap.Map(center = [center_lat, center_lon], zoom = zoomlevel)
wtmap.add_basemap("SATELLITE")
wtmap.addLayer(fc)
wtmap.addLayerControl()
wtmap

Solution

  • I would use the function ee.Filter.And:

    # FeatureCollection
    listOfFeatures = [
      ee.Feature(None, {'PROJ A': 'PROJECT NAME', 'PROJ B': 'PROJECT NAME'}),
      ee.Feature(None, {'PROJ A': 'PROJECT', 'PROJ B': 'NAME'})
    ]
    fc = ee.FeatureCollection(listOfFeatures)
    
    fcsubset = fc.filter(ee.Filter.And(
        ee.Filter.eq('PROJ A', 'PROJECT NAME'),
        ee.Filter.eq('PROJ B', 'PROJECT NAME')
    ))
    

    Inside the GEE Python API, as @Mutewinter mentioned in this post, the help(ee.Filter) command shows the available methods for ee.Filter:

    And(*args)
    Combine two or more filters using boolean AND
    Or(*args)
    Combine two or more filters using boolean OR.

    Note the capital letters in And/Or methods.

    I hope this answer helps you.