diff --git a/projects/hslayers/services/add-data/url/add-data-url.service.ts b/projects/hslayers/services/add-data/url/add-data-url.service.ts index a898c07054..a569e9ad92 100644 --- a/projects/hslayers/services/add-data/url/add-data-url.service.ts +++ b/projects/hslayers/services/add-data/url/add-data-url.service.ts @@ -108,6 +108,20 @@ export class HsAddDataUrlService { records?.some((l) => l.checked) ?? this.typeSelected == 'arcgis'; } + /** + * Display layers extent parsing error + */ + private layerExtentParsingError(): void { + this.hsToastService.createToastPopupMessage( + 'ADDLAYERS.capabilitiesParsingProblem', + 'ADDLAYERS.layerExtentParsingProblem', + { + serviceCalledFrom: 'HsAddDataUrlService', + toastStyleClasses: 'bg-warning text-white', + }, + ); + } + /** * Calculate cumulative bounding box which encloses all the provided layers (service layer definitions) * Common for WMS/WMTS (WFS has its own implementation) @@ -117,17 +131,10 @@ export class HsAddDataUrlService { return undefined; } try { - const layerExtents = layers.map((lyr) => [...lyr?.getExtent()]); //Spread need to not create reference + const layerExtents = layers.map((lyr) => [...(lyr?.getExtent() || [])]); //Spread need to not create reference return this.calcCombinedExtent(layerExtents); } catch (error) { - this.hsToastService.createToastPopupMessage( - 'ADDLAYERS.capabilitiesParsingProblem', - 'ADDLAYERS.layerExtentParsingProblem', - { - serviceCalledFrom: 'HsAddDataUrlService', - toastStyleClasses: 'bg-warning text-white', - }, - ); + this.layerExtentParsingError(); return undefined; } } @@ -136,30 +143,36 @@ export class HsAddDataUrlService { * For given array of layers (service layer definitions) it calculates a cumulative bounding box which encloses all the layers */ calcCombinedExtent(extents: number[][]): number[] { - const currentMapProj = this.hsMapService.getCurrentProj(); - const bounds = transform([180, 90], 'EPSG:4326', currentMapProj); + try { + const currentMapProj = this.hsMapService.getCurrentProj(); + const bounds = transform([180, 90], 'EPSG:4326', currentMapProj); - return extents.reduce((acc, curr) => { - //some services define layer bboxes beyond the canonical 180/90 degrees intervals, the checks are necessary then - const [west, south, east, north] = curr; - //minimum easting - if (bounds[1] * -1 <= west && west < acc[0]) { - acc[0] = west; - } - //minimum northing - if (bounds[0] * -1 <= south && south < acc[1]) { - acc[1] = south; - } - //maximum easting - if (bounds[1] >= east && east > acc[2]) { - acc[2] = east; - } - //maximum northing - if (bounds[0] >= north && north > acc[3]) { - acc[3] = north; - } - return acc; - }); + const extent = extents.reduce((acc, curr) => { + //some services define layer bboxes beyond the canonical 180/90 degrees intervals, the checks are necessary then + const [west, south, east, north] = curr; + //minimum easting + if (bounds[1] * -1 <= west && west < acc[0]) { + acc[0] = west; + } + //minimum northing + if (bounds[0] * -1 <= south && south < acc[1]) { + acc[1] = south; + } + //maximum easting + if (bounds[1] >= east && east > acc[2]) { + acc[2] = east; + } + //maximum northing + if (bounds[0] >= north && north > acc[3]) { + acc[3] = north; + } + return acc; + }); + return extent.length > 0 ? extent : undefined; + } catch (error) { + this.layerExtentParsingError(); + return undefined; + } } /** diff --git a/projects/hslayers/services/add-data/url/wms.service.ts b/projects/hslayers/services/add-data/url/wms.service.ts index afb67839e1..c035bb5b36 100644 --- a/projects/hslayers/services/add-data/url/wms.service.ts +++ b/projects/hslayers/services/add-data/url/wms.service.ts @@ -250,6 +250,13 @@ export class HsUrlWmsService implements HsUrlTypeServiceModel { * Get extent for a single layer */ private getSingleLayerExtent(serviceLayer: any, crs: string) { + /** + * serviceLayer might be undefined when trying to parse extent of WMS sublayer + * while creating layer. Will be added along with metadata parsing + */ + if (!serviceLayer) { + return; + } let boundingbox = serviceLayer.BoundingBox; if ( crs !== undefined && @@ -498,11 +505,6 @@ export class HsUrlWmsService implements HsUrlTypeServiceModel { ), this.hsMapService.getCurrentProj(), ), - /** - * Control preventing duplicated extent parsing - * during capability attributes parsing - */ - capsExtentSet: true, path: options.path, dimensions: dimensions, legends: legends, @@ -510,6 +512,12 @@ export class HsUrlWmsService implements HsUrlTypeServiceModel { base: this.data.base, visible: this.data.visible, }; + /** + * Control preventing duplicated extent parsing + * during capability attributes parsing + */ + layerOptions['capsExtentSet'] = !!layerOptions.extent; + const new_layer = USE_TILES ? new Tile(layerOptions as TileOptions) : new ImageLayer(layerOptions as ImageOptions); diff --git a/projects/hslayers/services/utils/layer-utils.service.ts b/projects/hslayers/services/utils/layer-utils.service.ts index 3adbcfc26b..cd02c5f2f4 100644 --- a/projects/hslayers/services/utils/layer-utils.service.ts +++ b/projects/hslayers/services/utils/layer-utils.service.ts @@ -551,6 +551,9 @@ export class HsLayerUtilsService { * NOTE: Not using OL because we want to extend width and height independently */ bufferExtent(extent: Extent, currentMapProj: Projection) { + if (!extent) { + return undefined; + } //EPSG:4087 world bounds const [pMinX, pMinY, pMaxX, pMaxY] = [ -20037508.342789, -10018754.171394, 20037508.342789, 10018754.171394,