|
| 1 | +--- |
| 2 | +title: 'Map location data with Axiom and Hex' |
| 3 | +description: "This page exlains how to visualize geospatial log data from Axiom using Hex interactive maps." |
| 4 | +sidebarTitle: 'Hex' |
| 5 | +--- |
| 6 | + |
| 7 | +import Prerequisites from "/snippets/standard-prerequisites.mdx" |
| 8 | +import ReplaceDatasetToken from "/snippets/replace-dataset-token.mdx" |
| 9 | +import ReplaceDomain from "/snippets/replace-domain.mdx" |
| 10 | +import ReplaceDataset from "/snippets/replace-dataset.mdx" |
| 11 | + |
| 12 | +Hex is a powerful collaborative data platform that allows you to create notebooks with Python/SQL code and interactive visualizations. |
| 13 | + |
| 14 | +This page explains how to integrate Hex with Axiom to visualize geospatial data from your logs. You ingest location data into Axiom, query it using APL, and create interactive map visualizations in Hex. |
| 15 | + |
| 16 | +<Prerequisites /> |
| 17 | +{/* list separator */} |
| 18 | +- [Create a Hex account](https://app.hex.tech/). |
| 19 | + |
| 20 | +## Send geospatial data to Axiom |
| 21 | + |
| 22 | +Send your sample location data to Axiom using the API endpoint. For example, the following HTTP request sends sample robot location data with latitude, longitude, status, and satellite information. |
| 23 | + |
| 24 | +```bash |
| 25 | +curl -X 'POST' 'https://AXIOM_DOMAIN/v1/datasets/DATASET_NAME/ingest' \ |
| 26 | +-H 'Authorization: Bearer API_TOKEN' \ |
| 27 | +-H 'Content-Type: application/json' \ |
| 28 | +-d '[ |
| 29 | + { |
| 30 | + "data": { |
| 31 | + "robot_id": "robot-001", |
| 32 | + "latitude": 37.7749, |
| 33 | + "longitude": -122.4194, |
| 34 | + "num_satellites": 8, |
| 35 | + "status": "active" |
| 36 | + } |
| 37 | + } |
| 38 | +]' |
| 39 | +``` |
| 40 | + |
| 41 | +<ReplaceDatasetToken /> |
| 42 | +<ReplaceDomain /> |
| 43 | + |
| 44 | +Verify that your data has been ingested correctly by running an APL query in the Axiom UI. |
| 45 | + |
| 46 | +## Set up your Hex project |
| 47 | + |
| 48 | +1. Create a new Hex project. For more information, see the [Hex documentation](https://learn.hex.tech/docs/getting-started/create-your-first-project). |
| 49 | +1. Save your Axiom API token as a secret in Hex. This example uses the secret name `AXIOM_API_TOKEN`. For more information, see the [Hex documentation](https://learn.hex.tech/docs/explore-data/projects/environment-configuration/environment-views#secrets). |
| 50 | + |
| 51 | +## Query data from Axiom |
| 52 | + |
| 53 | +Write the Python code in your Hex notebook that retrieves data from Axiom. For example, customize the code below: |
| 54 | + |
| 55 | +```python |
| 56 | +import requests |
| 57 | +import pandas as pd |
| 58 | +from datetime import datetime, timedelta |
| 59 | +import os |
| 60 | + |
| 61 | +# Retrieve the API token from Hex secrets |
| 62 | +axiom_token = os.environ.get("AXIOM_API_TOKEN") |
| 63 | + |
| 64 | +# Define Axiom API endpoint and headers |
| 65 | +base_url = "https://AXIOM_DOMAIN/v1/datasets/_apl" |
| 66 | +headers = { |
| 67 | + 'Authorization': f'Bearer {axiom_token}', |
| 68 | + 'Content-Type': 'application/json', |
| 69 | + 'Accept': 'application/json', |
| 70 | + 'Accept-Encoding': 'gzip' |
| 71 | +} |
| 72 | + |
| 73 | +# Define the time range for your query |
| 74 | +end_time = datetime.utcnow() |
| 75 | +start_time = end_time - timedelta(days=3) # Get data from the last 3 days |
| 76 | + |
| 77 | +# Construct the APL query |
| 78 | +query = { |
| 79 | + "apl": """DATASET_NAME |
| 80 | +| project ['data.latitude'], ['data.longitude'], ['data.num_satellites'], ['data.robot_id'], ['data.status']""", |
| 81 | + "startTime": start_time.strftime("%Y-%m-%dT%H:%M:%SZ"), |
| 82 | + "endTime": end_time.strftime("%Y-%m-%dT%H:%M:%SZ") |
| 83 | +} |
| 84 | + |
| 85 | +try: |
| 86 | + # Send the request to Axiom API |
| 87 | + response = requests.post( |
| 88 | + f"{base_url}?format=tabular", |
| 89 | + headers=headers, |
| 90 | + json=query, |
| 91 | + timeout=10 |
| 92 | + ) |
| 93 | + |
| 94 | + # Print request details for debugging |
| 95 | + print("Request Details:") |
| 96 | + print(f"URL: {base_url}?format=tabular") |
| 97 | + print(f"Query: {query['apl']}") |
| 98 | + print(f"Response Status: {response.status_code}") |
| 99 | + |
| 100 | + if response.status_code == 200: |
| 101 | + data = response.json() |
| 102 | + if 'tables' in data: |
| 103 | + table = data['tables'][0] |
| 104 | + if table.get('columns') and len(table['columns']) > 0: |
| 105 | + columns = [field['name'] for field in table['fields']] |
| 106 | + rows = table['columns'] |
| 107 | + |
| 108 | + # Create DataFrame with proper column orientation |
| 109 | + df = pd.DataFrame(list(zip(*rows)), columns=columns) |
| 110 | + |
| 111 | + # Ensure data types are appropriate for mapping |
| 112 | + df['data.latitude'] = pd.to_numeric(df['data.latitude']) |
| 113 | + df['data.longitude'] = pd.to_numeric(df['data.longitude']) |
| 114 | + df['data.num_satellites'] = pd.to_numeric(df['data.num_satellites']) |
| 115 | + |
| 116 | + # Display the first few rows to verify our data |
| 117 | + print("\nDataFrame Preview:") |
| 118 | + display(df.head()) |
| 119 | + |
| 120 | + # Store the DataFrame for visualization |
| 121 | + robot_locations = df |
| 122 | + else: |
| 123 | + print("\nNo data found in the specified time range.") |
| 124 | + else: |
| 125 | + print("\nNo tables found in response") |
| 126 | + print("Response structure:", data.keys()) |
| 127 | + |
| 128 | +except Exception as e: |
| 129 | + print(f"\nError: {str(e)}") |
| 130 | +``` |
| 131 | + |
| 132 | +<ReplaceDomain /> |
| 133 | +<ReplaceDataset /> |
| 134 | + |
| 135 | +## Create map visualisation |
| 136 | + |
| 137 | +Create an interactive map visualization in Hex and customize it. For more information, see the [Hex documentation](https://learn.hex.tech/docs/explore-data/cells/visualization-cells/map-cells). |
0 commit comments