Skip to content

[New] Apply style to WMSLayer #581

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

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions Samples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
3E9F77732C6A60FA0022CAB5 /* QueryFeatureCountAndExtentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E9F77722C6A60FA0022CAB5 /* QueryFeatureCountAndExtentView.swift */; };
3E9F77742C6A6E670022CAB5 /* QueryFeatureCountAndExtentView.swift in Copy Source Code Files */ = {isa = PBXBuildFile; fileRef = 3E9F77722C6A60FA0022CAB5 /* QueryFeatureCountAndExtentView.swift */; };
3EEDE7CE2C5D73F700510104 /* SetSpatialReferenceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EEDE7CD2C5D73F700510104 /* SetSpatialReferenceView.swift */; };
4C8126DD2DBBCF0B006EF7D2 /* ApplyStyleToWMSLayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C8126DC2DBBCEFE006EF7D2 /* ApplyStyleToWMSLayerView.swift */; };
4C8126E22DBFD9EF006EF7D2 /* ApplyStyleToWMSLayerView.swift in Copy Source Code Files */ = {isa = PBXBuildFile; fileRef = 4C8126DC2DBBCEFE006EF7D2 /* ApplyStyleToWMSLayerView.swift */; };
4D126D6D29CA1B6000CFB7A7 /* ShowDeviceLocationWithNMEADataSourcesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D126D6929CA1B6000CFB7A7 /* ShowDeviceLocationWithNMEADataSourcesView.swift */; };
4D126D7229CA1E1800CFB7A7 /* FileNMEASentenceReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D126D7129CA1E1800CFB7A7 /* FileNMEASentenceReader.swift */; };
4D126D7329CA1EFD00CFB7A7 /* ShowDeviceLocationWithNMEADataSourcesView.swift in Copy Source Code Files */ = {isa = PBXBuildFile; fileRef = 4D126D6929CA1B6000CFB7A7 /* ShowDeviceLocationWithNMEADataSourcesView.swift */; };
Expand Down Expand Up @@ -605,6 +607,7 @@
1C29C95A2DBAE5770074028F /* AddWMTSLayerView.swift in Copy Source Code Files */,
004421912DB96A7800249FEE /* AddFeatureCollectionLayerFromQueryView.swift in Copy Source Code Files */,
0044218B2DB9575600249FEE /* AddFeatureCollectionLayerFromPortalItemView.swift in Copy Source Code Files */,
4C8126E22DBFD9EF006EF7D2 /* ApplyStyleToWMSLayerView.swift in Copy Source Code Files */,
D703F04E2D9334BD0077E3A8 /* SnapGeometryEditsWithUtilityNetworkRulesView.Model.swift in Copy Source Code Files */,
D71A9DE52D8CC8B500CA03CB /* SnapGeometryEditsWithUtilityNetworkRulesView.swift in Copy Source Code Files */,
D789AAAE2D66C737007A8E0E /* CreateKMLMultiTrackView.Model.swift in Copy Source Code Files */,
Expand Down Expand Up @@ -923,6 +926,7 @@
3E720F9C2C619B1700E22A9E /* SetInitialViewpointView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetInitialViewpointView.swift; sourceTree = "<group>"; };
3E9F77722C6A60FA0022CAB5 /* QueryFeatureCountAndExtentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryFeatureCountAndExtentView.swift; sourceTree = "<group>"; };
3EEDE7CD2C5D73F700510104 /* SetSpatialReferenceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetSpatialReferenceView.swift; sourceTree = "<group>"; };
4C8126DC2DBBCEFE006EF7D2 /* ApplyStyleToWMSLayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplyStyleToWMSLayerView.swift; sourceTree = "<group>"; };
4D126D6929CA1B6000CFB7A7 /* ShowDeviceLocationWithNMEADataSourcesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShowDeviceLocationWithNMEADataSourcesView.swift; sourceTree = "<group>"; };
4D126D7129CA1E1800CFB7A7 /* FileNMEASentenceReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileNMEASentenceReader.swift; sourceTree = "<group>"; };
4D126D7B29CA3E6000CFB7A7 /* Redlands.nmea */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Redlands.nmea; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1323,6 +1327,7 @@
955AFAC52C10FD74009C8FE5 /* Apply mosaic rule to rasters */,
D771D0C52CD55211004C13CB /* Apply raster rendering rule */,
004A2BA12BED456500C297CE /* Apply scheduled updates to preplanned map area */,
4C8126DB2DBBCED7006EF7D2 /* Apply style to WMS layer */,
D75362CC2A1E862B00D83028 /* Apply unique value renderer */,
1C8EC7422BAE2891001A6929 /* Augment reality to collect data */,
D7084FA42AD771AA00EC7F4F /* Augment reality to fly over scene */,
Expand Down Expand Up @@ -1880,6 +1885,14 @@
path = "Set spatial reference";
sourceTree = "<group>";
};
4C8126DB2DBBCED7006EF7D2 /* Apply style to WMS layer */ = {
isa = PBXGroup;
children = (
4C8126DC2DBBCEFE006EF7D2 /* ApplyStyleToWMSLayerView.swift */,
);
path = "Apply style to WMS layer";
sourceTree = "<group>";
};
4D126D6829CA1B6000CFB7A7 /* Show device location with NMEA data sources */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3792,6 +3805,7 @@
00CB9138284814A4005C2C5D /* SearchWithGeocodeView.swift in Sources */,
1C43BC7F2A43781200509BF8 /* SetVisibilityOfSubtypeSublayerView.Views.swift in Sources */,
D731F3C12AD0D2AC00A8431E /* IdentifyGraphicsView.swift in Sources */,
4C8126DD2DBBCF0B006EF7D2 /* ApplyStyleToWMSLayerView.swift in Sources */,
00E5401C27F3CCA200CF66D5 /* SamplesApp.swift in Sources */,
D73E61962BDAEE6600457932 /* MatchViewpointOfGeoViewsView.swift in Sources */,
E066DD4028610F55004D3D5B /* AddSceneLayerFromServiceView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2025 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import ArcGIS
import SwiftUI

struct ApplyStyleToWMSLayerView: View {
/// The map displayed by the map view.
@State private var map: Map = {
let map = Map(spatialReference: SpatialReference(wkid: .init(26915)!))
map.minScale = 7_000_000
return map
}()
/// The viewpoint of the map view.
@State private var viewpoint: Viewpoint?

/// A WMS layer with multiple styles.
@State private var wmsSublayer: WMSSublayer?
/// The error thrown by the WMS sublayer load operation.
@State private var wmsSublayerLoadError: Error?

/// The styles of the WMS layer.
@State private var styles: [String] = []
/// The current style.
@State private var selectedStyle = ""

var body: some View {
// Display the map in a map view.
MapView(map: map, viewpoint: viewpoint)
.onViewpointChanged(kind: .centerAndScale) { newViewpoint in
viewpoint = newViewpoint
}
.task {
// Create the WMS layer and add it to the map.
let wmsLayer = WMSLayer(
url: URL(string: "https://imageserver.gisdata.mn.gov/cgi-bin/mncomp?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities")!,
layerNames: ["mncomp"]
)
map.addOperationalLayer(wmsLayer)
// Load the WMS layer to get the styles.
do {
try await wmsLayer.load()
viewpoint = wmsLayer.fullExtent.map { Viewpoint(boundingGeometry: $0) }
wmsSublayer = wmsLayer.sublayers.first as? WMSSublayer
styles = wmsLayer.layerInfos.first?.styles ?? []
selectedStyle = styles.first ?? ""
} catch {
wmsSublayerLoadError = error
}
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Picker("Style", selection: $selectedStyle) {
ForEach(styles, id: \.self) { style in
let title = switch style {
case "default": "Default"
case "stretch": "Contrast Stretch"
default: "Unknown"
}
Text(title)
.tag(style)
}
}
.pickerStyle(.segmented)
.onChange(of: selectedStyle) {
wmsSublayer?.currentStyle = selectedStyle
}
}
}
.errorAlert(presentingError: $wmsSublayerLoadError)
}
}
33 changes: 33 additions & 0 deletions Shared/Samples/Apply style to WMS layer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Apply style to WMS layer

Change the style of a Web Map Service (WMS) layer.

![Image of apply style to WMS layer](apply-style-to-wms-layer.png)

## Use case

Layers hosted on WMS may have different pre-set styles available to apply to them. Swapping between these styles can help during visual examination of the data. For example, increasing the contrast of satellite images can help in identifying urban and agricultural areas within forested areas.

## How to use the sample

Once the layer loads, the toggle button will be enabled. Click it to toggle between the first and second styles of the WMS layer.

## How it works

1. Create a WMS layer with the URL of the service and the names of the layers you want using `WMSLayer.init(url:layerNames:)`.
2. When the layer is done loading, get its list of style strings using `WMSLayerInfo.styles`.
3. Set one of the styles using `WMSSublayer.currentStyle`.

## Relevant API

* WMSLayer
* WMSSublayer
* WMSSublayerInfo

## About the data

This sample uses a public service managed by the State of Minnesota and provides composite imagery for the state and the surrounding areas.

## Tags

imagery, styles, visualization, WMS
27 changes: 27 additions & 0 deletions Shared/Samples/Apply style to WMS layer/README.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"category": "Layers",
"description": "Change the style of a Web Map Service (WMS) layer.",
"ignore": false,
"images": [
"apply-style-to-wms-layer.png"
],
"keywords": [
"WMS",
"imagery",
"styles",
"visualization",
"WMSLayer",
"WMSSublayer",
"WMSSublayerInfo"
],
"redirect_from": [],
"relevant_apis": [
"WMSLayer",
"WMSSublayer",
"WMSSublayerInfo"
],
"snippets": [
"ApplyStyleToWMSLayerView.swift"
],
"title": "Apply style to WMS layer"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading