cancel
Showing results for 
Search instead for 
Did you mean: 
Technical Blog
Explore in-depth articles, tutorials, and insights on data analytics and machine learning in the Databricks Technical Blog. Stay updated on industry trends, best practices, and advanced techniques.
cancel
Showing results for 
Search instead for 
Did you mean: 
smthumala
Databricks Employee
Databricks Employee

As we start building agents that deliver business value, it is inevitable that subsets of the data an agent needs are geospatial in nature. When agents are left to make geospatial decisions on their own, they can be accurate at low granularity, i.e., the level of a country or a region, because that coarse knowledge is well represented in the training data of the underlying LLM. 

But built-in knowledge only goes so far. At the level of zip codes or city blocks, agentic reasoning can become less reliable, and the agent needs a semantic layer to stay dependable. In this blog post, we'll cover how Genie Agents work with spatial data and the patterns for building a geospatial semantic layer that improves accuracy.

Why we need a semantic layer

Imagine you're a market intelligence analyst at a large coffee shop chain, and a new competitor just opened a few blocks from one of your stores. You want to understand the impact, so you ask your Genie Agent:

smthumala_0-1792632093444.png

“A new coffee shop just opened in my neighborhood. How will it affect sales at my location?”

Without a semantic layer, the agent might conclude there will be a heavy impact on your sales. The question says "a few blocks away," and a quick spatial distance calculation confirms it, so the agent assumes closer means worse. But distance alone doesn't show what's actually happening around your store.

With a semantic layer, the agent stops guessing and starts working from your business logic. The semantic layer defines what "impact" means, how a store's trade area is drawn, and which signals matter beyond distance, such as population density and local demand. It also tells the agent where that data lives and which geospatial functions to use to compute it. In a dense, high-foot-traffic area with enough demand for two coffee shops, the agent might find that a competitor a few blocks away barely dents your sales. In a quieter area where your store is the only option, the same competitor could take a real share of your customers.

Same question, two opposite answers. The difference isn't the model. It's whether the agent has the definitions, data, and spatial logic to answer the question the way your analysts would. That's what the semantic layer provides.

How Genie Agents handle geospatial data

Genie Agents are conversational AI agents for analytics that answer natural-language questions using curated business semantics, governed Unity Catalog data, and organizational context from tables, dashboards, notebooks, and documentation. Users can refine results through follow-up prompts, while product-provided SQL or custom functions let agents apply domain-specific logic.

smthumala_1-1792632093444.png

 

Right out of the box, a Genie Agent on Databricks gives you two things:

  • A native spatial engine. 
    • Spatial SQL supports GEOMETRY (planar) and GEOGRAPHY (geodetic, WGS 84; in Public Preview) as native column types, with 80+ ST_ functions like ST_Distance, ST_Contains, ST_Intersects, and ST_DWithin that run directly in SQL. 
    • Performance holds up at scale too: Spatial joins run up to 17x faster than on classic clusters running Apache Sedona, and Lakehouse//RT runs geospatial queries up to 38x faster than a traditional cloud data warehouse at high concurrency.
  • Genie Agents generate the spatial SQL for you. When you ask a question, the agent finds the relevant tables, columns, and instructions, plans its approach, and then writes the query. If your tables store locations as GEOMETRY(4326) points, and your Genie Agent has clear column descriptions and example SQL queries, it can generate spatial SQL like an ST_Distance or ST_DWithin query on its own (based on the prompt)

Augmenting Genie Agents with a spatial semantic layer

To answer more complex or layered questions reliably, we will cover three patterns. Let's take the same question a market intelligence analyst would ask: 

“A new coffee shop just opened in my neighborhood. How will it affect sales at my location?”

Pattern 1: Spatial agents need more than a chat box

To aid Genie Agents’ inference, the user’s question needs to be specific. "In my neighborhood" and "across the street" are not locations a function can act on, and if the agent has to interpret them, it is back to guessing. At the same time, the user of the Genie Agent should not be copy-pasting coordinates.

To solve this, pair an interactive map with the Genie Agent. A map click supplies a precise location as coordinates rather than free text. The competitor is no longer somewhere in the neighborhood, it has coordinates that the Genie Agent can work with.

smthumala_2-1792632093445.png

Pattern 2: Break the question into atomic functions

To define competitor impact, you need more than distance. You also need to know whether the competitor falls inside your trade area, and how strongly it pulls your customers away. Each of these factors becomes an atomic spatial SQL function, registered in Unity Catalog and added to the Genie Agent as a trusted asset. 

Genie can't see the function body, so it relies on each function's description and parameter comments to decide which one to call and what to pass in. That makes the metadata as important as the logic. With clear descriptions, the agent can combine the right functions to answer a complex question. For the question above, this would look like:

  • in_trade_area(): a containment test. Is the competitor within my trade area or area of influence?
  • distance_to(): distance measurement to the competitor.
  • competitor_impact(): Uses Huff's gravity model to estimate the probability a shopper chooses your store over the new competitor, based on each store's attractiveness and distance (demand-weighted, comparing just your store and the competitor). The function returns the share of demand you'd lose: 1 − that probability. It's only evaluated when in_trade_area() is true.

Register all three in Unity Catalog, then add them to your Genie Agent under Configure → Sources → Add → SQL functions. Grant EXECUTE on each function to the agent's users. 

in_trade_area(): the gate

