@itwin/core-frontend 3.5.4 → 3.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"WmsMapLayerImageryProvider.js","sourceRoot":"","sources":["../../../../../src/tile/map/ImageryProviders/WmsMapLayerImageryProvider.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F;;GAEG;AACH,sDAAmD;AACnD,oDAA2G;AAC3G,wDAA+C;AAC/C,6CAGwB;AAExB,wCAAwC;AACxC,IAAI,UAAU,GAAG,IAAI,CAAC;AAEtB,MAAM,cAAc,GAAG,uBAAO,CAAC,UAAU,EAAE,CAAC;AAQ5C,gBAAgB;AAChB,MAAa,0BAA2B,SAAQ,kCAAuB;IAQrE,YAAY,QAA+B;QACzC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QANjB,oBAAe,GAAG,IAAI,GAAG,EAA6B,CAAC;QAO7D,IAAI,CAAC,QAAQ,GAAG,uBAAY,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC9D,CAAC;IAEe,KAAK,CAAC,UAAU;QAC9B,IAAI;YACF,MAAM,WAAW,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;YAC3J,IAAI,CAAC,aAAa,GAAG,MAAM,0BAAe,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;YAC9E,IAAI,SAAS,KAAK,IAAI,CAAC,aAAa,EAAE;gBACpC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,C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yright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Tiles\r\n */\r\nimport { IModelStatus } from \"@itwin/core-bentley\";\r\nimport { Cartographic, ImageMapLayerSettings, MapSubLayerSettings, ServerError } from \"@itwin/core-common\";\r\nimport { Point2d } from \"@itwin/core-geometry\";\r\nimport {\r\n ImageryMapTileTree, MapCartoRectangle, MapLayerImageryProvider, MapLayerImageryProviderStatus, QuadId, WmsCapabilities,\r\n WmsCapability, WmsUtilities,\r\n} from \"../../internal\";\r\n\r\n// eslint-disable-next-line prefer-const\r\nlet doToolTips = true;\r\n\r\nconst scratchPoint2d = Point2d.createZero();\r\n\r\n/** @internal */\r\nexport interface WmsCrsSupport {\r\n support3857: boolean;\r\n support4326: boolean;\r\n}\r\n\r\n/** @internal */\r\nexport class WmsMapLayerImageryProvider extends MapLayerImageryProvider {\r\n private _capabilities?: WmsCapabilities;\r\n private _allLayersRange?: MapCartoRectangle;\r\n private _subLayerRanges = new Map<string, MapCartoRectangle>();\r\n private _baseUrl: string;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n private _crsSupport: WmsCrsSupport|undefined;\r\n\r\n constructor(settings: ImageMapLayerSettings) {\r\n super(settings, false);\r\n this._baseUrl = WmsUtilities.getBaseUrl(this._settings.url);\r\n }\r\n\r\n public override async initialize(): Promise<void> {\r\n try {\r\n const credentials = (this._settings.userName && this._settings.password ? {user: this._settings.userName, password: this._settings.password} : undefined);\r\n this._capabilities = await WmsCapabilities.create(this._baseUrl, credentials);\r\n if (undefined !== this._capabilities) {\r\n this._allLayersRange = this._capabilities.cartoRange;\r\n if (this._capabilities.layer && Array.isArray(this._capabilities.layer.subLayers)) {\r\n const mapCartoRanges = ((subLayer: WmsCapability.SubLayer) => {\r\n if (Array.isArray(subLayer.children))\r\n subLayer.children.forEach((child) => mapCartoRanges(child));\r\n else if (subLayer.cartoRange)\r\n this._subLayerRanges.set(subLayer.name, subLayer.cartoRange);\r\n });\r\n this._capabilities.layer.subLayers.forEach((subLayer) => mapCartoRanges(subLayer));\r\n this._settings.subLayers.forEach((subLayer) => {\r\n if (subLayer.isNamed && this._settings.isSubLayerVisible(subLayer)) {\r\n const subLayerRange = this._subLayerRanges.get(subLayer.name);\r\n if (subLayerRange)\r\n if (this.cartoRange)\r\n this.cartoRange.extendRange(subLayerRange);\r\n else\r\n this.cartoRange = subLayerRange.clone();\r\n }\r\n });\r\n }\r\n\r\n if (!this.cartoRange)\r\n this.cartoRange = this._allLayersRange;\r\n\r\n this._crsSupport = this.getCrsSupport();\r\n }\r\n } catch (error: any) {\r\n // Don't throw error if unauthorized status:\r\n // We want the tile tree to be created, so that end-user can get feedback on which layer is missing credentials.\r\n // When credentials will be provided, a new provider will be created, and initialization should be fine.\r\n if (error?.status === 401) {\r\n this.setStatus(MapLayerImageryProviderStatus.RequireAuth);\r\n } else {\r\n throw new ServerError(IModelStatus.ValidationFailed, \"\");\r\n }\r\n }\r\n }\r\n\r\n private getVisibleLayerString() {\r\n const layerNames = this.getVisibleLayers().map((layer)=>layer.name);\r\n return layerNames.join(\"%2C\");\r\n }\r\n\r\n private getVisibleLayers(): MapSubLayerSettings[] {\r\n return this._settings.subLayers.filter((subLayer) => this._settings.isSubLayerVisible(subLayer) && subLayer.isNamed);\r\n }\r\n\r\n private getVisibleLayersSrs() {\r\n const visibleLayers = this.getVisibleLayers();\r\n const visibleLayerNames = visibleLayers.map((layer) => layer.name);\r\n return this._capabilities?.getSubLayersCrs(visibleLayerNames);\r\n }\r\n\r\n private getQueryableLayers(): string[] {\r\n const layerNames = new Array<string>();\r\n const getQueryableSubLayers = ((subLayer: WmsCapability.SubLayer) => {\r\n if (!subLayer)\r\n return;\r\n\r\n if (subLayer.queryable)\r\n layerNames.push(subLayer.name);\r\n\r\n subLayer.children?.forEach((childSubLayer) => getQueryableSubLayers(childSubLayer));\r\n });\r\n\r\n this._capabilities?.layer?.subLayers?.forEach((subLayer) => getQueryableSubLayers(subLayer));\r\n return layerNames;\r\n }\r\n\r\n private getVisibleQueryableLayersString(): string {\r\n const layers = new Array<string>();\r\n const queryable = this.getQueryableLayers();\r\n const visibleLayerNames = this.getVisibleLayers().map((layer) => layer.name);\r\n queryable.forEach((layer: string) => {\r\n if (visibleLayerNames.includes(layer))\r\n layers.push(layer);\r\n });\r\n\r\n return layers.join(\"%2C\");\r\n }\r\n\r\n public getCrsSupport(): WmsCrsSupport {\r\n const layersCrs = this.getVisibleLayersSrs();\r\n\r\n let support3857: boolean|undefined;\r\n let support4326: boolean|undefined;\r\n if (layersCrs) {\r\n for (const [_layerName, crs] of layersCrs) {\r\n if (crs.find((layerCrs) => layerCrs.includes(\"3857\")) === undefined ) {\r\n support3857 = false;\r\n } else if (support3857 === undefined) {\r\n support3857 = true;\r\n }\r\n\r\n if (crs.find((layerCrs) => layerCrs.includes(\"4326\")) === undefined ) {\r\n support4326 = false;\r\n } else if (support4326 === undefined) {\r\n support4326 = true;\r\n }\r\n }\r\n }\r\n\r\n return {support3857: support3857 ?? false, support4326: support4326 ?? false};\r\n }\r\n\r\n // construct the Url from the desired Tile\r\n public async constructUrl(row: number, column: number, zoomLevel: number): Promise<string> {\r\n\r\n let bboxString =\"\";\r\n let crsString =\"\";\r\n\r\n // We support 2 SRS: EPSG:3857 and EPSG:4326, we prefer EPSG:3857.\r\n if (this._crsSupport?.support3857) {\r\n bboxString = this.getEPSG3857ExtentString(row, column, zoomLevel);\r\n crsString= \"EPSG%3A3857\";\r\n } else if (this._crsSupport?.support4326) {\r\n // The WMS 1.3.0 specification mandates using the axis ordering as defined in the EPSG database.\r\n // For instance, for EPSG:4326 the axis ordering is latitude/longitude, or north/east.\r\n // WMS 1.1.0 always requires the axis ordering to be longitude/latitude. *sigh*\r\n if (this._capabilities !== undefined) {\r\n bboxString = this.getEPSG4326ExtentString(row, column, zoomLevel, this._capabilities?.isVersion13); // lat/long ordering\r\n crsString= \"EPSG%3A4326\";\r\n }\r\n\r\n }\r\n\r\n const layerString = this.getVisibleLayerString();\r\n\r\n if (bboxString.length === 0 || crsString.length === 0 ||layerString.length === 0)\r\n return \"\";\r\n\r\n const crsParamName = this._capabilities?.isVersion13 ? \"CRS\" : \"SRS\";\r\n return `${this._baseUrl}?SERVICE=WMS&VERSION=${this._capabilities?.version}&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=${this.transparentBackgroundString}&LAYERS=${layerString}&WIDTH=${this.tileSize}&HEIGHT=${this.tileSize}&${crsParamName}=${crsString}&STYLES=&BBOX=${bboxString}`;\r\n }\r\n\r\n public override async getToolTip(strings: string[], quadId: QuadId, carto: Cartographic, tree: ImageryMapTileTree): Promise<void> {\r\n await super.getToolTip(strings, quadId, carto, tree);\r\n const infoFormats = this._capabilities?.featureInfoFormats;\r\n if (!doToolTips || undefined === infoFormats)\r\n return;\r\n let formatString = infoFormats.find((format) => format === \"text/html\");\r\n if (!formatString)\r\n formatString = infoFormats[0];\r\n\r\n const bboxString = this.getEPSG3857ExtentString(quadId.row, quadId.column, quadId.level);\r\n const layerString = this.getVisibleQueryableLayersString();\r\n if (layerString.length === 0)\r\n return;\r\n const rectangle = tree.getTileRectangle(quadId);\r\n const fraction = rectangle.worldToLocal(Point2d.create(carto.longitude, carto.latitude, scratchPoint2d))!;\r\n const x = Math.floor(.5 + fraction.x * this.tileSize);\r\n const y = Math.floor(.5 + (1.0 - fraction.y) * this.tileSize);\r\n const coordinateString = this._capabilities?.isVersion13 ? `&i=${x}&j=${y}` : `&x=${x}&y=${y}`;\r\n const crsParamName = this._capabilities?.isVersion13 ? \"CRS\" : \"SRS\";\r\n const getFeatureUrl = `${this._baseUrl}?SERVICE=WMS&VERSION=${this._capabilities?.version}&REQUEST=GetFeatureInfo&LAYERS=${layerString}&WIDTH=${this.tileSize}&HEIGHT=${this.tileSize}&${crsParamName}=EPSG%3A3857&BBOX=${bboxString}&QUERY_LAYERS=${layerString}${coordinateString}&info_format=${formatString}`;\r\n return this.toolTipFromUrl(strings, getFeatureUrl);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"WmsMapLayerImageryProvider.js","sourceRoot":"","sources":["../../../../../src/tile/map/ImageryProviders/WmsMapLayerImageryProvider.ts"],"names":[],"mappings":";;;AAAA;;;+FAG+F;AAC/F;;GAEG;AACH,sDAAmD;AACnD,oDAA2G;AAC3G,wDAA+C;AAC/C,6CAGwB;AAExB,wCAAwC;AACxC,IAAI,UAAU,GAAG,IAAI,CAAC;AAEtB,MAAM,cAAc,GAAG,uBAAO,CAAC,UAAU,EAAE,CAAC;AAQ5C,gBAAgB;AAChB,MAAa,0BAA2B,SAAQ,kCAAuB;IAQrE,YAAY,QAA+B;QACzC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QANjB,oBAAe,GAAG,IAAI,GAAG,EAA6B,CAAC;QAO7D,IAAI,CAAC,QAAQ,GAAG,uBAAY,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC9D,CAAC;IAEe,KAAK,CAAC,UAAU;QAC9B,IAAI;YACF,MAAM,WAAW,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,QAAQ,EAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;YAC3J,IAAI,CAAC,aAAa,GAAG,MAAM,0BAAe,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;YAC9E,IAAI,SAAS,KAAK,IAAI,CAAC,aAAa,EAAE;gBACpC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC;gBACrD,IAAI,IAAI,CAAC,aAAa,CAAC,KAAK,IAAI,KAAK,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Aa,CAAC,CAAC;IACrD,CAAC;CACF;AAlLD,gEAkLC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Tiles\r\n */\r\nimport { IModelStatus } from \"@itwin/core-bentley\";\r\nimport { Cartographic, ImageMapLayerSettings, MapSubLayerSettings, ServerError } from \"@itwin/core-common\";\r\nimport { Point2d } from \"@itwin/core-geometry\";\r\nimport {\r\n ImageryMapTileTree, MapCartoRectangle, MapLayerImageryProvider, MapLayerImageryProviderStatus, QuadId, WmsCapabilities,\r\n WmsCapability, WmsUtilities,\r\n} from \"../../internal\";\r\n\r\n// eslint-disable-next-line prefer-const\r\nlet doToolTips = true;\r\n\r\nconst scratchPoint2d = Point2d.createZero();\r\n\r\n/** @internal */\r\nexport interface WmsCrsSupport {\r\n support3857: boolean;\r\n support4326: boolean;\r\n}\r\n\r\n/** @internal */\r\nexport class WmsMapLayerImageryProvider extends MapLayerImageryProvider {\r\n private _capabilities?: WmsCapabilities;\r\n private _allLayersRange?: MapCartoRectangle;\r\n private _subLayerRanges = new Map<string, MapCartoRectangle>();\r\n private _baseUrl: string;\r\n // eslint-disable-next-line @typescript-eslint/naming-convention\r\n private _crsSupport: WmsCrsSupport|undefined;\r\n\r\n constructor(settings: ImageMapLayerSettings) {\r\n super(settings, false);\r\n this._baseUrl = WmsUtilities.getBaseUrl(this._settings.url);\r\n }\r\n\r\n public override async initialize(): Promise<void> {\r\n try {\r\n const credentials = (this._settings.userName && this._settings.password ? {user: this._settings.userName, password: this._settings.password} : undefined);\r\n this._capabilities = await WmsCapabilities.create(this._baseUrl, credentials);\r\n if (undefined !== this._capabilities) {\r\n this._allLayersRange = this._capabilities.cartoRange;\r\n if (this._capabilities.layer && Array.isArray(this._capabilities.layer.subLayers)) {\r\n const mapCartoRanges = ((subLayer: WmsCapability.SubLayer) => {\r\n if (Array.isArray(subLayer.children))\r\n subLayer.children.forEach((child) => mapCartoRanges(child));\r\n else if (subLayer.cartoRange)\r\n this._subLayerRanges.set(subLayer.name, subLayer.cartoRange);\r\n });\r\n this._capabilities.layer.subLayers.forEach((subLayer) => mapCartoRanges(subLayer));\r\n this._settings.subLayers.forEach((subLayer) => {\r\n if (subLayer.isNamed && this._settings.isSubLayerVisible(subLayer)) {\r\n const subLayerRange = this._subLayerRanges.get(subLayer.name);\r\n if (subLayerRange)\r\n if (this.cartoRange)\r\n this.cartoRange.extendRange(subLayerRange);\r\n else\r\n this.cartoRange = subLayerRange.clone();\r\n }\r\n });\r\n }\r\n\r\n if (!this.cartoRange)\r\n this.cartoRange = this._allLayersRange;\r\n\r\n this._crsSupport = this.getCrsSupport();\r\n }\r\n } catch (error: any) {\r\n // Don't throw error if unauthorized status:\r\n // We want the tile tree to be created, so that end-user can get feedback on which layer is missing credentials.\r\n // When credentials will be provided, a new provider will be created, and initialization should be fine.\r\n if (error?.status === 401) {\r\n this.setStatus(MapLayerImageryProviderStatus.RequireAuth);\r\n } else {\r\n throw new ServerError(IModelStatus.ValidationFailed, \"\");\r\n }\r\n }\r\n }\r\n\r\n private getVisibleLayerString() {\r\n const layerNames = this.getVisibleLayers().map((layer)=>layer.name);\r\n return layerNames.join(\"%2C\");\r\n }\r\n\r\n private getVisibleLayers(): MapSubLayerSettings[] {\r\n return this._settings.subLayers.filter((subLayer) => this._settings.isSubLayerVisible(subLayer) && subLayer.isNamed);\r\n }\r\n\r\n private getVisibleLayersSrs() {\r\n const visibleLayers = this.getVisibleLayers();\r\n const visibleLayerNames = visibleLayers.map((layer) => layer.name);\r\n return this._capabilities?.getSubLayersCrs(visibleLayerNames);\r\n }\r\n\r\n private getQueryableLayers(): string[] {\r\n const layerNames = new Array<string>();\r\n const getQueryableSubLayers = ((subLayer: WmsCapability.SubLayer) => {\r\n if (!subLayer)\r\n return;\r\n\r\n if (subLayer.queryable)\r\n layerNames.push(subLayer.name);\r\n\r\n subLayer.children?.forEach((childSubLayer) => getQueryableSubLayers(childSubLayer));\r\n });\r\n\r\n this._capabilities?.layer?.subLayers?.forEach((subLayer) => getQueryableSubLayers(subLayer));\r\n return layerNames;\r\n }\r\n\r\n private getVisibleQueryableLayersString(): string {\r\n const layers = new Array<string>();\r\n const queryable = this.getQueryableLayers();\r\n const visibleLayerNames = this.getVisibleLayers().map((layer) => layer.name);\r\n queryable.forEach((layer: string) => {\r\n if (visibleLayerNames.includes(layer))\r\n layers.push(layer);\r\n });\r\n\r\n return layers.join(\"%2C\");\r\n }\r\n\r\n public getCrsSupport(): WmsCrsSupport {\r\n const layersCrs = this.getVisibleLayersSrs();\r\n\r\n let support3857: boolean|undefined;\r\n let support4326: boolean|undefined;\r\n if (layersCrs) {\r\n for (const [_layerName, crs] of layersCrs) {\r\n if (crs.find((layerCrs) => layerCrs.includes(\"3857\")) === undefined ) {\r\n support3857 = false;\r\n } else if (support3857 === undefined) {\r\n support3857 = true;\r\n }\r\n\r\n if (crs.find((layerCrs) => layerCrs.includes(\"4326\")) === undefined ) {\r\n support4326 = false;\r\n } else if (support4326 === undefined) {\r\n support4326 = true;\r\n }\r\n }\r\n }\r\n\r\n return {support3857: support3857 ?? false, support4326: support4326 ?? false};\r\n }\r\n\r\n // WMS standard requires 'TRUE' or 'FALSE' (case sensitive) values.\r\n public override get transparentBackgroundString(): string { return this._settings.transparentBackground ? \"TRUE\" : \"FALSE\"; }\r\n\r\n // construct the Url from the desired Tile\r\n public async constructUrl(row: number, column: number, zoomLevel: number): Promise<string> {\r\n\r\n let bboxString =\"\";\r\n let crsString =\"\";\r\n\r\n // We support 2 SRS: EPSG:3857 and EPSG:4326, we prefer EPSG:3857.\r\n if (this._crsSupport?.support3857) {\r\n bboxString = this.getEPSG3857ExtentString(row, column, zoomLevel);\r\n crsString= \"EPSG%3A3857\";\r\n } else if (this._crsSupport?.support4326) {\r\n // The WMS 1.3.0 specification mandates using the axis ordering as defined in the EPSG database.\r\n // For instance, for EPSG:4326 the axis ordering is latitude/longitude, or north/east.\r\n // WMS 1.1.0 always requires the axis ordering to be longitude/latitude. *sigh*\r\n if (this._capabilities !== undefined) {\r\n bboxString = this.getEPSG4326ExtentString(row, column, zoomLevel, this._capabilities?.isVersion13); // lat/long ordering\r\n crsString= \"EPSG%3A4326\";\r\n }\r\n\r\n }\r\n\r\n const layerString = this.getVisibleLayerString();\r\n\r\n if (bboxString.length === 0 || crsString.length === 0 ||layerString.length === 0)\r\n return \"\";\r\n\r\n const crsParamName = this._capabilities?.isVersion13 ? \"CRS\" : \"SRS\";\r\n return `${this._baseUrl}?SERVICE=WMS&VERSION=${this._capabilities?.version}&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=${this.transparentBackgroundString}&LAYERS=${layerString}&WIDTH=${this.tileSize}&HEIGHT=${this.tileSize}&${crsParamName}=${crsString}&STYLES=&BBOX=${bboxString}`;\r\n }\r\n\r\n public override async getToolTip(strings: string[], quadId: QuadId, carto: Cartographic, tree: ImageryMapTileTree): Promise<void> {\r\n await super.getToolTip(strings, quadId, carto, tree);\r\n const infoFormats = this._capabilities?.featureInfoFormats;\r\n if (!doToolTips || undefined === infoFormats)\r\n return;\r\n let formatString = infoFormats.find((format) => format === \"text/html\");\r\n if (!formatString)\r\n formatString = infoFormats[0];\r\n\r\n const bboxString = this.getEPSG3857ExtentString(quadId.row, quadId.column, quadId.level);\r\n const layerString = this.getVisibleQueryableLayersString();\r\n if (layerString.length === 0)\r\n return;\r\n const rectangle = tree.getTileRectangle(quadId);\r\n const fraction = rectangle.worldToLocal(Point2d.create(carto.longitude, carto.latitude, scratchPoint2d))!;\r\n const x = Math.floor(.5 + fraction.x * this.tileSize);\r\n const y = Math.floor(.5 + (1.0 - fraction.y) * this.tileSize);\r\n const coordinateString = this._capabilities?.isVersion13 ? `&i=${x}&j=${y}` : `&x=${x}&y=${y}`;\r\n const crsParamName = this._capabilities?.isVersion13 ? \"CRS\" : \"SRS\";\r\n const getFeatureUrl = `${this._baseUrl}?SERVICE=WMS&VERSION=${this._capabilities?.version}&REQUEST=GetFeatureInfo&LAYERS=${layerString}&WIDTH=${this.tileSize}&HEIGHT=${this.tileSize}&${crsParamName}=EPSG%3A3857&BBOX=${bboxString}&QUERY_LAYERS=${layerString}${coordinateString}&info_format=${formatString}`;\r\n return this.toolTipFromUrl(strings, getFeatureUrl);\r\n }\r\n}\r\n"]}
@@ -1,6 +1,58 @@
1
- import { XYZProps } from "@itwin/core-geometry";
1
+ import { BeEvent, Dictionary, SortedArray } from "@itwin/core-bentley";
2
+ import { WritableXYAndZ, XYAndZ, XYZProps } from "@itwin/core-geometry";
2
3
  import { GeoCoordinatesResponseProps, GeographicCRSProps, IModelCoordinatesResponseProps, PointWithStatus } from "@itwin/core-common";
