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.
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:
“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.
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.
Right out of the box, a Genie Agent on Databricks gives you two things:
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?”
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.
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:
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
);
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.
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:
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 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.