<jupyter_start><jupyter_text>Part 1 - Data collection https://developers.planet.com/tutorials/index2.html To make it easy, I'll use geojson.io to quickly draw a shape & generate GeoJSON output for our box:<jupyter_code># # Stockton, CA bounding box (created via geojson.io) 
# geojson_geometry = {
#   "type": "Polygon",
#   "coordinates": [
#     [ 
#       [-121.59290313720705, 37.93444993515032],
#       [-121.27017974853516, 37.93444993515032],
#       [-121.27017974853516, 38.065932950547484],
#       [-121.59290313720705, 38.065932950547484],
#       [-121.59290313720705, 37.93444993515032]
#     ]
#   ]
# }



# Los Angeles

geojson_geometry = {
        "type": "Polygon",
        "coordinates": [
          
            [-118.71773970000001,34.2047156],
            [-118.71773970000001,34.224715599999996],
            [-118.6977397,34.224715599999996],
            [-118.6977397,34.2047156],
            [-118.71773970000001,34.2047156]
              
              
          
        ]
      }
# Stockton, CA bounding box (created via geojson.io) 
geojson_geometry = {
  "type": "Polygon",
  "coordinates": [
    [ 
      [-121.59290313720705, 37.93444993515032],
      [-121.27017974853516, 37.93444993515032],
      [-121.27017974853516, 38.065932950547484],
      [-121.59290313720705, 38.065932950547484],
      [-121.59290313720705, 37.93444993515032]
    ]
  ]
}
geojson_geometry<jupyter_output><empty_output><jupyter_text>Now let's set up some filters to further constrain our Data API search:<jupyter_code># get images that overlap with our AOI 
geometry_filter = {
  "type": "GeometryFilter",
  "field_name": "geometry",
  "config": geojson_geometry
}

# get images acquired within a date range
date_range_filter = {
  "type": "DateRangeFilter",
  "field_name": "acquired",
  "config": {
    "gte": "2018-11-01T00:00:00.000Z",
    "lte": "2018-11-21T00:00:00.000Z"
  }
}

# only get images which have <50% cloud coverage
cloud_cover_filter = {
  "type": "RangeFilter",
  "field_name": "cloud_cover",
  "config": {
    "lte": 0.5
  }
}

# combine our geo, date, cloud filters
combined_filter = {
  "type": "AndFilter",
  "config": [geometry_filter, date_range_filter, cloud_cover_filter]
}<jupyter_output><empty_output><jupyter_text>Planet's products are categorized as items and assets: an item is a single picture taken by a satellite at a certain time. Items have multiple asset types including the image in different formats, along with supporting metadata files.For this demonstration, let's get a satellite image that is best suited for visual applications; e.g., basemaps or visual analysis. Since we're not doing any spectral analysis outside of the visual range, we only need a 3-band (RGB) image. To get the image we want, we will specify an item type of PSScene3Band, and asset type visual.You can learn more about item & asset types in Planet's Data API here.Now let's search for all the items that match our filters:<jupyter_code>import os
import json
import requests
from requests.auth import HTTPBasicAuth

# API Key stored as an env variable
PLANET_API_KEY = os.getenv("PL_API_KEY")

item_type = "PSScene3Band"

# API request object
search_request = {
  
  "item_types": [item_type], 
  "filter": combined_filter
}

# fire off the POST request
search_result = \
  requests.post(
    'https://api.planet.com/data/v1/quick-search',
    auth=HTTPBasicAuth(PLANET_API_KEY, ''),
    json=search_request)

#print(json.dumps(search_result.json(), indent=1))

