<jupyter_start><jupyter_text>Search Filters and Stats Endpoint--------------------------------------------------There's a lot of data available on the Planet Data API and in order to find what we're looking for, we'll have to perform various types of searches. To construct a search, we use **filters** that let us limit results based on date, geography or other metadata properties.Using these filters, we are able to search for items or get statistics for search results. Understanding how filters work is key to using Planet's api effectively.  In this exercise, we'll experiment with using filters on the **stats** endpoint to the api before continuing on to searching for scenes.The `/stats` endpoint provides a summary of the available data based on our search criteria.https://api.planet.com/data/v1/statsIn this case, it's a good way to understand how filters work without being overwhelmed with results.  We'll POST a JSON filter definition to the stats endpoint to see how many items match our search criteria.Using a Search Filter-------------------------------Search filters should have the following properties:  * **Type** (`type`) - The type of filter being used  * **Configuration** (`config`) - The configuration for this filter  * **Field Name** (`field_name`) - The field on which to filterFor this example, let's use a filter to get some stats on what data is available for Planet Scope (3 Band) and Rapid Eye (Ortho Tile) products taken from 2013 until now:<jupyter_code>stats_url = 'https://api.planet.com/data/v1/stats'

# Specify the sensors/satellites or "item types" to include in our results
item_types = ["PSScene3Band", "REOrthoTile"]

# Create filter object for all imagery captured between 2013-01-01 and present.
date_filter = {
    "type": "DateRangeFilter", # Type of filter -> Date Range
    "field_name": "acquired", # The field to filter on: "acquired" -> Date on which the "image was taken"
    "config": {
        "gte": "2013-01-01T00:00:00.000Z", # "gte" -> Greater than or equal to
    }
}<jupyter_output><empty_output><jupyter_text>Depending on the filter type, some requests may need an interval field:The following intervals are possible:  * year  * month  * week  * day  * hourAn interval must be provided with the request so that the number of matching items can be aggregated. We'll use an interval with our date filter.  In this case, we'll get the number of `PSScene3Band` + `REOrthoTile` scenes acquired each year from 2013 to present.Now let's perform our request using the date_filter filter we created above:<jupyter_code># Construct the request.
request = {
    "item_types" : item_types,
    "interval" : "year",
    "filter" : date_filter
}

# Send the POST request to the API stats endpoint
res = session.post(stats_url, json=request)

# Print response
pprint(res.json())<jupyter_output><empty_output><jupyter_text>Good Job! You've received a response from the API that contains some statistics like item counts for the search criteria! **Exercise:** Create a new date filter to find data from before 2013.<jupyter_code># Fill in this filter to complete the exercise! 
date_filter2 = {
}

request = {
    "item_types" : item_types,
    "interval" : "year",
    "filter" : date_filter
}

res = session.post(stats_url, json=request)
pprint(res.json())<jupyter_output><empty_output><jupyter_text>---Next, let's take a closer look at some of the available filters: Filter typesThe Planet Data API supports several filter types. The most common are the following: Field Filters* `DateRangeFilter`* `RangeFilter`* `StringInFilter`* `PermissionFilter`* `GeometryFilter` Logical Filters* `NotFilter`* `AndFilter`* `OrFilter`More information and examples on filter types can be found at the [API Docs](https://www.planet.com/docs/reference/data-api/search-api/). --- (Field) Filter Type examples:  `DateRangeFilter`Find imagery that was `acquired` or `published` before, after or between certain dates.```{  "type": "DateRangeFilter",  "field_name": "acquired",  "config": {    "gt": "2016-01-01T00:00:00Z",    "lte": "2016-03-01T00:00:00Z"  }}```The upper or lower bound may be omitted.  `RangeFilter`Find imagery that has a metadata that matches a number within a range of numbers.```{  "type": "RangeFilter",  "field_name": "cloud_cover",  "config": {    "lt": 0.2,    "gt": 0.1  }}``` The following **operators** are supported by the Data API's `DateRangeFilter` and `RangeFilter`:* `gt`: Greater Than* `gte`: Greater Than or Equal To* `lt`: Less Than* `lte`: Less Than or Equal To  `StringInFilter`Find imagery that has a metadata that matches a string within the array of provided strings.```{  "type": "StringInFilter",  "field_name": "instrument",  "config": ["PS2"]}```  `PermissionFilter`Find data which has assets that are accessible by the user.```{  "type": "PermissionFilter",  "config": ["assets.analytic:download"]}```***Note:*** `assets:download` means *any* downloadable asset.  `GeometryFilter`Find data contained within a given geometry. The filter's config value may be any valid GeoJSON object.```{  "type": "GeometryFilter",  "field_name": "geometry",  "config": {    "type": "Polygon",    "coordinates": [      [        [          -120.27282714843749,          38.348118547988065        ],        [          -120.27282714843749,          38.74337300148126        ],        [          -119.761962890625,          38.74337300148126        ],        [          -119.761962890625,          38.348118547988065        ],        [          -120.27282714843749,          38.348118547988065        ]      ]    ]  }}```A few quick ways to get a GeoJSON geometry to use in your search:* Draw an Area of Interst (AOI) in [Planet Explorer](http://planet.com/explorer), and use the API button to see the geometry filter config.* Use your favorite GIS tools like [Quantum GIS (QGIS)](http://www.qgis.org) and export GeoJSON.* Draw a polygon in [GeoJSON.io](http://geojson.io).Make sure the `config` property in the geometry filter is in the right format, which should be similar to a `feature.geometry` property in a GeoJSON object.  ---Let's try a few more requests and get some more stats, this time using different filters:<jupyter_code># Search for imagery only from PlanetScope satellites that have a PS2 telescope (a.k.a "Dove classic")

