Skip to content

Commit

Permalink
updated WPF samples
Browse files Browse the repository at this point in the history
  • Loading branch information
duffh committed Aug 1, 2023
1 parent b713d65 commit df2ce75
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 181 deletions.

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 @@ -14,6 +14,7 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows;
Expand All @@ -35,6 +36,7 @@ public partial class CreateAndSaveKmlFile
private KmlDataset _kmlDataset;
private KmlLayer _kmlLayer;
private KmlPlacemark _currentPlacemark;
private GeometryType _creationMode;

public CreateAndSaveKmlFile()
{
Expand Down Expand Up @@ -104,26 +106,23 @@ private async void Edit_Click(object sender, RoutedEventArgs e)
CompleteButton.Visibility = Visibility.Visible;
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).Name)
{
case nameof(PointButton):
creationMode = SketchCreationMode.Point;
_creationMode = GeometryType.Point;
InstructionsText.Text = "Tap to add a point.";
StyleText.Text = "Select an icon for the placemark.";
break;

case nameof(PolylineButton):
creationMode = SketchCreationMode.Polyline;
_creationMode = GeometryType.Polyline;
InstructionsText.Text = "Tap to add a vertex.";
StyleText.Text = "Select a color for the placemark.";
break;

case nameof(PolygonButton):
creationMode = SketchCreationMode.Polygon;
_creationMode = GeometryType.Polygon;
InstructionsText.Text = "Tap to add a vertex.";
StyleText.Text = "Select a color for the placemark.";
break;
Expand All @@ -132,43 +131,13 @@ private async void Edit_Click(object sender, RoutedEventArgs e)
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);

// Enable the style editing UI.
StyleBorder.Visibility = Visibility.Visible;
MainUI.IsEnabled = false;

// Choose whether to enable the icon picker or color picker.
IconPicker.Visibility = creationMode == SketchCreationMode.Point ? Visibility.Visible : Visibility.Collapsed;
ColorPicker.Visibility = creationMode != SketchCreationMode.Point ? Visibility.Visible : Visibility.Collapsed;
// Start the geometry editor.
MyMapView.GeometryEditor.Start(_creationMode);
}
catch (ArgumentException)
{
MessageBox.Show("Unsupported Geometry", "Error");
}
finally
{
// Reset the UI.
ShapesPanel.Visibility = Visibility.Visible;
CompleteButton.Visibility = Visibility.Collapsed;
InstructionsText.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, RoutedEventArgs e)
Expand Down Expand Up @@ -213,11 +182,46 @@ private void Complete_Click(object sender, RoutedEventArgs e)
{
try
{
// Finish the sketch.
MyMapView.SketchEditor.CompleteCommand.Execute(null);
// Get the user-drawn geometry.
Geometry geometry = MyMapView.GeometryEditor.Stop();

// Check to see if a geometry has been drawn.
if (!geometry.IsEmpty)
{
// 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);

// Enable the style editing UI.
StyleBorder.Visibility = Visibility.Visible;
MainUI.IsEnabled = false;

// Choose whether to enable the icon picker or color picker.
IconPicker.Visibility = _creationMode == GeometryType.Point ? Visibility.Visible : Visibility.Collapsed;
ColorPicker.Visibility = _creationMode != GeometryType.Point ? Visibility.Visible : Visibility.Collapsed;
}
}
catch (ArgumentException)
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
finally
{
// Reset the UI.
ShapesPanel.Visibility = Visibility.Visible;
CompleteButton.Visibility = Visibility.Collapsed;
InstructionsText.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 @@ Click on one of the buttons in the middle row to start adding a geometry. Click
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 @@ -28,15 +28,15 @@ Click on one of the buttons in the middle row to start adding a geometry. Click
## Relevant API

* GeometryEngine.Project
* GeometryEditor
* KmlDataset
* KmlDocument
* KmlGeometry
* KmlLayer
* 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 @@ -31,6 +31,7 @@ public partial class FindServiceArea
// Uri for the service area around San Diego.
private Uri _serviceAreaUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ServiceArea");

private GeometryType _creationMode;
public FindServiceArea()
{
InitializeComponent();
Expand All @@ -50,46 +51,22 @@ private void Initialize()

// Create graphics overlays for all of the elements of the map.
MyMapView.GraphicsOverlays.Add(new GraphicsOverlay());

// Add a new behavior for double taps on the MapView.
MyMapView.GeoViewDoubleTapped += (s, e) =>
{
// If the sketch editor complete command is enabled, a sketch is in progress.
if (MyMapView.SketchEditor.CompleteCommand.CanExecute(null))
{
// Set the event as handled.
e.Handled = true;

// Finish the drawing.
MyMapView.SketchEditor.CompleteCommand.Execute(null);
DrawBarrierButton.Content = "Draw barrier";
}
};
}

private async void PlaceFacilityButton_Click(object sender, RoutedEventArgs e)
private void PlaceFacilityButton_Click(object sender, RoutedEventArgs e)
{
try
{
// Let the user tap on the map view using the point sketch mode.
SketchCreationMode creationMode = SketchCreationMode.Point;
Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, false);

// Symbology for a facility.
PictureMarkerSymbol facilitySymbol = new PictureMarkerSymbol(new Uri("https://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png"))
{
Height = 30,
Width = 30
};

// Create a graphic for the facility.
Graphic facilityGraphic = new Graphic(geometry, new Dictionary<string, object>() { { "Type", "Facility" } }, facilitySymbol)
if (MyMapView.GeometryEditor.IsStarted)
{
ZIndex = 2
};
MyMapView.GeometryEditor.Stop();
}

// Add the graphic to the graphics overlay.
MyMapView.GraphicsOverlays[0].Graphics.Add(facilityGraphic);
// Let the user tap on the map view using the point sketch mode.
_creationMode = GeometryType.Point;

MyMapView.GeometryEditor.Start(_creationMode);
MyMapView.GeometryEditor.PropertyChanged += GeometryEditor_PropertyChanged; ;
}
catch (TaskCanceledException)
{
Expand All @@ -102,13 +79,49 @@ private async void PlaceFacilityButton_Click(object sender, RoutedEventArgs e)
}
}