3
4
  import { IModelConnection } from "./IModelConnection";
5
+ /** Options used to create a [[CoordinateConverter]].
6
+ * @internal exported strictly for tests.
7
+ */
8
+ export interface CoordinateConverterOptions {
9
+ /** The iModel for which to perform the conversions. */
10
+ iModel: IModelConnection;
11
+ /** Asynchronously convert each point. The resultant array should have the same number and order of points as the input. */
12
+ requestPoints: (points: XYAndZ[]) => Promise<PointWithStatus[]>;
13
+ /** Maximum number of points to include in each request. Default: 300. */
14
+ maxPointsPerRequest?: number;
15
+ }
16
+ declare type CoordinateConverterState = "idle" | "scheduled" | "in-flight";
17
+ /** Performs conversion of coordinates from one coordinate system to another.
18
+ * A [[GeoConverter]] has a pair of these for converting between iModel coordinates and geographic coordinates.
19
+ * Uses a cache to avoid repeatedly requesting the same points, and a batching strategy to avoid making frequent small requests.
20
+ * The cache stores every point that was ever converted by [[convert]]. It is currently permitted to grow to unbounded size.
21
+ * The batching works as follows:
22
+ * When a conversion is requested via [[convert]], if all the requested points are in the cache, they are returned immediately.
23
+ * Otherwise, any points not in the cache and not in the current in-flight request (if any) are placed onto the queue of pending requests.
24
+ * A pending request is scheduled if one hasn't already been scheduled, via requestAnimationFrame.
25
+ * In the animation frame callback, the pending requests are split into batches of no more than options.maxPointsPerRequest and dispatched to the backend.
26
+ * Once the response is received, the results are loaded into and returned from the cache.
27
+ * If more calls to convert occurred while the request was in flight, another request is dispatched.
28
+ * @internal exported strictly for tests.
29
+ */
30
+ export declare class CoordinateConverter {
31
+ protected readonly _cache: Dictionary<XYAndZ, PointWithStatus>;
32
+ protected _state: CoordinateConverterState;
33
+ protected _pending: SortedArray<XYAndZ>;
34
+ protected _inflight: SortedArray<XYAndZ>;
35
+ protected _onCompleted: BeEvent<() => void>;
36
+ protected readonly _scratchXYZ: {
37
+ x: number;
38
+ y: number;
39
+ z: number;
40
+ };
41
+ protected readonly _maxPointsPerRequest: number;
42
+ protected readonly _iModel: IModelConnection;
43
+ protected readonly _requestPoints: (points: XYAndZ[]) => Promise<PointWithStatus[]>;
44
+ protected toXYAndZ(input: XYZProps, output: WritableXYAndZ): XYAndZ;
45
+ constructor(opts: CoordinateConverterOptions);
46
+ protected dispatch(): Promise<void>;
47
+ protected enqueue(points: XYZProps[]): number;
48
+ protected getFromCache(inputs: XYZProps[]): PointWithStatus[];
49
+ protected scheduleDispatch(): Promise<void>;
50
+ convert(inputs: XYZProps[]): Promise<{
51
+ points: PointWithStatus[];
52
+ fromCache: number;
53
+ }>;
54
+ findCached(inputs: XYZProps[]): CachedIModelCoordinatesResponseProps;
55
+ }
4
56
  /** Response to a request to obtain imodel coordinates from cache.
5
57
  * @internal
6
58
  */
