Tuesday, June 29, 2010

Cloning Path Geometry in Silverlight

image

To add depth to a line rendered on a map, I experimented with the drop shadow bitmap effect. The shadow looked great but it had a heavy performance cost. As a workaround, I substituted the bitmap effect with a cloned path. Unfortunately Silverlight does not support path geometry cloning, unlike WPF, so based on a Silverlight.net forums thread I implemented a value converter that clones path geometry.

The second path below is the original street route displayed in blue above. The first path clones the blue path’s geometry and offsets it by a few pixels.

<Path Opacity="0.5"
      StrokeThickness="5"
      StrokeLineJoin="Round"
      Stroke="Gray"
      Data="{Binding ElementName=Element,
                     Path=Data,
                     Mode=OneWay,
                     Converter={StaticResource GeometryConverter}}">
    <Path.Resources>
        <local:GeometryConverter x:Key="GeometryConverter"/>
    </Path.Resources>
    <Path.RenderTransform>
        <TranslateTransform X="6" Y="6" />
    </Path.RenderTransform>
</Path>
<Path x:Name="Element"
StrokeThickness="5"
StrokeLineJoin="Round"
Stroke="Blue"/>

Below is the code for the value converter.  The code uses reflection to clone the geometry of the original path.

public class GeometryConverter : IValueConverter {
    public object Convert(object value, Type targetType,
object parameter, CultureInfo culture) { // Test for Null if (value == null) { return null; } Geometry g = value as Geometry; if (g == null) { return null; } Geometry clone = Clone(g) as Geometry; return clone; } public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture) { throw new NotImplementedException(); } private static object Clone(Object obj) { // Get all properties and clone them. PropertyInfo[] properties = obj.GetType().GetProperties(); object cloneObj = obj.GetType().GetConstructors()[0].Invoke(null); foreach (PropertyInfo property in properties) { object value = property.GetValue(obj, null); if (value != null) { if (IsPresentationFrameworkCollection(value.GetType())) { object collection = property.GetValue(obj, null); int count = (int)collection.GetType().
GetProperty("Count").GetValue(collection, null); for (int i = 0; i < count; i++) { // Get each child of the collection. object child = collection.GetType().
GetProperty("Item").
GetValue(collection, new Object[] { i }); object cloneChild = Clone(child); object cloneCollection = property.
GetValue(cloneObj, null); collection.GetType().
InvokeMember("Add",
BindingFlags.InvokeMethod,
null,
cloneCollection,
new object[] { cloneChild }); } } // If the property is a UIElement, we also need to clone it. else if (value is UIElement) { object obj2 = property.PropertyType.
GetConstructors()[0].Invoke(null); Clone(obj2); property.SetValue(cloneObj, obj2, null); } // For a normal property, its value doesn't need to be
// cloned.
So just copy its value to the new object. else if (property.CanWrite) { property.SetValue(cloneObj, value, null); } } } return cloneObj; } private static bool IsPresentationFrameworkCollection(Type type) { if (type == typeof(object)) { return false; } if (type.Name.StartsWith("PresentationFrameworkCollection")) { return true; } return IsPresentationFrameworkCollection(type.BaseType); } }

Wednesday, May 19, 2010

Moveable graphics with ESRI’s ArcGIS API for Silverlight

image

By default, graphics added to an ESRI Silverlight map are static.  This post will demonstrate how to add map behavior so that graphics can be repositioned with regular mouse interaction.  In some situations, providing moveable graphics is a more intuitive method of achieving user interaction.  For example, you could have a moveable start and end pushpin in a routing application.

In this sample, the web application will create ten Elvis symbols at random locations.  Whenever a graphic is added to the map, the application will begin for listen for mouse events (on the graphic).  When there is a LeftMouseButtonDown event on a graphic, a DragGraphic object is created.  This class stores a reference to the selected graphic and the original position of the graphic and mouse cursor.  If the mouse moves again the graphic or map, information from the DragGraphic class is used to update the selected graphic’s position.

To view a live sample, please click the link below.
http://maps.esri.com/sldemos/mg/default.html

<UserControl
    x:Class="MoveableGraphicsSample.MainPage"
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;
assembly=ESRI.ArcGIS.Client"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid Background="White"> <esri:Map Name="Map"> <esri:Map.Resources> <esriSymbols:MarkerSymbol x:Key="Elvis"> <esriSymbols:MarkerSymbol.ControlTemplate> <ControlTemplate> <Grid> <Ellipse Height="50" Width="50"
Cursor="Hand" Stroke
="White"
StrokeThickness
="2"> <Ellipse.Fill> <!-- see http://commons.wikimedia.org/wiki/File:Elvis_Presley_1970.jpg –> <ImageBrush Stretch="UniformToFill" ImageSource="http://upload.wikimedia.org/wikipedia/commons/8/82/
Elvis_Presley_1970.jpg" /> </
Ellipse.Fill
> <Ellipse.Effect> <DropShadowEffect /> </Ellipse.Effect> <Ellipse.RenderTransform> <TranslateTransform X="-25"
Y
="-25"/> </Ellipse.RenderTransform> </Ellipse> </Grid> </ControlTemplate> </esriSymbols:MarkerSymbol.ControlTemplate> </esriSymbols:MarkerSymbol> </esri:Map.Resources> <esri:Map.Layers> <esri:ArcGISTiledMapServiceLayer
Url
="http://server.arcgisonline.com/ArcGIS/rest/services/
World_Topo_Map/MapServer"/> <
esri:GraphicsLayer
/> </esri:Map.Layers> </esri:Map> </Grid> </UserControl>

