-
Notifications
You must be signed in to change notification settings - Fork 12
[New] Apply class breaks renderer to sublayer #603
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
pgruenler
merged 8 commits into
v.next
from
phil2533/new-ApplyClassBreaksRendererToSublayer
May 13, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63bb5a3
add apply class breaks renderer to sublayer sample
pgruenler f021d17
Merge branch 'v.next' into phil2533/new-ApplyClassBreaksRendererToSub…
pgruenler 172fd47
Update README.metadata.json
pgruenler eb3b12b
PR review feedback
pgruenler 21e0a74
Fix json error
pgruenler 7f06e97
Update Shared/Samples/Apply class breaks renderer to sublayer/ApplyCl…
pgruenler 3f552f0
use if assignment
pgruenler 2364f0d
PR review feedback
pgruenler 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
117 changes: 117 additions & 0 deletions
117
...ples/Apply class breaks renderer to sublayer/ApplyClassBreaksRendererToSublayerView.swift
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,117 @@ | ||
| // 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 ApplyClassBreaksRendererToSublayerView: View { | ||
| /// The map image layer. | ||
| @State private var mapImageLayer = ArcGISMapImageLayer(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer")!) | ||
|
|
||
| /// The counties sublayer. | ||
| @State private var countiesSublayer: ArcGISMapImageSublayer? | ||
|
|
||
| /// The original renderer used to symbolize the sublayer. | ||
| @State private var originalRenderer: Renderer? | ||
|
|
||
| /// A Boolean value indicating that the class breaks renderer is applied. | ||
| @State private var classBreaksRendererIsApplied = false | ||
|
|
||
| /// The map with a topographic basemap style. | ||
| @State private var map: Map = { | ||
| let map = Map(basemapStyle: .arcGISTopographic) | ||
|
|
||
| // Sets the initial viewpoint. | ||
| map.initialViewpoint = Viewpoint( | ||
| boundingGeometry: Envelope( | ||
| xMin: -13934661.666904, | ||
| yMin: 331181.323482, | ||
| xMax: -7355704.998713, | ||
| yMax: 9118038.075882, | ||
| spatialReference: .webMercator | ||
| ) | ||
| ) | ||
| return map | ||
| }() | ||
|
|
||
| /// The error shown in the error alert. | ||
| @State private var error: Error? | ||
|
|
||
| var body: some View { | ||
| MapView(map: map) | ||
| .task { | ||
| // Adds the map image layer to the map. | ||
| map.addOperationalLayer(mapImageLayer) | ||
| do { | ||
| // Loads the map image layer. | ||
| try await mapImageLayer.load() | ||
| // Gets the sublayers. | ||
| let mapImageSublayers = mapImageLayer.mapImageSublayers | ||
| // Gets the third sublayer. | ||
| let sublayer = mapImageSublayers[2] | ||
| // Loads the sublayer. | ||
| try await sublayer.load() | ||
| countiesSublayer = sublayer | ||
| originalRenderer = sublayer.renderer | ||
| } catch { | ||
| self.error = error | ||
| } | ||
| } | ||
| .toolbar { | ||
| ToolbarItem(placement: .bottomBar) { | ||
| Toggle("Change Sublayer Renderer", isOn: $classBreaksRendererIsApplied) | ||
| .disabled(countiesSublayer == nil) | ||
| } | ||
| } | ||
| .onChange(of: classBreaksRendererIsApplied) { | ||
| countiesSublayer?.renderer = if classBreaksRendererIsApplied { | ||
| // Applies the class breaks renderer. | ||
| .populationRenderer | ||
| } else { | ||
| // Applies the original renderer. | ||
| originalRenderer | ||
| } | ||
| } | ||
| .errorAlert(presentingError: $error) | ||
| } | ||
| } | ||
|
|
||
| private extension Renderer { | ||
| /// Creates a class breaks renderer for counties in the US based on their | ||
| /// population in 2007. | ||
| static var populationRenderer: ClassBreaksRenderer { | ||
| // Outline symbol. | ||
| let lineSymbol = SimpleLineSymbol(style: .solid, color: UIColor(white: 0.6, alpha: 1), width: 0.5) | ||
|
|
||
| // Symbol for each class break. | ||
| let symbol1 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.89, green: 0.92, blue: 0.81, alpha: 1), outline: lineSymbol) | ||
| let symbol2 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.59, green: 0.76, blue: 0.75, alpha: 1), outline: lineSymbol) | ||
| let symbol3 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.38, green: 0.65, blue: 0.71, alpha: 1), outline: lineSymbol) | ||
| let symbol4 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.27, green: 0.49, blue: 0.59, alpha: 1), outline: lineSymbol) | ||
| let symbol5 = SimpleFillSymbol(style: .solid, color: UIColor(red: 0.16, green: 0.33, blue: 0.47, alpha: 1), outline: lineSymbol) | ||
|
|
||
| // Class breaks. | ||
| let classBreak1 = ClassBreak(description: "-99 to 8,560", label: "-99 to 8,560", minValue: -99, maxValue: 8_560, symbol: symbol1) | ||
| let classBreak2 = ClassBreak(description: "> 8,560 to 18,109", label: "> 8,560 to 18,109", minValue: 8_561, maxValue: 18_109, symbol: symbol2) | ||
| let classBreak3 = ClassBreak(description: "> 18,109 to 35,501", label: "> 18,109 to 35,501", minValue: 18_110, maxValue: 35_501, symbol: symbol3) | ||
| let classBreak4 = ClassBreak(description: "> 35,501 to 86,100", label: "> 35,501 to 86,100", minValue: 35_502, maxValue: 86_100, symbol: symbol4) | ||
| let classBreak5 = ClassBreak(description: "> 86,100 to 10,110,975", label: "> 86,100 to 10,110,975", minValue: 86_101, maxValue: 10_110_975, symbol: symbol5) | ||
|
|
||
| return ClassBreaksRenderer(fieldName: "POP2007", classBreaks: [classBreak1, classBreak2, classBreak3, classBreak4, classBreak5]) | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| ApplyClassBreaksRendererToSublayerView() | ||
| } |
40 changes: 40 additions & 0 deletions
40
Shared/Samples/Apply class breaks renderer to sublayer/README.md
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,40 @@ | ||
| # Apply class breaks renderer to sublayer | ||
|
|
||
| Apply a renderer to a sublayer. | ||
|
|
||
|  | ||
|
|
||
| ## Use case | ||
|
|
||
| A layer showing animal populations contains sublayers for different species. A renderer could be applied which gives each sublayer a different color, so that populations of each species can be compared visually. | ||
|
|
||
| ## How to use the sample | ||
|
|
||
| Wait for the map image layer to load. Tap the 'Change Sublayer Renderer' button to apply a unique value renderer to see different population ranges in the counties sub-layer data. | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. Create an `ArcGISMapImageLayer` from its URL. | ||
| 2. After it is done loading, get its map image sublayers. | ||
| 3. Get the `MapImageSublayer` you want. | ||
| 4. Create a `ClassBreaksRenderer` with a collection of `ClassBreak`s for different population ranges. | ||
| 5. Set class breaks renderer as the renderer of the sublayer. | ||
|
|
||
| ## Relevant API | ||
|
|
||
| * ArcGISMapImageLayer | ||
| * ArcGISMapImageSubLayer | ||
| * ClassBreak | ||
| * ClassBreaksRenderer | ||
|
|
||
| ## About the data | ||
|
|
||
| This application displays census data from an ArcGIS Server map service. It contains various population statistics, including total population for each county in 2007. | ||
|
|
||
| ## Additional information | ||
|
|
||
| The service hosting the layer must support dynamic layers to be able to change the rendering of sublayers. | ||
|
|
||
| ## Tags | ||
|
|
||
| class breaks, dynamic layer, dynamic rendering, renderer, sublayer, symbology, visualization |
32 changes: 32 additions & 0 deletions
32
Shared/Samples/Apply class breaks renderer to sublayer/README.metadata.json
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,32 @@ | ||
| { | ||
| "category": "Visualization", | ||
| "description": "Apply a renderer to a sublayer.", | ||
| "ignore": false, | ||
| "images": [ | ||
| "apply-class-breaks-renderer-to-sublayer.png" | ||
| ], | ||
| "keywords": [ | ||
| "class breaks", | ||
| "dynamic layer", | ||
| "dynamic rendering", | ||
| "renderer", | ||
| "sublayer", | ||
| "symbology", | ||
| "visualization", | ||
| "ArcGISMapImageLayer", | ||
| "ArcGISMapImageSubLayer", | ||
| "ClassBreak", | ||
| "ClassBreaksRenderer" | ||
| ], | ||
| "redirect_from": [], | ||
| "relevant_apis": [ | ||
| "ArcGISMapImageLayer", | ||
| "ArcGISMapImageSubLayer", | ||
| "ClassBreak", | ||
| "ClassBreaksRenderer" | ||
| ], | ||
| "snippets": [ | ||
| "ApplyClassBreaksRendererToSublayerView.swift" | ||
| ], | ||
| "title": "Apply class breaks renderer to sublayer" | ||
| } |
Binary file added
BIN
+220 KB
...y class breaks renderer to sublayer/apply-class-breaks-renderer-to-sublayer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Uh oh!
There was an error while loading. Please reload this page.