@@ -14,13 +66,12 @@ export interface CachedIModelCoordinatesResponseProps {
14
66
  * @internal
15
67
  */
16
68
  export declare class GeoConverter {
17
- private _datumOrGCRS;
18
- private _gCtoIMCResultCache;
19
- private _iMCtoGCResultCache;
69
+ private readonly _geoToIModel;
70
+ private readonly _iModelToGeo;
20
71
  constructor(iModel: IModelConnection, datumOrGCRS: string | GeographicCRSProps);
21
72
  getIModelCoordinatesFromGeoCoordinates(geoPoints: XYZProps[]): Promise<IModelCoordinatesResponseProps>;
22
- getCachedIModelCoordinatesFromGeoCoordinates(geoPoints: XYZProps[]): CachedIModelCoordinatesResponseProps;
23
73
  getGeoCoordinatesFromIModelCoordinates(iModelPoints: XYZProps[]): Promise<GeoCoordinatesResponseProps>;
74
+ getCachedIModelCoordinatesFromGeoCoordinates(geoPoints: XYZProps[]): CachedIModelCoordinatesResponseProps;
24
75
  }
25
76
  /** The Geographic Services available for an [[IModelConnection]].
26
77
  * @internal
@@ -30,4 +81,5 @@ export declare class GeoServices {
30
81
  constructor(iModel: IModelConnection);
31
82
  getConverter(datumOrGCRS?: string | GeographicCRSProps): GeoConverter | undefined;
32
83
  }
84
+ export {};
33
85
  //# sourceMappingURL=GeoServices.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"GeoServices.d.ts","sourceRoot":"","sources":["../../src/GeoServices.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EACuB,2BAA2B,EAAkB,kBAAkB,EAAiC,8BAA8B,EAClI,eAAe,EACxC,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,oCAAoC;IACnD,oIAAoI;IACpI,MAAM,EAAE,KAAK,CAAC,eAAe,GAAG,SAAS,CAAC,CAAC;IAC3C,mIAAmI;IACnI,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAC;CACtB;AA+LD;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,mBAAmB,CAAqB;IAChD,OAAO,CAAC,mBAAmB,CAAqB;gBACpC,MAAM,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,GAAG,kBAAkB;IASjE,sCAAsC,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,8BAA8B,CAAC;IAK5G,4CAA4C,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,oCAAoC;IAInG,sCAAsC,CAAC,YA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MAAM,GAAG,kBAAkB;IAwBjE,sCAAsC,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,8BAA8B,CAAC;IAQtG,sCAAsC,CAAC,YAAY,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,2BAA2B,CAAC;IAQ5G,4CAA4C,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,oCAAoC;CAGjH;AAED;;GAEG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAAmB;gBAEtB,MAAM,EAAE,gBAAgB;IAI7B,YAAY,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,kBAAkB,GAAG,YAAY,GAAG,SAAS;CAGzF"}
@@ -1,156 +1,163 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ // cspell:ignore GCRS
6
+ import { assert, BeEvent, Dictionary, Logger, SortedArray, } from "@itwin/core-bentley";
1
7
  import { GeoCoordStatus, IModelReadRpcInterface, } from "@itwin/core-common";
2
- // this class is used to cache results from conversion of geoCoordinates to IModelCoordinates.
3
- class GCtoIMCResultCache {
4
- constructor(iModel, source) {
5
- this._iModel = iModel;
6
- this._cache = {};
7
- this._source = source;
8
+ import { FrontendLoggerCategory } from "./FrontendLoggerCategory";
9
+ function compareXYAndZ(lhs, rhs) {
10
+ return lhs.x - rhs.x || lhs.y - rhs.y || lhs.z - rhs.z;
11
+ }
12
+ function cloneXYAndZ(xyz) {
13
+ return { x: xyz.x, y: xyz.y, z: xyz.z };
14
+ }
15
+ /** Performs conversion of coordinates from one coordinate system to another.
16
+ * A [[GeoConverter]] has a pair of these for converting between iModel coordinates and geographic coordinates.
17
+ * Uses a cache to avoid repeatedly requesting the same points, and a batching strategy to avoid making frequent small requests.
18
+ * The cache stores every point that was ever converted by [[convert]]. It is currently permitted to grow to unbounded size.
19
+ * The batching works as follows:
20
+ * When a conversion is requested via [[convert]], if all the requested points are in the cache, they are returned immediately.
21
+ * Otherwise, any points not in the cache and not in the current in-flight request (if any) are placed onto the queue of pending requests.
22
+ * A pending request is scheduled if one hasn't already been scheduled, via requestAnimationFrame.
23
+ * In the animation frame callback, the pending requests are split into batches of no more than options.maxPointsPerRequest and dispatched to the backend.
24
+ * Once the response is received, the results are loaded into and returned from the cache.
25
+ * If more calls to convert occurred while the request was in flight, another request is dispatched.
26
+ * @internal exported strictly for tests.
27
+ */
28
+ export class CoordinateConverter {
29
+ constructor(opts) {
30
+ var _a;
31
+ this._state = "idle";
32
+ // An event fired when the next request completes.
33
+ this._onCompleted = new BeEvent();
34
+ // Used for creating cache keys (XYAndZ) from XYZProps without having to allocate temporary objects.
35
+ this._scratchXYZ = { x: 0, y: 0, z: 0 };
36
+ this._maxPointsPerRequest = Math.max(1, (_a = opts.maxPointsPerRequest) !== null && _a !== void 0 ? _a : 300);
37
+ this._iModel = opts.iModel;
38
+ this._requestPoints = opts.requestPoints;
39
+ this._cache = new Dictionary(compareXYAndZ, cloneXYAndZ);
40
+ this._pending = new SortedArray(compareXYAndZ, false, cloneXYAndZ);
41
+ this._inflight = new SortedArray(compareXYAndZ, false, cloneXYAndZ);
8
42
  }
9
- /** @internal */
10
- findInCache(geoPoints) {
11
- const result = [];
12
- let missing;
13
- for (const geoPoint of geoPoints) {
14
- const key = JSON.stringify(geoPoint);
15
- const imodelCoord = this._cache[key];
16
- result.push(imodelCoord);
17
- if (undefined === imodelCoord) {
18
- if (undefined === missing)
19
- missing = [];
20
- missing.push(geoPoint);
21
- }
43
+ toXYAndZ(input, output) {
44
+ var _a, _b, _c, _d, _e, _f;
45
+ if (Array.isArray(input)) {
46
+ output.x = (_a = input[0]) !== null && _a !== void 0 ? _a : 0;
47
+ output.y = (_b = input[1]) !== null && _b !== void 0 ? _b : 0;
48
+ output.z = (_c = input[2]) !== null && _c !== void 0 ? _c : 0;
22
49
  }
23
- return { result, missing };
24
- }
25
- async findInCacheOrRequest(request) {
26
- const response = { iModelCoords: [], fromCache: 0 };
27
- let missing;
28
- // Index by cache key to obtain index in input array.
29
- const originalPositions = {};
30
- for (let iPoint = 0; iPoint < request.geoCoords.length; ++iPoint) {
31
- const thisGeoCoord = request.geoCoords[iPoint];
32
- // we use the JSON string as the key into our cache of previously returned results.
33
- const thisCacheKey = JSON.stringify(thisGeoCoord);
34
- // put something in each output that corresponds to the input.
35
- if (this._cache[thisCacheKey]) {
36
- response.iModelCoords.push(this._cache[thisCacheKey]);
37
- }
38
- else {
39
- if (undefined === missing)
40
- missing = [];
41
- // add this geoCoord to the request we are going to send.
42
- missing.push(thisGeoCoord);
43
- // keep track of the original position of this point.
44
- if (originalPositions.hasOwnProperty(thisCacheKey)) {
45
- // it is a duplicate of an earlier point (or points)
46
- if (Array.isArray(originalPositions[thisCacheKey])) {
47
- originalPositions[thisCacheKey].push(iPoint);
48
- }
49
- else {
50
- const list = [originalPositions[thisCacheKey], iPoint];
51
- originalPositions[thisCacheKey] = list;
52
- }
53
- }
54
- else {
55
- originalPositions[thisCacheKey] = iPoint;
56
- }
57
- // mark the response as pending.
58
- response.iModelCoords.push({ p: [0, 0, 0], s: GeoCoordStatus.Pending });
59
- }
50
+ else {
51
+ output.x = (_d = input.x) !== null && _d !== void 0 ? _d : 0;
52
+ output.y = (_e = input.y) !== null && _e !== void 0 ? _e : 0;
53
+ output.z = (_f = input.z) !== null && _f !== void 0 ? _f : 0;
60
54
  }
61
- // if none are missing from the cache, resolve the promise immediately
62
- if (undefined === missing) {
63
- response.fromCache = request.geoCoords.length;
55
+ return output;
56
+ }
57
+ async dispatch() {
58
+ assert(this._state === "scheduled");
59
+ if (this._iModel.isClosed || this._pending.isEmpty) {
60
+ this._state = "idle";
61
+ this._onCompleted.raiseEvent();
62
+ return;
64
63
  }
65
- else {
66
- // keep track of how many came from the cache (mostly for tests).
67
- response.fromCache = request.geoCoords.length - missing.length;
68
- // Avoiding requesting too many points at once, exceeding max request length (this definition of "too many" should be safely conservative) - but enough to load 4 levels of tile corners.
69
- const maxPointsPerRequest = 300;
70
- const promises = [];
71
- for (let i = 0; i < missing.length; i += maxPointsPerRequest) {
72
- const remainingRequest = { source: this._source, geoCoords: missing.slice(i, i + maxPointsPerRequest) };
73
- const promise = IModelReadRpcInterface.getClientForRouting(this._iModel.routingContext.token).getIModelCoordinatesFromGeoCoordinates(this._iModel.getRpcProps(), remainingRequest).then((remainingResponse) => {
74
- // put the responses into the cache, and fill in the output response for each
75
- for (let iResponse = 0; iResponse < remainingResponse.iModelCoords.length; ++iResponse) {
76
- const thisPoint = remainingResponse.iModelCoords[iResponse];
77
- // put the answer in the cache.
78
- const thisGeoCoord = remainingRequest.geoCoords[iResponse];
79
- const thisCacheKey = JSON.stringify(thisGeoCoord);
80
- this._cache[thisCacheKey] = thisPoint;
81
- // transfer the answer stored in remainingResponse to the correct position in the overall response.
82
- const responseIndex = originalPositions[thisCacheKey];
83
- if (Array.isArray(responseIndex)) {
84
- for (const thisIndex of responseIndex) {
85
- response.iModelCoords[thisIndex] = thisPoint;
86
- }
87
- }
88
- else {
89
- response.iModelCoords[responseIndex] = thisPoint;
90
- }
91
- }
92
- });
93
- promises.push(promise);
94
- }
95
- await Promise.all(promises);
64
+ this._state = "in-flight";
65
+ // Ensure subsequently-enqueued requests listen for the *next* response to be received.
66
+ const onCompleted = this._onCompleted;
67
+ this._onCompleted = new BeEvent();
68
+ // Pending requests are now in flight. Start a new list of pending requests. It's cheaper to swap than to allocate new objects.
69
+ const inflight = this._pending;
70
+ this._pending = this._inflight;
71
+ assert(this._pending.isEmpty);
72
+ this._inflight = inflight;
73
+ // Split requests if necessary to avoid requesting more than the maximum allowed number of points.
74
+ const promises = [];
75
+ for (let i = 0; i < inflight.length; i += this._maxPointsPerRequest) {
76
+ const requests = inflight.slice(i, i + this._maxPointsPerRequest).extractArray();
77
+ const promise = this._requestPoints(requests).then((results) => {
78
+ if (this._iModel.isClosed)
79
+ return;
80
+ if (results.length !== requests.length)
81
+ Logger.logError(`${FrontendLoggerCategory.Package}.geoservices`, `requested conversion of ${requests.length} points, but received ${results.length} points`);
82
+ for (let j = 0; j < results.length; j++) {
83
+ if (j < requests.length)
84
+ this._cache.set(requests[j], results[j]);
85
+ }
86
+ }).catch((err) => {
87
+ Logger.logException(`${FrontendLoggerCategory.Package}.geoservices`, err);
88
+ });
89
+ promises.push(promise);
96
90
  }
97
- return response;
91
+ await Promise.all(promises);
92
+ assert(this._state === "in-flight");
93
+ this._state = "idle";
94
+ this._inflight.clear();
95
+ // If any more pending conversions arrived while awaiting this request, schedule another request.
96
+ if (!this._pending.isEmpty)
97
+ this.scheduleDispatch(); // eslint-disable-line @typescript-eslint/no-floating-promises
98
+ // Resolve promises of all callers who were awaiting this request.
99
+ onCompleted.raiseEvent();
98
100
  }
99
- }
100
- // this class is used to cache results from conversion of IModelCoordinates to GeoCoordinates.
101
- class IMCtoGCResultCache {
102
- constructor(iModel, target) {
103
- this._iModel = iModel;
104
- this._cache = {};
105
- this._target = target;
101
+ // Add any points not present in cache to pending request list.
102
+ // Return the number of points present in cache.
103
+ enqueue(points) {
104
+ let numInCache = 0;
105
+ for (const point of points) {
106
+ const xyz = this.toXYAndZ(point, this._scratchXYZ);
107
+ if (this._cache.get(xyz))
108
+ ++numInCache;
109
+ else if (!this._inflight.contains(xyz))
110
+ this._pending.insert(xyz);
111
+ }
112
+ return numInCache;
106
113
  }
107
- async findInCacheOrRequest(request) {
108
- let missing = false;
109
- const response = { geoCoords: [], fromCache: 0 };
110
- let remainingRequest;
111
- const originalPositions = [];
112
- for (let iPoint = 0; iPoint < request.iModelCoords.length; ++iPoint) {
113
- const thisIModelCoord = request.iModelCoords[iPoint];
114
- // we use the JSON string as the key into our cache of previously returned results.
115
- const thisCacheKey = JSON.stringify(thisIModelCoord);
116
- // put something in each output that corresponds to the input.
117
- if (this._cache[thisCacheKey]) {
118
- response.geoCoords.push(this._cache[thisCacheKey]);
119
- }
120
- else {
121
- if (!remainingRequest)
122
- remainingRequest = { target: this._target, iModelCoords: [] };
123
- // add this geoCoord to the request we are going to send.
124
- remainingRequest.iModelCoords.push(thisIModelCoord);
125
- // keep track of the original position of this point.
126
- originalPositions.push(iPoint);
127
- // mark the response as pending.
128
- response.geoCoords.push({ p: [0, 0, 0], s: GeoCoordStatus.Pending });
129
- missing = true;
130
- }
114
+ // Obtain converted points from the cache. The assumption is that every point in `inputs` is already present in the cache.
115
+ // Any point not present will be returned unconverted with an error status.
116
+ getFromCache(inputs) {
117
+ const outputs = [];
118
+ for (const input of inputs) {
119
+ const xyz = this.toXYAndZ(input, this._scratchXYZ);
120
+ let output = this._cache.get(xyz);
121
+ if (!output)
122
+ output = { p: { ...xyz }, s: GeoCoordStatus.CSMapError };
123
+ outputs.push(output);
131
124
  }
132
- // if none are missing from the cache, resolve the promise immediately
133
- if (!missing) {
134
- response.fromCache = request.iModelCoords.length;
135
- return response;
125
+ return outputs;
126
+ }
127
+ async scheduleDispatch() {
128
+ if ("idle" === this._state) {
129
+ this._state = "scheduled";
130
+ requestAnimationFrame(() => {
131
+ this.dispatch(); // eslint-disable-line @typescript-eslint/no-floating-promises
132
+ });
136
133
  }
137
- else {
138
- // keep track of how many came from the cache (mostly for tests).
139
- response.fromCache = request.iModelCoords.length - originalPositions.length;
140
- const remainingResponse = await IModelReadRpcInterface.getClientForRouting(this._iModel.routingContext.token).getGeoCoordinatesFromIModelCoordinates(this._iModel.getRpcProps(), remainingRequest);
141
- // put the responses into the cache, and fill in the output response for each
142
- for (let iResponse = 0; iResponse < remainingResponse.geoCoords.length; ++iResponse) {
143
- const thisPoint = remainingResponse.geoCoords[iResponse];
144
- // transfer the answer stored in remainingResponse to the correct position in the overall response.
145
- const responseIndex = originalPositions[iResponse];
146
- response.geoCoords[responseIndex] = thisPoint;
147
- // put the answer in the cache.
148
- const thisIModelCoord = remainingRequest.iModelCoords[iResponse];
149
- const thisCacheKey = JSON.stringify(thisIModelCoord);
150
- this._cache[thisCacheKey] = thisPoint;
134
+ return new Promise((resolve) => {
135
+ this._onCompleted.addOnce(() => resolve());
136
+ });
137
+ }
138
+ async convert(inputs) {
139
+ const fromCache = this.enqueue(inputs);
140
+ assert(fromCache >= 0);
141
+ assert(fromCache <= inputs.length);
142
+ if (fromCache === inputs.length)
143
+ return { points: this.getFromCache(inputs), fromCache };
144
+ await this.scheduleDispatch();
145
+ return { points: this.getFromCache(inputs), fromCache };
146
+ }
147
+ findCached(inputs) {
148
+ const result = [];
149
+ let missing;
150
+ for (const input of inputs) {
151
+ const key = this.toXYAndZ(input, this._scratchXYZ);
152
+ const output = this._cache.get(key);
153
+ result.push(output);
154
+ if (!output) {
155
+ if (!missing)
156
+ missing = [];
157
+ missing.push(input);
151
158
  }
152
- return response;
153
159
  }
160
+ return { result, missing };
154
161
  }
155
162
  }
156
163
  /** The GeoConverter class communicates with the backend to convert longitude/latitude coordinates to iModel coordinates and vice-versa
@@ -158,23 +165,42 @@ class IMCtoGCResultCache {
158
165
  */
159
166
  export class GeoConverter {
160
167
  constructor(iModel, datumOrGCRS) {
161
- if (typeof (datumOrGCRS) === "object")
162
- this._datumOrGCRS = JSON.stringify(datumOrGCRS);
163
- else
164
- this._datumOrGCRS = datumOrGCRS;
165
- this._gCtoIMCResultCache = new GCtoIMCResultCache(iModel, this._datumOrGCRS);
166
- this._iMCtoGCResultCache = new IMCtoGCResultCache(iModel, this._datumOrGCRS);
168
+ const datum = typeof datumOrGCRS === "object" ? JSON.stringify(datumOrGCRS) : datumOrGCRS;
169
+ this._geoToIModel = new CoordinateConverter({
170
+ iModel,
171
+ requestPoints: async (geoCoords) => {
172
+ const request = { source: datum, geoCoords };
173
+ const rpc = IModelReadRpcInterface.getClientForRouting(iModel.routingContext.token);
174
+ const response = await rpc.getIModelCoordinatesFromGeoCoordinates(iModel.getRpcProps(), request);
175
+ return response.iModelCoords;
176
+ },
177
+ });
178
+ this._iModelToGeo = new CoordinateConverter({
179
+ iModel,
180
+ requestPoints: async (iModelCoords) => {
181
+ const request = { target: datum, iModelCoords };
182
+ const rpc = IModelReadRpcInterface.getClientForRouting(iModel.routingContext.token);
183
+ const response = await rpc.getGeoCoordinatesFromIModelCoordinates(iModel.getRpcProps(), request);
184
+ return response.geoCoords;
185
+ },
186
+ });
167
187
  }
168
188
  async getIModelCoordinatesFromGeoCoordinates(geoPoints) {
169
- const requestProps = { source: this._datumOrGCRS, geoCoords: geoPoints };
170
- return this._gCtoIMCResultCache.findInCacheOrRequest(requestProps);
171
- }
172
- getCachedIModelCoordinatesFromGeoCoordinates(geoPoints) {
173
- return this._gCtoIMCResultCache.findInCache(geoPoints);
189
+ const result = await this._geoToIModel.convert(geoPoints);
190
+ return {
191
+ iModelCoords: result.points,
192
+ fromCache: result.fromCache,
193
+ };
174
194
  }
175
195
  async getGeoCoordinatesFromIModelCoordinates(iModelPoints) {
176
- const requestProps = { target: this._datumOrGCRS, iModelCoords: iModelPoints };
177
- return this._iMCtoGCResultCache.findInCacheOrRequest(requestProps);
196
+ const result = await this._iModelToGeo.convert(iModelPoints);
197
+ return {
198
+ geoCoords: result.points,
199
+ fromCache: result.fromCache,
200
+ };
201
+ }
202
+ getCachedIModelCoordinatesFromGeoCoordinates(geoPoints) {
203
+ return this._geoToIModel.findCached(geoPoints);
178
204
  }
179
205
  }
180
206
  /** The Geographic Services available for an [[IModelConnection]].