<jupyter_start><jupyter_text>Getting started with the Playground APIThis Notebook shows how to connect to the Intelligence Playground Python Client API and do amazing stuff!<jupyter_code># Install some extra python module if needed...
#!pip install -r requirements.txt
# Standard imports
import os
import matplotlib.pyplot as plt
import cv2
from PIL import Image
import numpy as np
import mercantile
import requests
import csv
import uuid
import json
import geohash2
import uuid
import warnings
import geojson
import time
warnings.filterwarnings("ignore")

from shapely.geometry import mapping, shape

# Widgets imports
from ipyleaflet import Map, TileLayer, Polygon
import ipywidgets as widgets
from IPython.core.display import HTML<jupyter_output><empty_output><jupyter_text>Connecting to the APITo connect to the API, you need to retrieve your API_KEYS from the OneAtlas website. Follow the simple steps below:1. Visit this URL: https://data.api.oneatlas.airbus.com/api-keys2. Click the **Create and API key** button3. Enter a description for your API_KEY (e.g. Playground Keys)4. Store the file in the same folder than this notebook and name it **api_key.txt**Make sure to keep your **api_key.txt** safe! Do not include it in a public github repository for example :-)The following script will then use this **api_key.txt** file to generate an ACCESS_TOKEN. We will store this ACCESS_TOKEN in HEADERS that we will send with each requests. The ACCESS_TOKEN has a timeout so we will create a function that renew the ACCESS_TOKEN when half of the timeout has expired.   Check connection<jupyter_code># Create a PlaygroundClient object
from playgroundclient import PlaygroundClient
play = PlaygroundClient()
# Logged in user
user = play.get_logged_user()
print("Logged as user: {} {}".format(user['firstname'], user['lastname']))<jupyter_output>Logged as user: <NAME><jupyter_text>Some useful functions<jupyter_code>def print_json(parsed):
    print(json.dumps(parsed, indent=4, sort_keys=True))
    
def getBounds(coords):
    xmax = ymax = -np.Infinity
    xmin = ymin = np.Infinity
    for v in coords:
        xmax = xmax if xmax > v[0] else v[0]
        ymax = ymax if ymax > v[1] else v[1]
        xmin = xmin if xmin < v[0] else v[0]
        ymin = ymin if ymin < v[1] else v[1]
    return (xmin, ymin, xmax, ymax)<jupyter_output><empty_output><jupyter_text>Get available projects for this user<jupyter_code>p_database = {}
p_label = []
r = play.get_projects()
#print(r)
for project in r:
    #print(process)
    project_id = project['project_id']
    p_label.append((project['name'], project_id))
    p_database[project_id] = project
    
p_label.sort() 
p_w = widgets.Dropdown(
    options=p_label,
    description='Project:',
    disabled=False
)
display(p_w)<jupyter_output><empty_output><jupyter_text>Get available datasets in this project<jupyter_code>projectId = p_w.value

d_database = {}
d_label = []
r = play.get_datasets(projectId)
#print_json(r)
for dataset in r['datasets']:
    #print_json(dataset)
    dataset_id = dataset['datasetId']
    d_label.append((dataset['name'], dataset_id))
    d_database[dataset_id] = dataset
    
d_label.sort() 
d_w = widgets.Dropdown(
    options=d_label,
    description='Dataset:',
    disabled=False
)
display(d_w)
# Dataset
datasetId = d_w.value

# display information about this dataset
r = play.get_dataset(datasetId)
#print_json(r)
print("This dataset contains {} zones.".format(r['zones']))
print("This dataset contains {} records.".format(r['records']))
z_database = {}
z_label = []
zones = play.get_zones_in_dataset(datasetId)
#print_json(zones)
for zone in zones:
    zone_id = zone['zone_id']
    z_label.append((zone['name'], zone_id))
    z_database[zone_id] = zone
    
z_label.sort() 
z_w = widgets.Dropdown(
    options=z_label,
    description='Zones:',
    disabled=False
)
display(z_w)<jupyter_output><empty_output><jupyter_text>Get zone bounding box<jupyter_code>zone_id = z_w.value
print("Zone_ID={}".format(zone_id))
print_json(z_database[zone_id])
print_json(z_database[zone_id]['geom']['coordinates'][0])<jupyter_output>Zone_ID=99555067-be79-4ea4-8a9d-ed11076bca55
{
    "created_at": "2019-06-04T13:38:08.549629Z",
    "dataset_id": "b5b41429-3399-4599-953d-3fbbb53d9488",
    "description": null,
    "geom": {
        "coordinates": [
            [
                [
                    -0.751896,
                    44.818977
                ],
                [
                    -0.727501,
                    44.82763
                ],
                [
                    -0.751896,
                    44.829865
                ],
                [
                    -0.751896,
                    44.844888
                ],
                [
                    -0.716858,
                    44.834448
                ],
                [
                    -0.648537,
                    44.884093
                ],
                [
                    -0.611801,
                    44.867063
                ],
                [
                    -0.698662,
                    44.830552
    [...]<jupyter_text>Get records in zone<jupyter_code>bounds = getBounds(z_database[zone_id]['geom']['coordinates'][0])
bbox = ",".join(map(str, bounds))
r = play.get_records_count_in_zone(zone_id, bbox)
#print_json(r)
print("This zone contains {} records.".format(r['count']))<jupyter_output>This zone contains 322 records.
{'fraction_non_alphanumeric': 0.10734046477314645, 'fraction_numerical': 0.03762449280708226, 'mean_word_length': 2.9354136429608126, 'pattern_counts': {'":': 5, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '93151', 'license': 'MIT', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1761, 'n_words': 439, 'path': '01-Getting-Started.ipynb', 'repo_name': 'airbusgeo/playground-python-client', 'size': '5435'}