-
Notifications
You must be signed in to change notification settings - Fork 673
Map: OSM provider — Render tile maps with OpenLayers #34769
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
Open
AlisherAmonulloev
wants to merge
6
commits into
feature/26_2_osm-provider-for-dxmap/main
Choose a base branch
from
feature/26_2_osm-provider-for-dxmap/tiles
base: feature/26_2_osm-provider-for-dxmap/main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b006926
Map: add OpenLayers-backed OSM tile provider
AlisherAmonulloev bb33757
Map: update generated API for the OSM tile provider
AlisherAmonulloev 55b6c7e
Map: add tests for the OSM tile provider
AlisherAmonulloev 5459140
Map: fix generated OSM type reexports
AlisherAmonulloev 4a72693
Map: update OSM warning messages
AlisherAmonulloev dbcb62c
Update packages/devextreme/js/__internal/ui/errors.ts
AlisherAmonulloev 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
26 changes: 26 additions & 0 deletions
26
packages/devextreme/js/__internal/ui/map/provider.dynamic.osm.engine.ts
This file contains hidden or 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,26 @@ | ||
| import type { MapLocation } from '@js/ui/map'; | ||
|
|
||
| export interface MapEngineTileLayerOptions { | ||
| attribution: string; | ||
| maxZoom: number; | ||
| // eslint-disable-next-line spellcheck/spell-checker -- tile server option name | ||
| subdomains?: string | string[]; | ||
| url: string; | ||
| } | ||
|
|
||
| export interface MapEngineSetViewOptions { | ||
| center?: MapLocation; | ||
| zoom?: number; | ||
| } | ||
|
|
||
| export interface MapEngineMap { | ||
| readonly originalMap: unknown; | ||
| dispose: () => void; | ||
| replaceTileLayer: (options: MapEngineTileLayerOptions) => void; | ||
| setView: (options: MapEngineSetViewOptions) => void; | ||
| updateDimensions: () => void; | ||
| } | ||
|
|
||
| export interface MapEngine { | ||
| createMap: (container: Element, view?: MapEngineSetViewOptions) => MapEngineMap; | ||
| } |
189 changes: 189 additions & 0 deletions
189
packages/devextreme/js/__internal/ui/map/provider.dynamic.osm.openlayers.ts
This file contains hidden or 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,189 @@ | ||
| import type { MapLocation } from '@js/ui/map'; | ||
|
|
||
| import type { | ||
| MapEngine, | ||
| MapEngineMap, | ||
| MapEngineSetViewOptions, | ||
| MapEngineTileLayerOptions, | ||
| } from './provider.dynamic.osm.engine'; | ||
|
|
||
| type Options = Record<string, unknown>; | ||
| type Coordinate = [number, number]; | ||
|
|
||
| interface ViewLike { | ||
| setCenter: (center: Coordinate) => void; | ||
| setZoom: (zoom: number) => void; | ||
| } | ||
|
|
||
| interface TileLayerLike { | ||
| setSource: (source: unknown) => void; | ||
| } | ||
|
|
||
| interface MapLike { | ||
| addLayer: (layer: unknown) => void; | ||
| getView: () => ViewLike; | ||
| removeLayer: (layer: unknown) => void; | ||
| setTarget: (target?: Element) => void; | ||
| updateSize: () => void; | ||
| } | ||
|
|
||
| interface OpenLayersApi { | ||
| Map: new (options: Options) => MapLike; | ||
| View: new (options: Options) => ViewLike; | ||
| control: { | ||
| defaults: { | ||
| defaults: (options?: Options) => unknown; | ||
| }; | ||
| }; | ||
| interaction: { | ||
| defaults: { | ||
| defaults: (options?: Options) => unknown; | ||
| }; | ||
| }; | ||
| layer: { | ||
| Tile: new (options: Options) => TileLayerLike; | ||
| }; | ||
| proj: { | ||
| fromLonLat: (coordinate: Coordinate) => Coordinate; | ||
| }; | ||
| source: { | ||
| ImageTile: new (options: Options) => unknown; | ||
| }; | ||
| } | ||
|
|
||
| const isRecord = (value: unknown): value is Record<string, unknown> => ( | ||
| Boolean(value) && (typeof value === 'object' || typeof value === 'function') | ||
| ); | ||
|
|
||
| const hasFunction = (value: unknown, property: string): boolean => ( | ||
| isRecord(value) && typeof value[property] === 'function' | ||
| ); | ||
|
|
||
| const isOpenLayersApi = (api: unknown): api is OpenLayersApi => { | ||
| if (!isRecord(api)) { | ||
| return false; | ||
| } | ||
|
|
||
| return typeof api.Map === 'function' | ||
| && typeof api.View === 'function' | ||
| && isRecord(api.control) | ||
| && isRecord(api.control.defaults) | ||
| && hasFunction(api.control.defaults, 'defaults') | ||
| && isRecord(api.interaction) | ||
| && isRecord(api.interaction.defaults) | ||
| && hasFunction(api.interaction.defaults, 'defaults') | ||
| && hasFunction(api.layer, 'Tile') | ||
| && hasFunction(api.proj, 'fromLonLat') | ||
| && hasFunction(api.source, 'ImageTile'); | ||
| }; | ||
|
|
||
| const toCoordinate = (api: OpenLayersApi, location: MapLocation): Coordinate => ( | ||
| api.proj.fromLonLat([location.lng, location.lat]) | ||
| ); | ||
|
|
||
| const createTileUrlList = ( | ||
| url: string, | ||
| // eslint-disable-next-line spellcheck/spell-checker -- tile server option name | ||
| subdomains: MapEngineTileLayerOptions['subdomains'], | ||
| ): string[] => { | ||
| // eslint-disable-next-line spellcheck/spell-checker -- tile server option name | ||
| const values = Array.isArray(subdomains) ? subdomains : [...(subdomains ?? '')]; | ||
|
|
||
| return values.map((value) => url.replace('{s}', value)); | ||
| }; | ||
|
|
||
| class OpenLayersMap implements MapEngineMap { | ||
| readonly originalMap: MapLike; | ||
|
|
||
| private readonly _keyboardEventTarget: Element; | ||
|
|
||
| private readonly _ownsKeyboardTabIndex: boolean; | ||
|
|
||
| private _tileLayer?: TileLayerLike; | ||
|
|
||
| private _disposed = false; | ||
|
|
||
| constructor( | ||
| private readonly _api: OpenLayersApi, | ||
| container: Element, | ||
| view: MapEngineSetViewOptions = {}, | ||
| ) { | ||
| const rootNode = container.getRootNode(); | ||
| const ShadowRootConstructor = container.ownerDocument.defaultView?.ShadowRoot; | ||
| const shadowRoot = ShadowRootConstructor && rootNode instanceof ShadowRootConstructor | ||
| ? rootNode | ||
| : undefined; | ||
|
|
||
| this._keyboardEventTarget = shadowRoot?.host ?? container; | ||
| this._ownsKeyboardTabIndex = !this._keyboardEventTarget.hasAttribute('tabindex'); | ||
| if (this._ownsKeyboardTabIndex) { | ||
| this._keyboardEventTarget.setAttribute('tabindex', '0'); | ||
| } | ||
|
|
||
| this.originalMap = new _api.Map({ | ||
| controls: _api.control.defaults.defaults({ attribution: true, rotate: false, zoom: false }), | ||
| interactions: _api.interaction.defaults.defaults({ onFocusOnly: false }), | ||
| target: container, | ||
| view: new _api.View({ | ||
| center: toCoordinate(_api, view.center ?? { lat: 0, lng: 0 }), | ||
| zoom: view.zoom ?? 1, | ||
| }), | ||
| }); | ||
| } | ||
|
|
||
| dispose(): void { | ||
| if (this._disposed) { | ||
| return; | ||
| } | ||
|
|
||
| this._disposed = true; | ||
| if (this._tileLayer) { | ||
| this.originalMap.removeLayer(this._tileLayer); | ||
| this._tileLayer = undefined; | ||
| } | ||
| this.originalMap.setTarget(undefined); | ||
| if (this._ownsKeyboardTabIndex && this._keyboardEventTarget.getAttribute('tabindex') === '0') { | ||
| this._keyboardEventTarget.removeAttribute('tabindex'); | ||
| } | ||
| } | ||
|
|
||
| replaceTileLayer(options: MapEngineTileLayerOptions): void { | ||
| const source = new this._api.source.ImageTile({ | ||
| attributions: options.attribution, | ||
| maxZoom: options.maxZoom, | ||
| url: options.url.includes('{s}') | ||
| // eslint-disable-next-line spellcheck/spell-checker -- tile server option name | ||
| ? createTileUrlList(options.url, options.subdomains) | ||
| : options.url, | ||
| }); | ||
|
|
||
| if (this._tileLayer) { | ||
| this._tileLayer.setSource(source); | ||
| return; | ||
| } | ||
|
|
||
| this._tileLayer = new this._api.layer.Tile({ source }); | ||
| this.originalMap.addLayer(this._tileLayer); | ||
| } | ||
|
|
||
| setView(options: MapEngineSetViewOptions): void { | ||
| const view = this.originalMap.getView(); | ||
|
|
||
| if (options.center) { | ||
| view.setCenter(toCoordinate(this._api, options.center)); | ||
| } | ||
| if (options.zoom !== undefined) { | ||
| view.setZoom(options.zoom); | ||
| } | ||
| } | ||
|
|
||
| updateDimensions(): void { | ||
| this.originalMap.updateSize(); | ||
| } | ||
| } | ||
|
|
||
| export const createOpenLayersEngine = (api: unknown): MapEngine | undefined => ( | ||
| isOpenLayersApi(api) | ||
| ? { createMap: (container, view) => new OpenLayersMap(api, container, view) } | ||
| : undefined | ||
| ); | ||
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.
The OpenLayers browser build exposes these functions as
ol.control.defaults.defaultsandol.interaction.defaults.defaults. ESM exports will be adapted to this internal shape separately.