using System;
using System.Collections.ObjectModel;
using System.Windows;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WpfDesktopEdition;
namespace Post8272
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
private ShapeFileFeatureLayer worldLayer;
private LayerOverlay layerOverlay;
public Window1()
{
InitializeComponent();
worldLayer = new ShapeFileFeatureLayer("data\\polygondemo.shp", ShapeFileReadWriteMode.ReadWrite);
// make sure all features are cleared in the shapefile.
worldLayer.Open();
var features = worldLayer.QueryTools.GetAllFeatures(ReturningColumnsType.NoColumns);
worldLayer.EditTools.BeginTransaction();
foreach (Feature feature in features)
{
worldLayer.EditTools.Delete(feature.Id);
}
worldLayer.EditTools.CommitTransaction();
worldLayer.Close();
}
private void WpfMap_Loaded(object sender, RoutedEventArgs e)
{
Map1.MapUnit = GeographyUnit.DecimalDegree;
Map1.CurrentExtent = new RectangleShape(-155.733, 95.60, 104.42, -81.9);
CustomEditInteractiveOverlay editOverlay = new CustomEditInteractiveOverlay();
editOverlay.CanDrag = true;
editOverlay.CanReshape = false;
editOverlay.CanResize = false;
editOverlay.CanRotate = false;
editOverlay.EditShapesLayer.InternalFeatures.Add(new Feature(new RectangleShape(-130, 40, -110, 20)));
editOverlay.CalculateAllControlPoints();
editOverlay.EditEnded += new EventHandler(editOverlay_EditEnded);
Map1.EditOverlay = editOverlay;
Map1.Refresh();
}
private void editOverlay_EditEnded(object sender, EditEndedEditInteractiveOverlayEventArgs e)
{
worldLayer.Open();
worldLayer.EditTools.BeginTransaction();
worldLayer.EditTools.Add(e.EditedFeature);
worldLayer.EditTools.CommitTransaction();
worldLayer.Close();
if (layerOverlay != null) { layerOverlay.Refresh(); }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Map1.Overlays.Contains("layerOverlay"))
{
layerOverlay = (LayerOverlay)Map1.Overlays["layerOverlay"];
worldLayer = (ShapeFileFeatureLayer)layerOverlay.Layers["worldLayer"];
}
else
{
worldLayer = new ShapeFileFeatureLayer(@"data\\polygondemo.shp", ShapeFileReadWriteMode.ReadWrite);
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.SimpleColors.Transparent, GeoColor.FromArgb(100, GeoColor.SimpleColors.Black));
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
layerOverlay = new LayerOverlay();
layerOverlay.Layers.Add("worldLayer", worldLayer);
Map1.Overlays.Add("layerOverlay", layerOverlay);
}
Collection allFeatures = new Collection();
lock (worldLayer)
{
if (!worldLayer.IsOpen) worldLayer.Open();
allFeatures = worldLayer.QueryTools.GetAllFeatures(ReturningColumnsType.NoColumns);
worldLayer.Close();
}
Map1.TrackOverlay.TrackMode = TrackMode.None;
Map1.EditOverlay.EditShapesLayer.InternalFeatures.Clear();
foreach (Feature feature in allFeatures)
{
Map1.EditOverlay.EditShapesLayer.InternalFeatures.Add(feature);
}
Map1.EditOverlay.CalculateAllControlPoints();
Map1.TrackOverlay.TrackShapeLayer.InternalFeatures.Clear();
Map1.Refresh();
}
}
public class CustomEditInteractiveOverlay : EditInteractiveOverlay
{
public event EventHandler EditEnded;
public CustomEditInteractiveOverlay()
{ }
protected override void EndEditingCore(PointShape targetPointShape)
{
base.EndEditingCore(targetPointShape);
EditEndedEditInteractiveOverlayEventArgs args = new EditEndedEditInteractiveOverlayEventArgs() { EditedFeature = EditShapesLayer.InternalFeatures[0] };
OnEditEnded(args);
}
protected virtual void OnEditEnded(EditEndedEditInteractiveOverlayEventArgs e)
{
EventHandler handler = EditEnded;
if (handler != null)
{
handler(this, e);
}
}
}
public class EditEndedEditInteractiveOverlayEventArgs : EventArgs
{
public Feature EditedFeature { get; set; }
}
}