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

Update samples using sketch editor to use geometry editor #1274

Merged
merged 6 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ When the sample loads, a feature service is taken offline as a geodatabase. When
* Geodatabase.CommitTransaction
* Geodatabase.IsInTransaction
* Geodatabase.RollbackTransaction
* GeometryEditor

## About the data

The sample uses a publicly-editable, sync-enabled [feature service](https://sampleserver6.arcgisonline.com/arcgis/rest/services/Sync/SaveTheBaySync/FeatureServer) demonstrating a schema for recording wildlife sightings.

## Tags

commit, database, geodatabase, transact, transactions
commit, database, geodatabase, geometry editor, transact, transactions
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"commit",
"database",
"geodatabase",
"geometry editor",
"transact",
"transactions"
],
Expand All @@ -22,7 +23,8 @@
"Geodatabase.BeginTransaction",
"Geodatabase.CommitTransaction",
"Geodatabase.IsInTransaction",
"Geodatabase.RollbackTransaction"
"Geodatabase.RollbackTransaction",
"GeometryEditor"
],
"snippets": [
"GeodatabaseTransactions.xaml.cs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ArcGIS.Samples.CreateAndSaveKmlFile
category: "Layers",
description: "Construct a KML document and save it as a KMZ file.",
instructions: "Tap on one of the buttons in the middle row to start adding a geometry. Tap on the map view to place vertices. Tap the \"Complete Sketch\" button to add the geometry to the KML document as a new KML placemark. Use the style interface to edit the style of the placemark. If you do not wish to set a style, tap the \"Don't Apply Style\" button. When you are finished adding KML nodes, tap on the \"Save KMZ file\" button to save the active KML document as a .kmz file on your system. Use the \"Reset\" button to clear the current KML document and start a new one.",
tags: new[] { "KML", "KMZ", "Keyhole", "OGC" })]
tags: new[] { "KML", "KMZ", "Keyhole", "OGC", "geometry editor" })]
[ArcGIS.Samples.Shared.Attributes.OfflineData()]
[ArcGIS.Samples.Shared.Attributes.ClassFile("Converters/ImageConverter.cs", "Converters/ColorConverter.cs")]
public partial class CreateAndSaveKmlFile : ContentPage
Expand All @@ -38,6 +38,7 @@ public partial class CreateAndSaveKmlFile : ContentPage
private KmlDataset _kmlDataset;
private KmlLayer _kmlLayer;
private KmlPlacemark _currentPlacemark;
private GeometryType _geometryType;

public CreateAndSaveKmlFile()
{
Expand Down Expand Up @@ -103,68 +104,35 @@ private async void Edit_Click(object sender, EventArgs e)
CompleteButton.IsVisible = true;
SaveResetGrid.IsEnabled = false;

// Create variables for the sketch creation mode and color.
SketchCreationMode creationMode;

// Set the creation mode and UI based on which button called this method.
switch (((Button)sender).Text)
{
case "Point":
creationMode = SketchCreationMode.Point;
_geometryType = GeometryType.Point;
Status.Text = "Tap to add a point.";
break;

case "Polyline":
creationMode = SketchCreationMode.Polyline;
_geometryType = GeometryType.Polyline;
Status.Text = "Tap to add a vertex.";
break;

case "Polygon":
creationMode = SketchCreationMode.Polygon;
_geometryType = GeometryType.Polygon;
Status.Text = "Tap to add a vertex.";
break;

default:
return;
}

// Get the user-drawn geometry.
Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, true);

// Project the geometry to WGS84 (WGS84 is required by the KML standard).
Geometry projectedGeometry = geometry.Project(SpatialReferences.Wgs84);

// Create a KmlGeometry using the new geometry.
KmlGeometry kmlGeometry = new KmlGeometry(projectedGeometry, KmlAltitudeMode.ClampToGround);

// Create a new placemark.
_currentPlacemark = new KmlPlacemark(kmlGeometry);

// Add the placemark to the KmlDocument.
_kmlDocument.ChildNodes.Add(_currentPlacemark);

// Choose whether to enable the icon picker or color picker.
IconPicker.IsVisible = creationMode == SketchCreationMode.Point;
ColorPicker.IsVisible = creationMode != SketchCreationMode.Point;