# Setup item types
item_types = ["PSScene3Band"]

# Setup a filter for instrument type
instrument_filter = {
    "type": "StringInFilter",
    "field_name": "instrument",
    "config": ["PS2"]
}

# Setup the request
request = {
    "item_types" : item_types,
    "interval" : "year",
    "filter" : instrument_filter
}

# Send the POST request to the API stats endpoint
res = session.post(stats_url, json=request)

pprint(res.json())<jupyter_output><empty_output><jupyter_text>**Exercise:** Create a new filter that finds all data from PS2.SD (aka Dove-R) telescopes.<jupyter_code># Fill in this filter to complete the exercise! 

instrument_filter2 = {
}
request = {
    "item_types" : item_types,
    "interval" : "year",
    "filter" : instrument_filter2
}

# Send the POST request to the API stats endpoint
res=session.post(stats_url, json=request)

# Print response
pprint(res.json())<jupyter_output><empty_output><jupyter_text>**Exercise:** Create a filter that finds all scenes of the specified item types for an AOI and get scene counts per year.For this one, we'll create a custom GeoJSON geometry using http://geojson.io. Draw a small polygon and copy one `feature`.<jupyter_code># Search for imagery that only intersects with 40N, 90W

# Setup GeoJSON (use your aoi or this one if you want to skip drawing an AOI)
geom = {
    "type": "Point",
    "coordinates": [
        -90,
         40
    ]
}

# Setup Geometry Filter
geometry_filter = {
    "type": "GeometryFilter",
    "field_name": "geometry",
    "config": "POINT(-90 40)" # Hint: Look closer at this value
}

# Setup the request
request = {
    "item_types" : item_types,
    "filter" : geometry_filter
}

# Send the POST request to the API stats endpoint
res=session.post(stats_url, json=request)

# Print response
pprint(res.json())<jupyter_output><empty_output><jupyter_text>Complex Queries using Logical FiltersComplex queries may require combining field filters using **logical filters** (see Filter Types above).Let's try a complex stats search:<jupyter_code># Setup an "AND" logical filter
and_filter = {
    "type": "AndFilter",
    "config": [instrument_filter, geometry_filter, date_filter]
}

# Print the logical filter
pprint(and_filter)
# Setup the request
request = {
    "item_types" : item_types,
    "interval" : "year",
    "filter" : and_filter
}

# Send the POST request to the API stats endpoint
res=session.post(stats_url, json=request)

# Print response
pprint(res.json())<jupyter_output><empty_output><jupyter_text>Here's another complex search query example using a "Not" logical filter and "string in" field filter. Can you tell what it's requesting?<jupyter_code># Setup Item Types
item_types = ["PSScene4Band"]

# Setup Instrument Filter
instrument_filter = {
    "type": "StringInFilter",
    "field_name": "instrument",
    "config": ["PS2"]
}

# Setup "not" Logical Filter
not_filter = {
    "type": "NotFilter",
    "config": instrument_filter
}

# Setup the request
request = {
    "item_types" : item_types,
    "interval" : "year",
    "filter" : not_filter
}

# Send the POST request to the API stats endpoint
res=session.post(stats_url, json=request)

# Print response
pprint(res.json())<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup