Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

feat: add extra map export resolutions (#3357)

bdjulbicb5ffed55b439
diff --git a/src/components/src/modals/export-image-modal.tsx b/src/components/src/modals/export-image-modal.tsx
index 8a7df2149..45d81e21a 100644
--- a/src/components/src/modals/export-image-modal.tsx
+++ b/src/components/src/modals/export-image-modal.tsx
@@ -1,13 +1,27 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import React, {useEffect} from 'react';
+import React, {useEffect, useMemo} from 'react';
import styled from 'styled-components';
import ImagePreview from '../common/image-preview';
import {SetExportImageSettingUpdaterAction} from '@kepler.gl/actions';
-import {EXPORT_IMG_RATIO_OPTIONS, EXPORT_IMG_RESOLUTION_OPTIONS} from '@kepler.gl/constants';
-import {ExportImage} from '@kepler.gl/types';
+import {
+ EXPORT_IMG_RATIO_OPTIONS,
+ EXPORT_IMG_RATIOS,
+ OneXResolutionOption,
+ TwoXResolutionOption,
+ Resolution1024x768Option,
+ Resolution1280x960Option,
+ Resolution1600x1200Option,
+ Resolution1920x1440Option,
+ Resolution1280x720Option,
+ Resolution1600x900Option,
+ Resolution1920x1080Option,
+ Resolution2560x1440Option
+} from '@kepler.gl/constants';
+import type {ExportResolutionOption} from '@kepler.gl/constants';
+import type {ExportImage} from '@kepler.gl/types';
import {StyledModalContent, SelectionButton, CheckMark} from '../common/styled-components';
import Switch from '../common/switch';
import {injectIntl, IntlShape} from 'react-intl';
@@ -16,7 +30,7 @@ import {FormattedMessage} from '@kepler.gl/localization';
const ImageOptionList = styled.div`
display: flex;
flex-direction: column;
- justify-content: space-around;
+ gap: 16px;
width: 250px;
.image-option-section {
@@ -30,6 +44,44 @@ const ImageOptionList = styled.div`
display: flex;
flex-direction: row;
padding: 8px 0px;
+ flex-wrap: wrap;
+ gap: 4px;
+ }
+
+ .resolution-dropdown {
+ padding: 0px;
+ margin-top: 8px;
+ margin-bottom: 8px;
+
+ select {
+ width: 100%;
+ padding: 6px 12px;
+ border: 1px solid #d3d3d3;
+ border-radius: 2px;
+ font-size: 14px;
+ cursor: pointer;
+ background-color: white;
+ font-family: inherit;
+ appearance: none;
+ height: 32px;
+
+ &:hover {
+ border-color: #999;
+ background-color: #f9f9f9;
+ }
+
+ &:focus {
+ outline: none;
+ border-color: #0066cc;
+ box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.1);
+ }
+
+ &:disabled {
+ background-color: #f5f5f5;
+ color: #999;
+ cursor: not-allowed;
+ }
+ }
}
input {
@@ -37,6 +89,23 @@ const ImageOptionList = styled.div`
}
`;
+// Define resolution options for each ratio as constants
+const SCREEN_RESOLUTION_OPTIONS = [OneXResolutionOption, TwoXResolutionOption];
+
+const FOUR_BY_THREE_RESOLUTION_OPTIONS = [
+ Resolution1024x768Option,
+ Resolution1280x960Option,
+ Resolution1600x1200Option,
+ Resolution1920x1440Option
+];
+
+const SIXTEEN_BY_NINE_RESOLUTION_OPTIONS = [
+ Resolution1280x720Option,
+ Resolution1600x900Option,
+ Resolution1920x1080Option,
+ Resolution2560x1440Option
+];
+
export interface ExportImageModalProps {
exportImage: ExportImage;
mapW: number;
@@ -57,6 +126,19 @@ const ExportImageModalFactory = () => {
}) => {
const {legend, ratio, resolution} = exportImage;
+ // Filter resolutions based on selected ratio
+ const filteredResolutions = useMemo(() => {
+ if (ratio === EXPORT_IMG_RATIOS.SCREEN) {
+ return SCREEN_RESOLUTION_OPTIONS;
+ } else if (ratio === EXPORT_IMG_RATIOS.FOUR_BY_THREE) {
+ return FOUR_BY_THREE_RESOLUTION_OPTIONS;
+ } else if (ratio === EXPORT_IMG_RATIOS.SIXTEEN_BY_NINE) {
+ return SIXTEEN_BY_NINE_RESOLUTION_OPTIONS;
+ }
+ // For CUSTOM, don't show resolution options
+ return [];
+ }, [ratio]);
+
useEffect(() => {
onUpdateImageSetting({
exporting: true
@@ -73,6 +155,21 @@ const ExportImageModalFactory = () => {
}
}, [mapH, mapW, exportImage, onUpdateImageSetting]);
+ useEffect(() => {
+ // Keep resolution in sync with the selected ratio and available options.
+ // If the current resolution is not available for this ratio, reset it to the first option.
+ if (!filteredResolutions || !filteredResolutions.length) {
+ return;
+ }
+
+ const isValidResolution =
+ Boolean(resolution) && filteredResolutions.some(op => op.id === resolution);
+
+ if (!isValidResolution) {
+ onUpdateImageSetting({resolution: filteredResolutions[0].id});
+ }
+ }, [ratio, filteredResolutions, resolution, onUpdateImageSetting]);
+
return (
<StyledModalContent className="export-image-modal">
<ImageOptionList>
@@ -94,24 +191,59 @@ const ExportImageModalFactory = () => {
))}
</div>
</div>
- <div className="image-option-section">
- <div className="image-option-section-title">
- <FormattedMessage id={'modal.exportImage.resolutionTitle'} />
+ {ratio !== EXPORT_IMG_RATIOS.CUSTOM && (
+ <div className="image-option-section">
+ <div
+ className="image-option-section-title"
+ id="export-image-modal__resolution-title"
+ >
+ <FormattedMessage id={'modal.exportImage.resolutionTitle'} />
+ </div>
+ <FormattedMessage id={'modal.exportImage.resolutionDescription'} />
+ {ratio === EXPORT_IMG_RATIOS.SCREEN ? (
+ <div className="button-list" id="export-image-modal__option_resolution">
+ {filteredResolutions.map(op => (
+ <SelectionButton
+ key={op.id}
+ selected={resolution === op.id}
+ onClick={() => op.available && onUpdateImageSetting({resolution: op.id})}
+ >
+ {op.label}
+ {resolution === op.id && <CheckMark />}
+ </SelectionButton>
+ ))}
+ </div>
+ ) : (
+ <div className="resolution-dropdown" id="export-image-modal__option_resolution">
+ <select
+ value={resolution || ''}
+ aria-labelledby="export-image-modal__resolution-title"
+ onChange={e => {
+ const value = e.target.value;
+ // Only update if a valid resolution is selected
+ if (value && filteredResolutions.some(op => op.id === value)) {
+ // Type-safe: we've verified the value exists in filteredResolutions
+ onUpdateImageSetting({resolution: value as ExportResolutionOption});
+ }
+ // If empty string selected, optionally reset to first available resolution
+ else if (!value && filteredResolutions.length > 0) {
+ onUpdateImageSetting({resolution: filteredResolutions[0].id});
+ }
+ }}
+ >
+ <option value="">
+ {intl.formatMessage({id: 'modal.exportImage.resolutionPlaceholder'})}
+ </option>
+ {filteredResolutions.map(op => (
+ <option key={op.id} value={op.id} disabled={!op.available}>
+ {op.label}
+ </option>
+ ))}
+ </select>
+ </div>
+ )}
</div>
- <FormattedMessage id={'modal.exportImage.resolutionDescription'} />
- <div className="button-list" id="export-image-modal__option_resolution">
- {EXPORT_IMG_RESOLUTION_OPTIONS.map(op => (
- <SelectionButton
- key={op.id}
- selected={resolution === op.id}
- onClick={() => op.available && onUpdateImageSetting({resolution: op.id})}
- >
- {op.label}
- {resolution === op.id && <CheckMark />}
- </SelectionButton>
- ))}
- </div>
- </div>
+ )}
<div className="image-option-section">
<div className="image-option-section-title">
<FormattedMessage id={'modal.exportImage.mapLegendTitle'} />
@@ -133,4 +265,4 @@ const ExportImageModalFactory = () => {
return injectIntl(ExportImageModal);
};
-export default ExportImageModalFactory;
+export default ExportImageModalFactory;
\ No newline at end of file
diff --git a/src/components/src/plot-container.tsx b/src/components/src/plot-container.tsx
index 53eb9879b..8b71613d9 100644
--- a/src/components/src/plot-container.tsx
+++ b/src/components/src/plot-container.tsx
@@ -39,11 +39,37 @@ const CLASS_FILTER = [
const DOM_FILTER_FUNC = node => !CLASS_FILTER.includes(node.className);
const OUT_OF_SCREEN_POSITION = -9999;
+/**
+ * Calculates legend zoom based on export height
+ * Maps export height to a zoom factor between 0.5 and 3
+ * @param height - export height in pixels
+ * @returns zoom factor for legend panel
+ */
+function calculateLegendZoom(height: number): number {
+ const baseHeight = 1080; // 1x zoom reference height
+ const minZoom = 0.5;
+ const maxZoom = 3;
+
+ // For initial/invalid heights, avoid shrinking the legend; use no scaling.
+ if (!Number.isFinite(height) || height <= 0) {
+ return 1;
+ }
+
+ // Linear mapping: height / baseHeight gives us the scale
+ const zoomFactor = height / baseHeight;
+ // Clamp between min and max
+ return Math.max(minZoom, Math.min(maxZoom, zoomFactor));
+}
+
PlotContainerFactory.deps = [MapContainerFactory, MapsLayoutFactory];
// Remove mapbox logo in exported map, because it contains non-ascii characters
// Remove split viewport UI controls from exported images when the legend is shown
-const StyledPlotContainer = styled.div`
+interface StyledPlotContainerProps {
+ legendZoom?: number;
+}
+
+const StyledPlotContainer = styled.div<StyledPlotContainerProps>`
.maplibregl-ctrl-bottom-left,
.maplibregl-ctrl-bottom-right,
.maplibre-attribution-container,
@@ -57,6 +83,11 @@ const StyledPlotContainer = styled.div`
position: absolute;
top: ${OUT_OF_SCREEN_POSITION}px;
left: ${OUT_OF_SCREEN_POSITION}px;
+
+ /* Apply zoom to legend panel based on export height */
+ .map-control-panel {
+ zoom: ${props => props.legendZoom || 1} !important;
+ }
`;
interface StyledMapContainerProps {
@@ -153,6 +184,11 @@ export default function PlotContainerFactory(
mapState.isSplit
]);
+ // Memoize the legend zoom calculation based on export height
+ const legendZoom = useMemo(() => {
+ return calculateLegendZoom(imageSize.imageH);
+ }, [imageSize.imageH]);
+
// Memoize the map style
const scaledMapStyle = useMemo(() => {
const mapStyle = mapFields.mapStyle;
@@ -312,7 +348,7 @@ export default function PlotContainerFactory(
);
return (
- <StyledPlotContainer className="export-map-instance">
+ <StyledPlotContainer className="export-map-instance" legendZoom={legendZoom}>
<StyledMapContainer ref={plottingAreaRef} width={size.width} height={size.height}>
<MapViewStateContextProvider mapState={newMapState}>
{mapContainers}
@@ -323,4 +359,4 @@ export default function PlotContainerFactory(
}
return React.memo(PlotContainer);
-}
+}
\ No newline at end of file
diff --git a/src/constants/src/default-settings.ts b/src/constants/src/default-settings.ts
index 39e1b94ee..579c4f1de 100644
--- a/src/constants/src/default-settings.ts
+++ b/src/constants/src/default-settings.ts
@@ -1020,16 +1020,27 @@ export const SixteenByNineRatioOption: ImageRatioOption = {
export const EXPORT_IMG_RATIO_OPTIONS: ReadonlyArray<ImageRatioOption> = [
ScreenRatioOption,
- CustomRatioOption,
FourByThreeRatioOption,
- SixteenByNineRatioOption
+ SixteenByNineRatioOption,
+ CustomRatioOption
];
+export type ExportResolutionOption =
+ | keyof typeof RESOLUTIONS
+ | '1280x720'
+ | '1920x1080'
+ | '2560x1440'
+ | '1600x900'
+ | '1024x768'
+ | '1280x960'
+ | '1600x1200'
+ | '1920x1440';
+
export type ImageResolutionOption = {
- id: keyof typeof RESOLUTIONS;
+ id: ExportResolutionOption;
label: string;
available: boolean;
- scale: number;
+ scale?: number;
getSize: (screenW: number, screenH: number) => {width: number; height: number};
};
@@ -1055,9 +1066,74 @@ export const TwoXResolutionOption: ImageResolutionOption = {
})
};
+// Fixed dimension options
+export const Resolution1920x1080Option: ImageResolutionOption = {
+ id: '1920x1080',
+ label: '1920 × 1080 (16:9)',
+ available: true,
+ getSize: () => ({width: 1920, height: 1080})
+};
+
+export const Resolution1280x720Option: ImageResolutionOption = {
+ id: '1280x720',
+ label: '1280 × 720 (16:9)',
+ available: true,
+ getSize: () => ({width: 1280, height: 720})
+};
+
+export const Resolution2560x1440Option: ImageResolutionOption = {
+ id: '2560x1440',
+ label: '2560 × 1440 (16:9)',
+ available: true,
+ getSize: () => ({width: 2560, height: 1440})
+};
+
+export const Resolution1600x900Option: ImageResolutionOption = {
+ id: '1600x900',
+ label: '1600 × 900 (16:9)',
+ available: true,
+ getSize: () => ({width: 1600, height: 900})
+};
+
+export const Resolution1024x768Option: ImageResolutionOption = {
+ id: '1024x768',
+ label: '1024 × 768 (4:3)',
+ available: true,
+ getSize: () => ({width: 1024, height: 768})
+};
+
+export const Resolution1280x960Option: ImageResolutionOption = {
+ id: '1280x960',
+ label: '1280 × 960 (4:3)',
+ available: true,
+ getSize: () => ({width: 1280, height: 960})
+};
+
+export const Resolution1600x1200Option: ImageResolutionOption = {
+ id: '1600x1200',
+ label: '1600 × 1200 (4:3)',
+ available: true,
+ getSize: () => ({width: 1600, height: 1200})
+};
+
+export const Resolution1920x1440Option: ImageResolutionOption = {
+ id: '1920x1440',
+ label: '1920 × 1440 (4:3)',
+ available: true,
+ getSize: () => ({width: 1920, height: 1440})
+};
+
export const EXPORT_IMG_RESOLUTION_OPTIONS: ReadonlyArray<ImageResolutionOption> = [
OneXResolutionOption,
- TwoXResolutionOption
+ TwoXResolutionOption,
+ Resolution1280x720Option,
+ Resolution1920x1080Option,
+ Resolution2560x1440Option,
+ Resolution1600x900Option,
+ Resolution1024x768Option,
+ Resolution1280x960Option,
+ Resolution1600x1200Option,
+ Resolution1920x1440Option
];
export const EXPORT_DATA_TYPE = keyMirror({
@@ -1587,4 +1663,4 @@ export const getLoaderOptions = () => {
workerUrl: `${LOADERS_CDN_URL}/textures@${LOADERS_CDN_VERSION}/dist/npy-worker.js`
}
};
-};
+};
\ No newline at end of file
diff --git a/src/localization/src/translations/ca.ts b/src/localization/src/translations/ca.ts
index b1369424a..538ded648 100644
--- a/src/localization/src/translations/ca.ts
+++ b/src/localization/src/translations/ca.ts
@@ -254,6 +254,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: 'Resolució',
resolutionDescription: 'Alta resolució és millor per a les impressions.',
+ resolutionPlaceholder: 'Selecciona resolució...',
mapLegendTitle: 'Llegenda del mapa',
mapLegendAdd: 'Afegir llegenda al mapa'
},
diff --git a/src/localization/src/translations/cn.ts b/src/localization/src/translations/cn.ts
index 0a9d4d591..b748bb2dc 100644
--- a/src/localization/src/translations/cn.ts
+++ b/src/localization/src/translations/cn.ts
@@ -247,6 +247,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: '分辨率',
resolutionDescription: '高分辨率更适合打印。',
+ resolutionPlaceholder: '选择分辨率...',
mapLegendTitle: '地图图例',
mapLegendAdd: '在地图上添加图例'
},
diff --git a/src/localization/src/translations/en.ts b/src/localization/src/translations/en.ts
index 96fd28428..e2d542805 100644
--- a/src/localization/src/translations/en.ts
+++ b/src/localization/src/translations/en.ts
@@ -319,6 +319,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: 'Resolution',
resolutionDescription: 'High resolution is better for prints.',
+ resolutionPlaceholder: 'Select resolution...',
mapLegendTitle: 'Map Legend',
mapLegendAdd: 'Add legend on map'
},
diff --git a/src/localization/src/translations/es.ts b/src/localization/src/translations/es.ts
index c64e4cf1d..699671346 100644
--- a/src/localization/src/translations/es.ts
+++ b/src/localization/src/translations/es.ts
@@ -255,6 +255,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: 'Resolución',
resolutionDescription: 'Una alta resolución es mejor para las impresiones.',
+ resolutionPlaceholder: 'Seleccionar resolución...',
mapLegendTitle: 'Leyenda del mapa',
mapLegendAdd: 'Añadir leyenda al mapa'
},
diff --git a/src/localization/src/translations/fi.ts b/src/localization/src/translations/fi.ts
index baef47615..4e39c3bf7 100644
--- a/src/localization/src/translations/fi.ts
+++ b/src/localization/src/translations/fi.ts
@@ -246,6 +246,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: 'Resoluutio',
resolutionDescription: 'Korkea resoluutio on parempi tulostamista varten.',
+ resolutionPlaceholder: 'Valitse resoluutio...',
mapLegendTitle: 'Kartan selite',
mapLegendAdd: 'Lisää selite karttaan'
},
diff --git a/src/localization/src/translations/ja.ts b/src/localization/src/translations/ja.ts
index 00e8a5079..8d822b432 100644
--- a/src/localization/src/translations/ja.ts
+++ b/src/localization/src/translations/ja.ts
@@ -251,6 +251,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: '解像度',
resolutionDescription: '印刷には高解像度が適しています。',
+ resolutionPlaceholder: '解像度を選択...',
mapLegendTitle: '地図の凡例',
mapLegendAdd: '地図に判例を追加'
},
diff --git a/src/localization/src/translations/pt.ts b/src/localization/src/translations/pt.ts
index dd87d5609..1f0adf3a1 100644
--- a/src/localization/src/translations/pt.ts
+++ b/src/localization/src/translations/pt.ts
@@ -247,6 +247,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: 'Resolução',
resolutionDescription: 'Alta resolução é melhor para impressões.',
+ resolutionPlaceholder: 'Selecionar resolução...',
mapLegendTitle: 'Legenda do Mapa',
mapLegendAdd: 'Adicionar Legenda no mapa'
},
diff --git a/src/localization/src/translations/ru.ts b/src/localization/src/translations/ru.ts
index c3d2f5530..575ee8ccb 100644
--- a/src/localization/src/translations/ru.ts
+++ b/src/localization/src/translations/ru.ts
@@ -251,6 +251,7 @@ export default {
ratio16_9: '16:9',
resolutionTitle: 'Разрешение',
resolutionDescription: 'Для печати лучше использовать высокое разрешение',
+ resolutionPlaceholder: 'Выберите разрешение...',
mapLegendTitle: 'Легенда карты',
mapLegendAdd: 'Добавить легенду на карту'
},
diff --git a/src/types/reducers.d.ts b/src/types/reducers.d.ts
index 8a545e331..26d19454f 100644
--- a/src/types/reducers.d.ts
+++ b/src/types/reducers.d.ts
@@ -3,6 +3,7 @@
import {Field, Millisecond} from './types';
import type {MapViewState} from '@deck.gl/core/typed';
+import type {ExportResolutionOption} from '@kepler.gl/constants';
export type MapState = {
pitch: number;
@@ -371,8 +372,8 @@ export type BaseMapStyle = {
};
export declare type ExportImage = {
- ratio: 'SCREEN' | 'FOUR_BY_THREE' | 'SIXTEEN_BY_NINE' | 'CUSTOM';
- resolution: 'ONE_X' | 'TWO_X';
+ ratio: EXPORT_IMG_RATIOS;
+ resolution: ExportResolutionOption;
legend: boolean;
mapH: number;
mapW: number;
@@ -606,4 +607,4 @@ export type TypedFilter =
| TypedTimeRangeFilter
| TypedSelectFilter
| TypedMultiSelectFilter
- | TypedPolygonFilter;
+ | TypedPolygonFilter;
\ No newline at end of file
diff --git a/src/utils/src/export-utils.ts b/src/utils/src/export-utils.ts
index 7e97dd983..090f2782e 100644
--- a/src/utils/src/export-utils.ts
+++ b/src/utils/src/export-utils.ts
@@ -7,10 +7,10 @@ import get from 'lodash/get';
import {
EXPORT_IMG_RESOLUTION_OPTIONS,
EXPORT_IMG_RATIO_OPTIONS,
- RESOLUTIONS,
EXPORT_IMG_RATIOS,
FourByThreeRatioOption,
- OneXResolutionOption
+ OneXResolutionOption,
+ type ExportResolutionOption
} from '@kepler.gl/constants';
import {ExportImage} from '@kepler.gl/types';
import {generateHashId} from '@kepler.gl/common-utils';
@@ -47,7 +47,7 @@ export function calculateExportImageSize({
mapW: number;
mapH: number;
ratio: keyof typeof EXPORT_IMG_RATIOS;
- resolution: keyof typeof RESOLUTIONS;
+ resolution: ExportResolutionOption;
}) {
if (mapW <= 0 || mapH <= 0) {
return null;
@@ -203,4 +203,4 @@ const exporters = {
exportHtml
};
-export default exporters;
+export default exporters;
\ No newline at end of file
diff --git a/test/node/utils/export-utils-test.js b/test/node/utils/export-utils-test.js
index 6c8e7e407..3a61a9195 100644
--- a/test/node/utils/export-utils-test.js
+++ b/test/node/utils/export-utils-test.js
@@ -114,6 +114,40 @@ test('exportUtils -> calculateExportImageSize', t => {
'Should return a correct valid with a non valid resolution param'
);
+ // Test fixed resolution options
+ t.deepEqual(
+ calculateExportImageSize({
+ mapW: 1440,
+ mapH: 990,
+ ratio: EXPORT_IMG_RATIOS.SCREEN,
+ resolution: '1920x1080'
+ }),
+ {scale: undefined, imageW: 1920, imageH: 1080},
+ 'Should return fixed resolution 1920x1080'
+ );
+
+ t.deepEqual(
+ calculateExportImageSize({
+ mapW: 1440,
+ mapH: 990,
+ ratio: EXPORT_IMG_RATIOS.SCREEN,
+ resolution: '1280x720'
+ }),
+ {scale: undefined, imageW: 1280, imageH: 720},
+ 'Should return fixed resolution 1280x720'
+ );
+
+ t.deepEqual(
+ calculateExportImageSize({
+ mapW: 1440,
+ mapH: 990,
+ ratio: EXPORT_IMG_RATIOS.SCREEN,
+ resolution: '2560x1440'
+ }),
+ {scale: undefined, imageW: 2560, imageH: 1440},
+ 'Should return fixed resolution 2560x1440'
+ );
+
t.end();
});
@@ -135,4 +169,4 @@ test('exportUtils -> isMSEdge', t => {
test('exportUtils -> exportToJsonString', t => {
t.equal(exportToJsonString({test: 1}), '{"test":1}', 'Should convert object to string');
t.end();
-});
+});
\ No newline at end of file