…and the code behind….

using System;
using System.Collections.Specialized;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;

namespace MoveableGraphicsSample {
    public partial class MainPage : UserControl {
        private DragGraphic _dragGraphic = null;
        private Random _random = new Random();

        public MainPage() {
            InitializeComponent();

            // Get layers
            ArcGISTiledMapServiceLayer topoLayer = this.Map.Layers[0]
as ArcGISTiledMapServiceLayer; GraphicsLayer graphicsLayer = this.Map.Layers[1]
as GraphicsLayer;
// Wait until the map service layer has loaded and initialized topoLayer.Initialized += (a, b) => { for (int i = 0; i < 10; i++) { graphicsLayer.Graphics.Add( new Graphic() { // Create a random location Geometry = new MapPoint() { X = topoLayer.InitialExtent.XMin +
this._random.NextDouble() *
(topoLayer.InitialExtent.XMax -
topoLayer.InitialExtent.XMin), Y = topoLayer.InitialExtent.YMin +
this._random.NextDouble() *
(topoLayer.InitialExtent.YMax -
topoLayer.InitialExtent.YMin) },
// Use the markersymbol defined in the resource Symbol = this.Map.Resources["Elvis"]
as
MarkerSymbol } ); } }; graphicsLayer.Graphics.CollectionChanged += (a, b) => { // Exit if not added graphic if (b.Action != NotifyCollectionChangedAction.Add) { return;} // Get added graphic Graphic graphic = b.NewItems[0] as Graphic; // When the graphic is clicked, store the current state graphic.MouseLeftButtonDown += (sender, e) => { this._dragGraphic = new DragGraphic() { Graphic = graphic, GraphicOrigin = graphic.Geometry as MapPoint, MouseOrigin = this.Map.ScreenToMap(e.GetPosition(null)) }; e.Handled = true; }; // When the mouse is moved update the graphic's position graphic.MouseMove += (sender, e) => { if (this._dragGraphic == null) { return; } this._dragGraphic.Graphic.Geometry = new MapPoint() { X = this._dragGraphic.GraphicOrigin.X +
(this.Map.ScreenToMap(e.GetPosition(null)).X -
this._dragGraphic.MouseOrigin.X), Y = this._dragGraphic.GraphicOrigin.Y +
(this.Map.ScreenToMap(e.GetPosition(null)).Y -
this._dragGraphic.MouseOrigin.Y) }; };
// Clear the seleced graphic on mouse up graphic.MouseLeftButtonUp += (sender, e) => { this._dragGraphic = null; }; }; // Adjust the selected graphics position if the mouse moves this.Map.MouseMove += (sender, e) => { if (this._dragGraphic == null) { return; } this._dragGraphic.Graphic.Geometry = new MapPoint() { X = this._dragGraphic.GraphicOrigin.X +
(this.Map.ScreenToMap(e.GetPosition(null)).X -
this._dragGraphic.MouseOrigin.X), Y = this._dragGraphic.GraphicOrigin.Y +
(this.Map.ScreenToMap(e.GetPosition(null)).Y -
this._dragGraphic.MouseOrigin.Y) }; }; } } public class DragGraphic { public Graphic Graphic { get; set; } public MapPoint MouseOrigin { get; set; } public MapPoint GraphicOrigin { get; set; } } }

Tuesday, May 18, 2010

Cross Country Mobility for Silverlight

Almost a year ago, ESRI’s Applications Prototype Lab published a video demonstrating a cross country mobility (or CCM) application for Microsoft’s Surface device.  This application is now available as a public Silverlight-based web application.  Click here to launch the web application or you can watch the embedded video above.

