|
| 1 | +// Copyright 2023 Esri |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ArcGIS |
| 16 | +import UIKit.UIColor |
| 17 | + |
| 18 | +extension AddClusteringFeatureReductionToAPointFeatureLayerView { |
| 19 | + /// The model used to store the geo model and other expensive objects |
| 20 | + /// used in this view. |
| 21 | + class Model: ObservableObject { |
| 22 | + /// A Zurich buildings web map. |
| 23 | + let map = Map( |
| 24 | + item: PortalItem( |
| 25 | + portal: .arcGISOnline(connection: .anonymous), |
| 26 | + id: .zurichBuildings |
| 27 | + ) |
| 28 | + ) |
| 29 | + |
| 30 | + /// A custom feature reduction for dynamically aggregating and |
| 31 | + /// summarizing groups of features as the map scale changes. |
| 32 | + private let clusteringFeatureReduction = makeCustomFeatureReduction(renderer: makeClassBreaksRenderer()) |
| 33 | + |
| 34 | + /// The buildings feature layer in the web map. |
| 35 | + var featureLayer: FeatureLayer? { |
| 36 | + map.operationalLayers.first as? FeatureLayer |
| 37 | + } |
| 38 | + |
| 39 | + /// A Boolean value indicating whether cluster labels are displayed. |
| 40 | + var showsLabels: Bool { |
| 41 | + didSet { |
| 42 | + clusteringFeatureReduction.showsLabels = showsLabels |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// The maximum scale of feature clusters. |
| 47 | + /// - Note: The default value for max scale is 0. |
| 48 | + var maxScale: Double { |
| 49 | + didSet { |
| 50 | + clusteringFeatureReduction.maxScale = maxScale |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// The radius of feature clusters. |
| 55 | + /// - Note: The default value for cluster radius is 60. |
| 56 | + /// Larger radius allows more features to be grouped into a cluster. |
| 57 | + var radius: Double { |
| 58 | + didSet { |
| 59 | + clusteringFeatureReduction.radius = radius |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + init() { |
| 64 | + // Set initial values for controls. |
| 65 | + showsLabels = clusteringFeatureReduction.showsLabels |
| 66 | + radius = clusteringFeatureReduction.radius |
| 67 | + maxScale = clusteringFeatureReduction.maxScale ?? .zero |
| 68 | + } |
| 69 | + |
| 70 | + /// Loads the web map and set up the feature layer. |
| 71 | + func setup() async throws { |
| 72 | + try await map.load() |
| 73 | + featureLayer?.featureReduction = clusteringFeatureReduction |
| 74 | + } |
| 75 | + |
| 76 | + /// Creates a class breaks renderer for the custom feature reduction |
| 77 | + /// - Returns: A `ClassBreaksRenderer` object. |
| 78 | + private static func makeClassBreaksRenderer() -> ClassBreaksRenderer { |
| 79 | + // For each feature cluster with a given average building height, |
| 80 | + // a color is assigned to each symbol. |
| 81 | + let colors = [ |
| 82 | + (4, 251, 255), |
| 83 | + (44, 211, 255), |
| 84 | + (74, 181, 255), |
| 85 | + (120, 135, 255), |
| 86 | + (165, 90, 255), |
| 87 | + (194, 61, 255), |
| 88 | + (224, 31, 255), |
| 89 | + (254, 1, 255) |
| 90 | + ].map { |
| 91 | + UIColor(red: $0.0 / 255, green: $0.1 / 255, blue: $0.2 / 255, alpha: 1) |
| 92 | + } |
| 93 | + |
| 94 | + // Create a class break and a symbol to display the features |
| 95 | + // in each value range. |
| 96 | + // In this case, the average building height ranges from 0 to 7 stories. |
| 97 | + let classBreaks = zip([Int](0...7), colors).map { value, color in |
| 98 | + ClassBreak( |
| 99 | + description: "\(value) floor", |
| 100 | + label: String(value), |
| 101 | + minValue: Double(value), |
| 102 | + maxValue: Double(value) + 1, |
| 103 | + symbol: SimpleMarkerSymbol(color: color) |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + // Create a class breaks renderer to apply to the custom |
| 108 | + // feature reduction. |
| 109 | + // Define the field to use for the class breaks renderer. |
| 110 | + // Note that this field name must match the name of an |
| 111 | + // aggregate field contained in the clustering feature reduction's |
| 112 | + // aggregate fields property. |
| 113 | + let renderer = ClassBreaksRenderer(fieldName: "Average Building Height", classBreaks: classBreaks) |
| 114 | + |
| 115 | + // Create a default symbol for features that do not fall within |
| 116 | + // any of the ranges defined by the class breaks. |
| 117 | + renderer.defaultSymbol = SimpleMarkerSymbol(color: .systemPink) |
| 118 | + |
| 119 | + return renderer |
| 120 | + } |
| 121 | + |
| 122 | + /// Creates a custom feature reduction for the sample. |
| 123 | + /// - Parameter renderer: A renderer for drawing clustered features. |
| 124 | + /// - Returns: A `ClusteringFeatureReduction` object. |
| 125 | + private static func makeCustomFeatureReduction(renderer: ClassBreaksRenderer) -> ClusteringFeatureReduction { |
| 126 | + // Create a new clustering feature reduction using the |
| 127 | + // class breaks renderer. |
| 128 | + let clusteringFeatureReduction = ClusteringFeatureReduction(renderer: renderer) |
| 129 | + |
| 130 | + // Set the feature reduction's aggregate fields. |
| 131 | + // Note that the field names must match those in the feature layer. |
| 132 | + // The aggregate fields summarize values based on the defined |
| 133 | + // aggregate statistic type. |
| 134 | + clusteringFeatureReduction.addAggregateFields([ |
| 135 | + AggregateField( |
| 136 | + name: "Total Residential Buildings", |
| 137 | + statisticFieldName: "Residential_Buildings", |
| 138 | + statisticType: .sum |
| 139 | + ), |
| 140 | + AggregateField( |
| 141 | + name: "Average Building Height", |
| 142 | + statisticFieldName: "Most_common_number_of_storeys", |
| 143 | + statisticType: .mode |
| 144 | + ) |
| 145 | + ]) |
| 146 | + |
| 147 | + // Enable the feature reduction. |
| 148 | + clusteringFeatureReduction.isEnabled = true |
| 149 | + |
| 150 | + // Set the popup definition for the custom feature reduction. |
| 151 | + clusteringFeatureReduction.popupDefinition = PopupDefinition(popupSource: clusteringFeatureReduction) |
| 152 | + |
| 153 | + // Set values for the feature reduction's cluster minimum |
| 154 | + // and maximum symbol sizes. Note that the default values |
| 155 | + // for max and min symbol size are 70 and 12 respectively. |
| 156 | + clusteringFeatureReduction.minSymbolSize = 5 |
| 157 | + clusteringFeatureReduction.maxSymbolSize = 90 |
| 158 | + |
| 159 | + // Create a label definition with a simple label expression. |
| 160 | + let labelDefinition = LabelDefinition( |
| 161 | + labelExpression: SimpleLabelExpression(simpleExpression: "[cluster_count]"), |
| 162 | + textSymbol: TextSymbol(color: .black, size: 15) |
| 163 | + ) |
| 164 | + labelDefinition.placement = .pointCenterCenter |
| 165 | + |
| 166 | + // Add the label definition to the feature reduction. |
| 167 | + clusteringFeatureReduction.addLabelDefinition(labelDefinition) |
| 168 | + |
| 169 | + return clusteringFeatureReduction |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +private extension PortalItem.ID { |
| 175 | + /// The ID used in the Zurich buildings web map. |
| 176 | + static var zurichBuildings: Self { Self("aa44e79a4836413c89908e1afdace2ea")! } |
| 177 | +} |
0 commit comments