private async void DrawBarrierButton_Click(object sender, RoutedEventArgs e)
private void GeometryEditor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Geometry")
{
if (_creationMode == GeometryType.Point)
{
// Disconnect event handler to prevent multiple calls.
MyMapView.GeometryEditor.PropertyChanged -= GeometryEditor_PropertyChanged;

// Get the active geometry.
Geometry geometry = MyMapView.GeometryEditor.Geometry;

// Symbology for a facility.
PictureMarkerSymbol facilitySymbol = new PictureMarkerSymbol(new Uri("https://static.arcgis.com/images/Symbols/SafetyHealth/Hospital.png"))
{
Height = 30,
Width = 30
};

// Create a graphic for the facility.
Graphic facilityGraphic = new Graphic(geometry, new Dictionary<string, object>() { { "Type", "Facility" } }, facilitySymbol)
{
ZIndex = 2
};

// Add the graphic to the graphics overlay.
MyMapView.GraphicsOverlays[0].Graphics.Add(facilityGraphic);

// Stop the geometry editor to clear the active geometry.
MyMapView.GeometryEditor.Stop();
}
}
}

private void DrawBarrierButton_Click(object sender, RoutedEventArgs e)
{
// Finish the drawing if already started.
if ((string)DrawBarrierButton.Content != "Draw barrier")
{
if (MyMapView.SketchEditor.CompleteCommand.CanExecute(null))
MyMapView.SketchEditor.CompleteCommand.Execute(null);
if (MyMapView.GeometryEditor.IsStarted)
{
FinishBarrier();
}
DrawBarrierButton.Content = "Draw barrier";
return;
}
Expand All @@ -118,9 +131,27 @@ private async void DrawBarrierButton_Click(object sender, RoutedEventArgs e)
DrawBarrierButton.Content = "Finish barrier";

// Let the user draw on the map view using the polyline sketch mode.
SketchCreationMode creationMode = SketchCreationMode.Polyline;
Geometry geometry = await MyMapView.SketchEditor.StartAsync(creationMode, false);
_creationMode = GeometryType.Polyline;

MyMapView.GeometryEditor.Start(_creationMode);
}
catch (TaskCanceledException)
{
// Ignore this exception.
}
catch (Exception ex)
{
// Report exceptions.
MessageBox.Show("Error drawing barrier:\n" + ex.Message);
}
}

private void FinishBarrier()
{
Geometry geometry = MyMapView.GeometryEditor.Stop();

if (!geometry.IsEmpty)
{
// Symbol for the barriers.
SimpleLineSymbol barrierSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Black, 5.0f);

Expand All @@ -133,25 +164,17 @@ private async void DrawBarrierButton_Click(object sender, RoutedEventArgs e)
// Add a graphic from the polyline the user drew.
MyMapView.GraphicsOverlays[0].Graphics.Add(barrierGraphic);
}
catch (TaskCanceledException)
{
// Ignore this exception.
}
catch (Exception ex)
{
// Report exceptions.
MessageBox.Show("Error drawing barrier:\n" + ex.Message);
}
}

private async void ShowServiceAreasButton_Click(object sender, RoutedEventArgs e)
{
// Finish any drawings.
if (MyMapView.SketchEditor.CompleteCommand.CanExecute(null))
if (MyMapView.GeometryEditor.IsStarted)
{
MyMapView.SketchEditor.CompleteCommand.Execute(null);
MyMapView.GeometryEditor.Stop();
DrawBarrierButton.Content = "Draw barrier";
}

// Use a local variable for the graphics overlay.
GraphicCollection allGraphics = MyMapView.GraphicsOverlays[0].Graphics;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ In order to find any service areas at least one facility needs to be added to th

## Tags

barriers, facilities, impedance, logistics, routing
barriers, facilities, geometry editor, impedance, logistics, routing

0 comments on commit df2ce75

Please sign in to comment.