How to run the demonstration:

  1. Open a web browser to:
    http://maps.esri.com/sldemos/ccm/default.html
    (you may be prompted to install Silverlight 4)
  2. Move the target icon anywhere on the map.  This is the destination of the three flagged icons.
  3. Move the three flagged icons to any location on the map.  These icons represent three vehicles that are seeking the most efficient route to the target icon.
  4. Click the Weighting button to adjust the parameters for the cost surface.  In this menu you can refine the definition of “cost” for each pixel on the map.  Moving a slider to the left indicates that the category or sub-category is less expensive (in time, money, fuel or some other resource).  Similarly, moving a slider to the right indicates that category or sub-category is more expensive or costly.
  5. Click the Submit button to send the weighting parameters to the server.  The server will create and return a cost surface shaded from green to red.  A green area represents areas that are cheap to traverse whereas red areas are more expensive.  Unshaded areas are “no-go” areas that cannot be traversed whatsoever such as water bodies (as defined in the weighting menu above).
  6. Click the Show/Hide Surface button to hide the cost surface (optional),
  7. Click the Path button to display best path, that is,the path of least cost (in fuel, time, money etc).
  8. Move the flagged icons and click the Path button to repeat the best path analysis.  You cannot change the position of the target icon at this stage because the cost surface has been optimized with respect to the destination.  To change the destination click the Reset button to start over.
  9. Before moving to the next step move the vertical slider on the right hand slide so that the map only displays satellite imagery.  This will make the results of the next step a little easier to visualize.
  10. Click the Corridor button.  Rather than showing just the ideal routing solution, the server will now calculate the top three percentile solution set.  The result is a multi-shaded corridor with the lightest shade represent the top three percentile and the darker shade the top one percentile.  This type of analysis is useful in highlighting choke points, that is, locations where people and vehicle must pass through.  Conversely it is a useful tool to locate possible ambush location.

Monday, May 17, 2010

Augmented Reality and ESRI’s ArcGIS API for Silverlight

   

The lab has just completed a prototype that combines ESRI’s ArcGIS API for Silverlight with the open source Silverlight Augmented Reality Toolkit.

The toolkit allows developers to detect the spatial orientation of a predefined marker.  The lab used the orientation information to overlay a map from ESRI’s ArcGIS API for Silverlight.  As shown in the video, the map is fully functional even with the perspective distortion.

The demonstration starts with the user browsing a default basemap from ESRI’s resource center and then performs a search of GeoEye imagery in New Zealand.  All imagery is tagged with sensor information such as the capture date, percentage cloud cover and satellite position.  In the video, this information is used to order and then offset overlapping imagery.  This effect is useful to quickly find imagery based on specific criteria, for example, find the newest imagery with the least amount of cloud cover.

Finally, why not try this demonstration yourself?  You will need a computer with a webcam.  Click here to start the Augmented Reality web application and here to download and print the marker symbol.

Wednesday, March 31, 2010

DMS Value Converter for Silverlight/WPF

Longitudes, latitudes and other angular measurements are normally stored as decimals degrees such as 10.25.  However angles most commonly displayed as degrees minutes and second, for example, 10°15’00”.  This post contains code for a simple application that demonstrates a decimal degrees to degrees minutes seconds value converter.  The converter is defined in code but applied in XAML for any Silverlight or WPF application.

In this example, the code is for the following WPF windows application:

DMS Value Converter

In the code behind for the window looks like this:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace DmsConverterSample {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
    }

    [ValueConversion(typeof(string), typeof(string))]
    public class DmsConverter : IValueConverter {
        public object Convert(object value,
                              Type targetType,
                              object parameter,
                              CultureInfo culture) {
            // Test for Null
            if (value == null) { return "Null"; }
            string st = value.ToString();
            if (string.IsNullOrEmpty(st)) { return "Empty"; }

            // Get Angle
            double angle;
            if (!double.TryParse(st, out angle)) { return "Not an angle"; }

            // Set flag if number is negative
            bool neg = angle < 0d;

            // Work with a positive number
            angle = Math.Abs(angle);

            // Get d/m/s components
            double d = Math.Floor(angle);
            angle -= d;
            angle *= 60;
            double m = Math.Floor(angle);
            angle -= m;
            angle *= 60;
            double s = Math.Round(angle);

            // Format
            return string.Format("{0:00}°{1:00}'{2:00}\"", d, m, s);
        }
        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
}

And the XAML is here:

<Window x:Class="DmsConverterSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DmsConverterSample"
        Title="Convert from DD to DMS"
        Height="150"
        Width="400">
    <Grid Margin="5,5,5,5">
        <Grid.Resources>
            <local:DmsConverter x:Key="DmsConverter"/>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="3"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0"
                   Grid.Row="0"
                   Text="Decimal Degrees" />
        <TextBlock Grid.Column="0"
                   Grid.Row="1"
                   Text="Degrees Minutes Seconds"/>
        <TextBox x:Name="DD" Grid.Column="2" Grid.Row="0"/>
        <TextBox x:Name="DMS"
                 Grid.Column="2"
                 Grid.Row="1"
                 IsEnabled="False"
                 Text="{Binding
                    ElementName=DD,
                    Path=Text,
                    Mode=OneWay,
                    Converter={StaticResource ResourceKey=DmsConverter}}"
                 />
    </Grid>
</Window>

This samples utilises element binding.  As you type an angle in decimal degrees, the angle will automatically be converted to degrees minutes seconds in the second text box.