Maria globe client/Draw object interaction
Draw object interaction
In this section, we will create and update three different draw objects, a area object, a line object, and a point object, and see how they are presented in 2D and 3D mode.
For more details on track handling, see ''''', Draw object interaction and Draw objects visualisation
Adding draw object layer and draw object view model
To be able to display draw objects, we need to include a draw object layer in the globe client.
First, add a view model class, DrawObjectViewModel, to handle the draw object interaction:
- Inheriting ViewModelBase
- Add a constructor taking a draw layer object as input
- Create and implement event handlers for:
- LayerInitialized event
- ServiceConnected event
public class DrawObjectViewModel : ViewModelBase
{
private IMariaDrawObjectLayer _drawObjectLayer;
private IMariaService _drawObjectService;
public DrawObjectViewModel(DrawObjectLayer drawObjectLayer)
{
_drawObjectLayer = drawObjectLayer;
_drawObjectLayer.LayerInitialized += DrawObjectLayer_LayerInitialized;
_drawObjectLayer.ServiceConnected += DrawObjectLayer_ServiceConnected;
}
private void DrawObjectLayer_LayerInitialized()
{
_drawObjectService = new MariaService(
"DrawObjectService",
"http://localhost:9008/drawobjects",
BindingType.BasicHttp);
_drawObjectLayer.DrawObjectServices = new ObservableCollection<IMariaService> { _drawObjectService };
_drawObjectLayer.CreateDrawObjectList("local", true);
}
private void DrawObjectLayer_ServiceConnected(object sender, MariaServiceEventArgs args)
{
_drawObjectLayer.ActiveDrawObjectService = _drawObjectLayer.DrawObjectServices[0];
_drawObjectLayer.CreateDrawObjectList("test", false);
_drawObjectLayer.ActiveDrawObjectList = "test";
}
}
In the MainViewModel, add a draw object layer field, and in the constructor, add creation of the layer and DrawObjectViewModel and add the layer to the Layers list.
...
public DrawObjectViewModel DrawObjectViewModel { get; private set; }
public MainViewModel()
{
...
DrawObjectLayerFactory = new DrawObjectLayerFactory();
_drawObjectLayer = new DrawObjectLayer(GlobeMapManager, DrawObjectLayerFactory, true)
{ InitializeCreationWorkflows = true };
DrawObjectViewModel = new DrawObjectViewModel(_drawObjectLayer);
Layers.Add(_drawObjectLayer);
}
...
Draw object utilities
Add the following GUI elements to your main window:
- Button for adding/updating a volume object.
- Button for adding/updating a line object.
- Button for adding/updating a point object.
The XAML could look something like this:
...
<GroupBox Header="Draw Object" >
<StackPanel VerticalAlignment="Top">
<Button Margin="3"
Content="Volume"
Command="{Binding DrawObjectViewModel.VolumeTestCmd}"/>
<Button Margin="3"
Content="Line"
Command="{Binding DrawObjectViewModel.LineTestCmd}"/>
<Button Margin="3"
Content="Point"
Command="{Binding DrawObjectViewModel.PointTestCmd}"/>
</StackPanel>
</GroupBox>
...
Implement command handler and command handler delegate in DrawObjectViewModel.
...
public ICommand VolumeTestCmd { get { return new DelegateCommand(DrawObjectViewModel.CreateOrUpdateVolumeObject); } }
public ICommand LineTestCmd { get { return new DelegateCommand(DrawObjectViewModel.CreateOrUpdateLineObject); } }
public ICommand PointTestCmd { get { return new DelegateCommand(DrawObjectViewModel.CreateOrUpdatePointObject); } }
public void CreateOrUpdatePointObject(object obj)
{
var pos = RandomProvider.GetRandomPosition(_drawObjectLayer.GeoContext.Viewport.GeoRect);
var altitude = RandomProvider.GetRandomInt(1, 5) * 500.0;
var pointObject = _drawObjectLayer.DrawObjectFactory.CreateTacticalGraphic("STBOPS.OPN.HJKG.APL");
pointObject.TacticalDataFields.StandardIdentity = StandardIdentity.Hostile;
pointObject.TacticalDataFields.Name = "Globe-Point";
pointObject.Id = new ItemId("local", "Globe-Point");
pointObject.Points[0] = new GeoPoint() { Latitude = pos.Lat, Longitude = pos.Lon, Altitude = altitude };
_drawObjectLayer.UpdateStore(pointObject);
}
public void CreateOrUpdateVolumeObject(object obj)
{
var volumeObject = _drawObjectLayer.DrawObjectFactory.CreateTacticalGraphic("TACGRP.FSUPP.ARS.C2ARS.ACA.IRR");
volumeObject.TacticalDataFields.StandardIdentity = StandardIdentity.Friendly;
volumeObject.Id = new ItemId("local", "Globe-Volume");
var pos = RandomProvider.GetRandomPosition(_drawObjectLayer.GeoContext.Viewport.GeoRect, 0.8);
var factorLat = _drawObjectLayer.GeoContext.Viewport.GeoRect.DeltaLat * RandomProvider.GetRandomDouble(0.05, 0.1);
var factorLon = _drawObjectLayer.GeoContext.Viewport.GeoRect.DeltaLon * RandomProvider.GetRandomDouble(0.05, 0.1);
volumeObject.Points = new GeoPoint[] {
new GeoPoint() { Latitude = pos.Lat + factorLat, Longitude = pos.Lon + factorLon },
new GeoPoint() { Latitude = pos.Lat + factorLat, Longitude = pos.Lon - factorLon },
new GeoPoint() { Latitude = pos.Lat - factorLat, Longitude = pos.Lon - factorLon },
new GeoPoint() { Latitude = pos.Lat - factorLat, Longitude = pos.Lon + factorLon },
};
_drawObjectLayer.UpdateStore(volumeObject);
}
public void CreateOrUpdateLineObject(object obj)
{
var lineObject = _drawObjectLayer.DrawObjectFactory.CreatePolyLine(new ItemId("local", "Globe-Line"));
var pt0 = RandomProvider.GetRandomPosition(_drawObjectLayer.GeoContext.Viewport.GeoRect, 0.5);
var pt1 = RandomProvider.GetRandomPosition(_drawObjectLayer.GeoContext.Viewport.GeoRect, 0.5);
var pt2 = RandomProvider.GetRandomPosition(_drawObjectLayer.GeoContext.Viewport.GeoRect, 0.5);
lineObject.Points = new GeoPoint[] {
new GeoPoint() { Latitude = pt0.Lat, Longitude = pt0.Lon, Altitude = RandomProvider.GetRandomInt(1, 5) * 500.0 },
new GeoPoint() { Latitude = pt1.Lat, Longitude = pt1.Lon, Altitude = RandomProvider.GetRandomInt(1, 5) * 500.0 },
new GeoPoint() { Latitude = pt2.Lat, Longitude = pt2.Lon, Altitude = RandomProvider.GetRandomInt(1, 5) * 500.0 },
};
lineObject.GenericDataFields.LineColor = Colors.Navy;
var dashStyle = DashStyles.DashDot;
lineObject.GenericDataFields.LineDashStyle = dashStyle.Dashes.ToList();
lineObject.GenericDataFields.LineDashStyleOffset = dashStyle.Offset;
lineObject.GenericDataFields.LineWidth = 6.0;
_drawObjectLayer.UpdateStore(lineObject);
}
...
The RandomProvider class is described in context with Map interaction client, track creation.
Draw object styling
Add a draw object style xml file to your project, and include it as a resource. You can find style xml to use here
Add the resource to the draw object layer in the LayerInitialized event handler in DrawObjectViewModel.
...
private void DrawObjectLayer_LayerInitialized()
{
_drawObjectLayer.StyleXml = Properties.Resources.DrawObjectStyle;
...
Running with draw object interaction
Running your application, observe the following:
- Pressing the draw object buttons, draw objects created / moved around in the map area.
- In 2D mode, the objects can also be selected and moved/resized manually.