-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(carto): Support custom basemaps in fetchMap (#8856)
- Loading branch information
Showing
13 changed files
with
740 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import {MapViewState} from '@deck.gl/core'; | ||
import { | ||
GOOGLE_BASEMAPS, | ||
CARTO_MAP_STYLES, | ||
applyLayerGroupFilters, | ||
fetchStyle, | ||
getStyleUrl, | ||
someLayerGroupsDisabled | ||
} from '../basemap'; | ||
import {APIErrorContext, Basemap, KeplerMapConfig, MapLibreBasemapProps} from './types'; | ||
|
||
const CUSTOM_STYLE_ID_PREFIX = 'custom:'; | ||
const DEFAULT_CARTO_STYLE = 'positron'; | ||
|
||
function mapLibreViewpros(config: KeplerMapConfig): Omit<MapLibreBasemapProps, 'style'> { | ||
const {longitude, latitude, ...rest} = config.mapState as MapViewState; | ||
return { | ||
center: [longitude, latitude], | ||
...rest | ||
}; | ||
} | ||
|
||
/** | ||
* Get basemap properties for Carto map. | ||
* | ||
* For maplibre-based basemaps it returns style or style URL that can be used with `maplibregl.Map` compatible component. | ||
* * style url is returned for non-filtered standard Carto basemaps or if user used style URL directly in configuration | ||
* * filtered style object returned for Carto basemaps with layer groups filtered | ||
* | ||
* For Google-maps base maps, it returns options that can be used with `google.maps.Map` constructor. | ||
*/ | ||
export async function fetchBasemapProps({ | ||
config, | ||
errorContext, | ||
|
||
applyLayerFilters = true | ||
}: { | ||
config: KeplerMapConfig; | ||
|
||
/** By default `fetchBasemapProps` applies layers filters to style. Set this to `false` to disable it. */ | ||
applyLayerFilters?: boolean; | ||
errorContext?: APIErrorContext; | ||
}): Promise<Basemap | null> { | ||
const {mapStyle} = config; | ||
const styleType = mapStyle.styleType || DEFAULT_CARTO_STYLE; | ||
if (styleType.startsWith(CUSTOM_STYLE_ID_PREFIX)) { | ||
const currentCustomStyle = config.customBaseMaps?.customStyle; | ||
if (currentCustomStyle) { | ||
return { | ||
type: 'maplibre', | ||
props: { | ||
style: currentCustomStyle.style || currentCustomStyle.url, | ||
...mapLibreViewpros(config) | ||
}, | ||
attribution: currentCustomStyle.customAttribution | ||
}; | ||
} | ||
} | ||
|
||
if (CARTO_MAP_STYLES.includes(styleType)) { | ||
const {visibleLayerGroups} = mapStyle; | ||
const styleUrl = getStyleUrl(styleType); | ||
let style = styleUrl; | ||
let rawStyle = styleUrl; | ||
if (applyLayerFilters && visibleLayerGroups && someLayerGroupsDisabled(visibleLayerGroups)) { | ||
rawStyle = await fetchStyle({styleUrl, errorContext}); | ||
style = applyLayerGroupFilters(rawStyle, visibleLayerGroups); | ||
} | ||
return { | ||
type: 'maplibre', | ||
props: { | ||
style, | ||
...mapLibreViewpros(config) | ||
}, | ||
visibleLayerGroups, | ||
rawStyle | ||
}; | ||
} | ||
const googleBasemapDef = GOOGLE_BASEMAPS[styleType]; | ||
if (googleBasemapDef) { | ||
const {mapState} = config; | ||
return { | ||
type: 'google-maps', | ||
props: { | ||
...googleBasemapDef, | ||
center: {lat: mapState.latitude, lng: mapState.longitude}, | ||
zoom: mapState.zoom + 1, | ||
tilt: mapState.pitch, | ||
heading: mapState.bearing | ||
} | ||
}; | ||
} | ||
return { | ||
type: 'maplibre', | ||
props: { | ||
style: getStyleUrl(DEFAULT_CARTO_STYLE), | ||
...mapLibreViewpros(config) | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
export {CartoAPIError} from './carto-api-error'; | ||
export {fetchMap} from './fetch-map'; | ||
export type {FetchMapOptions} from './fetch-map'; | ||
export type {APIErrorContext, Format, MapType, RequestType, QueryParameters} from './types'; | ||
export type {FetchMapOptions, FetchMapResult} from './fetch-map'; | ||
export type { | ||
APIErrorContext, | ||
Format, | ||
MapType, | ||
RequestType, | ||
QueryParameters, | ||
Basemap, | ||
MapLibreBasemap, | ||
GoogleBasemap | ||
} from './types'; | ||
export {query} from './query'; | ||
export type {QueryOptions} from './query'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.