<jupyter_start><jupyter_text><jupyter_code># Reproducible-ish dependencies
!pip install \
    pandas==1.3.5 \
    requests==2.23.0 \
    geopy==1.17.0 \
    imageio==2.4.1 \
    Pillow==7.1.2

from google.colab import drive
drive.mount('/content/drive')
from getpass import getpass
API_KEY = getpass('Enter the google maps API key: ')<jupyter_output>Enter the google maps API key: ··········<jupyter_text>Get pickled data<jupyter_code>import pickle


with open('/content/drive/MyDrive/all_gps_data.pkl', 'rb') as f:
  all_gps_data = pickle.load(f)

training_data = [trajectory 
                 for trajectory in all_gps_data["train"]
                 if len(trajectory[1]) != 0]<jupyter_output><empty_output><jupyter_text>The distance between the first and last points in a trajectory<jupyter_code>from itertools import groupby

import geopy.distance
import pandas as pd


pd.set_option('display.float_format', lambda x: '%.3f' % x)

first_and_last_points = [(
                  (trajectory[1][0]["latitude"], trajectory[1][0]["longitude"]),   # # The coordinates of the first point in the trajectory.
                  (trajectory[1][-1]["latitude"], trajectory[1][-1]["longitude"])  # The coordinates of the last point in the trajectory.
                  ) 
                  for trajectory in training_data]    
distance_between_frist_and_last_point_in_each_trajectory = [
                round(geopy.distance.vincenty(x, y).meters)
                for (x, y) in first_and_last_points]


df = pd.DataFrame({"Distance between first andf last point in trajectories (m)": distance_between_frist_and_last_point_in_each_trajectory})

more_than_50_m_first_last = df[df['Distance between first andf last point in trajectories (m)'] >= 50]
more_than_50_m_first_last.describe()<jupyter_output><empty_output><jupyter_text>The total distance of trajectory<jupyter_code>total_trajectory_distance = [
                             (trajectory[0], geopy.distance.vincenty(
                              (x["latitude"], x["longitude"]), 
                              (y["latitude"], y["longitude"])).meters
                              )
                             for trajectory in training_data
                             for x, y in zip(trajectory[1], trajectory[1][1:])
                            ]

total_distance_of_each_trajectory = pd.DataFrame(
                                                total_trajectory_distance,
                                                columns=['Trajectory', 'Total Distance (m)']
                                                ).groupby('Trajectory').sum()
more_than_50_m_total = total_distance_of_each_trajectory[total_distance_of_each_trajectory["Total Distance (m)"] >= 50]
more_than_50_m_total.describe()
# with open('/content/drive/MyDrive/total_distance_df.pkl', 'wb') as f:
#   pickle.dump(more_than_50_m_total, f)<jupyter_output><empty_output><jupyter_text>The results are colse so we are going forward.<jupyter_code>selected_trajectories_names = more_than_50_m_total[more_than_50_m_total["Total Distance (m)"] <= 2500].index.to_list()
selected_trajectories_names = set(selected_trajectories_names)  # for faster lookup
selected_trajecotories = [trajectory for trajectory in training_data if trajectory[0] in selected_trajectories_names]<jupyter_output><empty_output><jupyter_text>Store `selected_trajectories` to avoid recomputation<jupyter_code>with open('/content/drive/MyDrive/selected_trajectories.pkl', 'wb') as f:
  pickle.dump(selected_trajecotories, f)<jupyter_output><empty_output><jupyter_text>Read the stored `selected_trajectories` file<jupyter_code># with open('/content/drive/MyDrive/selected_trajectories.pkl', 'rb') as f:
#   selected_trajectories = pickle.load(f)<jupyter_output><empty_output><jupyter_text>What disatance google image can cover at `zoom=20`<jupyter_code>import io

import numpy as np
import PIL
import imageio
import requests



def mark_trajectory(points, *,zoom, scale, size):
  base_url = "https://maps.googleapis.com/maps/api/staticmap"
  key=API_KEY
  markers = '|'.join(f'{point["latitude"]}, {point["longitude"]}' for point in points)

  img_url = f"{base_url}?key={key}&zoom={zoom}&scale={scale}" \
                f"&size={size}&maptype=satellite&format=png" \
                f"&visual_refresh=true&markers={markers}"
  return PIL.Image.open(
      io.BytesIO(requests.get(img_url).content)).convert('RGB')

mark_trajectory(training_data[6][1][1:9], zoom=20, scale=1, size="800x800")
first_point = training_data[6][1][1]
last_point = training_data[6][1][8]
geopy.distance.vincenty(
    (first_point["latitude"], first_point["longitude"]),
    (last_point["latitude"], last_point["longitude"])
    ).meters<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12185318383752909, 'fraction_numerical': 0.016077850645229534, 'mean_word_length': 3.6765578635014835, 'pattern_counts': {'":': 1, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '419642', 'license': 'Unlicense', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1533, 'n_words': 277, 'path': 'select_initial_trajectories.ipynb', 'repo_name': 'AbdulrhmnGhanem/bdd100k-trajectories', 'size': '4727'}