-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Carto: fetchMap to support custom basemaps #8856
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a3c5bb2
Carto: fetchMap to support custom basemaps
zbigg 57be34e
add attribution in test
zbigg 962db78
attribution support
zbigg 976d692
tileSize is part of basemap object in keplerMapConfig
zbigg 057f534
fetchMap basemap returns style as it's either style or url
zbigg cf93623
keplerMapConfig contains whole style
zbigg bdf58e6
cleanup / self-review
zbigg a5a368d
support visibleLayerGroups in carto styles
zbigg 796a3dd
cleanup / CR suggestions
zbigg fae0f75
use errorContext
zbigg 37f0147
tests for api/basemap
zbigg c5689fa
fetchMap/mapStyle: add deprecation warning
zbigg 4c280da
carto: basemap support for google maps
zbigg c64e8f3
cleanup + docs
zbigg b73ed0f
carto: test app support for google basemaps
zbigg 0ebe9c8
docs
zbigg 1be5fc6
CR: typos and docs clarifications
zbigg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) { | ||
donmccurdy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
}, | ||
donmccurdy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old
mapStyle
was actually an object with various internal properties (like styleType - an actual id of style).New api is more robust and thus i propose to completly hide
mapStyle
in documentation.(it's still returned for backwards compatibility, but it can be considered deprecated now)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good hiding it! Shouldn't we also remove it or comment it from example in line 27?
I understand the API still returns it for compatibility but using it in the example without context looks confusing to me