json = search_result;
json<jupyter_output><empty_output><jupyter_text>Our search returns metadata for all of the images within our AOI that match our date range and cloud coverage filters. It looks like there are multiple images here; let's extract a list of just those image IDs:<jupyter_code># extract image IDs only
image_ids = [feature['id'] for feature in search_result.json()['features']]
print(image_ids)<jupyter_output>['20181120_182423_1035', '20181120_182422_1035', '20181120_182421_1035', '20181120_182420_1035', '20181119_182534_101b', '20181119_182347_103c', '20181119_182346_103c', '20181119_182345_103c', '20181119_182531_101b', '20181118_175900_1048', '20181118_175858_1048', '20181118_175857_1048', '20181118_175859_1048', '20181118_182243_1027', '20181118_182450_1038', '20181118_182449_1038', '20181118_182448_1038', '20181118_182447_1038', '20181117_182022_0e20', '20181117_182021_0e20', '20181114_182534_102e', '20181114_182533_102e', '20181114_182532_102e', '20181114_182431_1022', '20181114_182430_1022', '20181114_182429_1022', '20181114_182349_1044', '20181114_182348_1044', '20181112_182025_0e0f', '20181112_182027_0e0f', '20181112_182514_1010', '20181112_182513_1010', '20181112_182512_1010', '20181112_182026_0e0f', '20181111_180148_0f21', '20181111_180146_0f21', '20181111_182135_0e16', '20181111_182133_0e16', '20181111_180147_0f21', '20181110_180125_1051', '20181109_182459_1039', '20181109_18245[...]<jupyter_text>Since we just want a single image, and this is only a demonstration, for our purposes here we can arbitrarily select the first image in that list. Let's do that, and get the asset list available for that image:<jupyter_code># For demo purposes, just grab the first image ID
id0 = image_ids[0]
id0_url = 'https://api.planet.com/data/v1/item-types/{}/items/{}/assets'.format(item_type, id0)

# Returns JSON metadata for assets in this ID. Learn more: planet.com/docs/reference/data-api/items-assets/#asset
result = \
  requests.get(
    id0_url,
    auth=HTTPBasicAuth(PLANET_API_KEY, '')
  )

# List of asset types available for this particular satellite image
print(result.json().keys())<jupyter_output>dict_keys(['analytic', 'analytic_dn', 'analytic_dn_xml', 'analytic_xml', 'basic_analytic', 'basic_analytic_dn', 'basic_analytic_dn_rpc', 'basic_analytic_dn_xml', 'basic_analytic_rpc', 'basic_analytic_xml', 'basic_udm', 'udm', 'visual', 'visual_xml'])<jupyter_text>Activation and DownloadingThe Data API does not pre-generate assets, so they are not always immediately availiable to download. In order to download an asset, we first have to activate it.Remember, earlier we decided we wanted a color-corrected image best suited for visual applications. We can check the status of the visual asset we want to download like so:<jupyter_code># This is "inactive" if the "visual" asset has not yet been activated; otherwise 'active'
print(result.json()['visual']['status'])<jupyter_output>active<jupyter_text>Let's now go ahead and activate that asset for download:<jupyter_code># Parse out useful links
links = result.json()[u"visual"]["_links"]
self_link = links["_self"]
activation_link = links["activate"]

# Request activation of the 'visual' asset:
activate_result = \
  requests.get(
    activation_link,
    auth=HTTPBasicAuth(PLANET_API_KEY, '')
  )<jupyter_output><empty_output><jupyter_text>At this point, we wait for the activation status for the asset we are requesting to change from inactive to active. We can monitor this by polling the "status" of the asset:<jupyter_code>activation_status_result = \
  requests.get(
    self_link,
    auth=HTTPBasicAuth(PLANET_API_KEY, '')
  )
    
print(activation_status_result.json()["status"])<jupyter_output>active<jupyter_text>Once the asset has finished activating (status is "active"), we can download it. Note: the download link on an active asset is temporary<jupyter_code># Image can be downloaded by making a GET with your Planet API key, from here:
download_link = activation_status_result.json()["location"]
print(download_link)<jupyter_output>https://api.planet.com/data/v1/download?token=<KEY>
{'fraction_non_alphanumeric': 0.11067245690222398, 'fraction_numerical': 0.16423213580734308, 'mean_word_length': 5.104417670682731, 'pattern_counts': {'":': 22, '<': 34, '<?xml version=': 0, '>': 33, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '523150', 'license': 'MIT', 'n_tokens_mistral': 3415, 'n_tokens_neox': 2617, 'n_words': 665, 'path': 'codes/archived/(archive) P01_Data_Collection.ipynb', 'repo_name': 'saiedmighani/Damage_classification_post_fire_satellite', 'size': '8022'}