In the previous article Read, calculate and update answer, you saw how to create and read values. Now, lets see an example where you can read and write location values, and read and write Images. 


Let's get straight to an example as you are familiar with the basics of the script. 

import os
import uuid
from io import BytesIO

from PIL import Image
from PIL.ImageShow import show
from kes.client import Client, Config
from geopy.distance import geodesic
from generated import site_a_table_def, site_b_table_def, SiteBRow, distance_asset_table_def, DistanceAssetRow

config = Config(
    kes_service_address='kes-table-service-training.delightfuldesert-b9ce0345.westeurope.azurecontainerapps.io:443',
    access_token='meEm359HTJ')
client = Client(config)
activity_id = uuid.UUID('caadb114-9eae-47b4-af5d-24355e9dcec1')
activity = client.open_activity_by_id(activity_id=activity_id)

# Load Site A Values
site_a_table = activity.build_table(site_a_table_def)
site_a_table.load()
if len(site_a_table) == 0:
    exit("There are no values to read from KES. Please add site_a location in KES activity.")

# Read location data, Each location can contain multiple points. Hence, you need to access points using index.
location_a = site_a_table[0].location.get_points()[0]
print(location_a.name, location_a.latitude, location_a.longitude, location_a.address)

# Read Image from Site A
image_data = site_a_table.load_image(site_a_table[0].location_image)
if image_data is None:
    raise RuntimeError("No image found.")
image = Image.open(BytesIO(bytes(image_data)))
show(image, "Test image")

#Load Site B table and add location to it
site_b_table = activity.build_table(site_b_table_def)

site_b_table.load()

if len(site_b_table) > 0:
    site_b_table.clear()

site_b_row = SiteBRow()
# Write location data, You can add multiple points by calling add_point function on location property.
site_b_row.location.add_point(address="Meppel - Assen",
                              latitude=52.77035725849205,
                              longitude=6.49079267010161,
                              name="Switch")

# Add an image to Site B
scriptPath = os.path.realpath(__file__)
filePath = os.path.join(os.path.dirname(scriptPath), "pingu.jpeg")
imageFile = open(filePath, "rb")
site_b_table.save_image(site_b_row.location_image, "Image name", imageFile.read())

# Add the row and get a reference
ref = site_b_table.append_row(site_b_row)

#Load Site B to fetch the added location point
site_b_table.load()
location_b = site_b_table[0].location.get_points()[0]

# Calculate distance between site_a and site_b
distance_a_b = geodesic((location_a.latitude, location_a.longitude), (location_b.latitude, location_b.longitude)).km

distance_table = activity.build_table(distance_asset_table_def)
distance_table.load()
if len(distance_table) > 0:
    distance_table.clear()

ditance_row = DistanceAssetRow(
    distance=distance_a_b
)

ref = distance_table.append_row(ditance_row)

Here are the steps that is followed in the example above.

  1. We are reading Site A location.
  2. Reading Image from Site A 
  3. Add location and Image to Site B
  4. Calculate distance between Site A to Site B and then update it in distance_row.