-
Notifications
You must be signed in to change notification settings - Fork 11
[New] Identify layer features #195
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
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
8c32968
Add ReadME.
CalebRas 49c6d1a
Add swift source file.
CalebRas 7f8ad0d
Add metadata.
CalebRas 30eb6b3
Add source to Build Phases.
CalebRas 28e63cc
Add screenshot.
CalebRas 8ba09cd
Remove ProgressView.
CalebRas d1089ed
Update with review suggestions.
CalebRas 4a41863
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas 273d9f5
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas b97ff83
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas f57816b
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas f31b7a4
Sort after conflict merge.
CalebRas 05da329
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas 71c08b9
Update Shared/Samples/Identify layer features/README.md
CalebRas f3599a1
Update Shared/Samples/Identify layer features/README.md
CalebRas 2aae258
Update Shared/Samples/Identify layer features/README.md
CalebRas 222cc5d
Update Shared/Samples/Identify layer features/README.md
CalebRas 83c6fd2
Update Shared/Samples/Identify layer features/IdentifyLayerFeaturesVi…
CalebRas 24398c0
Update Shared/Samples/Identify layer features/README.md
CalebRas 4cf087d
Update Shared/Samples/Identify layer features/IdentifyLayerFeaturesVi…
CalebRas c7d418e
Update Shared/Samples/Identify layer features/IdentifyLayerFeaturesVi…
CalebRas 0d6dcc9
Update Shared/Samples/Identify layer features/IdentifyLayerFeaturesVi…
CalebRas 69d1fae
Update Shared/Samples/Identify layer features/IdentifyLayerFeaturesVi…
CalebRas 7dcd929
Update Shared/Samples/Identify layer features/README.md
CalebRas 0af85bf
Update Shared/Samples/Identify layer features/README.md
CalebRas 3be73dc
Update Shared/Samples/Identify layer features/IdentifyLayerFeaturesVi…
CalebRas ed90f3d
Update metadata.
CalebRas 189315f
Update screenshot.
CalebRas 3deb9b1
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas f66c414
Update with review suggestions.
CalebRas fe5f044
Update naming.
CalebRas 955b113
Merge branch 'v.next' into Caleb/New-IdentifyLayerFeatures
CalebRas 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
141 changes: 141 additions & 0 deletions
141
Shared/Samples/Identify layer features/IdentifyLayerFeaturesView.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,141 @@ | ||
// Copyright 2023 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 IdentifyLayerFeaturesView: View { | ||
/// A map with a topographic basemap centered on the United States. | ||
@State var map: Map = { | ||
let map = Map(basemapStyle: .arcGISTopographic) | ||
map.initialViewpoint = Viewpoint( | ||
center: Point(x: -10977012.785807, y: 4514257.550369, spatialReference: .webMercator), | ||
scale: 68015210 | ||
) | ||
return map | ||
}() | ||
|
||
/// The tapped screen point. | ||
@State var tapScreenPoint: CGPoint? | ||
|
||
/// The string text for the identify layer results overlay. | ||
@State var overlayText = "Tap on the map to identify feature layers." | ||
|
||
/// A Boolean value indicating whether to show an error alert. | ||
@State var isShowingAlert = false | ||
|
||
/// The error shown in the error alert. | ||
@State var error: Error? { | ||
didSet { isShowingAlert = error != nil } | ||
} | ||
|
||
var body: some View { | ||
ZStack { | ||
MapViewReader { proxy in | ||
MapView(map: map) | ||
.onSingleTapGesture { screenPoint, _ in | ||
tapScreenPoint = screenPoint | ||
} | ||
.task { | ||
do { | ||
// Add map image layer to the map. | ||
let mapImageLayer = ArcGISMapImageLayer(url: .worldCities) | ||
try await mapImageLayer.load() | ||
|
||
// Hide continent and world layers. | ||
mapImageLayer.subLayerContents[1].isVisible = false | ||
mapImageLayer.subLayerContents[2].isVisible = false | ||
map.addOperationalLayer(mapImageLayer) | ||
|
||
// Add feature layer to the map. | ||
let featureTable = ServiceFeatureTable(url: .damageAssessment) | ||
try await featureTable.load() | ||
let featureLayer = FeatureLayer(featureTable: featureTable) | ||
map.addOperationalLayer(featureLayer) | ||
} catch { | ||
// Present error load the layers if any. | ||
self.error = error | ||
} | ||
} | ||
.task(id: tapScreenPoint) { | ||
// Identify layers using the screen point. | ||
if let screenPoint = tapScreenPoint, | ||
let results = try? await proxy.identifyLayers( | ||
screenPoint: screenPoint, | ||
tolerance: 12, | ||
returnPopupsOnly: false, | ||
maximumResultsPerLayer: 10 | ||
) { | ||
handleIdentifyResults(results) | ||
} | ||
} | ||
.overlay(alignment: .top) { | ||
Text(overlayText) | ||
.frame(maxWidth: .infinity, alignment: .center) | ||
.padding(8) | ||
.background(.thinMaterial, ignoresSafeAreaEdges: .horizontal) | ||
} | ||
.alert(isPresented: $isShowingAlert, presentingError: error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension IdentifyLayerFeaturesView { | ||
/// Updates the overlay text based on the identify layer results. | ||
/// - Parameter results: An `IdentifyLayerResult` array to handle. | ||
func handleIdentifyResults(_ results: [IdentifyLayerResult]) { | ||
// Get layer names and geoelement counts from the results. | ||
let identifyLayerResultInfo: [(layerName: String, geoElementsCount: Int)] = results.map { identifyLayerResult in | ||
let layerName = identifyLayerResult.layerContent.name | ||
let geoElementsCount = geoElementsCountFromResult(identifyLayerResult) | ||
return (layerName, geoElementsCount) | ||
} | ||
|
||
let message = identifyLayerResultInfo | ||
.map { "\($0.layerName): \($0.geoElementsCount)" } | ||
.joined(separator: "\n") | ||
|
||
let totalGeoElementsCount = identifyLayerResultInfo.map(\.geoElementsCount).reduce(0, +) | ||
|
||
// Update overlay text with the geo-elements found if any. | ||
overlayText = totalGeoElementsCount > 0 ? message : "No element found." | ||
} | ||
|
||
/// Counts the geo-elements from an identify layer result using recursion. | ||
/// - Parameter result: The `IdentifyLayerResult` to count. | ||
/// - Returns: The count of the geo-elements. | ||
private func geoElementsCountFromResult(_ identifyResult: IdentifyLayerResult) -> Int { | ||
// Get geoElements count from the result. | ||
var count = identifyResult.geoElements.count | ||
|
||
// Get the count using recursion from the result's sublayer results if any. | ||
for result in identifyResult.sublayerResults { | ||
count += geoElementsCountFromResult(result) | ||
} | ||
return count | ||
} | ||
} | ||
|
||
private extension URL { | ||
/// A world cities image layer URL. | ||
static var worldCities: URL { | ||
URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer")! | ||
} | ||
|
||
/// A damage assessment feature layer URL. | ||
static var damageAssessment: URL { | ||
URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0")! | ||
} | ||
} |
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 @@ | ||
# Identify layer features | ||
|
||
Identify features in all layers in a map. | ||
|
||
 | ||
|
||
## Use case | ||
|
||
"Identify layers" operation allows users to tap on a map, returning features at that location across multiple layers. Because some layer types have sublayers, the sample recursively counts results for sublayers within each layer. | ||
|
||
## How to use the sample | ||
|
||
Tap to identify features. The text overlay will update to show all layers with features under the tapped location, as well as a layer count. | ||
|
||
## How it works | ||
|
||
1. The tapped position is passed to `MapViewProxy.identifyLayers(screenPoint:tolerance:returnPopupsOnly:maximumResultsPerLayer:)` method. | ||
2. For each `IdentifyLayerResult` in the results, features are counted. | ||
* Note: there is one identify result per layer with matching features; if the feature count is 0, that means a sublayer contains the matching features. | ||
|
||
## Relevant API | ||
|
||
* IdentifyLayerResult | ||
* LayerContent | ||
|
||
## Additional information | ||
|
||
The GeoView supports two methods of identify: `identifyLayer`, which identifies features within a specific layer and `identifyLayers`, which identifies features for all layers in the current view. | ||
|
||
## Tags | ||
|
||
identify, recursion, recursive, sublayers |
25 changes: 25 additions & 0 deletions
25
Shared/Samples/Identify layer features/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,25 @@ | ||
{ | ||
"category": "Maps", | ||
"description": "Identify features in all layers in a map.", | ||
"ignore": false, | ||
"images": [ | ||
"identify-layer-features.png" | ||
], | ||
"keywords": [ | ||
"identify", | ||
"recursion", | ||
"recursive", | ||
"sublayers", | ||
"IdentifyLayerResult", | ||
"LayerContent" | ||
], | ||
"redirect_from": [], | ||
"relevant_apis": [ | ||
"IdentifyLayerResult", | ||
"LayerContent" | ||
], | ||
"snippets": [ | ||
"IdentifyLayerFeaturesView.swift" | ||
], | ||
"title": "Identify layer features" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.