// Enable the style editing UI.
StyleUI.IsVisible = true;
MainUI.IsVisible = false;
// Start the geometry editor.
MyMapView.GeometryEditor.Start(_geometryType);
}
catch (ArgumentException)
{
await Application.Current.MainPage.DisplayAlert("Error", "Unsupported Geometry", "OK");
}
finally
{
// Reset the UI.
ShapeGrid.IsVisible = true;
CompleteButton.IsVisible = false;
Status.Text = "Select the type of feature you would like to add.";

// Enable the save and reset buttons.
SaveResetGrid.IsEnabled = true;
}
}

private void Apply_Style_Click(object sender, SelectedItemChangedEventArgs e)
Expand Down Expand Up @@ -218,11 +186,59 @@ private void Complete_Click(object sender, EventArgs e)
{
try
{
// Finish the sketch.
MyMapView.SketchEditor.CompleteCommand.Execute(null);
// Get the user-drawn geometry.
Geometry geometry = MyMapView.GeometryEditor.Stop();

// Hold a reference for the new placemark geometry.
KmlGeometry kmlGeometry;

// Check to see if a geometry has been drawn.
if (!geometry.IsEmpty)
{

if (MyMapView.SpatialReference != null &&
geometry.SpatialReference != MyMapView.SpatialReference)
{
// Project the geometry to WGS84 (WGS84 is required by the KML standard).
Geometry projectedGeometry = geometry.Project(SpatialReferences.Wgs84);

// Create a KmlGeometry using the projected geometry.
kmlGeometry = new KmlGeometry(projectedGeometry, KmlAltitudeMode.ClampToGround);
}
else
{
// Create a KmlGeometry using the user-drawn geometry.
kmlGeometry = new KmlGeometry(geometry, KmlAltitudeMode.ClampToGround);
}

// Create a new placemark.
_currentPlacemark = new KmlPlacemark(kmlGeometry);

// Add the placemark to the KmlDocument.
_kmlDocument.ChildNodes.Add(_currentPlacemark);

// Choose whether to enable the icon picker or color picker.
IconPicker.IsVisible = _geometryType == GeometryType.Point;
ColorPicker.IsVisible = _geometryType != GeometryType.Point;

// Enable the style editing UI.
StyleUI.IsVisible = true;
MainUI.IsVisible = false;
}
}
catch (ArgumentException)
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
finally
{
// Reset the UI.
ShapeGrid.IsVisible = true;
CompleteButton.IsVisible = false;
Status.Text = "Select the type of feature you would like to add.";

// Enable the save and reset buttons.
SaveResetGrid.IsEnabled = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Tap on one of the buttons in the middle row to start adding a geometry. Tap on t
1. Create a `KmlDocument`
2. Create a `KmlDataset` using the `KmlDocument`.
3. Create a `KmlLayer` using the `KmlDataset` and add it to `Map.OperationalLayers`.
4. Create `Geometry` using `SketchEditor`.
4. Create `Geometry` using `GeometryEditor`.
5. Project that `Geometry` to WGS84 using `GeometryEngine.Project`.
6. Create a `KmlGeometry` object using that projected `Geometry`.
7. Create a `KmlPlacemark` using the `KmlGeometry`.
Expand All @@ -27,6 +27,7 @@ Tap on one of the buttons in the middle row to start adding a geometry. Tap on t

## Relevant API

* GeometryEditor
* GeometryEngine.Project
* KmlDataset
* KmlDocument
Expand All @@ -35,8 +36,7 @@ Tap on one of the buttons in the middle row to start adding a geometry. Tap on t
* KmlNode.SaveAsASync
* KmlPlacemark
* KmlStyle
* SketchEditor

## Tags

Keyhole, KML, KMZ, OGC
geometry editor, Keyhole, KML, KMZ, OGC
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@
"KML",
"KMZ",
"Keyhole",
"OGC"
"OGC",
"geometry editor"
],
"offline_data": [],
"redirect_from": [
"/net/latest/maui/sample-code/createandsavekmlfile.htm"
],
"relevant_apis": [
"GeometryEditor",
"GeometryEngine.Project",
"KmlDataset",
"KmlDocument",
"KmlGeometry",
"KmlLayer",
"KmlNode.SaveAsASync",
"KmlPlacemark",
"KmlStyle",
"SketchEditor"
"KmlStyle"
],
"snippets": [
"CreateAndSaveKmlFile.xaml.cs",
Expand Down
Loading
Loading