Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/layers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { registerProj4 } from '@swissgeo/coordinates'
import proj4 from 'proj4'

// let's export the types "globally"
export * from '@/types/layers'
export * from '@/types/capabilities'
export * from '@/types/timeConfig'

export * from '@/validation'
import { register } from 'ol/proj/proj4'

registerProj4(proj4)
register(proj4)
46 changes: 28 additions & 18 deletions packages/layers/src/parsers/WMSCapabilitiesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,41 +467,50 @@ function getFeatureInfoCapability(
* @param options.params URL parameters to pass to WMS server
* @param options.ignoreError Don't throw exception in case of error, but return a default value or
* undefined
* @param options.parentsArray Optional parents array (internal parameter for recursion)
* @returns Layer object, or undefined in case of error (and ignoreError is equal to true)
*/
function getExternalLayer(
capabilities: WMSCapabilitiesResponse,
layerOrLayerId: WMSCapabilityLayer | string,
options?: ExternalLayerParsingOptions<ExternalWMSLayer>
options?: ExternalLayerParsingOptions<ExternalWMSLayer>,
): ExternalWMSLayer | undefined {
if (!layerOrLayerId) {
// without a layer object or layer ID we can do nothing
return
}

const { outputProjection = WGS84, initialValues = {}, ignoreErrors = true } = options ?? {}
const { outputProjection = WGS84, initialValues = {}, ignoreErrors = true, parentsArray } = options ?? {}
const { currentYear, params } = initialValues

let layerId: string
if (typeof layerOrLayerId === 'string') {
layerId = layerOrLayerId
let layer: WMSCapabilityLayer | undefined
let parents: WMSCapabilityLayer[] | undefined

// If we have a layer object, use it directly with provided parents
if (typeof layerOrLayerId !== 'string') {
layer = layerOrLayerId
parents = (parentsArray as WMSCapabilityLayer[]) ?? []
} else {
layerId = layerOrLayerId.Name
}
if (!capabilities.Capability?.Layer?.Layer) {
return
}
// If we have a layer ID, search for it
const layerId = layerOrLayerId
if (!capabilities.Capability?.Layer?.Layer) {
return
}

const layerAndParents = findLayerRecurse(
layerId,
[capabilities.Capability.Layer],
[capabilities.Capability.Layer]
)
if (!layerAndParents) {
return
const layerAndParents = findLayerRecurse(
layerId,
[capabilities.Capability.Layer],
[capabilities.Capability.Layer]
)
if (!layerAndParents) {
return
}
layer = layerAndParents.layer
parents = layerAndParents.parents
}
const { layer, parents } = layerAndParents

if (!layer) {
const layerId = typeof layerOrLayerId === 'string' ? layerOrLayerId : layerOrLayerId.Name
const msg = `No WMS layer ${layerId} found in Capabilities ${capabilities.originUrl.toString()}`
log.error({
title: 'WMS Capabilities parser',
Expand All @@ -513,6 +522,7 @@ function getExternalLayer(
}
throw new CapabilitiesError(msg, 'no_layer_found')
}

// Go through the child to get valid layers
let layers: ExternalWMSLayer[] = []

Expand Down
29 changes: 23 additions & 6 deletions packages/layers/src/parsers/WMTSCapabilitiesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,39 @@ function getExternalLayer(
}
const attributes = getLayerAttributes(capabilities, layer, outputProjection, ignoreErrors)

if (!attributes) {
if (!attributes || !attributes.id) {
log.error(`No attributes found for layer ${layer.Identifier}`)
return
}

let olOptions
try {
olOptions = optionsFromCapabilities(capabilities, {
layer: attributes.id,
projection: outputProjection.epsg,
}) ?? undefined
} catch (error) {
log.warn({
title: 'WMTS Capabilities parser',
titleColor: LogPreDefinedColor.Indigo,
messages: [`Failed to get OpenLayers options for layer ${attributes.id}`, error],
})
if (!ignoreErrors) {
throw new CapabilitiesError(
`Failed to parse WMTS layer options: ${error?.toString()}`,
'invalid_wmts_layer'
)
}
olOptions = undefined
}

return layerUtils.makeExternalWMTSLayer({
type: LayerType.WMTS,
...attributes,
opacity,
isVisible,
isLoading: false,
options:
optionsFromCapabilities(capabilities, {
layer: attributes.id,
projection: outputProjection.epsg,
}) ?? undefined,
options: olOptions,
timeConfig: getTimeConfig(attributes.dimensions),
currentYear,
})
Expand Down
5 changes: 5 additions & 0 deletions packages/layers/src/types/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ export interface ExternalLayerParsingOptions<ExternalLayerType> {
* layer's visibility or opacity after parsing, without using its default values
*/
initialValues?: Partial<ExternalLayerType>
/**
* Optional parents array (internal parameter for recursion)
* @internal
*/
parentsArray?: unknown[]
}

export interface CapabilitiesParser<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { type FlatExtent, LV95, type NormalizedExtent } from '@swissgeo/coordinates'
import { type GeoAdminGroupOfLayers, type Layer, LayerType } from '@swissgeo/layers'
import { type ExternalWMSLayer, type GeoAdminGroupOfLayers, type Layer, LayerType } from '@swissgeo/layers'
import log from '@swissgeo/log'
import { booleanContains, polygon } from '@turf/turf'
import { computed, onMounted, ref, watch } from 'vue'
Expand Down Expand Up @@ -61,8 +61,11 @@ const showItem = computed(() => {
const isGroupOfLayers = (layer: Layer): layer is GeoAdminGroupOfLayers => {
return layer.type === LayerType.GROUP
}
const isWmsLayer = (layer: Layer): layer is ExternalWMSLayer => {
return layer.type === LayerType.WMS
}

const hasChildren = computed(() => isGroupOfLayers(item) && item?.layers?.length > 0)
const hasChildren = computed(() => (isGroupOfLayers(item) || isWmsLayer(item)) && item?.layers?.length && item?.layers?.length > 0)
const hasDescription = computed(() => canBeAddedToTheMap.value && item?.hasDescription)
const isPhoneMode = computed(() => uiStore.isPhoneMode)

Expand All @@ -72,8 +75,8 @@ const isPhoneMode = computed(() => uiStore.isPhoneMode)
*/
const hasChildrenMatchSearch = computed(() => {
if (search) {
if (isGroupOfLayers(item) && hasChildren.value) {
return containsLayer(item.layers, search)
if (hasChildren.value) {
return containsLayer((item as GeoAdminGroupOfLayers | ExternalWMSLayer).layers as Layer[], search)
}
return false
}
Expand Down Expand Up @@ -320,12 +323,12 @@ function containsLayer(layers: Layer[], searchText: string): boolean {
</button>
</div>
<ul
v-if="showChildren && isGroupOfLayers(item)"
v-if="showChildren && hasChildren"
class="menu-catalogue-item-children"
:class="`ps-${2 + depth}`"
>
<LayerCatalogueItem
v-for="(child, index) in item.layers"
v-for="(child, index) in (item as GeoAdminGroupOfLayers | ExternalWMSLayer).layers"
:key="`${index}-${child.id}`"
:item="child"
:search="search"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,11 @@ const timeConfigEntriesWithYear = computed<(LayerTimeConfigEntry & { year: strin
)
)

const humanReadableCurrentTimestamp = computed<string>(() => {
if (timeConfig.currentTimeEntry) {
return renderHumanReadableTimestamp(
timeConfig.currentTimeEntry as LayerTimeConfigEntry & { year: string }
)
}
return ''
})
const humanReadableCurrentTimestamp = computed<string>(() => renderHumanReadableTimestamp(
timeConfig.currentTimeEntry as LayerTimeConfigEntry & { year: string }
))

function renderHumanReadableTimestamp(timeEntry: LayerTimeConfigEntry & { year: string }): string {
function renderHumanReadableTimestamp(timeEntry?: LayerTimeConfigEntry & { year: string }): string {
if (!timeEntry || !timeEntry.year) {
return '-'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export function useCapabilities(newUrl: MaybeRef<string>) {

function handleWms(content: string, fullUrl: URL): UseCapabilitiesResponse {
let wmsMaxSize: WMSMaxSize | undefined

const capabilities = wmsCapabilitiesParser.parse(content, fullUrl)

if (capabilities.Service.MaxWidth && capabilities.Service.MaxHeight) {
wmsMaxSize = {
width: capabilities.Service.MaxWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@ import { LayerType } from '@swissgeo/layers'

import type { LayerActionFilter, LayersStore } from '@/store/modules/layers/types/layers'

import { EXTERNAL_PROVIDER_WHITELISTED_URL_REGEXES } from '@/config/regex.config'

export default function hasDataDisclaimer(
this: LayersStore
): (layerId: string, options?: LayerActionFilter) => boolean {
return (layerId: string, options?: LayerActionFilter) =>
this.getActiveLayersById(layerId, options).some(
(layer: Layer) =>
layer &&
(layer.isExternal || (layer.type === LayerType.KML && !(layer as KMLLayer).adminId))
(layer && layer.isExternal && !checkLayerUrlWhitelisting(options?.baseUrl)) ||
(layer.type === LayerType.KML && !(layer as KMLLayer).adminId)
)
}

function checkLayerUrlWhitelisting(layerBaseUrl?: string): boolean {
if (!layerBaseUrl) {
return false
}
return EXTERNAL_PROVIDER_WHITELISTED_URL_REGEXES.some(
(regex) => !!layerBaseUrl.match(regex)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default async function loadLayerFromCapabilities(
opacity: layer.opacity,
isVisible: layer.isVisible,
customAttributes: layer.customAttributes,
currentYear: (layer as ExternalWMSLayer).currentYear
},
}
)
Expand All @@ -72,6 +73,7 @@ export default async function loadLayerFromCapabilities(
opacity: layer.opacity,
isVisible: layer.isVisible,
customAttributes: layer.customAttributes,
currentYear: (layer as ExternalWMTSLayer).currentYear
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ export default function loadTopic(
messages: ['Error while loading topic tree', error],
})
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function createLayerObject(parsedLayer: Partial<Layer>, currentLayer?: La
layer.opacity = parsedLayer.opacity ?? DEFAULT_OPACITY

if (adminId && layer.type === LayerType.KML) {
;(layer as KMLLayer).adminId = adminId
; (layer as KMLLayer).adminId = adminId
}
} else if (parsedLayer.type === LayerType.KML) {
// format is KML|FILE_URL
Expand Down Expand Up @@ -184,7 +184,7 @@ export function createLayerObject(parsedLayer: Partial<Layer>, currentLayer?: La
const internalLayer = layer as GeoAdminLayer

if (internalLayer.type === LayerType.GEOJSON && updateDelay !== undefined) {
;(internalLayer as GeoAdminGeoJSONLayer).updateDelay = updateDelay
; (internalLayer as GeoAdminGeoJSONLayer).updateDelay = updateDelay
}

// only highlightable feature will output something, for the others a click coordinate is required
Expand Down
5 changes: 1 addition & 4 deletions packages/viewer/tests/cypress/tests-e2e/importToolMaps.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('The Import Maps Tool', () => {
const positionStore = usePositionStore(pinia)
const center = positionStore.center
expect(center).to.have.length(2)
const expectedCenter = [2764440, 1187890]
const expectedCenter = [2764416, 1187917]
cy.wrap(center[0]).should('be.closeTo', expectedCenter[0], 5)
cy.wrap(center[1]).should('be.closeTo', expectedCenter[1], 5)
})
Expand Down Expand Up @@ -243,7 +243,6 @@ describe('The Import Maps Tool', () => {
}
).as('getLegendOfficialSurvey2')
cy.get(`[data-cy="catalogue-tree-item-info-${legendWithoutAbstractLayerId}"]`)
.should('be.visible')
.click()
cy.wait('@getLegendOfficialSurvey2')
cy.get(`[data-cy="simple-window-title"]`)
Expand Down Expand Up @@ -531,15 +530,13 @@ describe('The Import Maps Tool', () => {
cy.openMenuIfMobile()

cy.checkOlLayer([bgLayer, layer1Id, layer2Id, layer4Id])

cy.get('[data-cy="menu-active-layers"]').should('be.visible').click()
cy.get('[data-cy="active-layer-name-layer4-2"]').should('be.visible')
cy.get('[data-cy="time-selector-layer4-2"]').should('not.exist')

//-----------------------------------------------------------------------------------------
cy.log('Reload and check that everything is still present')
cy.reload()
cy.checkOlLayer([bgLayer, layer1Id, layer2Id, layer4Id])
cy.openMenuIfMobile()
cy.get('[data-cy="active-layer-name-layer1-0"]').should('be.visible')
cy.get('[data-cy="time-selector-layer1-0"]').should('not.exist')
Expand Down
Loading