CREATE OR REPLACE FUNCTION coffee.spatial.in_trade_area(
  store_id       STRING COMMENT 'Clover store ID from stores.store_id, e.g. CLV-006.',
  competitor_lon DOUBLE COMMENT 'Competitor longitude in decimal degrees (WGS84). West is negative, e.g. -71.0589.',
  competitor_lat DOUBLE COMMENT 'Competitor latitude in decimal degrees (WGS84), e.g. 42.3601.'
) RETURNS BOOLEAN
COMMENT 'GATE: call this first. Returns true if the competitor location falls inside the store''s trade area polygon. In this model a competitor can only affect a store when this is true. If false, report no impact and do not call distance_to or competitor_impact.'
RETURN (
  SELECT BOOL_OR(ST_Contains(s.trade_area, p.g))
  FROM coffee.spatial.stores s
  JOIN (SELECT ST_SetSRID(ST_Point(competitor_lon, competitor_lat), 4326) AS g) p
  WHERE s.store_id = in_trade_area.store_id
);

distance_to(): distance in meters

CREATE OR REPLACE FUNCTION coffee.spatial.distance_to(
  origin_lon DOUBLE COMMENT 'Origin longitude in decimal degrees (WGS84). West is negative, e.g. -71.0589.',
  origin_lat DOUBLE COMMENT 'Origin latitude in decimal degrees (WGS84).',
  target_lon DOUBLE COMMENT 'Target longitude in decimal degrees (WGS84). West is negative, e.g. -71.0589.',
  target_lat DOUBLE COMMENT 'Target latitude in decimal degrees (WGS84).'
) RETURNS DOUBLE
COMMENT 'Straight-line distance in meters between two lon/lat points, measured in the Massachusetts State Plane projection (EPSG:26986). Use to report how far a competitor is from a store. Never use ST_Distance on raw lon/lat, which returns degrees, not meters.'
RETURN ST_Distance(
  ST_Transform(ST_SetSRID(ST_Point(origin_lon, origin_lat), 4326), 26986),
  ST_Transform(ST_SetSRID(ST_Point(target_lon, target_lat), 4326), 26986)
);

competitor_impact(): Huff's model

CREATE OR REPLACE FUNCTION coffee.spatial.competitor_impact(
  store_id STRING COMMENT 'Clover store ID from stores.store_id, e.g. CLV-006.',
  competitor_lon DOUBLE COMMENT 'Competitor longitude in decimal degrees (WGS84). West is negative, e.g. -71.0589.',
  competitor_lat DOUBLE COMMENT 'Competitor latitude in decimal degrees (WGS84), e.g. 42.3601.',
  competitor_attractiveness DOUBLE COMMENT 'Competitor attractiveness score from competitors.attractiveness, on the same scale as stores.attractiveness.',
  distance_decay DOUBLE DEFAULT 2.0 COMMENT 'Huff distance-decay exponent. Higher means shoppers care more about distance; 1.5 to 2 is typical.'
) RETURNS DOUBLE
COMMENT 'Huff gravity model. Returns the demand-weighted share (0 to 1) of the store''s trade-area demand captured by the competitor, comparing only this store and the competitor. Higher means more sales at risk; above 0.30 is significant. 1 - impact is the share of demand the store keeps. Only call this when in_trade_area() is true.'
RETURN (
  SELECT SUM(dp.demand_weight * (
           (prm.ca / POWER(GREATEST(d.d_comp, 1.0), prm.dd)) /
           ( s.attractiveness / POWER(GREATEST(d.d_store, 1.0), prm.dd)
           + prm.ca           / POWER(GREATEST(d.d_comp,  1.0), prm.dd) )
         )) / SUM(dp.demand_weight)
  FROM coffee.spatial.stores s
  JOIN coffee.spatial.demand_points dp
    ON ST_Contains(s.trade_area, dp.location)
  JOIN (SELECT competitor_lon AS clon, competitor_lat AS clat,
               competitor_attractiveness AS ca, distance_decay AS dd) prm
  JOIN LATERAL (
    SELECT
      coffee.spatial.distance_to(ST_X(dp.location), ST_Y(dp.location), ST_X(s.location), ST_Y(s.location)) AS d_store,
      coffee.spatial.distance_to(ST_X(dp.location), ST_Y(dp.location), prm.clon, prm.clat)                 AS d_comp
  ) d
  WHERE s.store_id = competitor_impact.store_id
);

Pattern 3: Composition

Multi-step spatial reasoning is a documented soft spot for agents, purely due to the complexity of the logic. Genie Agents, however, can infer the order in which to call functions from their comments. To make the chaining more rigid, define a composition that specifies the order of the functions. This matters even more when the logic is sensitive to order.

For example, checking in_trade_area first can short-circuit the chain: if the competitor is not inside your trade area, it does not affect your location, and nothing else needs to run. If the order of operations is reversed and distance_to runs first, the agent wastes a call measuring a distance it may not need. At large scales of data and higher complexity of questions, this could dramatically slow down the response time.

smthumala_3-1792632093445.png

To summarize

Genie Agents can handle simple geospatial natural-language questions out of the box, using native geography types, ST_ functions, and H3 functions. For more complex logic, augment your Genie Agent with a spatial semantic layer built on three patterns:

smthumala_4-1792632093446.png

Together, these patterns move the agent to dependable, computed answers, no matter how granular the geography gets.

In Part 2, we'll go from a single question to a full competitor intelligence workflow that detects new threats, measures their impact, and guides your action.

Relevant sources: 

Geospatial Unbounded: Spatial SQL GA with AI/BI Maps, Delta Sharing, and Iceberg v3 | Databricks Blo...

Geospatial Queries up to 38X faster with Lakehouse//RT | Databricks Blog

GeoGenie 2: Ask your map a question in plain Engli... - Databricks Community

Tune Genie Agent quality - Azure Databricks | Microsoft Learn