using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using ThinkGeo.MapSuite.Core;
namespace MultiRingBuffering
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
ShapeFileFeatureLayer austin10Layer;
ShapeFileFeatureLayer austinSchoolsLayer;
Collection<Ring> rings = new Collection<Ring>();
//Class for the ring
class Ring
{
public double BufferDistance;
public GeoColor RingColor;
public MultipolygonShape RingMultipolygonShape;
public Ring(double BufferDistance, GeoColor RingColor)
{
this.BufferDistance = BufferDistance;
this.RingColor = RingColor;
this.RingMultipolygonShape = null;
}
}
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the full extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-97.99,30.49,-97.40,30.06), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.StandardColors.LightGoldenrodYellow);
// Add the static layers to the MapEngine
austin10Layer = new ShapeFileFeatureLayer(@"..\..\Data\austin10.shp", ShapeFileReadWriteMode.ReadOnly);
austin10Layer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.Interstate3;
austin10Layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
austin10Layer.RequireIndex = false;
mapEngine.StaticLayers.Add("austin10Layer", austin10Layer);
austinSchoolsLayer = new ShapeFileFeatureLayer(@"..\..\Data\austinschools.shp", ShapeFileReadWriteMode.ReadOnly);
austinSchoolsLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.LightGreen, 7, GeoColor.StandardColors.Black);
austinSchoolsLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
austinSchoolsLayer.RequireIndex = false;
mapEngine.StaticLayers.Add("austinSchoolsLayer", austinSchoolsLayer);
DrawImage();
}
private void btnBuffer_Click(object sender, EventArgs e)
{
//Sets buffer distance and color for each ring.
rings.Add(new Ring(1,GeoColor.FromArgb(150,GeoColor.StandardColors.Red)));
rings.Add(new Ring(3, GeoColor.FromArgb(150, GeoColor.StandardColors.DarkOrange)));
rings.Add(new Ring(5, GeoColor.FromArgb(150, GeoColor.StandardColors.Orange)));
rings.Add(new Ring(7, GeoColor.FromArgb(150, GeoColor.StandardColors.Yellow)));
Collection<MultipolygonShape> bufferMultiPolygonShapes = new Collection<MultipolygonShape>();
austin10Layer.Open();
Collection<Feature> features = austin10Layer.FeatureSource.GetAllFeatures(ReturningColumnsType.NoColumns);
austin10Layer.Close();
//Loops thru the rings to to get the buffer polygon.
foreach (Ring ring in rings)
{
//First, get the buffer for all the features of the roads.
Collection<AreaBaseShape> bufferAreaBaseShapes = new Collection<AreaBaseShape>();
foreach (Feature feature in features)
{
MultilineShape multiLineShape = (MultilineShape)feature.GetShape();
MultipolygonShape bufferShape = multiLineShape.Buffer(ring.BufferDistance, GeographyUnit.DecimalDegree, DistanceUnit.Kilometer);
bufferAreaBaseShapes.Add(bufferShape);
}
//Then, union all the buffers into one compact buffer polygon.
MultipolygonShape bufferMultipolygonShape = PolygonShape.Union(bufferAreaBaseShapes);
bufferMultiPolygonShapes.Add(bufferMultipolygonShape);
}
//Builds the ring polygons from the buffer polygons.
BuildMultipolygonShapeRings(bufferMultiPolygonShapes);
//Loops thru the rings to create an InMemoryFeatureLayer for each.
foreach (Ring ring in rings)
{
InMemoryFeatureLayer RingInMemoryFeatureLayer = new InMemoryFeatureLayer();
RingInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle (ring.RingColor);
RingInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.DynamicLayers.Add(RingInMemoryFeatureLayer);
Feature RingFeature = new Feature(ring.RingMultipolygonShape);
RingInMemoryFeatureLayer.InternalFeatures.Add(RingFeature);
//Gets the count of features within each ring.
austinSchoolsLayer.Open();
Collection<Feature> schoolFeatures = austinSchoolsLayer.QueryTools.GetFeaturesWithin(ring.RingMultipolygonShape, ReturningColumnsType.AllColumns);
lblResult.Text = lblResult.Text + ": " + schoolFeatures.Count + " feature(s) in ring of " + ring.BufferDistance + " km";
austinSchoolsLayer.Close();
}
DrawImage();
}
//Function for creating the Ring polygons from the buffer polygons.
private void BuildMultipolygonShapeRings(Collection<MultipolygonShape> BufferMultiPolygonShapes)
{
rings[0].RingMultipolygonShape = (BufferMultiPolygonShapes[0]);
int i = 1;
for (i = 1; i <= BufferMultiPolygonShapes.Count - 1; i += 1)
{
MultipolygonShape ringMultipolygonShape = BufferMultiPolygonShapes[i].GetDifference(BufferMultiPolygonShapes[i|- 1]);
rings[i].RingMultipolygonShape = (ringMultipolygonShape);
}
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.DrawDynamicLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.CloseAllLayers();
Map.Image = bitmap;
}
private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Zoom In":
mapEngine.CurrentExtent.ScaleDown(50);
break;
case "Zoom Out":
mapEngine.CurrentExtent.ScaleUp(50);
break;
case "Full Extent":
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
break;
case "Pan Left":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);
break;
case "Pan Right":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);
break;
case "Pan Up":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);
break;
case "Pan Down":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);
break;
default:
break;
}
DrawImage();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}