using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace StreetIntersection
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
mapEngine.CurrentExtent = new RectangleShape(-97.7402,30.2793,-97.7284,30.2738);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.StandardColors.LightGoldenrodYellow);
ShapeFileFeatureLayer streetLayer = new ShapeFileFeatureLayer(@"..\..\Data\austinstreets.shp");
streetLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.LocalRoad2;
streetLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
//Gets the features according to the street name.
streetLayer.Open();
Collection<Feature> features1 = streetLayer.FeatureSource.GetFeaturesByColumnValue("FENAME", "Red River"); //"Speedway");
Collection<Feature> features2 = streetLayer.FeatureSource.GetFeaturesByColumnValue("FENAME", "15th"); //"Martin Luther King Jr");
streetLayer.Close();
//InMemoryFeatureLayer for displaying the street candidates and the intersection point.
InMemoryFeatureLayer intersectionInMemoryFeatureLayer = new InMemoryFeatureLayer();
intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.StandardColors.LightPink, 3, true);
intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimplePointStyle(PointSymbolType.Circle, GeoColor.StandardColors.Red, 12);
intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
//Adds the street candidates feature and the intersection point feature to the InMemoryFeatureLayer
intersectionInMemoryFeatureLayer.Open();
intersectionInMemoryFeatureLayer.EditTools.BeginTransaction();
Collection<MultilineShape> multiLineShapes1 = new Collection<MultilineShape>();
Collection<MultilineShape> multiLineShapes2 = new Collection<MultilineShape>();
foreach (Feature feature1 in features1)
{
MultilineShape multiLineShape = feature1.GetShape() as MultilineShape;
multiLineShapes1.Add(multiLineShape);
intersectionInMemoryFeatureLayer.EditTools.Add(new Feature(multiLineShape));
}
foreach (Feature feature2 in features2)
{
MultilineShape multiLineShape = feature2.GetShape() as MultilineShape;
multiLineShapes2.Add(multiLineShape);
intersectionInMemoryFeatureLayer.EditTools.Add(new Feature(multiLineShape));
}
//Gets the intersection point (or points if this is the case)
intersectionInMemoryFeatureLayer.EditTools.Add(new Feature(GetIntersectionPoints(multiLineShapes1,multiLineShapes2,50,DistanceUnit.Meter,GeographyUnit.DecimalDegree)));
intersectionInMemoryFeatureLayer.EditTools.CommitTransaction();
intersectionInMemoryFeatureLayer.Close();
mapEngine.StaticLayers.Add("StreetLayer", streetLayer);
mapEngine.DynamicLayers.Add("Intersection", intersectionInMemoryFeatureLayer);
DrawImage();
}
//Gets the intersection point(s) based on the distance tolerance from the end points of the two collection of MultiLineShapes.
//We use a tolerance because we want to have an intersection being counted as such even if the points don't have the exact X and Y values.
private MultipointShape GetIntersectionPoints(Collection<MultilineShape> multiLineShapes1, Collection<MultilineShape> multiLineShapes2, double toleranceDistance, DistanceUnit toleranceUnit, GeographyUnit mapUnit)
{
MultipointShape intersectionMultiPointShape = new MultipointShape();
MultipointShape multiPointShape1 = GetEndPoints(multiLineShapes1);
MultipointShape multiPointShape2 = GetEndPoints(multiLineShapes2);
foreach (PointShape pointShape1 in multiPointShape1.Points)
{
foreach (PointShape pointShape2 in multiPointShape2.Points)
{
if (pointShape1.GetDistanceTo(pointShape2, mapUnit, toleranceUnit) <= toleranceDistance)
{
intersectionMultiPointShape.Points.Add(pointShape1);
}
}
}
return intersectionMultiPointShape;
}
private MultipointShape GetEndPoints(Collection<MultilineShape> multilineShapes)
{
MultipointShape multipointShape = new MultipointShape();
foreach (MultilineShape multilineShape in multilineShapes)
{
foreach (LineShape lineShape in multilineShape.Lines)
{
multipointShape.Points.Add(new PointShape(lineShape.Vertices[0]));
multipointShape.Points.Add(new PointShape(lineShape.Vertices[lineShape.Vertices.Count|- 1]));
}
}
return multipointShape;
}
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();
}
private void Map_MouseMove(object sender, MouseEventArgs e)
{
//Displays the X and Y in screen coordinates.
statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y;
//Gets the PointShape in world coordinates from screen coordinates.
PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height);
//Displays world coordinates.
statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4);
}
}
}