Skip to content
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

[New] Display alternate symbols at different scales #522

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 14 additions & 0 deletions Samples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@
D7BA8C442B2A4DAA00018633 /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7BA8C432B2A4DAA00018633 /* Array.swift */; };
D7BA8C462B2A8ACA00018633 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7BA8C452B2A8ACA00018633 /* String.swift */; };
D7BB3DD22C5D781800FFCD56 /* SaveTheBay.geodatabase in Resources */ = {isa = PBXBuildFile; fileRef = D7BB3DD02C5D781800FFCD56 /* SaveTheBay.geodatabase */; settings = {ASSET_TAGS = (EditGeodatabaseWithTransactions, ); }; };
D7BEBAD22CBDFE1C00F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7BEBACE2CBDFE1C00F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift */; };
D7BEBAD52CBDFE3900F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift in Copy Source Code Files */ = {isa = PBXBuildFile; fileRef = D7BEBACE2CBDFE1C00F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift */; };
D7BEBAA02CBD9CCA00F882E7 /* MontereyElevation.dt2 in Resources */ = {isa = PBXBuildFile; fileRef = D7BEBA9E2CBD9CCA00F882E7 /* MontereyElevation.dt2 */; settings = {ASSET_TAGS = (AddElevationSourceFromRaster, ); }; };
D7C16D1B2AC5F95300689E89 /* Animate3DGraphicView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C16D1A2AC5F95300689E89 /* Animate3DGraphicView.swift */; };
D7C16D1C2AC5F96900689E89 /* Animate3DGraphicView.swift in Copy Source Code Files */ = {isa = PBXBuildFile; fileRef = D7C16D1A2AC5F95300689E89 /* Animate3DGraphicView.swift */; };
Expand Down Expand Up @@ -555,6 +557,7 @@
dstPath = "";
dstSubfolderSpec = 7;
files = (
D7BEBAD52CBDFE3900F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift in Copy Source Code Files */,
D7848F012CBD987B00F6F546 /* AddElevationSourceFromRasterView.swift in Copy Source Code Files */,
D7848EDB2CBD85D100F6F546 /* AddPointSceneLayerView.swift in Copy Source Code Files */,
D7DFA0ED2CBA0260007C31F2 /* AddMapImageLayerView.swift in Copy Source Code Files */,
Expand Down Expand Up @@ -1003,6 +1006,7 @@
D7BA8C452B2A8ACA00018633 /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
D7BB3DD02C5D781800FFCD56 /* SaveTheBay.geodatabase */ = {isa = PBXFileReference; lastKnownFileType = file; path = SaveTheBay.geodatabase; sourceTree = "<group>"; };
D7BEBA9E2CBD9CCA00F882E7 /* MontereyElevation.dt2 */ = {isa = PBXFileReference; lastKnownFileType = text; path = MontereyElevation.dt2; sourceTree = "<group>"; };
D7BEBACE2CBDFE1C00F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayAlternateSymbolsAtDifferentScalesView.swift; sourceTree = "<group>"; };
D7C16D1A2AC5F95300689E89 /* Animate3DGraphicView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Animate3DGraphicView.swift; sourceTree = "<group>"; };
D7C16D1E2AC5FE8200689E89 /* Pyrenees.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Pyrenees.csv; sourceTree = "<group>"; };
D7C16D212AC5FE9800689E89 /* GrandCanyon.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = GrandCanyon.csv; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1229,6 +1233,7 @@
D71C5F602AAA7854006599FD /* Create symbol styles from web styles */,
E000E761286A0B07005D87C5 /* Cut geometry */,
D71099692A27D8880065A1C1 /* Densify and generalize geometry */,
D7BEBAD12CBDFE1C00F882E7 /* Display alternate symbols at different scales */,
D7AE861A2AC39D750049B626 /* Display annotation */,
D7635FEA2B9272CB0044AB97 /* Display clusters */,
00A7A1422A2FC58300F035F7 /* Display content of utility network container */,
Expand Down Expand Up @@ -2568,6 +2573,14 @@
path = 98092369c4ae4d549bbbd45dba993ebc;
sourceTree = "<group>";
};
D7BEBAD12CBDFE1C00F882E7 /* Display alternate symbols at different scales */ = {
isa = PBXGroup;
children = (
D7BEBACE2CBDFE1C00F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift */,
);
path = "Display alternate symbols at different scales";
sourceTree = "<group>";
};
D7C16D172AC5F6C100689E89 /* Animate 3D graphic */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3428,6 +3441,7 @@
0042E24528E4F82C001F33D6 /* ShowViewshedFromPointInSceneView.ViewshedSettingsView.swift in Sources */,
D7DDF8532AF47C6C004352D9 /* FindRouteAroundBarriersView.swift in Sources */,
1C9B74D929DB54560038B06F /* ChangeCameraControllerView.swift in Sources */,
D7BEBAD22CBDFE1C00F882E7 /* DisplayAlternateSymbolsAtDifferentScalesView.swift in Sources */,
D76000AE2AF19C2300B3084D /* FindRouteInMobileMapPackageView.swift in Sources */,
00273CF42A82AB5900A7A77D /* SamplesSearchView.swift in Sources */,
D78666AD2A2161F100C60110 /* FindNearestVertexView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2024 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 DisplayAlternateSymbolsAtDifferentScalesView: View {
/// A map with a layer of the incidents in San Francisco, CA.
@State private var map: Map = {
let map = Map(basemapStyle: .arcGISTopographic)

// Creates the feature layer from a URL and adds it to the map.
let featureTable = ServiceFeatureTable(url: .sf311IncidentsLayer)
let featureLayer = FeatureLayer(featureTable: featureTable)
map.addOperationalLayer(featureLayer)

// Sets the render on the feature layer.
featureLayer.renderer = makeUniqueValueRenderer()

let center = Point(x: -13631200, y: 4546830, spatialReference: .webMercator)
map.initialViewpoint = Viewpoint(center: center, scale: 7_500)

return map
}()

/// The current viewpoint of the map view.
@State private var viewpoint: Viewpoint?

var body: some View {
MapView(map: map, viewpoint: viewpoint)
.onViewpointChanged(kind: .centerAndScale) { newViewpoint in
viewpoint = newViewpoint
}
.overlay(alignment: .top) {
Text("Scale: 1:\(viewpoint?.targetScale ?? 0, format: .number.rounded(increment: 1))")
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
.padding(8)
.background(.regularMaterial, ignoresSafeAreaEdges: .horizontal)
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Reset Viewpoint") {
viewpoint = map.initialViewpoint
}
}
}
}

/// Creates a unique value renderer with alternate symbols for different scales.
/// - Returns: A new `UniqueValueRenderer` object.
private static func makeUniqueValueRenderer() -> UniqueValueRenderer {
// The multilayer symbol for the low range scale.
let redTriangle = SimpleMarkerSymbol(style: .triangle, color: .red, size: 30)
.toMultilayerSymbol()
redTriangle.referenceProperties = SymbolReferenceProperties(minScale: 5_000, maxScale: 0)

// The alternate multilayer symbol for the mid range scale.
let blueSquare = SimpleMarkerSymbol(style: .square, color: .blue, size: 30)
.toMultilayerSymbol()
blueSquare.referenceProperties = SymbolReferenceProperties(minScale: 10_000, maxScale: 5_000)

// The alternate multilayer symbol for the high range scale.
let yellowDiamond = SimpleMarkerSymbol(style: .diamond, color: .yellow, size: 30)
.toMultilayerSymbol()
yellowDiamond.referenceProperties = SymbolReferenceProperties(minScale: 20_000, maxScale: 10_000)

let uniqueValue = UniqueValue(
description: "unique values based on request type",
label: "unique value",
symbol: redTriangle,
values: ["Damaged Property"],
alternateSymbols: [blueSquare, yellowDiamond]
)

// The default symbol for values that don’t match the unique values.
let purpleDiamond = SimpleMarkerSymbol(style: .diamond, color: .purple, size: 15)
.toMultilayerSymbol()

return UniqueValueRenderer(
fieldNames: ["req_type"],
uniqueValues: [uniqueValue],
defaultSymbol: purpleDiamond
)
}
}

private extension URL {
/// The web URL to the SF311 feature service "Incidents" layer on ArcGIS Online.
static var sf311IncidentsLayer: URL {
URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0")!
}
}

#Preview {
DisplayAlternateSymbolsAtDifferentScalesView()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Display alternate symbols at different scales

Apply a unique value with alternate symbols at different scales.

![Display alternate symbols at different scales sample](display-alternate-symbols-at-different-scales.gif)

## Use case

When a layer is symbolized with unique value symbology, you can specify the visible scale range for each unique value. This is an effective strategy to limit the amount of detailed data at smaller scales without having to make multiple versions of the layer, each with a unique definition query.

Once scale ranges are applied to unique values, you can further refine the appearance of features within those scale ranges by establishing alternate symbols to different parts of the symbol class scale range.

## How to use the sample

Zoom in and out of the map to see alternate symbols at each scale. The symbology changes according to the following scale ranges: 0-5,000, 5,000-10,000, 10,000-20,000. To go back to the initial viewpoint, tap "Reset Viewpoint".

## How it works

1. Create a `FeatureLayer` using the service URL and add it to the map's list of operational layers.
2. Create two alternate symbols (a blue square and a yellow diamond) to be used as alternate symbols. To create an alternate symbol:
1. Create a symbol using `SimpleMarkerSymbol`.
2. Convert the simple marker symbol to an `MultilayerSymbol` using `SimpleMarkerSymbol.toMultilayerSymbol()`.
3. Set the multilayer symbol's `referenceProperties` to the valid scale ranges with the blue square and yellow diamond.
3. Create a third multilayer symbol to be used to create a `UniqueValue` class.
4. Create a unique value using the red triangle from step 3 and the list of alternate symbols from step 2.
5. Create a `UniqueValueRenderer` and add the unique value from step 4 to it.
6. Create a purple diamond simple marker and convert it to a multilayer symbol to be used as the default symbol.
7. Set the unique value renderer's `defaultSymbol` property to the purple diamond from step 6.
8. Set the unique value renderer's `fieldNames` property to "req_type".
9. Apply this unique value renderer to the renderer on feature layer.

## Relevant API

* MultilayerSymbol
* SimpleMarkerSymbol
* SymbolReferenceProperties
* UniqueValue
* UniqueValueRenderer

## About the data

The [San Francisco 311 incidents layer](https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0) in this sample displays point features related to crime incidents such as graffiti and tree damage that have been reported by city residents.

## Tags

alternate symbols, multilayer symbol, scale based rendering, simple marker symbol, symbol reference properties, symbology, unique value, unique value renderer
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"category": "Visualization",
"description": "Apply a unique value with alternate symbols at different scales.",
"ignore": false,
"images": [
"display-alternate-symbols-at-different-scales.gif"
],
"keywords": [
"alternate symbols",
"multilayer symbol",
"scale based rendering",
"simple marker symbol",
"symbol reference properties",
"symbology",
"unique value",
"unique value renderer",
"MultilayerSymbol",
"SimpleMarkerSymbol",
"SymbolReferenceProperties",
"UniqueValue",
"UniqueValueRenderer"
],
"redirect_from": [],
"relevant_apis": [
"MultilayerSymbol",
"SimpleMarkerSymbol",
"SymbolReferenceProperties",
"UniqueValue",
"UniqueValueRenderer"
],
"snippets": [
"DisplayAlternateSymbolsAtDifferentScalesView.swift"
],
"title": "Display alternate symbols at different scales"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading