Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Maps support static viz (#75387)

Nick Fitzpatrick89ec6adf08b9
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index e2cd2b8f19d..5121440d068 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -492,6 +492,7 @@
driver
events
formatter
+ geojson
lib
lib-be
models
@@ -505,6 +506,7 @@
slackbot
system
task
+ tiles
timeline
types
util}
@@ -993,7 +995,8 @@
geojson
{:team "UX West"
:api #{metabase.geojson.api
- metabase.geojson.init}
+ metabase.geojson.init
+ metabase.geojson.settings}
:uses #{api
config
permissions
@@ -2264,7 +2267,8 @@
tiles
{:team "UX West"
:api #{metabase.tiles.api
- metabase.tiles.init}
+ metabase.tiles.init
+ metabase.tiles.settings}
:uses #{api
legacy-mbql
lib
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_Custom_Region.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_Custom_Region.png
new file mode 100644
index 00000000000..682019f7d10
Binary files /dev/null and b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_Custom_Region.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_No_Data.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_No_Data.png
new file mode 100644
index 00000000000..ad897fc1468
Binary files /dev/null and b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_No_Data.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_US_States.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_US_States.png
new file mode 100644
index 00000000000..d97e4c6e241
Binary files /dev/null and b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_US_States.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_World_Countries.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_World_Countries.png
new file mode 100644
index 00000000000..826843a185d
Binary files /dev/null and b/.loki/reference/chrome_laptop_Viz_Static_Viz_StaticChoropleth_World_Countries.png differ
diff --git a/e2e/test/scenarios/sharing/subscriptions.cy.spec.js b/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
index 360ba4347d9..bb40501f86e 100644
--- a/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
+++ b/e2e/test/scenarios/sharing/subscriptions.cy.spec.js
@@ -527,6 +527,40 @@ describe("scenarios > dashboard > subscriptions", () => {
expect(email.html).to.include(questionDetails.name);
});
});
+
+ it("renders a region (choropleth) map as an image in a subscription email", () => {
+ const questionDetails = {
+ name: "Region map static-viz smoke",
+ native: {
+ query:
+ "SELECT 'CA' AS state, 99999 AS metric " +
+ "UNION ALL SELECT 'NY' AS state, 11111 AS metric",
+ },
+ display: "map",
+ visualization_settings: {
+ "map.type": "region",
+ "map.region": "us_states",
+ "map.dimension": "STATE",
+ "map.metric": "METRIC",
+ },
+ };
+
+ H.createNativeQuestionAndDashboard({ questionDetails }).then(
+ ({ dashboardId }) => {
+ assignRecipient({ dashboard_id: dashboardId });
+ },
+ );
+
+ H.sendEmailAndAssert(({ html }) => {
+ expect(html).not.to.include(
+ "An error occurred while displaying this card.",
+ );
+ // The map rasterizes to a PNG <img>; a table fallback would instead leak these values as text.
+ expect(html).to.include("<img");
+ expect(html).not.to.include("99999");
+ expect(html).not.to.include("11111");
+ });
+ });
});
describe("with Slack set up", () => {
diff --git a/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.stories.tsx b/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.stories.tsx
new file mode 100644
index 00000000000..91f88533ec2
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.stories.tsx
@@ -0,0 +1,48 @@
+import type { StoryFn } from "@storybook/react";
+
+import { createStaticRenderingContext } from "metabase/static-viz/lib/rendering-context";
+
+import {
+ StaticChoropleth,
+ type StaticChoroplethProps,
+} from "./StaticChoropleth";
+import { customRegion, noData, usStates, worldCountries } from "./stories-data";
+
+export default {
+ title: "Viz/Static Viz/StaticChoropleth",
+ component: StaticChoropleth,
+};
+
+const renderingContext = createStaticRenderingContext();
+
+const Template: StoryFn<StaticChoroplethProps> = (args) => {
+ return (
+ <div style={{ border: "1px solid black", display: "inline-block" }}>
+ <StaticChoropleth {...args} />
+ </div>
+ );
+};
+
+// us_states uses the geoAlbersUsa projection (with its composite Alaska/Hawaii insets).
+export const USStates = {
+ render: Template,
+ args: { ...usStates, renderingContext },
+};
+
+// Every other region (built-in or custom) uses geoMercator().fitWidth.
+export const WorldCountries = {
+ render: Template,
+ args: { ...worldCountries, renderingContext },
+};
+
+// A user-defined custom region with a non-default region_key, projected via Mercator + fitWidth.
+export const CustomRegion = {
+ render: Template,
+ args: { ...customRegion, renderingContext },
+};
+
+// No rows join to a feature: every region falls to the no-data gray and no legend is drawn.
+export const NoData = {
+ render: Template,
+ args: { ...noData, renderingContext },
+};
diff --git a/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.tsx b/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.tsx
new file mode 100644
index 00000000000..1953d1f3779
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.tsx
@@ -0,0 +1,159 @@
+import { geoAlbersUsa, geoMercator, geoPath } from "d3";
+import type { Feature } from "geojson";
+
+import {
+ HEAT_MAP_ZERO_COLOR,
+ buildColorScale,
+ getDefaultMapDimension,
+ getDefaultMapMetric,
+ getLegendTitles,
+} from "metabase/visualizations/lib/choropleth";
+import type {
+ ComputedVisualizationSettings,
+ RenderingContext,
+} from "metabase/visualizations/types";
+import type { GeoJSONData, RawSeries } from "metabase-types/api";
+
+import {
+ MAP_WIDTH,
+ getFeatureKey,
+ getFeatures,
+ getRegionValues,
+ toRgb,
+} from "./utils";
+
+type GeoJSONDetails = {
+ region_key: string;
+ region_name: string;
+};
+
+export interface StaticChoroplethProps {
+ rawSeries: RawSeries;
+ settings: ComputedVisualizationSettings;
+ geoJson: GeoJSONData;
+ geoJsonDetails: GeoJSONDetails;
+ renderingContext: RenderingContext;
+}
+
+export const StaticChoropleth = ({
+ rawSeries,
+ settings,
+ geoJson,
+ geoJsonDetails,
+ renderingContext,
+}: StaticChoroplethProps) => {
+ const [
+ {
+ data: { cols, rows },
+ },
+ ] = rawSeries;
+
+ const region = settings["map.region"];
+ // Fall back to the same defaults the live Map visualization computes, since a subscription card may
+ // not have these settings persisted (the backend pins map.region for us).
+ const dimensionName =
+ settings["map.dimension"] ?? getDefaultMapDimension(cols);
+ const metricName = settings["map.metric"] ?? getDefaultMapMetric(cols);
+ const dimensionIndex = cols.findIndex((col) => col.name === dimensionName);
+ const metricIndex = cols.findIndex((col) => col.name === metricName);
+
+ // Albers for the US (its Alaska/Hawaii insets are tuned for it), Mercator otherwise. fitWidth fits the
+ // projection to the GeoJSON's own bounds, so any region works (built-in or custom) without a hardcoded frame.
+ const projection = (
+ region === "us_states" ? geoAlbersUsa() : geoMercator()
+ ).fitWidth(MAP_WIDTH, geoJson);
+
+ const keyProperty = geoJsonDetails.region_key;
+
+ const valuesMap = getRegionValues(rows, dimensionIndex, metricIndex, region);
+ const getFeatureValue = (feature: Feature): number | undefined =>
+ valuesMap[getFeatureKey(feature, keyProperty)];
+
+ const domain = Array.from(new Set(Object.values(valuesMap)));
+ const hasData = domain.length > 0;
+ // HEAT_MAP_ZERO_COLOR is already hex (Batik-safe); only map.colors below needs the hsl()->rgb() pass.
+ const zeroColor = HEAT_MAP_ZERO_COLOR;
+
+ // buildColorScale runs ckmeans, which needs a non-empty domain — guard the no-data case so every
+ // feature falls to zeroColor.
+ const { colorScale, groups, heatMapColors } = hasData
+ ? buildColorScale(domain, settings["map.colors"], toRgb)
+ : {
+ colorScale: (_value: number) => zeroColor,
+ groups: [] as number[][],
+ heatMapColors: [] as string[],
+ };
+
+ const getColor = (feature: Feature): string => {
+ const value = getFeatureValue(feature);
+ return value == null ? zeroColor : colorScale(value);
+ };
+
+ const columnSettings =
+ (metricIndex >= 0 && settings.column?.(cols[metricIndex])) || {};
+ const legendTitles = hasData ? getLegendTitles(groups, columnSettings) : [];
+
+ const geo = geoPath(projection);
+ const [[minX, minY], [maxX, maxY]] = geo.bounds(geoJson);
+ const mapWidth = maxX - minX;
+ const mapHeight = maxY - minY;
+
+ // Legend strip rendered below the map, in the same projected coordinate space.
+ const legendGap = mapHeight * 0.04;
+ const swatchHeight = mapHeight * 0.045;
+ const fontSize = mapHeight * 0.035;
+ const legendY = maxY + legendGap;
+ const labelY = legendY + swatchHeight + fontSize * 1.2;
+ const itemWidth =
+ legendTitles.length > 0 ? mapWidth / legendTitles.length : 0;
+ const totalHeight = mapHeight + legendGap + swatchHeight + fontSize * 1.6;
+
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width={mapWidth}
+ height={totalHeight}
+ viewBox={`${minX} ${minY} ${mapWidth} ${totalHeight}`}
+ >
+ <g>
+ {getFeatures(geoJson).map((feature, index) => (
+ <path
+ key={index}
+ d={geo(feature) ?? undefined}
+ stroke="white"
+ strokeWidth={1}
+ fill={getColor(feature)}
+ />
+ ))}
+ </g>
+ {legendTitles.length > 0 && (
+ <g>
+ {legendTitles.map((title, index) => {
+ const x = minX + index * itemWidth;
+ return (
+ <g key={index}>
+ <rect
+ x={x}
+ y={legendY}
+ width={itemWidth}
+ height={swatchHeight}
+ fill={heatMapColors[index]}
+ />
+ <text
+ x={x + itemWidth / 2}
+ y={labelY}
+ fontSize={fontSize}
+ fontFamily="Lato, sans-serif"
+ textAnchor="middle"
+ fill={renderingContext.getColor("text-secondary")}
+ >
+ {title}
+ </text>
+ </g>
+ );
+ })}
+ </g>
+ )}
+ </svg>
+ );
+};
diff --git a/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.unit.spec.tsx b/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.unit.spec.tsx
new file mode 100644
index 00000000000..6e7767d4363
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/StaticChoropleth/StaticChoropleth.unit.spec.tsx
@@ -0,0 +1,157 @@
+/* eslint-disable testing-library/render-result-naming-convention --
+ These tests use ReactDOMServer.renderToStaticMarkup (server-side string render), not an RTL render,
+ so the "view"/"utils" naming convention doesn't apply. */
+import type { FeatureCollection } from "geojson";
+import ReactDOMServer from "react-dom/server";
+
+import type {
+ ComputedVisualizationSettings,
+ RenderingContext,
+} from "metabase/visualizations/types";
+import type { RawSeries } from "metabase-types/api";
+
+import { StaticChoropleth } from "./StaticChoropleth";
+
+const renderingContext = {
+ getColor: (name: string) =>
+ name === "text-secondary" ? "#888888" : "#000000",
+ formatValue: (value: unknown) => String(value),
+ measureText: () => 0,
+ fontFamily: "Lato",
+} as unknown as RenderingContext;
+
+// Two simple square "states" so d3.geoPath has coordinates to project.
+const geoJson: FeatureCollection = {
+ type: "FeatureCollection",
+ features: [
+ {
+ type: "Feature",
+ properties: { STATE: "CA", NAME: "California" },
+ geometry: {
+ type: "Polygon",
+ coordinates: [
+ [
+ [-122, 37],
+ [-121, 37],
+ [-121, 38],
+ [-122, 38],
+ [-122, 37],
+ ],
+ ],
+ },
+ },
+ {
+ type: "Feature",
+ properties: { STATE: "NY", NAME: "New York" },
+ geometry: {
+ type: "Polygon",
+ coordinates: [
+ [
+ [-74, 40],
+ [-73, 40],
+ [-73, 41],
+ [-74, 41],
+ [-74, 40],
+ ],
+ ],
+ },
+ },
+ ],
+};
+
+const rawSeries = [
+ {
+ card: { display: "map" },
+ data: {
+ cols: [
+ { name: "state", base_type: "type/Text", semantic_type: "type/State" },
+ {
+ name: "count",
+ base_type: "type/Integer",
+ semantic_type: "type/Quantity",
+ },
+ ],
+ rows: [
+ ["CA", 100],
+ // Provide NY by full name to exercise getCanonicalRowKey name->code mapping.
+ ["New York", 25],
+ ],
+ },
+ },
+] as unknown as RawSeries;
+
+const settings: ComputedVisualizationSettings = {
+ "map.region": "us_states",
+ "map.dimension": "state",
+ "map.metric": "count",
+};
+
+const toSvgString = (vizSettings: ComputedVisualizationSettings = settings) =>
+ ReactDOMServer.renderToStaticMarkup(
+ <StaticChoropleth
+ rawSeries={rawSeries}
+ settings={vizSettings}
+ geoJson={geoJson}
+ geoJsonDetails={{ region_key: "STATE", region_name: "NAME" }}
+ renderingContext={renderingContext}
+ />,
+ );
+
+describe("StaticChoropleth", () => {
+ it("renders a root <svg> (so the pipeline rasterizes it to PNG)", () => {
+ expect(toSvgString().startsWith("<svg")).toBe(true);
+ });
+
+ it("renders one <path> per GeoJSON feature", () => {
+ const matches = toSvgString().match(/<path/g) ?? [];
+ expect(matches).toHaveLength(2);
+ });
+
+ it("colors regions that have data (not the no-data gray) and matches by canonical key", () => {
+ const svg = toSvgString();
+ // Both CA (code) and "New York" (full name -> NY) should join and get heat-map colors.
+ // Colors are emitted as rgb() (Batik can't parse hsl()); #0061B5 === rgb(0, 97, 181).
+ expect(svg).toContain('fill="rgb(0, 97, 181)"'); // darkest bucket, highest value (CA = 100)
+ });
+
+ it("emits colors as rgb(), never hsl() (Batik can't parse hsl)", () => {
+ // map.colors can arrive as hsl() from getColorplethColorScale; they must be converted.
+ const svg = toSvgString({
+ "map.region": "us_states",
+ "map.dimension": "state",
+ "map.metric": "count",
+ "map.colors": ["hsl(210, 100%, 88%)", "hsl(210, 100%, 36%)"],
+ });
+ expect(svg).not.toContain("hsl(");
+ expect(svg).toMatch(/fill="rgb\(/);
+ });
+
+ it("renders a legend with formatted value ranges", () => {
+ const svg = toSvgString();
+ expect(svg).toContain("<text");
+ // Legend bins span the two data points.
+ expect(svg).toMatch(/25|100/);
+ // Legend labels use the rendering context color as-is (already Batik-safe hex), not re-converted.
+ expect(svg).toContain('fill="#888888"');
+ });
+
+ it("renders a custom (non-us_states) region via Mercator + fitWidth", () => {
+ // Any region other than us_states uses geoMercator().fitWidth, which fits arbitrary GeoJSON bounds —
+ // this is what makes user-defined custom maps work without hardcoded projection frames.
+ const svg = toSvgString({
+ "map.region": "my_custom_region",
+ "map.dimension": "state",
+ "map.metric": "count",
+ });
+ expect(svg.startsWith("<svg")).toBe(true);
+ expect(svg.match(/<path/g) ?? []).toHaveLength(2);
+ });
+
+ it("defaults map.dimension/map.metric from column types when they aren't persisted", () => {
+ // Only map.region is set (the backend pins it); dimension/metric must be inferred from columns.
+ const svg = toSvgString({ "map.region": "us_states" });
+ expect(svg).toContain('fill="rgb(0, 97, 181)"'); // CA still resolves and gets the darkest bucket
+ const paths = svg.match(/<path/g) ?? [];
+ expect(paths).toHaveLength(2);
+ });
+});
diff --git a/frontend/src/metabase/static-viz/components/StaticChoropleth/index.ts b/frontend/src/metabase/static-viz/components/StaticChoropleth/index.ts
new file mode 100644
index 00000000000..b058705afff
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/StaticChoropleth/index.ts
@@ -0,0 +1 @@
+export { StaticChoropleth } from "./StaticChoropleth";
diff --git a/frontend/src/metabase/static-viz/components/StaticChoropleth/stories-data.ts b/frontend/src/metabase/static-viz/components/StaticChoropleth/stories-data.ts
new file mode 100644
index 00000000000..c073e609060
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/StaticChoropleth/stories-data.ts
@@ -0,0 +1,141 @@
+import type { FeatureCollection } from "geojson";
+
+// Real built-in maps (via the `assets` alias) give recognizable Loki references and exercise the projection
+// against real (Multi)Polygon geometry. Faithful because the component only ever receives parsed GeoJSON.
+import usStatesGeoJson from "assets/geojson/us-states.json";
+import worldCountriesGeoJson from "assets/geojson/world.json";
+import type { ComputedVisualizationSettings } from "metabase/visualizations/types";
+import type { RawSeries } from "metabase-types/api";
+
+// A user-defined custom region (non-us_states, non-default region_key): exercises the Mercator + fitWidth branch.
+const customRegionGeoJson: FeatureCollection = {
+ type: "FeatureCollection",
+ features: [
+ {
+ type: "Feature",
+ properties: { ZONE: "north", LABEL: "North" },
+ geometry: {
+ type: "Polygon",
+ coordinates: [
+ [
+ [0, 10],
+ [10, 10],
+ [10, 5],
+ [0, 5],
+ [0, 10],
+ ],
+ ],
+ },
+ },
+ {
+ type: "Feature",
+ properties: { ZONE: "south", LABEL: "South" },
+ geometry: {
+ type: "Polygon",
+ coordinates: [
+ [
+ [0, 5],
+ [10, 5],
+ [10, 0],
+ [0, 0],
+ [0, 5],
+ ],
+ ],
+ },
+ },
+ ],
+};
+
+const stateCols = [
+ { name: "state", base_type: "type/Text", semantic_type: "type/State" },
+ { name: "count", base_type: "type/Integer", semantic_type: "type/Quantity" },
+];
+
+const makeRawSeries = (cols: unknown[], rows: unknown[][]): RawSeries =>
+ [
+ {
+ card: { display: "map" },
+ data: { cols, rows },
+ },
+ ] as unknown as RawSeries;
+
+const usStatesSettings: ComputedVisualizationSettings = {
+ "map.region": "us_states",
+ "map.dimension": "state",
+ "map.metric": "count",
+};
+
+const worldSettings: ComputedVisualizationSettings = {
+ "map.region": "world_countries",
+ "map.dimension": "country",
+ "map.metric": "count",
+};
+
+const customSettings: ComputedVisualizationSettings = {
+ "map.region": "sales_zones",
+ "map.dimension": "zone",
+ "map.metric": "count",
+};
+
+export const usStates = {
+ geoJson: usStatesGeoJson,
+ geoJsonDetails: { region_key: "STATE", region_name: "NAME" },
+ settings: usStatesSettings,
+ // "New York" given by full name to exercise getCanonicalRowKey name -> code mapping.
+ rawSeries: makeRawSeries(stateCols, [
+ ["CA", 100],
+ ["New York", 25],
+ ]),
+};
+
+export const worldCountries = {
+ geoJson: worldCountriesGeoJson,
+ geoJsonDetails: { region_key: "ISO_A2", region_name: "NAME" },
+ settings: worldSettings,
+ rawSeries: makeRawSeries(
+ [
+ {
+ name: "country",
+ base_type: "type/Text",
+ semantic_type: "type/Country",
+ },
+ {
+ name: "count",
+ base_type: "type/Integer",
+ semantic_type: "type/Quantity",
+ },
+ ],
+ [
+ ["US", 4200],
+ ["CA", 1700],
+ ],
+ ),
+};
+
+export const customRegion = {
+ geoJson: customRegionGeoJson,
+ geoJsonDetails: { region_key: "ZONE", region_name: "LABEL" },
+ settings: customSettings,
+ rawSeries: makeRawSeries(
+ [
+ { name: "zone", base_type: "type/Text" },
+ {
+ name: "count",
+ base_type: "type/Integer",
+ semantic_type: "type/Quantity",
+ },
+ ],
+ [
+ ["north", 8],
+ ["south", 3],
+ ],
+ ),
+};
+
+// Same region as `usStates` but no rows join — exercises the all-gray, no-legend (no-data) branch.
+export const noData = {
+ geoJson: usStatesGeoJson,
+ geoJsonDetails: { region_key: "STATE", region_name: "NAME" },
+ settings: usStatesSettings,
+ rawSeries: makeRawSeries(stateCols, []),
+};
diff --git a/frontend/src/metabase/static-viz/components/StaticChoropleth/utils.ts b/frontend/src/metabase/static-viz/components/StaticChoropleth/utils.ts
new file mode 100644
index 00000000000..d780e89bd13
--- /dev/null
+++ b/frontend/src/metabase/static-viz/components/StaticChoropleth/utils.ts
@@ -0,0 +1,45 @@
+import Color from "color";
+import type { Feature, FeatureCollection } from "geojson";
+
+import { getCanonicalRowKey } from "metabase/visualizations/lib/region-codes";
+import type { GeoJSONData, RowValue } from "metabase-types/api";
+
+// Color constants and legend helpers are shared with the runtime ChoroplethMap via
+// metabase/visualizations/lib/choropleth (a Leaflet-free module the static-viz bundle can load).
+
+// Internal projection resolution, not output size — the SVG is rasterized at a fixed width downstream.
+export const MAP_WIDTH = 1000;
+
+// Batik requires rgb colors
+export const toRgb = (color: string): string => Color(color).rgb().string();
+
+export function isFeatureCollection(
+ value: GeoJSONData,
+): value is FeatureCollection {
+ return value.type === "FeatureCollection";
+}
+
+export function getFeatures(geoJson: GeoJSONData): Feature[] {
+ return isFeatureCollection(geoJson) ? geoJson.features : [geoJson];
+}
+
+export function getFeatureKey(feature: Feature, keyProperty: string): string {
+ return String(feature.properties?.[keyProperty]).toLowerCase();
+}
+
+// Sum each row's metric by canonical region key (for feature lookup); non-numeric metrics count as 0.
+export function getRegionValues(
+ rows: RowValue[][],
+ dimensionIndex: number,
+ metricIndex: number,
+ region: string | undefined,
+): Record<string, number> {
+ const valuesMap: Record<string, number> = {};
+ for (const row of rows) {
+ const key = getCanonicalRowKey(row[dimensionIndex], region);
+ const value = row[metricIndex];
+ valuesMap[key] =
+ (valuesMap[key] || 0) + (typeof value === "number" ? value : 0);
+ }
+ return valuesMap;
+}
diff --git a/frontend/src/metabase/static-viz/index.tsx b/frontend/src/metabase/static-viz/index.tsx
index 84ffa510a80..4e04f2db3ac 100644
--- a/frontend/src/metabase/static-viz/index.tsx
+++ b/frontend/src/metabase/static-viz/index.tsx
@@ -8,6 +8,7 @@ import ReactDOMServer from "react-dom/server";
import enterpriseOverrides from "ee-overrides";
import "metabase/utils/dayjs";
+import { StaticChoropleth } from "metabase/static-viz/components/StaticChoropleth";
import { StaticVisualization } from "metabase/static-viz/components/StaticVisualization";
import { LegacyStaticChart } from "metabase/static-viz/containers/LegacyStaticChart";
import type { LegacyStaticChartType } from "metabase/static-viz/containers/LegacyStaticChart/LegacyStaticChart";
@@ -18,6 +19,7 @@ import { updateStartOfWeek } from "metabase/utils/i18n";
import MetabaseSettings from "metabase/utils/settings";
import { extractRemappings, isCartesianChart } from "metabase/visualizations";
import { extendCardWithDashcardSettings } from "metabase/visualizations/lib/settings/typed-utils";
+import type { ComputedVisualizationSettings } from "metabase/visualizations/types";
import {
createDataSource,
getVisualizationColumns,
@@ -32,6 +34,7 @@ import type {
DatasetData,
DayOfWeekId,
FormattingSettings,
+ GeoJSONData,
RawSeries,
SettingKey,
TokenFeatures,
@@ -174,6 +177,32 @@ export function RenderChart(
const hasDevWatermark = Boolean(options.tokenFeatures.development_mode);
+ // Region (choropleth) maps render via a standalone SVG component rather than StaticVisualization,
+ // because the "map" visualization isn't registered in the static-viz bundle (it depends on Leaflet).
+ // The backend resolves the built-in GeoJSON and embeds it in dashcardSettings.
+ if (rawSeriesWithRemappings[0].card.display === "map") {
+ const extraSettings = dashcardSettings as Record<string, unknown>;
+ const geoJson = extraSettings["map._geojson"] as GeoJSONData | undefined;
+ const geoJsonDetails = extraSettings["map._geojson_details"] as
+ | { region_key: string; region_name: string }
+ | undefined;
+
+ if (geoJson && geoJsonDetails) {
+ return ReactDOMServer.renderToStaticMarkup(
+ <StaticChoropleth
+ rawSeries={rawSeriesWithRemappings}
+ settings={
+ rawSeriesWithRemappings[0].card
+ .visualization_settings as ComputedVisualizationSettings
+ }
+ geoJson={geoJson}
+ geoJsonDetails={geoJsonDetails}
+ renderingContext={renderingContext}
+ />,
+ );
+ }
+ }
+
return ReactDOMServer.renderToStaticMarkup(
<StaticVisualization
rawSeries={rawSeriesWithRemappings}
diff --git a/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx b/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx
index d0ec4719f62..c703b289b00 100644
--- a/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx
+++ b/frontend/src/metabase/visualizations/components/ChoroplethMap.tsx
@@ -4,7 +4,6 @@ import * as d3 from "d3";
import type { Feature, FeatureCollection } from "geojson";
import type L from "leaflet";
import { useEffect, useState } from "react";
-import ss from "simple-statistics";
import { jt, t } from "ttag";
import _ from "underscore";
@@ -17,8 +16,12 @@ import type { State } from "metabase/redux/store";
import { getUserIsAdmin } from "metabase/selectors/user";
import { Flex, Text } from "metabase/ui";
import MetabaseSettings from "metabase/utils/settings";
+import {
+ HEAT_MAP_ZERO_COLOR,
+ buildColorScale,
+ getLegendTitles,
+} from "metabase/visualizations/lib/choropleth";
import { MinColumnsError } from "metabase/visualizations/lib/errors";
-import { formatValue } from "metabase/visualizations/lib/formatting";
import {
computeMinimalBounds,
getCanonicalRowKey,
@@ -28,7 +31,6 @@ import {
getMinSize,
} from "metabase/visualizations/shared/utils/sizes";
import type {
- ColumnSettings,
VisualizationDefinition,
VisualizationProps,
} from "metabase/visualizations/types";
@@ -45,12 +47,6 @@ import { ChartWithLegend } from "./ChartWithLegend";
import { LeafletChoropleth } from "./LeafletChoropleth";
import { LegacyChoropleth } from "./LegacyChoropleth";
-// TODO COLOR
-// eslint-disable-next-line metabase/no-color-literals
-const HEAT_MAP_COLORS = ["#C4E4FF", "#81C5FF", "#51AEFF", "#1E96FF", "#0061B5"];
-// eslint-disable-next-line metabase/no-color-literals
-const HEAT_MAP_ZERO_COLOR = "#CCC";
-
type ColorScaleOptions = {
lightness?: number;
darken?: number;
@@ -107,43 +103,6 @@ function loadGeoJson(
});
}
-export function getLegendTitles(
- groups: number[][],
- columnSettings: ColumnSettings,
-): string[] {
- const formatMetric = (value: number, compact: boolean): string =>
- String(formatValue(value, { ...columnSettings, compact }));
-
- const compact = shouldUseCompactFormatting(groups, formatMetric);
-
- return groups.map((group, index) => {
- const min = formatMetric(group[0], compact);
- const max = formatMetric(group[group.length - 1], compact);
- return index === groups.length - 1
- ? `${min} +` // the last value in the list
- : min !== max
- ? `${min} - ${max}` // typical case
- : min; // special case to avoid zero-width ranges e.g. $88-$88
- });
-}
-
-// if the average formatted length is greater than this, we switch to compact formatting
-const AVERAGE_LENGTH_CUTOFF = 5;
-
-function shouldUseCompactFormatting(
- groups: number[][],
- formatMetric: (value: number, compact: boolean) => string,
-): boolean {
- const minValues = groups.map(([x]) => x);
- const maxValues = groups.slice(0, -1).map((group) => group[group.length - 1]);
- const allValues = minValues.concat(maxValues);
- const formattedValues = allValues.map((value) => formatMetric(value, false));
- const averageLength =
- formattedValues.reduce((sum, { length }) => sum + length, 0) /
- formattedValues.length;
- return averageLength > AVERAGE_LENGTH_CUTOFF;
-}
-
type ChoroplethStateProps = {
sdkMetabaseInstanceUrl?: string;
};
@@ -309,28 +268,6 @@ function buildValuesMap(
return valuesMap;
}
-function buildColorScale(
- domain: number[],
- settingsColors: string[] | undefined,
-): {
- colorScale: (value: number) => string;
- groups: number[][];
- heatMapColors: string[];
-} {
- const baseColors = settingsColors ?? HEAT_MAP_COLORS;
- const heatMapColors = baseColors.slice(-domain.length);
-
- const groups = ss.ckmeans(domain, heatMapColors.length);
- const groupBoundaries = groups.slice(1).map((cluster) => cluster[0]);
-
- const colorScale = d3
- .scaleThreshold<number, string>()
- .domain(groupBoundaries)
- .range(heatMapColors);
-
- return { colorScale, groups, heatMapColors };
-}
-
function computeAspectRatio(
projection: Projection,
projectionFrame: ProjectionFrame | null,
diff --git a/frontend/src/metabase/visualizations/components/ChoroplethMap.unit.spec.ts b/frontend/src/metabase/visualizations/components/ChoroplethMap.unit.spec.ts
index 7277b0a4d53..8b33a47b5dd 100644
--- a/frontend/src/metabase/visualizations/components/ChoroplethMap.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/components/ChoroplethMap.unit.spec.ts
@@ -1,8 +1,6 @@
import { mockIsEmbeddingSdk } from "metabase/embedding-sdk/mocks/config-mock";
-import {
- getLegendTitles,
- getMapUrl,
-} from "metabase/visualizations/components/ChoroplethMap";
+import { getMapUrl } from "metabase/visualizations/components/ChoroplethMap";
+import { getLegendTitles } from "metabase/visualizations/lib/choropleth";
import type { ColumnSettings } from "metabase/visualizations/types";
const currencyColumnSettings: ColumnSettings = {
diff --git a/frontend/src/metabase/visualizations/lib/choropleth.ts b/frontend/src/metabase/visualizations/lib/choropleth.ts
new file mode 100644
index 00000000000..f5a3a436c7e
--- /dev/null
+++ b/frontend/src/metabase/visualizations/lib/choropleth.ts
@@ -0,0 +1,99 @@
+import * as d3 from "d3";
+import ss from "simple-statistics";
+
+import { formatValue } from "metabase/visualizations/lib/formatting";
+import type { ColumnSettings } from "metabase/visualizations/types";
+import {
+ isCountry,
+ isDimension,
+ isMetric,
+ isState,
+} from "metabase-lib/v1/types/utils/isa";
+import type { DatasetColumn } from "metabase-types/api";
+
+// Shared choropleth (region map) helpers, kept Leaflet-free (unlike the runtime ChoroplethMap) so the
+// static-viz bundle — which runs in GraalJS where Leaflet can't load — can use them too.
+export function getDefaultMapDimension(
+ cols: DatasetColumn[],
+): string | undefined {
+ const geoDimension = cols.find((col) => isCountry(col) || isState(col));
+ return geoDimension?.name ?? cols.find(isDimension)?.name;
+}
+
+export function getDefaultMapMetric(cols: DatasetColumn[]): string | undefined {
+ return cols.find(isMetric)?.name;
+}
+
+/* eslint-disable metabase/no-color-literals */
+export const HEAT_MAP_COLORS = [
+ "#C4E4FF",
+ "#81C5FF",
+ "#51AEFF",
+ "#1E96FF",
+ "#0061B5",
+];
+export const HEAT_MAP_ZERO_COLOR = "#CCC";
+/* eslint-enable metabase/no-color-literals */
+
+// if the average formatted length is greater than this, we switch to compact formatting
+const AVERAGE_LENGTH_CUTOFF = 5;
+
+// `transformColor` is applied to every emitted color; defaults to identity. The static-viz renderer
+// passes a hsl()->rgb() converter because its rasterizer can't parse hsl() (see toRgb).
+export function buildColorScale(
+ domain: number[],
+ settingsColors: string[] | undefined,
+ transformColor: (color: string) => string = (color) => color,
+): {
+ colorScale: (value: number) => string;
+ groups: number[][];
+ heatMapColors: string[];
+} {
+ const heatMapColors = (settingsColors ?? HEAT_MAP_COLORS)
+ .slice(-domain.length)
+ .map(transformColor);
+
+ const groups = ss.ckmeans(domain, heatMapColors.length);
+ const groupBoundaries = groups.slice(1).map((cluster) => cluster[0]);
+
+ const colorScale = d3
+ .scaleThreshold<number, string>()
+ .domain(groupBoundaries)
+ .range(heatMapColors);
+
+ return { colorScale, groups, heatMapColors };
+}
+
+export function getLegendTitles(
+ groups: number[][],
+ columnSettings: ColumnSettings,
+): string[] {
+ const formatMetric = (value: number, compact: boolean): string =>
+ String(formatValue(value, { ...columnSettings, compact }));
+
+ const compact = shouldUseCompactFormatting(groups, formatMetric);
+
+ return groups.map((group, index) => {
+ const min = formatMetric(group[0], compact);
+ const max = formatMetric(group[group.length - 1], compact);
+ return index === groups.length - 1
+ ? `${min} +` // the last value in the list
+ : min !== max
+ ? `${min} - ${max}` // typical case
+ : min; // special case to avoid zero-width ranges e.g. $88-$88
+ });
+}
+
+function shouldUseCompactFormatting(
+ groups: number[][],
+ formatMetric: (value: number, compact: boolean) => string,
+): boolean {
+ const minValues = groups.map(([x]) => x);
+ const maxValues = groups.slice(0, -1).map((group) => group[group.length - 1]);
+ const allValues = minValues.concat(maxValues);
+ const formattedValues = allValues.map((value) => formatMetric(value, false));
+ const averageLength =
+ formattedValues.reduce((sum, { length }) => sum + length, 0) /
+ formattedValues.length;
+ return averageLength > AVERAGE_LENGTH_CUTOFF;
+}
diff --git a/frontend/src/metabase/visualizations/lib/mapping.ts b/frontend/src/metabase/visualizations/lib/mapping.ts
index c3f83d5fd86..390516a955c 100644
--- a/frontend/src/metabase/visualizations/lib/mapping.ts
+++ b/frontend/src/metabase/visualizations/lib/mapping.ts
@@ -2,9 +2,9 @@ import * as d3 from "d3";
import type { Feature, Position } from "geojson";
import L from "leaflet";
-import type { RowValue } from "metabase-types/api";
-
-import { COUNTRY_NAME_TO_CODE, STATE_CODES } from "./mapping_codes";
+// getCanonicalRowKey lives in a Leaflet-free module so the static-viz bundle can use it; re-exported
+// here to keep existing import sites working.
+export { getCanonicalRowKey } from "./region-codes";
export function computeMinimalBounds(features: Feature[]) {
const points = getAllFeaturesPoints(features);
@@ -41,28 +41,3 @@ export function getAllFeaturesPoints(features: Feature[]): Position[] {
return points;
}
-
-const stateNamesMap = new Map(
- STATE_CODES.map(([key, name]) => [name.toLowerCase(), key.toLowerCase()]),
-);
-
-/**
- * Canonicalizes row values to match those in the GeoJSONs.
- *
- * Currently transforms US state names to state codes for the "us_states" region map, and just lowercases all others.
- */
-export function getCanonicalRowKey(key: RowValue, region?: string): string {
- const normalizedKey = String(key).toLowerCase();
- // Special case for supporting both US state names and state codes
- // This should be ok because we know there's no overlap between state names and codes, and we know the "us_states" region map expects codes
- if (region === "us_states" && stateNamesMap.has(normalizedKey)) {
- return stateNamesMap.get(normalizedKey) as string; // ok to cast because 1 line above we check presence of the key in the map
- } else if (
- region === "world_countries" &&
- normalizedKey in COUNTRY_NAME_TO_CODE
- ) {
- return COUNTRY_NAME_TO_CODE[normalizedKey];
- } else {
- return normalizedKey;
- }
-}
diff --git a/frontend/src/metabase/visualizations/lib/region-codes.ts b/frontend/src/metabase/visualizations/lib/region-codes.ts
new file mode 100644
index 00000000000..9b256c5903d
--- /dev/null
+++ b/frontend/src/metabase/visualizations/lib/region-codes.ts
@@ -0,0 +1,30 @@
+import type { RowValue } from "metabase-types/api";
+
+import { COUNTRY_NAME_TO_CODE, STATE_CODES } from "./mapping_codes";
+
+// This module deliberately avoids importing Leaflet (unlike ./mapping) so it can be used from the
+// static-viz bundle, which runs in a headless GraalJS environment where Leaflet cannot load.
+
+const stateNamesMap = new Map(
+ STATE_CODES.map(([key, name]) => [name.toLowerCase(), key.toLowerCase()]),
+);
+
+/**
+ * Canonicalizes row values to match those in the GeoJSONs.
+ *
+ * Currently transforms US state names to state codes for the "us_states" region map, and just lowercases all others.
+ */
+export function getCanonicalRowKey(key: RowValue, region?: string): string {
+ const normalizedKey = String(key).toLowerCase();
+ // State names and codes don't overlap, so for us_states (which expects codes) we map any name to its code.
+ if (region === "us_states" && stateNamesMap.has(normalizedKey)) {
+ return stateNamesMap.get(normalizedKey) as string;
+ } else if (
+ region === "world_countries" &&
+ normalizedKey in COUNTRY_NAME_TO_CODE
+ ) {
+ return COUNTRY_NAME_TO_CODE[normalizedKey];
+ } else {
+ return normalizedKey;
+ }
+}
diff --git a/frontend/src/metabase/visualizations/lib/mapping.unit.spec.ts b/frontend/src/metabase/visualizations/lib/region-codes.unit.spec.ts
similarity index 96%
rename from frontend/src/metabase/visualizations/lib/mapping.unit.spec.ts
rename to frontend/src/metabase/visualizations/lib/region-codes.unit.spec.ts
index a434b9b61d6..86db50e7ba7 100644
--- a/frontend/src/metabase/visualizations/lib/mapping.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/lib/region-codes.unit.spec.ts
@@ -1,4 +1,4 @@
-import { getCanonicalRowKey } from "./mapping";
+import { getCanonicalRowKey } from "./region-codes";
describe("getCanonicalRowKey", () => {
it("should convert US state names to their iso2 codes", () => {
diff --git a/frontend/src/metabase/visualizations/visualizations/Map/Map.tsx b/frontend/src/metabase/visualizations/visualizations/Map/Map.tsx
index ee0acf1522f..8df27f1acc7 100644
--- a/frontend/src/metabase/visualizations/visualizations/Map/Map.tsx
+++ b/frontend/src/metabase/visualizations/visualizations/Map/Map.tsx
@@ -5,6 +5,10 @@ import _ from "underscore";
import { ColorRangeSelector } from "metabase/common/components/ColorRangeSelector";
import { getAccentColors, getPreferredColor } from "metabase/ui/colors/groups";
import MetabaseSettings from "metabase/utils/settings";
+import {
+ getDefaultMapDimension,
+ getDefaultMapMetric,
+} from "metabase/visualizations/lib/choropleth";
import { ChartSettingsError } from "metabase/visualizations/lib/errors";
import { columnSettings } from "metabase/visualizations/lib/settings/column";
import {
@@ -24,7 +28,6 @@ import type {
import {
hasLatitudeAndLongitudeColumns,
isCountry,
- isDimension,
isLatitude,
isLongitude,
isMetric,
@@ -246,7 +249,7 @@ const MAP_VIZ_DEFINITION: VisualizationDefinition = {
{
data: { cols },
},
- ]) => cols.find(isMetric)?.name,
+ ]) => getDefaultMapMetric(cols),
getHidden: (_series, vizSettings) => vizSettings["map.type"] !== "region",
}),
...dimensionSetting("map.dimension", {
@@ -257,13 +260,7 @@ const MAP_VIZ_DEFINITION: VisualizationDefinition = {
{
data: { cols },
},
- ]) => {
- const geoDimension = cols.find((col) => isCountry(col) || isState(col));
- if (geoDimension) {
- return geoDimension.name;
- }
- return cols.find(isDimension)?.name;
- },
+ ]) => getDefaultMapDimension(cols),
getHidden: (_series, vizSettings) => vizSettings["map.type"] !== "region",
}),
"map.colors": {
diff --git a/frontend/src/types/import-geojson.d.ts b/frontend/src/types/import-geojson.d.ts
new file mode 100644
index 00000000000..dd59da8621a
--- /dev/null
+++ b/frontend/src/types/import-geojson.d.ts
@@ -0,0 +1,11 @@
+// Built-in GeoJSON maps imported via the `assets` alias (-> resources/frontend_client/app/assets).
+// rspack/Storybook resolve the file at build time; this ambient declaration lets tsc type the import.
+// (tsconfig `paths` has no `assets` entry, so without this the specifier wouldn't resolve.)
+declare module "assets/geojson/*.json" {
+ import type { FeatureCollection } from "geojson";
+
+ const data: FeatureCollection;
+
+ // eslint-disable-next-line import/no-default-export
+ export default data;
+}
diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.clj
index b802df0a211..e15ce6f330a 100644
--- a/mage/src/mage/modules.clj
+++ b/mage/src/mage/modules.clj
@@ -117,6 +117,7 @@
enterprise/transforms-inspector
events
formatter
+ geojson
initialization-status
interestingness
internal-stats
@@ -145,6 +146,7 @@
system
task
task-history
+ tiles
timeline
tracing
types
diff --git a/src/metabase/channel/render/body.clj b/src/metabase/channel/render/body.clj
index d48fceede95..c296e243f76 100644
--- a/src/metabase/channel/render/body.clj
+++ b/src/metabase/channel/render/body.clj
@@ -7,17 +7,21 @@
[metabase.channel.render.image-bundle :as image-bundle]
[metabase.channel.render.js.color :as js.color]
[metabase.channel.render.js.svg :as js.svg]
+ [metabase.channel.render.maps :as maps]
[metabase.channel.render.style :as style]
[metabase.channel.render.table :as table]
[metabase.channel.render.table-data :as table-data]
[metabase.channel.render.util :as render.util]
[metabase.channel.settings :as channel.settings]
[metabase.formatter.core :as formatter]
+ [metabase.geojson.api :as geojson.api]
+ [metabase.geojson.settings :as geojson.settings]
[metabase.models.visualization-settings :as mb.viz]
[metabase.pivot.core :as pivot.core]
[metabase.pivot.postprocess :as pivot.postprocess]
[metabase.query-processor.streaming :as qp.streaming]
[metabase.query-processor.streaming.common :as streaming.common]
+ [metabase.tiles.settings :as tiles.settings]
[metabase.timeline.core :as timeline]
[metabase.types.core :as types]
[metabase.util :as u]
@@ -452,6 +456,45 @@
(map add-dashcard-timeline-events)
(m/distinct-by #(get-in % [:card :id]))))
+(defn region-map-region-key
+ "Resolve the region key for a region map, mirroring the frontend `map.region` default: an explicit
+ `map.region` setting, else a legacy `:state`/`:country` display, else inferred from a State/Country
+ column. Returns the key only when it names a region we know about (built-in or a user-defined custom
+ map); logical false otherwise, so non-region maps fall through to the table fallback. Note this does
+ not fetch — it just checks the region is defined; the actual GeoJSON load happens at render time."
+ [display-type card dashcard {:keys [cols]}]
+ ;; Merge (dashcard overrides card) rather than `or`: a dashcard usually has an empty-but-present
+ ;; :visualization_settings that would otherwise shadow the card's map.region.
+ (let [viz-settings (render.util/merged-viz-settings card dashcard)
+ region-key (or (render.util/viz-setting viz-settings "map.region")
+ (case display-type :state "us_states" :country "world_countries" nil)
+ (cond
+ (render.util/any-col-of-type? cols :type/State) "us_states"
+ (render.util/any-col-of-type? cols :type/Country) "world_countries"))]
+ (and (geojson.settings/defined-region? region-key)
+ region-key)))
+
+(defn- png->rendered-part
+ "Wrap PNG `byte[]` as a RenderedPartCard `<img>`."
+ [render-type png-bytes]
+ (let [image-bundle (image-bundle/make-image-bundle render-type png-bytes)]
+ {:attachments
+ (when image-bundle
+ (image-bundle/image-bundle->attachment image-bundle))
+
+ :content
+ [:div
+ [:img {:style (style/style {:display :block :width :100%})
+ :src (:image-src image-bundle)}]]}))
+
+(defn- javascript-visualization->rendered-part
+ "Turn the `{:type :svg/:html :content ...}` result of [[js.svg/*javascript-visualization*]] into a RenderedPartCard.
+ SVG results are rasterized to a PNG `<img>`; HTML results are embedded as-is."
+ [render-type {rendered-type :type content :content}]
+ (case rendered-type
+ :html {:content [:div content] :attachments nil}
+ :svg (png->rendered-part render-type (js.svg/svg-string->bytes content))))
+
;; the `:javascript_visualization` render method
;; is and will continue to handle more and more 'isomorphic' chart types.
;; Isomorphic in this context just means the frontend Code is mostly shared between the app and the static-viz
@@ -462,23 +505,126 @@
[_chart-type render-type _timezone-id card dashcard data]
(let [cards-with-data (series-cards-with-data dashcard card data)
viz-settings (or (get dashcard :visualization_settings)
- (get card :visualization_settings))
- {rendered-type :type content :content} (js.svg/*javascript-visualization* cards-with-data viz-settings)]
- (case rendered-type
- :svg
- (let [image-bundle (image-bundle/make-image-bundle
- render-type
- (js.svg/svg-string->bytes content))]
- {:attachments
- (when image-bundle
- (image-bundle/image-bundle->attachment image-bundle))
-
- :content
- [:div
- [:img {:style (style/style {:display :block :width :100%})
- :src (:image-src image-bundle)}]]})
- :html
- {:content [:div content] :attachments nil})))
+ (get card :visualization_settings))]
+ (javascript-visualization->rendered-part
+ render-type
+ (js.svg/*javascript-visualization* cards-with-data viz-settings))))
+
+(mu/defmethod render :region_map :- ::RenderedPartCard
+ [_chart-type render-type timezone-id card dashcard data]
+ ;; Resolve the display type the same way detection does, so a visualizer dashcard's display wins.
+ (let [display-type (or (render.util/visualizer-display-type dashcard) (:display card))
+ region-key (region-map-region-key display-type card dashcard data)
+ geojson (some-> region-key geojson.api/region-geojson)]
+ (if-not geojson
+ ;; The region's GeoJSON couldn't be resolved (e.g. a custom map whose fetch failed); degrade to a
+ ;; table of the data rather than emit an empty map.
+ (render :table render-type timezone-id card dashcard data)
+ (let [cards-with-data (series-cards-with-data dashcard card data)
+ base-settings (render.util/merged-viz-settings card dashcard)
+ ;; Embed the resolved GeoJSON, and pin map.region to the resolved key so the bundle picks
+ ;; the right projection even when the region was only an inferred default (never persisted).
+ viz-settings (-> base-settings
+ (dissoc :map.region "map.region")
+ (assoc "map.region" region-key
+ "map._geojson" (:data geojson)
+ "map._geojson_details" {:region_key (:region_key geojson)
+ :region_name (:region_name geojson)}))]
+ (javascript-visualization->rendered-part
+ render-type
+ (js.svg/*javascript-visualization* cards-with-data viz-settings))))))
+
+(defn- number-at
+ "The value of `row` at `idx` when it's a number, else nil."
+ [row idx]
+ (let [v (nth row idx nil)]
+ (when (number? v)
+ v)))
+
+(defn- coordinate-col-index
+ "Resolve a coordinate column's index, mirroring the frontend default: the named column if the setting is
+ present, else the first column with semantic type `sem-type` (`:type/Latitude` / `:type/Longitude`)."
+ [cols col-name sem-type]
+ (or (when col-name (first (get-col-by-name cols col-name)))
+ (first (keep-indexed (fn [i col]
+ (when (render.util/col-of-type? col sem-type)
+ i))
+ cols))))
+
+(defn- coordinate-col-indexes
+ "`[lat-idx lon-idx]` for a coordinate map, honoring the `map.latitude_column`/`map.longitude_column`
+ settings, with semantic-type fallbacks."
+ [cols setting]
+ [(coordinate-col-index cols (setting "map.latitude_column") :type/Latitude)
+ (coordinate-col-index cols (setting "map.longitude_column") :type/Longitude)])
+
+(defn- metric-col-index
+ "Resolve the metric column's index: the named column if set, else the first numeric column that isn't the
+ lat/long column. Returns nil when there's no metric (the cells then render at a uniform colour)."
+ [cols lat-idx lon-idx col-name]
+ (or (when col-name (first (get-col-by-name cols col-name)))
+ (first (keep-indexed (fn [i col]
+ (when (and (not= i lat-idx) (not= i lon-idx)
+ (isa? (some-> (or (:semantic_type col) (:base_type col)) keyword)
+ :type/Number))
+ i))
+ cols))))
+
+(defn- column-bin-width
+ "The bin width (degrees) for a binned coordinate column, or an inferred fallback from the row values."
+ [col idx rows]
+ (or (get-in col [:binning_info :bin_width])
+ (let [vs (->> rows
+ (keep #(number-at % idx))
+ distinct
+ sort)]
+ (some->> (map - (rest vs) vs)
+ (filter pos?)
+ seq
+ (apply min)))))
+
+(mu/defmethod render :pin_map :- ::RenderedPartCard
+ [_chart-type render-type timezone-id card dashcard {:keys [cols rows] :as data}]
+ (let [viz-settings (render.util/merged-viz-settings card dashcard)
+ setting (partial render.util/viz-setting viz-settings)
+ [lat-idx lon-idx] (coordinate-col-indexes cols setting)
+ points (when (and lat-idx lon-idx)
+ (for [row rows
+ :let [lat (number-at row lat-idx)
+ lon (number-at row lon-idx)]
+ :when (and lat lon)]
+ [lat lon]))]
+ (if-let [png (when (seq points)
+ (maps/render-pin-map points {:tile-url (tiles.settings/map-tile-server-url)
+ :pin-type (setting "map.pin_type")}))]
+ (png->rendered-part render-type png)
+ ;; No usable coordinates (mismatched columns, all nil, etc.) or the render failed — degrade to a
+ ;; table of the data.
+ (render :table render-type timezone-id card dashcard data))))
+
+(mu/defmethod render :grid_map :- ::RenderedPartCard
+ [_chart-type render-type timezone-id card dashcard {:keys [cols rows] :as data}]
+ (let [viz-settings (render.util/merged-viz-settings card dashcard)
+ setting (partial render.util/viz-setting viz-settings)
+ [lat-idx lon-idx] (coordinate-col-indexes cols setting)
+ metric-idx (metric-col-index cols lat-idx lon-idx (setting "map.metric_column"))
+ lat-bin (when lat-idx (column-bin-width (nth cols lat-idx) lat-idx rows))
+ lon-bin (when lon-idx (column-bin-width (nth cols lon-idx) lon-idx rows))
+ cells (when (and lat-idx lon-idx lat-bin lon-bin)
+ (for [row rows
+ :let [lat (number-at row lat-idx)
+ lon (number-at row lon-idx)]
+ :when (and lat lon)]
+ {:lat lat
+ :lon lon
+ :lat-bin lat-bin
+ :lon-bin lon-bin
+ :metric (when metric-idx (number-at row metric-idx))}))]
+ (if-let [png (when (seq cells)
+ (maps/render-grid-map cells {:tile-url (tiles.settings/map-tile-server-url)}))]
+ (png->rendered-part render-type png)
+ ;; No usable cells or the render failed — degrade to a table of the data.
+ (render :table render-type timezone-id card dashcard data))))
;;; ------------------------------------------------ pivot tables ------------------------------------------------
diff --git a/src/metabase/channel/render/card.clj b/src/metabase/channel/render/card.clj
index 68335807904..c5e1ce9f04e 100644
--- a/src/metabase/channel/render/card.clj
+++ b/src/metabase/channel/render/card.clj
@@ -85,19 +85,66 @@
:margin-bottom :8px})}
(markdown/process-markdown description :html)]})))
-(defn- visualizer-display-type
- "Return dashcard's display type if it is a visualizer dashcard else nil"
- [dashcard]
- (if (render.util/is-visualizer-dashcard? dashcard)
- (keyword (get-in dashcard [:visualization_settings :visualization :display]))
- nil))
+(defn- has-lat-lng-columns?
+ "True when the result has both a Latitude and a Longitude column (a coordinate-based map)."
+ [cols]
+ (and (render.util/any-col-of-type? cols :type/Latitude)
+ (render.util/any-col-of-type? cols :type/Longitude)))
+
+(defn- binned-lat-lng-columns?
+ "True when both a Latitude and a Longitude column are binned (a grid map, per the frontend default)."
+ [cols]
+ (and (some #(and (render.util/col-of-type? % :type/Latitude) (get-in % [:binning_info :bin_width])) cols)
+ (some #(and (render.util/col-of-type? % :type/Longitude) (get-in % [:binning_info :bin_width])) cols)))
+
+(defn- effective-map-type
+ "Resolve the effective `map.type` the way the frontend's getDefault does: an explicit setting, else
+ inferred from the display type and columns. A `:map` with lat/long columns (or explicit lat/long column
+ settings) is a coordinate map — NOT a region map — even if the result also has a State/Country column;
+ it's a grid map when those lat/long columns are binned, otherwise a pin map. Returns
+ \"pin\" / \"grid\" / \"region\" / \"heat\"."
+ [display-type card maybe-dashcard {:keys [cols]}]
+ (let [viz-settings (render.util/merged-viz-settings card maybe-dashcard)
+ setting (partial render.util/viz-setting viz-settings)]
+ (or (setting "map.type")
+ (case display-type
+ :pin_map "pin"
+ (:state :country) "region"
+ (if (or (and (setting "map.latitude_column")
+ (setting "map.longitude_column"))
+ (has-lat-lng-columns? cols))
+ (if (binned-lat-lng-columns? cols)
+ "grid"
+ "pin")
+ "region")))))
+
+(defn- map-chart-type
+ "The static-viz chart type a map card renders as — `:region_map`, `:pin_map`, or `:grid_map` — or nil
+ when the card isn't a map, or is one we can't render statically yet (heat maps, region maps whose region
+ isn't defined). Mirrors the frontend's map-type defaulting, computing [[effective-map-type]] once for
+ all three outcomes."
+ [display-type card maybe-dashcard data]
+ (if (= :pin_map display-type)
+ :pin_map
+ (when (#{:map :state :country} display-type)
+ (case (effective-map-type display-type card maybe-dashcard data)
+ "pin" (when (= :map display-type)
+ :pin_map)
+ "grid" (when (= :map display-type)
+ :grid_map)
+ ;; a region map is only renderable when its region key names GeoJSON we know about — built-in, or
+ ;; a user-defined custom map (the latter is fetched at render time)
+ "region" (when (body/region-map-region-key display-type card maybe-dashcard data)
+ :region_map)
+ nil))))
(defn detect-pulse-chart-type
"Determine the pulse (visualization) type of a `card`, e.g. `:scalar` or `:bar`."
- [{display-type :display card-name :name} maybe-dashcard {:keys [cols rows] :as data}]
+ [{display-type :display card-name :name :as card} maybe-dashcard {:keys [cols rows] :as data}]
(let [col-sample-count (delay (count (take 3 cols)))
row-sample-count (delay (count (take 2 rows)))
- display-type (or (visualizer-display-type maybe-dashcard) display-type)]
+ display-type (or (render.util/visualizer-display-type maybe-dashcard) display-type)
+ map-type (map-chart-type display-type card maybe-dashcard data)]
(letfn [(chart-type [tyype reason & args]
(log/tracef "Detected chart type %s for Card %s because %s"
tyype (pr-str card-name) (apply format reason args))
@@ -108,7 +155,10 @@
(= [[nil]] (-> data :rows)))
(chart-type :empty "there are no rows in results")
- (#{:pin_map :state :country} display-type)
+ map-type
+ (chart-type map-type "card is a map renderable as %s" (name map-type))
+
+ (#{:state :country} display-type)
(chart-type nil "display-type is %s" display-type)
(and (some? maybe-dashcard)
diff --git a/src/metabase/channel/render/maps.clj b/src/metabase/channel/render/maps.clj
new file mode 100644
index 00000000000..16ad63b0ed8
--- /dev/null
+++ b/src/metabase/channel/render/maps.clj
@@ -0,0 +1,327 @@
+(ns metabase.channel.render.maps
+ "Render static pin and grid maps (basemap tiles + drawn markers/cells) to PNG bytes, entirely on the JVM
+ (Java2D), for use in email/Slack subscriptions. Unlike choropleth maps this bypasses the GraalJS
+ static-viz bundle. The Web Mercator projection is shared with [[metabase.tiles.api]] (which already
+ draws pin tiles for the live app); the new part is fetching + stitching the OSM basemap under the
+ overlay."
+ (:require
+ [clj-http.client :as http]
+ [clojure.core.memoize :as memoize]
+ [clojure.java.io :as io]
+ [clojure.string :as str]
+ [metabase.util :as u]
+ [metabase.util.log :as log]
+ [metabase.util.web-mercator :as mercator])
+ (:import
+ (java.awt Color RenderingHints)
+ (java.awt.image BufferedImage)
+ (java.io ByteArrayInputStream ByteArrayOutputStream)
+ (javax.imageio ImageIO)))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private ^:const default-tile-url "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
+
+;; The teardrop pin used by the live app's "markers" pin type (app/assets/img/pin.png), 28x32 anchored at
+;; its tip (15,24) — so the bottom point sits on the coordinate. Loaded once from the classpath.
+(def ^:private ^:const pin-icon-anchor-x 15)
+(def ^:private ^:const pin-icon-anchor-y 24)
+(def ^:private pin-marker-icon
+ (delay (some-> (io/resource "frontend_client/app/assets/img/pin.png") ImageIO/read)))
+
+(def ^:private default-render-options
+ "Defaults for the output-image `opts` accepted by [[render-pin-map]] and [[render-grid-map]], all in px.
+ The output image is sized to the data's bounding box (+ `:padding` on each side) at the best integer
+ zoom, clamped to the min/max bounds — so the data fills the frame rather than floating in a fixed,
+ mostly-empty window. `:max-zoom` caps the auto-fit zoom so a single/tight cluster isn't street-level."
+ {:max-width 1200
+ :max-height 800
+ :min-width 400
+ :min-height 300
+ :padding 48
+ :max-zoom 14})
+
+;;; ------------------------------------------------ projection ------------------------------------------------
+
+(defn- world-px-bounds
+ "Project `coords` (a seq of `[lat lon]`) to global pixel coordinates at `zoom` and return the bounding
+ box of the result."
+ [coords ^long zoom]
+ (let [pxs (mapv (fn [[lat lon]] (mercator/latlon->world-px lat lon zoom)) coords)
+ xs (map first pxs)
+ ys (map second pxs)]
+ {:min-x (double (apply min xs))
+ :max-x (double (apply max xs))
+ :min-y (double (apply min ys))
+ :max-y (double (apply max ys))}))
+
+(defn- choose-zoom
+ "Largest integer zoom (<= `:max-zoom`) at which `coords`' bounding box (plus padding on both sides)
+ still fits within the maximum output dimensions."
+ [coords {:keys [max-width max-height padding max-zoom]}]
+ (let [max-w (- max-width (* 2 padding))
+ max-h (- max-height (* 2 padding))]
+ (loop [z max-zoom]
+ (if (<= z 1)
+ 1
+ (let [{:keys [min-x max-x min-y max-y]} (world-px-bounds coords z)]
+ (if (and (<= (- max-x min-x) max-w)
+ (<= (- max-y min-y) max-h))
+ z
+ (recur (dec z))))))))
+
+;;; ------------------------------------------------ basemap tiles ------------------------------------------------
+
+(defn- expand-tile-url [template ^long z ^long x ^long y]
+ (-> template
+ (str/replace "{s}" "a")
+ (str/replace "{z}" (str z))
+ (str/replace "{x}" (str x))
+ (str/replace "{y}" (str y))))
+
+(def ^:private tile-cache-ttl-ms
+ "How long fetched basemap tiles stay cached. Tile contents change rarely; caching keeps repeated renders
+ from hammering the tile server — important for OSM's usage policy."
+ (u/hours->ms 1))
+
+(def ^:private fetch-tile*
+ ;; Throws on any failure (non-200, undecodable image) so that only successful fetches are cached —
+ ;; otherwise a transient failure would leave a blank strip in every render until the TTL expires.
+ (memoize/ttl
+ (fn [template ^long z ^long x ^long y]
+ (let [url (expand-tile-url template z x y)
+ resp (http/get url {:as :byte-array
+ :socket-timeout 5000
+ :connection-timeout 5000
+ :throw-exceptions false
+ ;; OSM rejects requests without a valid User-Agent.
+ :headers {"User-Agent" "Metabase static map renderer"}})]
+ (when-not (= 200 (:status resp))
+ (throw (ex-info (format "Tile fetch failed with status %d" (:status resp))
+ {:url url :status (:status resp)})))
+ (or (ImageIO/read (ByteArrayInputStream. ^bytes (:body resp)))
+ (throw (ex-info "Tile response is not a decodable image" {:url url})))))
+ :ttl/threshold tile-cache-ttl-ms))
+
+(defn- fetch-tile
+ "Fetch one basemap tile, caching successes for [[tile-cache-ttl-ms]]. Returns nil on failure, so the
+ render skips the tile rather than aborting; failures are logged and not cached."
+ [template z x y]
+ (try
+ (fetch-tile* template z x y)
+ (catch Throwable e
+ (log/warn e "Failed to fetch map tile")
+ nil)))
+
+;;; ------------------------------------------------ color (HCL) ------------------------------------------------
+;;; Grid maps colour cells on a linear green->red scale interpolated in HCL space, matching the live app
+;;; (LeafletGridHeatMap uses d3.scaleLinear(...).interpolate(d3.interpolateHcl)). Hand-rolled because the
+;;; JDK has no Lab/LCh support, and interpolating in sRGB instead diverges visibly (muddy brown mid-ramp).
+
+(def ^:private grid-low-color "success — low metric values" (Color. 0x3B 0x6B 0x3B))
+(def ^:private grid-high-color "error — high metric values" (Color. 0xDC 0x54 0x54))
+
+(defn- srgb->linear ^double [^double c]
+ (if (<= c 0.04045)
+ (/ c 12.92)
+ (Math/pow (/ (+ c 0.055) 1.055) 2.4)))
+
+(defn- linear->srgb ^double [^double c]
+ (if (<= c 0.0031308)
+ (* c 12.92)
+ (- (* 1.055 (Math/pow c (/ 1.0 2.4))) 0.055)))
+
+;; The standard CIE Lab transfer function and its inverse.
+(def ^:private ^:const lab-epsilon 0.008856)
+(def ^:private ^:const lab-kappa 7.787)
+(def ^:private ^:const lab-offset (/ 16.0 116.0))
+
+(defn- lab-f ^double [^double t]
+ (if (> t lab-epsilon)
+ (Math/cbrt t)
+ (+ (* lab-kappa t) lab-offset)))
+
+(defn- lab-f-inverse ^double [^double t]
+ (let [t3 (* t t t)]
+ (if (> t3 lab-epsilon)
+ t3
+ (/ (- t lab-offset) lab-kappa))))
+
+(defn- color->lch [^Color c]
+ (let [r (srgb->linear (/ (.getRed c) 255.0))
+ g (srgb->linear (/ (.getGreen c) 255.0))
+ b (srgb->linear (/ (.getBlue c) 255.0))
+ x (/ (+ (* 0.4124 r) (* 0.3576 g) (* 0.1805 b)) 0.95047)
+ y (+ (* 0.2126 r) (* 0.7152 g) (* 0.0722 b))
+ z (/ (+ (* 0.0193 r) (* 0.1192 g) (* 0.9505 b)) 1.08883)
+ fx (lab-f x)
+ fy (lab-f y)
+ fz (lab-f z)
+ l (- (* 116.0 fy) 16.0)
+ a (* 500.0 (- fx fy))
+ bb (* 200.0 (- fy fz))]
+ [l (Math/hypot a bb) (Math/toDegrees (Math/atan2 bb a))]))
+
+(defn- channel-255
+ "Clamp a 0-1 sRGB channel value and scale it to 0-255."
+ ^long [^double v]
+ (-> v (Math/max 0.0) (Math/min 1.0) (* 255.0) Math/round))
+
+(defn- rgb-color ^Color [^double r ^double g ^double b]
+ (Color. (int (channel-255 r)) (int (channel-255 g)) (int (channel-255 b))))
+
+(defn- lch->color ^Color [[^double l ^double c ^double h]]
+ (let [hr (Math/toRadians h)
+ a (* c (Math/cos hr))
+ bb (* c (Math/sin hr))
+ fy (/ (+ l 16.0) 116.0)
+ fx (+ fy (/ a 500.0))
+ fz (- fy (/ bb 200.0))
+ x (* 0.95047 (lab-f-inverse fx))
+ y (lab-f-inverse fy)
+ z (* 1.08883 (lab-f-inverse fz))
+ r (linear->srgb (+ (* 3.2406 x) (* -1.5372 y) (* -0.4986 z)))
+ g (linear->srgb (+ (* -0.9689 x) (* 1.8758 y) (* 0.0415 z)))
+ b (linear->srgb (+ (* 0.0557 x) (* -0.2040 y) (* 1.0570 z)))]
+ (rgb-color r g b)))
+
+(let [[l0 c0 h0] (color->lch grid-low-color)
+ [l1 c1 h1] (color->lch grid-high-color)
+ ;; interpolate hue along the shortest path
+ dh (let [d (- h1 h0)]
+ (cond
+ (> d 180) (- d 360)
+ (< d -180) (+ d 360)
+ :else d))]
+ (defn- grid-color
+ "Colour for a grid cell with metric value `m`, given the metric `mn`/`mx` range. Linear green->red in HCL."
+ ^Color [^double m ^double mn ^double mx]
+ (let [t (if (== mn mx)
+ 0.5
+ (-> (/ (- m mn) (- mx mn))
+ (Math/max 0.0)
+ (Math/min 1.0)))]
+ (lch->color [(+ l0 (* t (- l1 l0)))
+ (+ c0 (* t (- c1 c0)))
+ (+ h0 (* t dh))]))))
+
+;;; ------------------------------------------------ render ------------------------------------------------
+
+(defn- clamp
+ "Clamp `v` between `lo` and `hi` and round to the nearest whole number."
+ ^long [^double v ^double lo ^double hi]
+ (-> v (Math/max lo) (Math/min hi) Math/round))
+
+(defn- axis-window
+ "Fit one axis of the output window to the data: given the data's projected extent `[lo hi]` along that
+ axis, return `[size origin]` — the window size (data extent plus `padding` on both sides, clamped
+ between `min-size` and `max-size`) and the world-px of the window's near edge, centering the data
+ within the (possibly clamped) window."
+ [lo hi min-size max-size padding]
+ (let [size (clamp (+ (- hi lo) (* 2 padding)) min-size max-size)]
+ [size (- (/ (+ lo hi) 2.0) (/ size 2.0))]))
+
+(defn- do-render-map
+ "Render a basemap covering the lat/lon extent of `coords` (a seq of `[lat lon]`), then call
+ `(draw-fn graphics project)` to draw the overlay, where `project` is `(fn [lat lon] -> [px py])` in image
+ space. Returns PNG `byte[]`, or nil if the render fails. `opts`: `:zoom` (override auto-fit), `:tile-url`
+ (template), plus the output-sizing keys in [[default-render-options]]."
+ [coords opts draw-fn]
+ (let [{:keys [zoom tile-url min-width min-height
+ max-width max-height padding] :as opts} (merge default-render-options opts)
+ template (or tile-url default-tile-url)
+ z (or zoom (choose-zoom coords opts))
+ {:keys [min-x max-x min-y max-y]} (world-px-bounds coords z)
+ [w ox] (axis-window min-x max-x min-width max-width padding)
+ [h oy] (axis-window min-y max-y min-height max-height padding)
+ img (BufferedImage. w h BufferedImage/TYPE_INT_RGB)
+ g (.createGraphics img)
+ project (fn [lat lon] (let [[wx wy] (mercator/latlon->world-px lat lon z)] [(- wx ox) (- wy oy)]))]
+ (try
+ (.setColor g Color/WHITE)
+ (.fillRect g 0 0 w h)
+ (.setRenderingHint g RenderingHints/KEY_ANTIALIASING RenderingHints/VALUE_ANTIALIAS_ON)
+ ;; basemap: draw every tile overlapping the window at its pixel offset
+ (let [tx0 (long (Math/floor (/ ox mercator/tile-size)))
+ ty0 (long (Math/floor (/ oy mercator/tile-size)))
+ tx1 (long (Math/floor (/ (+ ox w) mercator/tile-size)))
+ ty1 (long (Math/floor (/ (+ oy h) mercator/tile-size)))]
+ (doseq [tx (range tx0 (inc tx1))
+ ty (range ty0 (inc ty1))
+ :let [^BufferedImage tile (fetch-tile template z tx ty)]
+ :when tile]
+ (.drawImage g tile (int (- (* tx mercator/tile-size) ox)) (int (- (* ty mercator/tile-size) oy)) nil)))
+ (draw-fn g project)
+ ;; attribution (OSM requires it)
+ (.setColor g (Color. 90 90 90))
+ (.drawString g "© OpenStreetMap contributors" (int (- w 210)) (int (- h 8)))
+ (let [out (ByteArrayOutputStream.)]
+ (ImageIO/write img "png" out)
+ (.toByteArray out))
+ ;; Return nil on failure so the caller can degrade to a table rather than email a blank/partial map.
+ (catch Throwable e
+ (log/warn e "Failed to render map")
+ nil)
+ (finally
+ (.dispose g)))))
+
+(defn- draw-dot [^java.awt.Graphics2D g ^double mx ^double my]
+ (let [r 5]
+ (.setColor g Color/WHITE)
+ (.fillOval g (int (- mx r 1)) (int (- my r 1)) (+ (* 2 r) 2) (+ (* 2 r) 2))
+ (.setColor g (Color. 76 157 230))
+ (.fillOval g (int (- mx r)) (int (- my r)) (* 2 r) (* 2 r))))
+
+(defn render-pin-map
+ "Render `points` (a seq of `[lat lon]`) onto a basemap as a PNG `byte[]`. `opts` may include `:zoom`
+ (override auto-fit), `:tile-url` (template, defaults to OSM), the output-sizing keys in
+ [[default-render-options]], and `:pin-type` — `\"markers\"` (default) draws the teardrop pin icon like
+ the live app; anything else (e.g. `\"tiles\"`, for dense data) draws a small dot. Returns nil if the
+ render fails."
+ [points & [{:keys [pin-type] :as opts}]]
+ (do-render-map
+ points opts
+ (fn [^java.awt.Graphics2D g project]
+ (let [icon @pin-marker-icon
+ dots? (or (= pin-type "tiles") (nil? icon))]
+ (doseq [[lat lon] points
+ :let [[mx my] (project lat lon)]]
+ (if dots?
+ (draw-dot g (double mx) (double my))
+ ;; place the pin so its tip (anchor) sits on the coordinate
+ (.drawImage g ^BufferedImage icon
+ (int (- mx pin-icon-anchor-x)) (int (- my pin-icon-anchor-y)) nil)))))))
+
+(defn- with-alpha
+ "`color` with its alpha channel set to `alpha` (0-255)."
+ ^Color [^Color color ^long alpha]
+ (Color. (.getRed color) (.getGreen color) (.getBlue color) (int alpha)))
+
+(defn render-grid-map
+ "Render binned grid `cells` onto a basemap as a PNG `byte[]`. Each cell is a map with `:lat`/`:lon` (the
+ cell's lower bound), `:lat-bin`/`:lon-bin` (bin widths in degrees), and `:metric` (value driving colour).
+ Cells are coloured on a linear green->red HCL scale across the metric range. `opts` as for pin maps.
+ Returns nil if the render fails."
+ [cells & [opts]]
+ (let [metrics (keep :metric cells)
+ [mn mx] (if (seq metrics)
+ [(double (apply min metrics)) (double (apply max metrics))]
+ [0.0 0.0])
+ ;; auto-fit to every cell corner so the whole grid is visible
+ corners (mapcat (fn [{:keys [lat lon lat-bin lon-bin]}]
+ [[lat lon] [(+ lat lat-bin) (+ lon lon-bin)]])
+ cells)]
+ (do-render-map
+ corners opts
+ (fn [^java.awt.Graphics2D g project]
+ ;; north edge (lat+bin) is the top (smaller y); west edge (lon) is the left (smaller x)
+ (doseq [{:keys [lat lon lat-bin lon-bin metric]} cells
+ :let [[x0 y0] (project (+ lat lat-bin) lon)
+ [x1 y1] (project lat (+ lon lon-bin))
+ x (int (Math/round (double (min x0 x1))))
+ y (int (Math/round (double (min y0 y1))))
+ w (max 1 (int (Math/round (Math/abs (- (double x1) (double x0))))))
+ h (max 1 (int (Math/round (Math/abs (- (double y1) (double y0))))))]]
+ ;; semi-transparent so the basemap shows through, like the live grid overlay
+ (.setColor g (with-alpha (grid-color (double (or metric mn)) mn mx) 170))
+ (.fillRect g x y w h))))))
diff --git a/src/metabase/channel/render/util.clj b/src/metabase/channel/render/util.clj
index cb6ab566e57..aab11042b18 100644
--- a/src/metabase/channel/render/util.clj
+++ b/src/metabase/channel/render/util.clj
@@ -40,6 +40,34 @@
(and (some? dashcard)
(get-in dashcard [:visualization_settings :visualization]))))
+(defn visualizer-display-type
+ "The display type a visualizer dashcard renders as (from its visualizer viz settings), or nil when
+ `dashcard` isn't a visualizer dashcard."
+ [dashcard]
+ (when (is-visualizer-dashcard? dashcard)
+ (-> dashcard :visualization_settings :visualization :display keyword)))
+
+(defn merged-viz-settings
+ "Merge a card's and dashcard's `:visualization_settings`, with the dashcard's taking precedence."
+ [card dashcard]
+ (merge (:visualization_settings card) (:visualization_settings dashcard)))
+
+(defn viz-setting
+ "Look up the `map.*`-style setting `k` (a string) in `viz-settings`, tolerating either string or keyword
+ keys — production stores viz-settings keys as keywords-with-dots, but they can also arrive as strings."
+ [viz-settings k]
+ (or (get viz-settings k) (get viz-settings (keyword k))))
+
+(defn col-of-type?
+ "Whether result column `col`'s semantic type derives from `sem-type` (e.g. `:type/Latitude`)."
+ [col sem-type]
+ (isa? (some-> (:semantic_type col) keyword) sem-type))
+
+(defn any-col-of-type?
+ "Whether any column in `cols` has a semantic type deriving from `sem-type`."
+ [cols sem-type]
+ (boolean (some #(col-of-type? % sem-type) cols)))
+
(defn is-scalar-funnel?
"Check if the visualization is a scalar funnel.
Matches the frontend implementation in frontend/src/metabase/visualizer/visualizations/funnel.ts"
diff --git a/src/metabase/geojson/api.clj b/src/metabase/geojson/api.clj
index cbbd6012e9a..f01f2a425be 100644
--- a/src/metabase/geojson/api.clj
+++ b/src/metabase/geojson/api.clj
@@ -1,12 +1,16 @@
(ns metabase.geojson.api
(:require
[clj-http.client :as http]
+ [clojure.core.memoize :as memoize]
[clojure.java.io :as io]
[clojure.string :as str]
[metabase.api.macros :as api.macros]
[metabase.geojson.settings :as geojson.settings]
[metabase.permissions.core :as perms]
+ [metabase.util :as u]
[metabase.util.i18n :refer [tru]]
+ [metabase.util.json :as json]
+ [metabase.util.log :as log]
[metabase.util.malli.schema :as ms]
[ring.util.codec :as codec]
[ring.util.response :as response])
@@ -69,6 +73,49 @@
(io/reader resource)
(url->geojson url)))
+(def ^:private custom-geojson-cache-ttl-ms
+ "User-defined custom maps are fetched over the network, so their GeoJSON is cached — but only briefly,
+ since the remote contents can change underneath us."
+ (u/hours->ms 1))
+
+(defn- fetch-geojson-data*
+ [url]
+ ;; url->reader handles both classpath (when MB_ALLOW_CLASSPATH_GEOJSON) and remote URLs.
+ (with-open [^java.io.Reader reader (url->reader url)]
+ (json/decode (slurp reader))))
+
+(def ^:private fetch-geojson-data
+ ;; Keyed by URL and only ever holds *successful* fetches: the enabled/entry-existence checks happen
+ ;; outside the cache, and a fetch failure throws (so it isn't cached). This avoids caching a nil that
+ ;; would otherwise keep a map broken until the TTL expires.
+ (memoize/ttl fetch-geojson-data* :ttl/threshold custom-geojson-cache-ttl-ms))
+
+(defn- custom-region-geojson
+ "Resolve GeoJSON for a user-defined `custom-geojson` region key by fetching its URL (cached with a short
+ TTL). Returns nil when custom GeoJSON is disabled, the key is unknown, or the fetch fails."
+ [region-key]
+ (when-let [{:keys [url region_key region_name]}
+ (and (geojson.settings/custom-geojson-enabled)
+ (get (geojson.settings/user-defined-custom-geojson) (keyword region-key)))]
+ (try
+ {:data (fetch-geojson-data url)
+ :region_key region_key
+ :region_name region_name}
+ (catch Throwable e
+ (log/warnf e "Failed to load custom GeoJSON for region %s from %s"
+ (pr-str region-key) (pr-str url))
+ nil))))
+
+(defn region-geojson
+ "Resolve GeoJSON `{:data :region_key :region_name}` for a `custom-geojson` region key, built-in or
+ user-defined. Built-in maps are read from the classpath; user maps are fetched from their URL and the
+ fetched data is cached with a short TTL. Returns nil for unknown keys, disabled custom GeoJSON, or fetch
+ failures. Used by static (email/Slack) rendering to embed GeoJSON without an HTTP round-trip."
+ [region-key]
+ (when region-key
+ (or (geojson.settings/builtin-region-geojson region-key)
+ (custom-region-geojson region-key))))
+
(defn- read-url-and-respond
"Reads the provided URL and responds with the contents as a stream."
[url respond]
diff --git a/src/metabase/geojson/settings.clj b/src/metabase/geojson/settings.clj
index abe072a658e..99746ea91c1 100644
--- a/src/metabase/geojson/settings.clj
+++ b/src/metabase/geojson/settings.clj
@@ -5,6 +5,7 @@
[metabase.settings.core :as setting :refer [defsetting]]
[metabase.util.http :as http]
[metabase.util.i18n :refer [deferred-tru tru]]
+ [metabase.util.json :as json]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms])
(:import
@@ -142,3 +143,33 @@
"Returns the subset of custom-geojson that users defined, without the built-in geojson entries."
[]
(reduce dissoc (custom-geojson) (keys (builtin-geojson))))
+
+(defn defined-region?
+ "Whether `region-key` names a region map whose GeoJSON we could resolve: a built-in region (when default
+ maps are enabled), or a user-defined one (when custom GeoJSON is enabled). Mirrors what
+ [[metabase.geojson.api/region-geojson]] can resolve, minus the fetch itself."
+ [region-key]
+ (let [k (keyword region-key)]
+ (or (contains? (builtin-geojson) k)
+ (and (custom-geojson-enabled)
+ (contains? (user-defined-custom-geojson) k)))))
+
+(def ^:private read-classpath-geojson
+ ;; Built-in GeoJSON files are static, so reading + parsing them once is safe to cache forever. The stored
+ ;; `url` (e.g. "app/assets/...") is a web path served out of resources/frontend_client, so resolve it there.
+ (memoize (fn [url]
+ (some-> (str "frontend_client/" url)
+ io/resource
+ slurp
+ json/decode))))
+
+(defn builtin-region-geojson
+ "For a built-in region key (e.g. \"us_states\"), return the parsed GeoJSON `:data` along with its
+ `:region_key`/`:region_name`, read straight from the classpath. Returns nil for unknown or non-built-in
+ keys. Used by static (email/Slack) rendering to embed GeoJSON without an HTTP round-trip."
+ [region-key]
+ (when-let [{:keys [url region_key region_name builtin]} (get (builtin-geojson) (keyword region-key))]
+ (when-let [data (and builtin url (read-classpath-geojson url))]
+ {:data data
+ :region_key region_key
+ :region_name region_name})))
diff --git a/src/metabase/tiles/api.clj b/src/metabase/tiles/api.clj
index bee7eec088b..c810be63e73 100644
--- a/src/metabase/tiles/api.clj
+++ b/src/metabase/tiles/api.clj
@@ -20,7 +20,8 @@
[metabase.util.json :as json]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
- [metabase.util.malli.schema :as ms])
+ [metabase.util.malli.schema :as ms]
+ [metabase.util.web-mercator :as mercator])
(:import
(java.awt Color)
(java.awt.image BufferedImage)
@@ -44,9 +45,6 @@
;;; ---------------------------------------------------- UTIL FNS ----------------------------------------------------
-(defn- degrees->radians ^double [^double degrees]
- (* degrees (/ Math/PI 180.0)))
-
(defn- radians->degrees ^double [^double radians]
(/ radians (/ Math/PI 180.0)))
@@ -85,36 +83,23 @@
;;; --------------------------------------------------- RENDERING ----------------------------------------------------
(defn- create-tile ^BufferedImage [zoom points]
- (let [num-tiles (bit-shift-left 1 zoom)
- tile (BufferedImage. tile-size tile-size (BufferedImage/TYPE_INT_ARGB))
- graphics (.getGraphics tile)
- color-blue (new Color 76 157 230)
+ (let [tile (BufferedImage. tile-size tile-size (BufferedImage/TYPE_INT_ARGB))
+ graphics (.getGraphics tile)
+ color-blue (new Color 76 157 230)
color-white (Color/white)]
(try
- (doseq [[^double lat ^double lon] points]
- (let [sin-y (-> (Math/sin (degrees->radians lat))
- (Math/max -0.9999) ; bound sin-y between -0.9999 and 0.9999 (why ?))
- (Math/min 0.9999))
- point {:x (+ pixel-origin
- (* lon pixels-per-lon-degree))
- :y (+ pixel-origin
- (* 0.5
- (Math/log (/ (inc sin-y)
- (- 1 sin-y)))
- pixels-per-lon-radian
- -1.0))} ; huh?
- map-pixel {:x (int (Math/floor (* (point :x) num-tiles)))
- :y (int (Math/floor (* (point :y) num-tiles)))}
- tile-pixel {:x (mod (map-pixel :x) tile-size)
- :y (mod (map-pixel :y) tile-size)}
- ;; cx/cy is needed to put center of a pin at the tile-pixel position
- cx (- (tile-pixel :x) pin-size-half)
- cy (- (tile-pixel :y) pin-size-half)]
- ;; now draw a "pin" at the given tile pixel location
- (.setColor graphics color-white)
- (.fillRect graphics cx cy pin-size pin-size)
- (.setColor graphics color-blue)
- (.fillRect graphics (inc cx) (inc cy) (- pin-size 2) (- pin-size 2))))
+ (doseq [[^double lat ^double lon] points
+ :let [[world-x world-y] (mercator/latlon->world-px lat lon zoom)
+ tile-x (-> (double world-x) Math/floor int (mod tile-size))
+ tile-y (-> (double world-y) Math/floor int (mod tile-size))
+ ;; cx/cy is needed to put center of a pin at the tile-pixel position
+ cx (- tile-x pin-size-half)
+ cy (- tile-y pin-size-half)]]
+ ;; now draw a "pin" at the given tile pixel location
+ (.setColor graphics color-white)
+ (.fillRect graphics cx cy pin-size pin-size)
+ (.setColor graphics color-blue)
+ (.fillRect graphics (inc cx) (inc cy) (- pin-size 2) (- pin-size 2)))
(catch Throwable e
(.printStackTrace e))
(finally
diff --git a/src/metabase/util/web_mercator.clj b/src/metabase/util/web_mercator.clj
new file mode 100644
index 00000000000..d444896ac6f
--- /dev/null
+++ b/src/metabase/util/web_mercator.clj
@@ -0,0 +1,26 @@
+(ns metabase.util.web-mercator
+ "Web Mercator projection math, shared by the live pin-tile renderer ([[metabase.tiles.api]]) and the
+ static map renderer ([[metabase.channel.render.maps]]).")
+
+(set! *warn-on-reflection* true)
+
+(def ^:const tile-size
+ "Standard Web Mercator tile size, in pixels."
+ 256)
+
+(defn latlon->world-px
+ "Project `lat`/`lon` to global Web Mercator pixel coordinates (origin top-left) at integer `zoom`,
+ returning `[x y]` doubles. Latitudes are clamped just short of the poles, where the projection
+ diverges."
+ [^double lat ^double lon ^long zoom]
+ (let [world (double (* tile-size (bit-shift-left 1 zoom)))
+ x (* world (/ (+ lon 180.0) 360.0))
+ sin-lat (-> lat
+ Math/toRadians
+ Math/sin
+ (Math/max -0.9999)
+ (Math/min 0.9999))
+ y (* world (- 0.5 (/ (Math/log (/ (+ 1.0 sin-lat)
+ (- 1.0 sin-lat)))
+ (* 4.0 Math/PI))))]
+ [x y]))
diff --git a/test/metabase/channel/render/body_test.clj b/test/metabase/channel/render/body_test.clj
index 29f658ae999..668356cc93b 100644
--- a/test/metabase/channel/render/body_test.clj
+++ b/test/metabase/channel/render/body_test.clj
@@ -1,6 +1,7 @@
(ns metabase.channel.render.body-test
{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase.channel.render.body-test]}}}}}}
(:require
+ [clj-http.fake :as fake]
[clojure.string :as str]
[clojure.test :refer :all]
[clojure.walk :as walk]
@@ -20,6 +21,8 @@
[metabase.test.data.interface :as tx]
[metabase.util :as u]))
+(set! *warn-on-reflection* true)
+
(use-fixtures :each
(fn warn-possible-rebuild
[thunk]
@@ -1417,3 +1420,15 @@
(is (every? #(str/includes? h %) ["11" "22" "33" "44" "100" "200" "300" "400"])))
(testing "only the four m2 value cells (> 50) are colored"
(is (= 4 (count (re-seq #"background-color" h))))))))
+
+(deftest render-pin-map-resolves-columns-by-semantic-type-test
+ (testing "render :pin_map finds lat/long columns by semantic type when the column settings aren't persisted"
+ (fake/with-fake-routes (render.tu/fake-tile-routes #"https://.*tile\.openstreetmap\.org/.*")
+ (let [card {:display :map :visualization_settings {}}
+ data {:cols [{:name "latitude" :semantic_type :type/Latitude}
+ {:name "longitude" :semantic_type :type/Longitude}
+ {:name "state" :semantic_type :type/State}]
+ :rows [[37.7749 -122.4194 "CA"] [40.7128 -74.0060 "NY"]]}
+ part (body/render :pin_map :inline "UTC" card nil data)]
+ ;; should be a rendered image, NOT a degraded table
+ (is (= :img (-> part :content second first)))))))
diff --git a/test/metabase/channel/render/card_test.clj b/test/metabase/channel/render/card_test.clj
index 6dd2f88f733..3efb638f829 100644
--- a/test/metabase/channel/render/card_test.clj
+++ b/test/metabase/channel/render/card_test.clj
@@ -11,6 +11,7 @@
[metabase.query-processor.test :as qp]
[metabase.test :as mt]
[metabase.util :as u]
+ [metabase.util.json :as json]
[metabase.util.match :as match]))
(set! *warn-on-reflection* true)
@@ -52,15 +53,140 @@
first
last))))))
-(deftest ^:parallel detect-pulse-chart-type-test
- (testing "Currently unsupported chart types for static-viz return `nil`."
- (are [tyype] (nil? (channel.render/detect-pulse-chart-type {:display tyype}
- {}
- {:cols [{:base_type :type/Number}]
- :rows [[2]]}))
- :pin_map
- :state
- :country)))
+(deftest ^:parallel detect-pulse-chart-type-region-map-test
+ (let [data {:cols [{:base_type :type/Text :name "state"}
+ {:base_type :type/Number :name "count"}]
+ :rows [["CA" 2] ["NY" 4]]}
+ card (fn [settings] {:display :map :visualization_settings settings})]
+ (testing "Built-in region maps are routed to :region_map (string and keyword setting keys)"
+ (are [settings] (= :region_map (channel.render/detect-pulse-chart-type (card settings) nil data))
+ {"map.type" "region" "map.region" "us_states"}
+ {"map.type" "region" "map.region" "world_countries"}
+ ;; production stores viz-settings keys as keywords-with-dots
+ {:map.type "region" :map.region "us_states"}))
+ (testing "map.type defaulting to region (unset) still counts as a region map"
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.region" "us_states"}) nil data))))
+ (testing "Pin/heat/grid maps and custom regions are not statically rendered as region maps"
+ (are [settings] (not= :region_map
+ (channel.render/detect-pulse-chart-type (card settings) nil data))
+ {"map.type" "pin" "map.region" "us_states"}
+ {"map.type" "heat" "map.region" "us_states"}
+ {"map.type" "region" "map.region" "my_custom_map"}))
+ (testing "A dashcard-level region map setting takes precedence over the card"
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.type" "pin"})
+ {:visualization_settings {"map.type" "region" "map.region" "us_states"}}
+ data))))
+ (testing "Card-level map.region is honored even when a dashcard has empty (non-nil) viz-settings"
+ ;; Dashboard subscriptions pass a dashcard whose :visualization_settings is {} — it must not
+ ;; shadow the card's map.region (data here has no State/Country column to fall back on).
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.type" "region" "map.region" "us_states"})
+ {:visualization_settings {}}
+ data))))
+ (testing "A dashcard that overrides map.type to a non-region type wins over the card"
+ (is (not= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.type" "region" "map.region" "us_states"})
+ {:visualization_settings {"map.type" "pin"}}
+ data))))
+ (testing "Region is inferred from a State/Country column when map.region isn't persisted"
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {})
+ nil
+ {:cols [{:semantic_type :type/State :name "st"} {:base_type :type/Number :name "n"}]
+ :rows [["CA" 2]]})))
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {})
+ nil
+ {:cols [{:semantic_type :type/Country :name "c"} {:base_type :type/Number :name "n"}]
+ :rows [["US" 2]]}))))
+ (testing "Legacy :state/:country displays are treated as region maps"
+ (is (= :region_map (channel.render/detect-pulse-chart-type {:display :state} nil data)))
+ (is (= :region_map (channel.render/detect-pulse-chart-type {:display :country} nil data))))))
+
+;; Not ^:parallel: temporarily writes the custom-geojson setting, which would race other tests.
+(deftest detect-pulse-chart-type-custom-region-test
+ (let [data {:cols [{:base_type :type/Text :name "zone"}
+ {:base_type :type/Number :name "count"}]
+ :rows [["a" 2] ["b" 4]]}
+ card (fn [settings] {:display :map :visualization_settings settings})]
+ ;; raw setting value: the setter's URL validation does live DNS, which we don't want in tests
+ (mt/with-temporary-raw-setting-values
+ [custom-geojson (json/encode {:sales_zones {:name "Zones"
+ :url "https://example.com/z.json"
+ :region_key "ZONE"
+ :region_name "NAME"}})]
+ (testing "A user-defined custom region map is detected when its key is in custom-geojson"
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.region" "sales_zones"}) nil data))))
+ (testing "...including in a dashboard subscription where the dashcard has empty viz-settings"
+ ;; This is the case that was rendering as a table: a custom region has no State/Country column
+ ;; to infer from, so an empty dashcard blob shadowing the card's map.region broke it.
+ (is (= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.region" "sales_zones"}) {:visualization_settings {}} data))))
+ (testing "but not when the key is absent from custom-geojson"
+ (is (not= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.region" "unconfigured_region"}) nil data))))
+ (testing "but not when custom GeoJSON is disabled (render-time resolution would fail too)"
+ (mt/with-temp-env-var-value! [mb-custom-geojson-enabled false]
+ (is (not= :region_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.region" "sales_zones"}) nil data))))))))
+
+(deftest ^:parallel detect-pulse-chart-type-pin-map-test
+ (let [data {:cols [{:name "lat"} {:name "lon"}] :rows [[1.0 2.0]]}
+ card (fn [settings] {:display :map :visualization_settings settings})]
+ (testing "pin maps route to :pin_map"
+ (is (= :pin_map (channel.render/detect-pulse-chart-type (card {"map.type" "pin"}) nil data)))
+ (testing "legacy :pin_map display too"
+ (is (= :pin_map (channel.render/detect-pulse-chart-type {:display :pin_map} nil data))))
+ (testing "and in a dashboard subscription with empty dashcard viz-settings"
+ (is (= :pin_map (channel.render/detect-pulse-chart-type
+ (card {"map.type" "pin"}) {:visualization_settings {}} data)))))
+ (testing "heat maps are not statically rendered yet (fall through to table)"
+ (is (= :table (channel.render/detect-pulse-chart-type (card {"map.type" "heat"}) nil data))))
+ (testing "a coordinate map is a pin map (not a region map) even with a State column and unset map.type"
+ ;; Regression: lat/long columns must win over region column-inference.
+ (let [pin+state {:cols [{:name "lat" :semantic_type :type/Latitude}
+ {:name "lon" :semantic_type :type/Longitude}
+ {:name "state" :semantic_type :type/State}]
+ :rows [[37.77 -122.42 "CA"]]}]
+ (is (= :pin_map
+ (channel.render/detect-pulse-chart-type
+ (card {"map.latitude_column" "lat" "map.longitude_column" "lon"}) nil pin+state)))))))
+
+(deftest ^:parallel detect-pulse-chart-type-grid-map-test
+ (let [card {:display :map :visualization_settings {}}
+ data {:cols [{:name "lat" :semantic_type :type/Latitude :binning_info {:bin_width 1.0}}
+ {:name "lon" :semantic_type :type/Longitude :binning_info {:bin_width 1.0}}
+ {:name "n" :base_type :type/Integer}]
+ :rows [[37.0 -122.0 5]]}]
+ (testing "binned lat/long maps route to :grid_map (map.type unset)"
+ (is (= :grid_map (channel.render/detect-pulse-chart-type card nil data))))
+ (testing "binned lat/long routes to :grid_map in a dashboard subscription with empty dashcard settings"
+ (is (= :grid_map (channel.render/detect-pulse-chart-type card {:visualization_settings {}} data))))))
+
+(deftest ^:parallel detect-pulse-chart-type-grid-map-test-2
+ (let [card {:display :map :visualization_settings {}}
+ data {:cols [{:name "lat" :semantic_type :type/Latitude}
+ {:name "lon" :semantic_type :type/Longitude}
+ {:name "n" :base_type :type/Integer}]
+ :rows [[37.0 -122.0 5]]}]
+ (testing "explicit map.type grid"
+ (is (= :grid_map (channel.render/detect-pulse-chart-type
+ (assoc card :visualization_settings {"map.type" "grid"}) nil data))))
+ (testing "unbinned lat/long is a pin map, not a grid map"
+ (is (= :pin_map (channel.render/detect-pulse-chart-type card nil data))))))
(deftest ^:parallel detect-pulse-chart-type-pivot-test
(testing "pivot cards route to :pivot"
diff --git a/test/metabase/channel/render/maps_test.clj b/test/metabase/channel/render/maps_test.clj
new file mode 100644
index 00000000000..e40ba193f2e
--- /dev/null
+++ b/test/metabase/channel/render/maps_test.clj
@@ -0,0 +1,63 @@
+(ns metabase.channel.render.maps-test
+ (:require
+ [clj-http.fake :as fake]
+ [clojure.test :refer :all]
+ [metabase.channel.render.maps :as maps]
+ [metabase.pulse.render.test-util :as render.tu])
+ (:import
+ (java.awt Color)
+ (java.io ByteArrayInputStream)
+ (javax.imageio ImageIO)))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private test-sizing-opts
+ {:min-width 400
+ :min-height 300
+ :max-width 600
+ :max-height 400})
+
+(defn- assert-png-within-bounds
+ "Assert that `png` decodes to an image whose dimensions fall within [[test-sizing-opts]]."
+ [^bytes png]
+ (let [img (ImageIO/read (ByteArrayInputStream. png))]
+ (is (pos? (count png)))
+ (is (<= (:min-width test-sizing-opts) (.getWidth img) (:max-width test-sizing-opts)))
+ (is (<= (:min-height test-sizing-opts) (.getHeight img) (:max-height test-sizing-opts)))))
+
+(deftest render-pin-map-test
+ (testing "renders a PNG (sized to the data, within bounds) from points + (mocked) basemap tiles"
+ (fake/with-fake-routes (render.tu/fake-tile-routes #"https://.*\.tile\.example\.com/.*")
+ (assert-png-within-bounds
+ (maps/render-pin-map [[37.7749 -122.4194] [40.7128 -74.0060]]
+ (assoc test-sizing-opts
+ :tile-url "https://{s}.tile.example.com/{z}/{x}/{y}.png"))))))
+
+(deftest render-grid-map-test
+ (testing "renders binned cells onto a (mocked) basemap, sized to the data within bounds"
+ (fake/with-fake-routes (render.tu/fake-tile-routes #"https://.*tile\.openstreetmap\.org/.*")
+ (assert-png-within-bounds
+ (maps/render-grid-map [{:lat 30
+ :lon -120
+ :lat-bin 4
+ :lon-bin 6
+ :metric 1}
+ {:lat 38
+ :lon -90
+ :lat-bin 4
+ :lon-bin 6
+ :metric 9}]
+ (assoc test-sizing-opts
+ :tile-url "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"))))))
+
+(deftest ^:parallel pin-marker-icon-loads-test
+ (testing "the teardrop pin marker icon is present on the classpath (markers pin type depends on it)"
+ (is (some? (deref @#'maps/pin-marker-icon)))))
+
+(deftest ^:parallel grid-color-endpoints-test
+ (testing "the grid color scale runs green (low) to red (high)"
+ (let [grid-color @#'maps/grid-color
+ ^Color low (grid-color 0.0 0.0 10.0)
+ ^Color high (grid-color 10.0 0.0 10.0)]
+ (is (> (.getGreen low) (.getRed low)) "low end is greenish")
+ (is (> (.getRed high) (.getGreen high)) "high end is reddish"))))
diff --git a/test/metabase/geojson/api_test.clj b/test/metabase/geojson/api_test.clj
index f2305cfa2e5..0a819a966a1 100644
--- a/test/metabase/geojson/api_test.clj
+++ b/test/metabase/geojson/api_test.clj
@@ -1,6 +1,7 @@
(ns metabase.geojson.api-test
(:require
[clj-http.fake :as fake]
+ [clojure.core.memoize :as memoize]
[clojure.test :refer :all]
[metabase.geojson.api :as api.geojson]
[metabase.geojson.settings :as geojson.settings]
@@ -187,6 +188,24 @@
(is (= "GeoJSON URL failed to load"
(mt/user-http-request :rasta :get 400 "geojson/middle-earth"))))))))
+(deftest region-geojson-test
+ (testing "built-in regions resolve from the classpath without a network fetch"
+ (let [us (api.geojson/region-geojson "us_states")]
+ (is (= "STATE" (:region_key us)))
+ (is (pos? (count (get-in us [:data "features"]))))))
+ (testing "unknown / nil regions resolve to nil"
+ (is (nil? (api.geojson/region-geojson "not-a-real-region")))
+ (is (nil? (api.geojson/region-geojson nil))))
+ (with-geojson-mocks
+ (mt/with-temporary-setting-values [custom-geojson test-custom-geojson]
+ (memoize/memo-clear! @#'api.geojson/fetch-geojson-data)
+ (testing "user-defined custom maps are fetched and parsed"
+ (is (= "Point" (get-in (api.geojson/region-geojson "middle-earth") [:data "type"]))))
+ (memoize/memo-clear! @#'api.geojson/fetch-geojson-data)
+ (testing "returns nil when custom GeoJSON is disabled"
+ (mt/with-temp-env-var-value! [mb-custom-geojson-enabled false]
+ (is (nil? (api.geojson/region-geojson "middle-earth"))))))))
+
(deftest disable-custom-geojson-test
(testing "Should be able to disable GeoJSON proxying endpoints by env var"
(mt/with-temporary-setting-values [custom-geojson test-custom-geojson]
diff --git a/test/metabase/geojson/settings_test.clj b/test/metabase/geojson/settings_test.clj
index 6dd8a6681b9..5f7a02461fb 100644
--- a/test/metabase/geojson/settings_test.clj
+++ b/test/metabase/geojson/settings_test.clj
@@ -22,6 +22,22 @@
(deftest ^:parallel geojson-schema-test
(is (@#'geojson.settings/CustomGeoJSONValidator test-custom-geojson)))
+(deftest ^:parallel builtin-region-geojson-test
+ (testing "Built-in regions resolve to parsed GeoJSON data read from the classpath"
+ (let [us (geojson.settings/builtin-region-geojson "us_states")]
+ (is (= "STATE" (:region_key us)))
+ (is (= "NAME" (:region_name us)))
+ (is (= "FeatureCollection" (get-in us [:data "type"])))
+ (is (pos? (count (get-in us [:data "features"])))))
+ (let [world (geojson.settings/builtin-region-geojson "world_countries")]
+ (is (= "ISO_A2" (:region_key world)))
+ (is (pos? (count (get-in world [:data "features"]))))))
+ (testing "Keyword region keys are accepted"
+ (is (some? (geojson.settings/builtin-region-geojson :us_states))))
+ (testing "Unknown / non-built-in regions return nil"
+ (is (nil? (geojson.settings/builtin-region-geojson "narnia")))
+ (is (nil? (geojson.settings/builtin-region-geojson nil)))))
+
(deftest ^:parallel validate-geojson-test
(testing "It validates URLs and files appropriately (classpath resources disabled by default)"
(let [examples {;; Internal metadata for GCP
diff --git a/test/metabase/pulse/render/test_util.clj b/test/metabase/pulse/render/test_util.clj
index e1d6ccdb7f8..6e42ba9db42 100644
--- a/test/metabase/pulse/render/test_util.clj
+++ b/test/metabase/pulse/render/test_util.clj
@@ -19,12 +19,30 @@
[metabase.query-processor.test :as qp]
[toucan2.core :as t2])
(:import
+ (java.awt.image BufferedImage)
+ (java.io ByteArrayOutputStream)
+ (javax.imageio ImageIO)
(org.apache.batik.anim.dom SVGOMDocument AbstractElement$ExtendedNamedNodeHashMap)
(org.apache.batik.dom GenericText)
(org.w3c.dom Element Node)))
(set! *warn-on-reflection* true)
+(defn blank-tile-png
+ "A blank 256×256 PNG tile, as bytes — a stand-in basemap tile for static map render tests."
+ ^bytes []
+ (let [img (BufferedImage. 256 256 BufferedImage/TYPE_INT_RGB)
+ out (ByteArrayOutputStream.)]
+ (ImageIO/write img "png" out)
+ (.toByteArray out)))
+
+(defn fake-tile-routes
+ "clj-http fake-routes map serving [[blank-tile-png]] for any tile URL matching `url-pattern`."
+ [url-pattern]
+ {url-pattern (constantly {:status 200
+ :headers {}
+ :body (blank-tile-png)})})
+
(def test-card
{:id 1
:visualization_settings